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_ESP32PAR16.cpp (12139B)

      1 #include "Arduino_ESP32PAR16.h"
      2 
      3 #if defined(ESP32) && (CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3)
      4 
      5 Arduino_ESP32PAR16::Arduino_ESP32PAR16(
      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_ESP32PAR16::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   pinMode(_d0, OUTPUT);
     75   pinMode(_d1, OUTPUT);
     76   pinMode(_d2, OUTPUT);
     77   pinMode(_d3, OUTPUT);
     78   pinMode(_d4, OUTPUT);
     79   pinMode(_d5, OUTPUT);
     80   pinMode(_d6, OUTPUT);
     81   pinMode(_d7, OUTPUT);
     82   pinMode(_d8, OUTPUT);
     83   pinMode(_d9, OUTPUT);
     84   pinMode(_d10, OUTPUT);
     85   pinMode(_d11, OUTPUT);
     86   pinMode(_d12, OUTPUT);
     87   pinMode(_d13, OUTPUT);
     88   pinMode(_d14, OUTPUT);
     89   pinMode(_d15, OUTPUT);
     90 
     91   // INIT 16-bit mask
     92   _data1ClrMask = 0;
     93   _data2ClrMask = 0;
     94   if (_d0 >= 32)
     95   {
     96     _data2ClrMask |= digitalPinToBitMask(_d0);
     97   }
     98   else
     99   {
    100     _data1ClrMask |= digitalPinToBitMask(_d0);
    101   }
    102   if (_d1 >= 32)
    103   {
    104     _data2ClrMask |= digitalPinToBitMask(_d1);
    105   }
    106   else
    107   {
    108     _data1ClrMask |= digitalPinToBitMask(_d1);
    109   }
    110   if (_d2 >= 32)
    111   {
    112     _data2ClrMask |= digitalPinToBitMask(_d2);
    113   }
    114   else
    115   {
    116     _data1ClrMask |= digitalPinToBitMask(_d2);
    117   }
    118   if (_d3 >= 32)
    119   {
    120     _data2ClrMask |= digitalPinToBitMask(_d3);
    121   }
    122   else
    123   {
    124     _data1ClrMask |= digitalPinToBitMask(_d3);
    125   }
    126   if (_d4 >= 32)
    127   {
    128     _data2ClrMask |= digitalPinToBitMask(_d4);
    129   }
    130   else
    131   {
    132     _data1ClrMask |= digitalPinToBitMask(_d4);
    133   }
    134   if (_d5 >= 32)
    135   {
    136     _data2ClrMask |= digitalPinToBitMask(_d5);
    137   }
    138   else
    139   {
    140     _data1ClrMask |= digitalPinToBitMask(_d5);
    141   }
    142   if (_d6 >= 32)
    143   {
    144     _data2ClrMask |= digitalPinToBitMask(_d6);
    145   }
    146   else
    147   {
    148     _data1ClrMask |= digitalPinToBitMask(_d6);
    149   }
    150   if (_d7 >= 32)
    151   {
    152     _data2ClrMask |= digitalPinToBitMask(_d7);
    153   }
    154   else
    155   {
    156     _data1ClrMask |= digitalPinToBitMask(_d7);
    157   }
    158   if (_d8 >= 32)
    159   {
    160     _data2ClrMask |= digitalPinToBitMask(_d8);
    161   }
    162   else
    163   {
    164     _data1ClrMask |= digitalPinToBitMask(_d8);
    165   }
    166   if (_d9 >= 32)
    167   {
    168     _data2ClrMask |= digitalPinToBitMask(_d9);
    169   }
    170   else
    171   {
    172     _data1ClrMask |= digitalPinToBitMask(_d9);
    173   }
    174   if (_d10 >= 32)
    175   {
    176     _data2ClrMask |= digitalPinToBitMask(_d10);
    177   }
    178   else
    179   {
    180     _data1ClrMask |= digitalPinToBitMask(_d10);
    181   }
    182   if (_d11 >= 32)
    183   {
    184     _data2ClrMask |= digitalPinToBitMask(_d11);
    185   }
    186   else
    187   {
    188     _data1ClrMask |= digitalPinToBitMask(_d11);
    189   }
    190   if (_d12 >= 32)
    191   {
    192     _data2ClrMask |= digitalPinToBitMask(_d12);
    193   }
    194   else
    195   {
    196     _data1ClrMask |= digitalPinToBitMask(_d12);
    197   }
    198   if (_d13 >= 32)
    199   {
    200     _data2ClrMask |= digitalPinToBitMask(_d13);
    201   }
    202   else
    203   {
    204     _data1ClrMask |= digitalPinToBitMask(_d13);
    205   }
    206   if (_d14 >= 32)
    207   {
    208     _data2ClrMask |= digitalPinToBitMask(_d14);
    209   }
    210   else
    211   {
    212     _data1ClrMask |= digitalPinToBitMask(_d14);
    213   }
    214   if (_d15 >= 32)
    215   {
    216     _data2ClrMask |= digitalPinToBitMask(_d15);
    217   }
    218   else
    219   {
    220     _data1ClrMask |= digitalPinToBitMask(_d15);
    221   }
    222 
    223   for (int32_t c = 0; c < 256; c++)
    224   {
    225     _xset_mask1_lo[c] = 0;
    226     _xset_mask2_lo[c] = 0;
    227     if (c & 0x01)
    228     {
    229       if (_d0 >= 32)
    230       {
    231         _xset_mask2_lo[c] |= digitalPinToBitMask(_d0);
    232       }
    233       else
    234       {
    235         _xset_mask1_lo[c] |= digitalPinToBitMask(_d0);
    236       }
    237     }
    238     if (c & 0x02)
    239     {
    240       if (_d1 >= 32)
    241       {
    242         _xset_mask2_lo[c] |= digitalPinToBitMask(_d1);
    243       }
    244       else
    245       {
    246         _xset_mask1_lo[c] |= digitalPinToBitMask(_d1);
    247       }
    248     }
    249     if (c & 0x04)
    250     {
    251       if (_d2 >= 32)
    252       {
    253         _xset_mask2_lo[c] |= digitalPinToBitMask(_d2);
    254       }
    255       else
    256       {
    257         _xset_mask1_lo[c] |= digitalPinToBitMask(_d2);
    258       }
    259     }
    260     if (c & 0x08)
    261     {
    262       if (_d3 >= 32)
    263       {
    264         _xset_mask2_lo[c] |= digitalPinToBitMask(_d3);
    265       }
    266       else
    267       {
    268         _xset_mask1_lo[c] |= digitalPinToBitMask(_d3);
    269       }
    270     }
    271     if (c & 0x10)
    272     {
    273       if (_d4 >= 32)
    274       {
    275         _xset_mask2_lo[c] |= digitalPinToBitMask(_d4);
    276       }
    277       else
    278       {
    279         _xset_mask1_lo[c] |= digitalPinToBitMask(_d4);
    280       }
    281     }
    282     if (c & 0x20)
    283     {
    284       if (_d5 >= 32)
    285       {
    286         _xset_mask2_lo[c] |= digitalPinToBitMask(_d5);
    287       }
    288       else
    289       {
    290         _xset_mask1_lo[c] |= digitalPinToBitMask(_d5);
    291       }
    292     }
    293     if (c & 0x40)
    294     {
    295       if (_d6 >= 32)
    296       {
    297         _xset_mask2_lo[c] |= digitalPinToBitMask(_d6);
    298       }
    299       else
    300       {
    301         _xset_mask1_lo[c] |= digitalPinToBitMask(_d6);
    302       }
    303     }
    304     if (c & 0x80)
    305     {
    306       if (_d7 >= 32)
    307       {
    308         _xset_mask2_lo[c] |= digitalPinToBitMask(_d7);
    309       }
    310       else
    311       {
    312         _xset_mask1_lo[c] |= digitalPinToBitMask(_d7);
    313       }
    314     }
    315   }
    316   for (int32_t c = 0; c < 256; c++)
    317   {
    318     _xset_mask1_hi[c] = 0;
    319     _xset_mask2_hi[c] = 0;
    320     if (c & 0x01)
    321     {
    322       if (_d8 >= 32)
    323       {
    324         _xset_mask2_hi[c] |= digitalPinToBitMask(_d8);
    325       }
    326       else
    327       {
    328         _xset_mask1_hi[c] |= digitalPinToBitMask(_d8);
    329       }
    330     }
    331     if (c & 0x02)
    332     {
    333       if (_d9 >= 32)
    334       {
    335         _xset_mask2_hi[c] |= digitalPinToBitMask(_d9);
    336       }
    337       else
    338       {
    339         _xset_mask1_hi[c] |= digitalPinToBitMask(_d9);
    340       }
    341     }
    342     if (c & 0x04)
    343     {
    344       if (_d10 >= 32)
    345       {
    346         _xset_mask2_hi[c] |= digitalPinToBitMask(_d10);
    347       }
    348       else
    349       {
    350         _xset_mask1_hi[c] |= digitalPinToBitMask(_d10);
    351       }
    352     }
    353     if (c & 0x08)
    354     {
    355       if (_d11 >= 32)
    356       {
    357         _xset_mask2_hi[c] |= digitalPinToBitMask(_d11);
    358       }
    359       else
    360       {
    361         _xset_mask1_hi[c] |= digitalPinToBitMask(_d11);
    362       }
    363     }
    364     if (c & 0x10)
    365     {
    366       if (_d12 >= 32)
    367       {
    368         _xset_mask2_hi[c] |= digitalPinToBitMask(_d12);
    369       }
    370       else
    371       {
    372         _xset_mask1_hi[c] |= digitalPinToBitMask(_d12);
    373       }
    374     }
    375     if (c & 0x20)
    376     {
    377       if (_d13 >= 32)
    378       {
    379         _xset_mask2_hi[c] |= digitalPinToBitMask(_d13);
    380       }
    381       else
    382       {
    383         _xset_mask1_hi[c] |= digitalPinToBitMask(_d13);
    384       }
    385     }
    386     if (c & 0x40)
    387     {
    388       if (_d14 >= 32)
    389       {
    390         _xset_mask2_hi[c] |= digitalPinToBitMask(_d14);
    391       }
    392       else
    393       {
    394         _xset_mask1_hi[c] |= digitalPinToBitMask(_d14);
    395       }
    396     }
    397     if (c & 0x80)
    398     {
    399       if (_d15 >= 32)
    400       {
    401         _xset_mask2_hi[c] |= digitalPinToBitMask(_d15);
    402       }
    403       else
    404       {
    405         _xset_mask1_hi[c] |= digitalPinToBitMask(_d15);
    406       }
    407     }
    408   }
    409   GPIO.out_w1tc = _data1ClrMask;
    410   GPIO.out1_w1tc.val = _data2ClrMask;
    411 
    412   return true;
    413 }
    414 
    415 void Arduino_ESP32PAR16::beginWrite()
    416 {
    417   DC_HIGH();
    418   CS_LOW();
    419 }
    420 
    421 void Arduino_ESP32PAR16::endWrite()
    422 {
    423   CS_HIGH();
    424 }
    425 
    426 void Arduino_ESP32PAR16::writeCommand(uint8_t c)
    427 {
    428   DC_LOW();
    429 
    430   WRITE(c);
    431 
    432   DC_HIGH();
    433 }
    434 
    435 void Arduino_ESP32PAR16::writeCommand16(uint16_t c)
    436 {
    437   DC_LOW();
    438 
    439   WRITE16(c);
    440 
    441   DC_HIGH();
    442 }
    443 
    444 void Arduino_ESP32PAR16::write(uint8_t d)
    445 {
    446   WRITE(d);
    447 }
    448 
    449 void Arduino_ESP32PAR16::write16(uint16_t d)
    450 {
    451   WRITE16(d);
    452 }
    453 
    454 void Arduino_ESP32PAR16::writeRepeat(uint16_t p, uint32_t len)
    455 {
    456   _data16.value = p;
    457   uint32_t d1 = _xset_mask1_hi[_data16.msb] | _xset_mask1_lo[_data16.lsb];
    458   uint32_t d2 = _xset_mask2_hi[_data16.msb] | _xset_mask2_lo[_data16.lsb];
    459   GPIO.out_w1tc = _data1ClrMask;
    460   GPIO.out1_w1tc.val = _data2ClrMask;
    461   GPIO.out_w1ts = d1;
    462   GPIO.out1_w1ts.val = d2;
    463   while (len--)
    464   {
    465     *_wrPortClr = _wrPinMask;
    466     *_wrPortSet = _wrPinMask;
    467   }
    468 }
    469 
    470 void Arduino_ESP32PAR16::writePixels(uint16_t *data, uint32_t len)
    471 {
    472   while (len--)
    473   {
    474     WRITE16(*data++);
    475   }
    476 }
    477 
    478 void Arduino_ESP32PAR16::writeC8D8(uint8_t c, uint8_t d)
    479 {
    480   DC_LOW();
    481 
    482   WRITE(c);
    483 
    484   DC_HIGH();
    485 
    486   WRITE(d);
    487 }
    488 
    489 void Arduino_ESP32PAR16::writeC8D16(uint8_t c, uint16_t d)
    490 {
    491   DC_LOW();
    492 
    493   WRITE(c);
    494 
    495   DC_HIGH();
    496 
    497   WRITE16(d);
    498 }
    499 
    500 void Arduino_ESP32PAR16::writeC8D16D16(uint8_t c, uint16_t d1, uint16_t d2)
    501 {
    502   DC_LOW();
    503 
    504   WRITE(c);
    505 
    506   DC_HIGH();
    507 
    508   WRITE16(d1);
    509   WRITE16(d2);
    510 }
    511 
    512 void Arduino_ESP32PAR16::writeC8D16D16Split(uint8_t c, uint16_t d1, uint16_t d2)
    513 {
    514   DC_LOW();
    515 
    516   WRITE(c);
    517 
    518   DC_HIGH();
    519 
    520   _data16.value = d1;
    521   WRITE(_data16.msb);
    522   WRITE(_data16.lsb);
    523 
    524   _data16.value = d2;
    525   WRITE(_data16.msb);
    526   WRITE(_data16.lsb);
    527 }
    528 
    529 void Arduino_ESP32PAR16::writeBytes(uint8_t *data, uint32_t len)
    530 {
    531   while (len > 1)
    532   {
    533     _data16.msb = *data++;
    534     _data16.lsb = *data++;
    535     WRITE16(_data16.value);
    536     len -= 2;
    537   }
    538   if (len)
    539   {
    540     WRITE(*data);
    541   }
    542 }
    543 
    544 void Arduino_ESP32PAR16::writePattern(uint8_t *data, uint8_t len, uint32_t repeat)
    545 {
    546   while (repeat--)
    547   {
    548     writeBytes(data, len);
    549   }
    550 }
    551 
    552 void Arduino_ESP32PAR16::writeIndexedPixels(uint8_t *data, uint16_t *idx, uint32_t len)
    553 {
    554   while (len--)
    555   {
    556     WRITE16(idx[*data++]);
    557   }
    558 }
    559 
    560 void Arduino_ESP32PAR16::writeIndexedPixelsDouble(uint8_t *data, uint16_t *idx, uint32_t len)
    561 {
    562   while (len--)
    563   {
    564     _data16.value = idx[*data++];
    565     GPIO.out_w1tc = _data1ClrMask;
    566     GPIO.out1_w1tc.val = _data2ClrMask;
    567     GPIO.out_w1ts = _xset_mask1_hi[_data16.msb] | _xset_mask1_lo[_data16.lsb];
    568     GPIO.out1_w1ts.val = _xset_mask2_hi[_data16.msb] | _xset_mask2_lo[_data16.lsb];
    569     *_wrPortClr = _wrPinMask;
    570     *_wrPortSet = _wrPinMask;
    571     *_wrPortClr = _wrPinMask;
    572     *_wrPortSet = _wrPinMask;
    573   }
    574 }
    575 
    576 INLINE void Arduino_ESP32PAR16::WRITE(uint8_t d)
    577 {
    578   GPIO.out_w1tc = _data1ClrMask;
    579   GPIO.out1_w1tc.val = _data2ClrMask;
    580   GPIO.out_w1ts = _xset_mask1_lo[d];
    581   GPIO.out1_w1ts.val = _xset_mask2_lo[d];
    582   *_wrPortClr = _wrPinMask;
    583   *_wrPortSet = _wrPinMask;
    584 }
    585 
    586 INLINE void Arduino_ESP32PAR16::WRITE16(uint16_t d)
    587 {
    588   _data16.value = d;
    589   uint32_t d1 = _xset_mask1_hi[_data16.msb] | _xset_mask1_lo[_data16.lsb];
    590   uint32_t d2 = _xset_mask2_hi[_data16.msb] | _xset_mask2_lo[_data16.lsb];
    591   GPIO.out_w1tc = _data1ClrMask;
    592   GPIO.out1_w1tc.val = _data2ClrMask;
    593   GPIO.out_w1ts = d1;
    594   GPIO.out1_w1ts.val = d2;
    595   *_wrPortClr = _wrPinMask;
    596   *_wrPortSet = _wrPinMask;
    597 }
    598 
    599 /******** low level bit twiddling **********/
    600 
    601 INLINE void Arduino_ESP32PAR16::DC_HIGH(void)
    602 {
    603   *_dcPortSet = _dcPinMask;
    604 }
    605 
    606 INLINE void Arduino_ESP32PAR16::DC_LOW(void)
    607 {
    608   *_dcPortClr = _dcPinMask;
    609 }
    610 
    611 INLINE void Arduino_ESP32PAR16::CS_HIGH(void)
    612 {
    613   if (_cs != GFX_NOT_DEFINED)
    614   {
    615     *_csPortSet = _csPinMask;
    616   }
    617 }
    618 
    619 INLINE void Arduino_ESP32PAR16::CS_LOW(void)
    620 {
    621   if (_cs != GFX_NOT_DEFINED)
    622   {
    623     *_csPortClr = _csPinMask;
    624   }
    625 }
    626 
    627 #endif // #if defined(ESP32) && (CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3)