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_ESP32PAR16Q.cpp (7437B)
1 #include "Arduino_ESP32PAR16Q.h" 2 3 #if defined(ESP32) && (CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3) 4 5 Arduino_ESP32PAR16Q::Arduino_ESP32PAR16Q( 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_ESP32PAR16Q::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] = 0; 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_ESP32PAR16Q::beginWrite() 174 { 175 DC_HIGH(); 176 CS_LOW(); 177 } 178 179 void Arduino_ESP32PAR16Q::endWrite() 180 { 181 CS_HIGH(); 182 } 183 184 void Arduino_ESP32PAR16Q::writeCommand(uint8_t c) 185 { 186 DC_LOW(); 187 188 WRITE(c); 189 190 DC_HIGH(); 191 } 192 193 void Arduino_ESP32PAR16Q::writeCommand16(uint16_t c) 194 { 195 DC_LOW(); 196 197 WRITE16(c); 198 199 DC_HIGH(); 200 } 201 202 void Arduino_ESP32PAR16Q::write(uint8_t d) 203 { 204 WRITE(d); 205 } 206 207 void Arduino_ESP32PAR16Q::write16(uint16_t d) 208 { 209 WRITE16(d); 210 } 211 212 void Arduino_ESP32PAR16Q::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 *_dataPortClr = _dataClrMask; 217 *_dataPortSet = d; 218 while (len--) 219 { 220 *_wrPortClr = _wrPinMask; 221 *_wrPortSet = _wrPinMask; 222 } 223 } 224 225 void Arduino_ESP32PAR16Q::writePixels(uint16_t *data, uint32_t len) 226 { 227 while (len--) 228 { 229 WRITE16(*data++); 230 } 231 } 232 233 void Arduino_ESP32PAR16Q::writeC8D8(uint8_t c, uint8_t d) 234 { 235 DC_LOW(); 236 237 WRITE(c); 238 239 DC_HIGH(); 240 241 WRITE(d); 242 } 243 244 void Arduino_ESP32PAR16Q::writeC8D16(uint8_t c, uint16_t d) 245 { 246 DC_LOW(); 247 248 WRITE(c); 249 250 DC_HIGH(); 251 252 WRITE16(d); 253 } 254 255 void Arduino_ESP32PAR16Q::writeC8D16D16(uint8_t c, uint16_t d1, uint16_t d2) 256 { 257 DC_LOW(); 258 259 WRITE(c); 260 261 DC_HIGH(); 262 263 WRITE16(d1); 264 WRITE16(d2); 265 } 266 267 void Arduino_ESP32PAR16Q::writeC8D16D16Split(uint8_t c, uint16_t d1, uint16_t d2) 268 { 269 DC_LOW(); 270 271 WRITE(c); 272 273 DC_HIGH(); 274 275 _data16.value = d1; 276 WRITE(_data16.msb); 277 WRITE(_data16.lsb); 278 279 _data16.value = d2; 280 WRITE(_data16.msb); 281 WRITE(_data16.lsb); 282 } 283 284 void Arduino_ESP32PAR16Q::writeBytes(uint8_t *data, uint32_t len) 285 { 286 while (len > 1) 287 { 288 _data16.msb = *data++; 289 _data16.lsb = *data++; 290 WRITE16(_data16.value); 291 len -= 2; 292 } 293 if (len) 294 { 295 WRITE(*data); 296 } 297 } 298 299 void Arduino_ESP32PAR16Q::writePattern(uint8_t *data, uint8_t len, uint32_t repeat) 300 { 301 while (repeat--) 302 { 303 writeBytes(data, len); 304 } 305 } 306 307 void Arduino_ESP32PAR16Q::writeIndexedPixels(uint8_t *data, uint16_t *idx, uint32_t len) 308 { 309 while (len--) 310 { 311 WRITE16(idx[*data++]); 312 } 313 } 314 315 void Arduino_ESP32PAR16Q::writeIndexedPixelsDouble(uint8_t *data, uint16_t *idx, uint32_t len) 316 { 317 while (len--) 318 { 319 _data16.value = idx[*data++]; 320 *_dataPortClr = _dataClrMask; 321 *_dataPortSet = _xset_mask_hi[_data16.msb] | _xset_mask_lo[_data16.lsb]; 322 *_wrPortSet = _wrPinMask; 323 *_wrPortClr = _wrPinMask; 324 *_wrPortSet = _wrPinMask; 325 } 326 } 327 328 INLINE void Arduino_ESP32PAR16Q::WRITE(uint8_t d) 329 { 330 *_dataPortClr = _dataClrMask; 331 *_dataPortSet = _xset_mask_lo[d]; 332 *_wrPortSet = _wrPinMask; 333 } 334 335 INLINE void Arduino_ESP32PAR16Q::WRITE16(uint16_t d) 336 { 337 _data16.value = d; 338 *_dataPortClr = _dataClrMask; 339 *_dataPortSet = _xset_mask_hi[_data16.msb] | _xset_mask_lo[_data16.lsb]; 340 *_wrPortSet = _wrPinMask; 341 } 342 343 /******** low level bit twiddling **********/ 344 345 INLINE void Arduino_ESP32PAR16Q::DC_HIGH(void) 346 { 347 *_dcPortSet = _dcPinMask; 348 } 349 350 INLINE void Arduino_ESP32PAR16Q::DC_LOW(void) 351 { 352 *_dcPortClr = _dcPinMask; 353 } 354 355 INLINE void Arduino_ESP32PAR16Q::CS_HIGH(void) 356 { 357 if (_cs != GFX_NOT_DEFINED) 358 { 359 *_csPortSet = _csPinMask; 360 } 361 } 362 363 INLINE void Arduino_ESP32PAR16Q::CS_LOW(void) 364 { 365 if (_cs != GFX_NOT_DEFINED) 366 { 367 *_csPortClr = _csPinMask; 368 } 369 } 370 371 #endif // #if defined(ESP32) && (CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3)