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_ESP32PAR8QQ.cpp (6519B)

      1 #include "Arduino_ESP32PAR8QQ.h"
      2 
      3 #if defined(ESP32) && (CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3)
      4 
      5 Arduino_ESP32PAR8QQ::Arduino_ESP32PAR8QQ(
      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_ESP32PAR8QQ::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] = (1 << _wr);
     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_ESP32PAR8QQ::beginWrite()
    126 {
    127   DC_HIGH();
    128   CS_LOW();
    129 }
    130 
    131 void Arduino_ESP32PAR8QQ::endWrite()
    132 {
    133   CS_HIGH();
    134 }
    135 
    136 void Arduino_ESP32PAR8QQ::writeCommand(uint8_t c)
    137 {
    138   DC_LOW();
    139 
    140   WRITE(c);
    141 
    142   DC_HIGH();
    143 }
    144 
    145 void Arduino_ESP32PAR8QQ::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_ESP32PAR8QQ::write(uint8_t d)
    157 {
    158   WRITE(d);
    159 }
    160 
    161 void Arduino_ESP32PAR8QQ::write16(uint16_t d)
    162 {
    163   _data16.value = d;
    164   WRITE(_data16.msb);
    165   WRITE(_data16.lsb);
    166 }
    167 
    168 void Arduino_ESP32PAR8QQ::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     while (len--)
    177     {
    178       *_wrPortClr = _wrPinMask;
    179       *_wrPortSet = _wrPinMask;
    180       *_wrPortClr = _wrPinMask;
    181       *_wrPortSet = _wrPinMask;
    182     }
    183   }
    184   else
    185   {
    186     uint32_t hiMask = _xset_mask[_data16.msb];
    187     uint32_t loMask = _xset_mask[_data16.lsb];
    188     while (len--)
    189     {
    190       GPIO.out_w1tc = _dataClrMask;
    191       GPIO.out_w1ts = hiMask;
    192 
    193       GPIO.out_w1tc = _dataClrMask;
    194       GPIO.out_w1ts = loMask;
    195     }
    196   }
    197 }
    198 
    199 void Arduino_ESP32PAR8QQ::writePixels(uint16_t *data, uint32_t len)
    200 {
    201   while (len--)
    202   {
    203     _data16.value = *data++;
    204     WRITE(_data16.msb);
    205     WRITE(_data16.lsb);
    206   }
    207 }
    208 
    209 void Arduino_ESP32PAR8QQ::writeC8D8(uint8_t c, uint8_t d)
    210 {
    211   DC_LOW();
    212 
    213   WRITE(c);
    214 
    215   DC_HIGH();
    216 
    217   WRITE(d);
    218 }
    219 
    220 void Arduino_ESP32PAR8QQ::writeC8D16(uint8_t c, uint16_t d)
    221 {
    222   DC_LOW();
    223 
    224   WRITE(c);
    225 
    226   DC_HIGH();
    227 
    228   _data16.value = d;
    229   WRITE(_data16.msb);
    230   WRITE(_data16.lsb);
    231 }
    232 
    233 void Arduino_ESP32PAR8QQ::writeC8D16D16(uint8_t c, uint16_t d1, uint16_t d2)
    234 {
    235   DC_LOW();
    236 
    237   WRITE(c);
    238 
    239   DC_HIGH();
    240 
    241   _data16.value = d1;
    242   WRITE(_data16.msb);
    243   WRITE(_data16.lsb);
    244 
    245   _data16.value = d2;
    246   WRITE(_data16.msb);
    247   WRITE(_data16.lsb);
    248 }
    249 
    250 void Arduino_ESP32PAR8QQ::writeC8D16D16Split(uint8_t c, uint16_t d1, uint16_t d2)
    251 {
    252   DC_LOW();
    253 
    254   WRITE(c);
    255 
    256   DC_HIGH();
    257 
    258   _data16.value = d1;
    259   WRITE(_data16.msb);
    260   WRITE(_data16.lsb);
    261 
    262   _data16.value = d2;
    263   WRITE(_data16.msb);
    264   WRITE(_data16.lsb);
    265 }
    266 
    267 void Arduino_ESP32PAR8QQ::writeBytes(uint8_t *data, uint32_t len)
    268 {
    269   while (len--)
    270   {
    271     WRITE(*data++);
    272   }
    273 }
    274 
    275 void Arduino_ESP32PAR8QQ::writePattern(uint8_t *data, uint8_t len, uint32_t repeat)
    276 {
    277   while (repeat--)
    278   {
    279     writeBytes(data, len);
    280   }
    281 }
    282 
    283 void Arduino_ESP32PAR8QQ::writeIndexedPixels(uint8_t *data, uint16_t *idx, uint32_t len)
    284 {
    285   while (len--)
    286   {
    287     _data16.value = idx[*data++];
    288     WRITE(_data16.msb);
    289     WRITE(_data16.lsb);
    290   }
    291 }
    292 
    293 void Arduino_ESP32PAR8QQ::writeIndexedPixelsDouble(uint8_t *data, uint16_t *idx, uint32_t len)
    294 {
    295   while (len--)
    296   {
    297     _data16.value = idx[*data++];
    298     WRITE(_data16.msb);
    299     WRITE(_data16.lsb);
    300     WRITE(_data16.msb);
    301     WRITE(_data16.lsb);
    302   }
    303 }
    304 
    305 INLINE void Arduino_ESP32PAR8QQ::WRITE(uint8_t d)
    306 {
    307   uint32_t setMask = _xset_mask[d];
    308   GPIO.out_w1tc = _dataClrMask;
    309   GPIO.out_w1ts = setMask;
    310 }
    311 
    312 /******** low level bit twiddling **********/
    313 
    314 INLINE void Arduino_ESP32PAR8QQ::DC_HIGH(void)
    315 {
    316   *_dcPortSet = _dcPinMask;
    317 }
    318 
    319 INLINE void Arduino_ESP32PAR8QQ::DC_LOW(void)
    320 {
    321   *_dcPortClr = _dcPinMask;
    322 }
    323 
    324 INLINE void Arduino_ESP32PAR8QQ::CS_HIGH(void)
    325 {
    326   if (_cs != GFX_NOT_DEFINED)
    327   {
    328     *_csPortSet = _csPinMask;
    329   }
    330 }
    331 
    332 INLINE void Arduino_ESP32PAR8QQ::CS_LOW(void)
    333 {
    334   if (_cs != GFX_NOT_DEFINED)
    335   {
    336     *_csPortClr = _csPinMask;
    337   }
    338 }
    339 
    340 #endif // #if defined(ESP32) && (CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3)