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_AVRPAR16.cpp (3854B)

      1 #ifdef __AVR__
      2 
      3 #include "Arduino_AVRPAR16.h"
      4 
      5 Arduino_AVRPAR16::Arduino_AVRPAR16(int8_t dc, int8_t cs, int8_t wr, int8_t rd, uint8_t portLow, uint8_t portHigh)
      6     : _dc(dc), _cs(cs), _wr(wr), _rd(rd), _portLow(portLow), _portHigh(portHigh)
      7 {
      8 }
      9 
     10 bool Arduino_AVRPAR16::begin(int32_t speed, int8_t dataMode)
     11 {
     12   UNUSED(speed);
     13   UNUSED(dataMode);
     14 
     15   pinMode(_dc, OUTPUT);
     16   digitalWrite(_dc, HIGH); // Data mode
     17   _dcPort = (PORTreg_t)portOutputRegister(digitalPinToPort(_dc));
     18   _dcPinMaskSet = digitalPinToBitMask(_dc);
     19   _dcPinMaskClr = ~_dcPinMaskSet;
     20 
     21   if (_cs != GFX_NOT_DEFINED)
     22   {
     23     pinMode(_cs, OUTPUT);
     24     digitalWrite(_cs, HIGH); // Disable chip select
     25     _csPort = (PORTreg_t)portOutputRegister(digitalPinToPort(_cs));
     26     _csPinMaskSet = digitalPinToBitMask(_cs);
     27   }
     28   _csPinMaskClr = ~_csPinMaskSet;
     29 
     30   pinMode(_wr, OUTPUT);
     31   digitalWrite(_wr, HIGH); // Set write strobe high (inactive)
     32   _wrPort = (PORTreg_t)portOutputRegister(digitalPinToPort(_wr));
     33   _wrPinMaskSet = digitalPinToBitMask(_wr);
     34   _wrPinMaskClr = ~_wrPinMaskSet;
     35 
     36   if (_rd != GFX_NOT_DEFINED)
     37   {
     38     pinMode(_rd, OUTPUT);
     39     digitalWrite(_rd, HIGH); // Disable RD
     40     _rdPort = (PORTreg_t)portOutputRegister(digitalPinToPort(_rd));
     41     _rdPinMaskSet = digitalPinToBitMask(_rd);
     42   }
     43   else
     44   {
     45     _rdPort = _dcPort;
     46     _rdPinMaskSet = 0;
     47   }
     48   _rdPinMaskClr = ~_rdPinMaskSet;
     49 
     50   *(portModeRegister(_portLow)) = 0xFF;
     51   _dataPortLow = portOutputRegister(_portLow);
     52   *_dataPortLow = 0xFF;
     53   *(portModeRegister(_portHigh)) = 0xFF;
     54   _dataPortHigh = portOutputRegister(_portHigh);
     55   *_dataPortHigh = 0xFF;
     56 
     57   return true;
     58 }
     59 
     60 void Arduino_AVRPAR16::beginWrite()
     61 {
     62   DC_HIGH();
     63   CS_LOW();
     64 }
     65 
     66 void Arduino_AVRPAR16::endWrite()
     67 {
     68   CS_HIGH();
     69 }
     70 
     71 void Arduino_AVRPAR16::writeCommand(uint8_t c)
     72 {
     73   DC_LOW();
     74 
     75   WRITE16(c);
     76 
     77   DC_HIGH();
     78 }
     79 
     80 void Arduino_AVRPAR16::writeCommand16(uint16_t c)
     81 {
     82   DC_LOW();
     83 
     84   WRITE16(c);
     85 
     86   DC_HIGH();
     87 }
     88 
     89 void Arduino_AVRPAR16::write(uint8_t d)
     90 {
     91   WRITE16(d);
     92 }
     93 
     94 void Arduino_AVRPAR16::write16(uint16_t d)
     95 {
     96   WRITE16(d);
     97 }
     98 
     99 void Arduino_AVRPAR16::writeRepeat(uint16_t p, uint32_t len)
    100 {
    101   uint8_t wrMaskBase = *_wrPort & _wrPinMaskClr;
    102   uint8_t wrMaskSet = wrMaskBase | _wrPinMaskSet;
    103   _data16.value = p;
    104   *_dataPortLow = _data16.lsb;
    105   *_dataPortHigh = _data16.msb;
    106   while (len--)
    107   {
    108     *_wrPort = wrMaskBase;
    109     *_wrPort = wrMaskSet;
    110   }
    111 }
    112 
    113 void Arduino_AVRPAR16::writePixels(uint16_t *data, uint32_t len)
    114 {
    115   while (len--)
    116   {
    117     WRITE16(*data++);
    118   }
    119 }
    120 
    121 void Arduino_AVRPAR16::writeC8D8(uint8_t c, uint8_t d)
    122 {
    123   DC_LOW();
    124 
    125   WRITE16(c);
    126 
    127   DC_HIGH();
    128 
    129   WRITE16(d);
    130 }
    131 
    132 void Arduino_AVRPAR16::writeC8D16(uint8_t c, uint16_t d)
    133 {
    134   DC_LOW();
    135 
    136   WRITE16(c);
    137 
    138   DC_HIGH();
    139 
    140   WRITE16(d);
    141 }
    142 
    143 void Arduino_AVRPAR16::writeC8D16D16(uint8_t c, uint16_t d1, uint16_t d2)
    144 {
    145   DC_LOW();
    146 
    147   WRITE16(c);
    148 
    149   DC_HIGH();
    150 
    151   WRITE16(d1);
    152   WRITE16(d2);
    153 }
    154 
    155 void Arduino_AVRPAR16::writeC8D16D16Split(uint8_t c, uint16_t d1, uint16_t d2)
    156 {
    157   DC_LOW();
    158 
    159   WRITE16(c);
    160 
    161   DC_HIGH();
    162 
    163   _data16.value = d1;
    164   WRITE16(_data16.msb);
    165   WRITE16(_data16.lsb);
    166 
    167   _data16.value = d2;
    168   WRITE16(_data16.msb);
    169   WRITE16(_data16.lsb);
    170 }
    171 
    172 INLINE void Arduino_AVRPAR16::WRITE16(uint16_t d)
    173 {
    174   uint8_t wrMaskBase = *_wrPort & _wrPinMaskClr;
    175   _data16.value = d;
    176   *_dataPortLow = _data16.lsb;
    177   *_dataPortHigh = _data16.msb;
    178   *_wrPort = wrMaskBase;
    179   *_wrPort = wrMaskBase | _wrPinMaskSet;
    180 }
    181 
    182 /******** low level bit twiddling **********/
    183 
    184 INLINE void Arduino_AVRPAR16::DC_HIGH(void)
    185 {
    186   *_dcPort |= _dcPinMaskSet;
    187 }
    188 
    189 INLINE void Arduino_AVRPAR16::DC_LOW(void)
    190 {
    191   *_dcPort &= _dcPinMaskClr;
    192 }
    193 
    194 INLINE void Arduino_AVRPAR16::CS_HIGH(void)
    195 {
    196   if (_cs != GFX_NOT_DEFINED)
    197   {
    198     *_csPort |= _csPinMaskSet;
    199   }
    200 }
    201 
    202 INLINE void Arduino_AVRPAR16::CS_LOW(void)
    203 {
    204   if (_cs != GFX_NOT_DEFINED)
    205   {
    206     *_csPort &= _csPinMaskClr;
    207   }
    208 }
    209 
    210 #endif // #ifdef __AVR__