acid-drop- Hacking the planet from a LilyGo T-Deck using custom firmware |
git clone git://git.acid.vegas/acid-drop.git |
Log | Files | Refs | Archive | README | LICENSE |
Arduino_ESP32PAR8Q.cpp (6667B)
1 #include "Arduino_ESP32PAR8Q.h" 2 3 #if defined(ESP32) && (CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3) 4 5 Arduino_ESP32PAR8Q::Arduino_ESP32PAR8Q( 6 int8_t dc, int8_t cs, int8_t wr, int8_t rd, 7 int8_t d0, int8_t d1, int8_t d2, int8_t d3, int8_t d4, int8_t d5, int8_t d6, int8_t d7) 8 : _dc(dc), _cs(cs), _wr(wr), _rd(rd), 9 _d0(d0), _d1(d1), _d2(d2), _d3(d3), _d4(d4), _d5(d5), _d6(d6), _d7(d7) 10 { 11 } 12 13 bool Arduino_ESP32PAR8Q::begin(int32_t speed, int8_t dataMode) 14 { 15 UNUSED(speed); 16 UNUSED(dataMode); 17 18 pinMode(_dc, OUTPUT); 19 digitalWrite(_dc, HIGH); // Data mode 20 if (_dc >= 32) 21 { 22 _dcPinMask = digitalPinToBitMask(_dc); 23 _dcPortSet = (PORTreg_t)&GPIO.out1_w1ts.val; 24 _dcPortClr = (PORTreg_t)&GPIO.out1_w1tc.val; 25 } 26 else 27 { 28 _dcPinMask = digitalPinToBitMask(_dc); 29 _dcPortSet = (PORTreg_t)&GPIO.out_w1ts; 30 _dcPortClr = (PORTreg_t)&GPIO.out_w1tc; 31 } 32 33 if (_cs != GFX_NOT_DEFINED) 34 { 35 pinMode(_cs, OUTPUT); 36 digitalWrite(_cs, HIGH); // disable chip select 37 } 38 if (_cs >= 32) 39 { 40 _csPinMask = digitalPinToBitMask(_cs); 41 _csPortSet = (PORTreg_t)&GPIO.out1_w1ts.val; 42 _csPortClr = (PORTreg_t)&GPIO.out1_w1tc.val; 43 } 44 else if (_cs != GFX_NOT_DEFINED) 45 { 46 _csPinMask = digitalPinToBitMask(_cs); 47 _csPortSet = (PORTreg_t)&GPIO.out_w1ts; 48 _csPortClr = (PORTreg_t)&GPIO.out_w1tc; 49 } 50 51 pinMode(_wr, OUTPUT); 52 digitalWrite(_wr, HIGH); // Set write strobe high (inactive) 53 if (_wr >= 32) 54 { 55 _wrPinMask = digitalPinToBitMask(_wr); 56 _wrPortSet = (PORTreg_t)&GPIO.out1_w1ts.val; 57 _wrPortClr = (PORTreg_t)&GPIO.out1_w1tc.val; 58 } 59 else 60 { 61 _wrPinMask = digitalPinToBitMask(_wr); 62 _wrPortSet = (PORTreg_t)&GPIO.out_w1ts; 63 _wrPortClr = (PORTreg_t)&GPIO.out_w1tc; 64 } 65 66 if (_rd != GFX_NOT_DEFINED) 67 { 68 pinMode(_rd, OUTPUT); 69 digitalWrite(_rd, HIGH); 70 } 71 72 // TODO: check pin range 0-31 73 pinMode(_d0, OUTPUT); 74 pinMode(_d1, OUTPUT); 75 pinMode(_d2, OUTPUT); 76 pinMode(_d3, OUTPUT); 77 pinMode(_d4, OUTPUT); 78 pinMode(_d5, OUTPUT); 79 pinMode(_d6, OUTPUT); 80 pinMode(_d7, OUTPUT); 81 82 // INIT 8-bit mask 83 _dataClrMask = (1 << _wr) | (1 << _d0) | (1 << _d1) | (1 << _d2) | (1 << _d3) | (1 << _d4) | (1 << _d5) | (1 << _d6) | (1 << _d7); 84 for (int32_t c = 0; c < 256; c++) 85 { 86 _xset_mask[c] = 0; 87 if (c & 0x01) 88 { 89 _xset_mask[c] |= (1 << _d0); 90 } 91 if (c & 0x02) 92 { 93 _xset_mask[c] |= (1 << _d1); 94 } 95 if (c & 0x04) 96 { 97 _xset_mask[c] |= (1 << _d2); 98 } 99 if (c & 0x08) 100 { 101 _xset_mask[c] |= (1 << _d3); 102 } 103 if (c & 0x10) 104 { 105 _xset_mask[c] |= (1 << _d4); 106 } 107 if (c & 0x20) 108 { 109 _xset_mask[c] |= (1 << _d5); 110 } 111 if (c & 0x40) 112 { 113 _xset_mask[c] |= (1 << _d6); 114 } 115 if (c & 0x80) 116 { 117 _xset_mask[c] |= (1 << _d7); 118 } 119 } 120 GPIO.out_w1tc = _dataClrMask; 121 122 return true; 123 } 124 125 void Arduino_ESP32PAR8Q::beginWrite() 126 { 127 DC_HIGH(); 128 CS_LOW(); 129 } 130 131 void Arduino_ESP32PAR8Q::endWrite() 132 { 133 CS_HIGH(); 134 } 135 136 void Arduino_ESP32PAR8Q::writeCommand(uint8_t c) 137 { 138 DC_LOW(); 139 140 WRITE(c); 141 142 DC_HIGH(); 143 } 144 145 void Arduino_ESP32PAR8Q::writeCommand16(uint16_t c) 146 { 147 DC_LOW(); 148 149 _data16.value = c; 150 WRITE(_data16.msb); 151 WRITE(_data16.lsb); 152 153 DC_HIGH(); 154 } 155 156 void Arduino_ESP32PAR8Q::write(uint8_t d) 157 { 158 WRITE(d); 159 } 160 161 void Arduino_ESP32PAR8Q::write16(uint16_t d) 162 { 163 _data16.value = d; 164 WRITE(_data16.msb); 165 WRITE(_data16.lsb); 166 } 167 168 void Arduino_ESP32PAR8Q::writeRepeat(uint16_t p, uint32_t len) 169 { 170 _data16.value = p; 171 if (_data16.msb == _data16.lsb) 172 { 173 uint32_t setMask = _xset_mask[_data16.msb]; 174 GPIO.out_w1tc = _dataClrMask; 175 GPIO.out_w1ts = setMask; 176 *_wrPortSet = _wrPinMask; 177 *_wrPortClr = _wrPinMask; 178 *_wrPortSet = _wrPinMask; 179 while (--len) 180 { 181 *_wrPortClr = _wrPinMask; 182 *_wrPortSet = _wrPinMask; 183 *_wrPortClr = _wrPinMask; 184 *_wrPortSet = _wrPinMask; 185 } 186 } 187 else 188 { 189 uint32_t hiMask = _xset_mask[_data16.msb]; 190 uint32_t loMask = _xset_mask[_data16.lsb]; 191 while (len--) 192 { 193 GPIO.out_w1tc = _dataClrMask; 194 GPIO.out_w1ts = hiMask; 195 *_wrPortSet = _wrPinMask; 196 197 GPIO.out_w1tc = _dataClrMask; 198 GPIO.out_w1ts = loMask; 199 *_wrPortSet = _wrPinMask; 200 } 201 } 202 } 203 204 void Arduino_ESP32PAR8Q::writePixels(uint16_t *data, uint32_t len) 205 { 206 while (len--) 207 { 208 _data16.value = *data++; 209 WRITE(_data16.msb); 210 WRITE(_data16.lsb); 211 } 212 } 213 214 void Arduino_ESP32PAR8Q::writeC8D8(uint8_t c, uint8_t d) 215 { 216 DC_LOW(); 217 218 WRITE(c); 219 220 DC_HIGH(); 221 222 WRITE(d); 223 } 224 225 void Arduino_ESP32PAR8Q::writeC8D16(uint8_t c, uint16_t d) 226 { 227 DC_LOW(); 228 229 WRITE(c); 230 231 DC_HIGH(); 232 233 _data16.value = d; 234 WRITE(_data16.msb); 235 WRITE(_data16.lsb); 236 } 237 238 void Arduino_ESP32PAR8Q::writeC8D16D16(uint8_t c, uint16_t d1, uint16_t d2) 239 { 240 DC_LOW(); 241 242 WRITE(c); 243 244 DC_HIGH(); 245 246 _data16.value = d1; 247 WRITE(_data16.msb); 248 WRITE(_data16.lsb); 249 250 _data16.value = d2; 251 WRITE(_data16.msb); 252 WRITE(_data16.lsb); 253 } 254 255 void Arduino_ESP32PAR8Q::writeC8D16D16Split(uint8_t c, uint16_t d1, uint16_t d2) 256 { 257 DC_LOW(); 258 259 WRITE(c); 260 261 DC_HIGH(); 262 263 _data16.value = d1; 264 WRITE(_data16.msb); 265 WRITE(_data16.lsb); 266 267 _data16.value = d2; 268 WRITE(_data16.msb); 269 WRITE(_data16.lsb); 270 } 271 272 void Arduino_ESP32PAR8Q::writeBytes(uint8_t *data, uint32_t len) 273 { 274 while (len--) 275 { 276 WRITE(*data++); 277 } 278 } 279 280 void Arduino_ESP32PAR8Q::writePattern(uint8_t *data, uint8_t len, uint32_t repeat) 281 { 282 while (repeat--) 283 { 284 writeBytes(data, len); 285 } 286 } 287 288 void Arduino_ESP32PAR8Q::writeIndexedPixels(uint8_t *data, uint16_t *idx, uint32_t len) 289 { 290 while (len--) 291 { 292 _data16.value = idx[*data++]; 293 WRITE(_data16.msb); 294 WRITE(_data16.lsb); 295 } 296 } 297 298 void Arduino_ESP32PAR8Q::writeIndexedPixelsDouble(uint8_t *data, uint16_t *idx, uint32_t len) 299 { 300 while (len--) 301 { 302 _data16.value = idx[*data++]; 303 WRITE(_data16.msb); 304 WRITE(_data16.lsb); 305 WRITE(_data16.msb); 306 WRITE(_data16.lsb); 307 } 308 } 309 310 INLINE void Arduino_ESP32PAR8Q::WRITE(uint8_t d) 311 { 312 uint32_t setMask = _xset_mask[d]; 313 GPIO.out_w1tc = _dataClrMask; 314 GPIO.out_w1ts = setMask; 315 *_wrPortSet = _wrPinMask; 316 } 317 318 /******** low level bit twiddling **********/ 319 320 INLINE void Arduino_ESP32PAR8Q::DC_HIGH(void) 321 { 322 *_dcPortSet = _dcPinMask; 323 } 324 325 INLINE void Arduino_ESP32PAR8Q::DC_LOW(void) 326 { 327 *_dcPortClr = _dcPinMask; 328 } 329 330 INLINE void Arduino_ESP32PAR8Q::CS_HIGH(void) 331 { 332 if (_cs != GFX_NOT_DEFINED) 333 { 334 *_csPortSet = _csPinMask; 335 } 336 } 337 338 INLINE void Arduino_ESP32PAR8Q::CS_LOW(void) 339 { 340 if (_cs != GFX_NOT_DEFINED) 341 { 342 *_csPortClr = _csPinMask; 343 } 344 } 345 346 #endif // #if defined(ESP32) && (CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3)