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