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_XL9535SWSPI.cpp (5355B)
1 #include "Arduino_XL9535SWSPI.h" 2 3 Arduino_XL9535SWSPI::Arduino_XL9535SWSPI(int8_t sda, int8_t scl, int8_t pwd, int8_t cs, int8_t sck, int8_t mosi, TwoWire *wire) 4 : _sda(sda), _scl(scl), _pwd(pwd), _cs(cs), _sck(sck), _mosi(mosi), _wire(wire) 5 { 6 } 7 8 bool Arduino_XL9535SWSPI::begin(int32_t speed, int8_t dataMode) 9 { 10 UNUSED(speed); 11 UNUSED(dataMode); 12 13 _address = XL9535_IIC_ADDRESS; 14 _wire->beginTransmission(_address); 15 if (!_wire->endTransmission()) 16 { 17 Serial.println("Found xl9535"); 18 is_found = true; 19 this->pinMode8(0, 0xFF, OUTPUT); 20 if (_pwd != GFX_NOT_DEFINED) 21 { 22 // this->pinMode(_pwd, OUTPUT); 23 this->digitalWrite(_pwd, 1); 24 } 25 // this->pinMode(_cs, OUTPUT); 26 this->digitalWrite(_cs, 1); 27 // this->pinMode(_sck, OUTPUT); 28 this->digitalWrite(_sck, 1); 29 // this->pinMode(_mosi, OUTPUT); 30 this->digitalWrite(_mosi, 1); 31 } 32 else 33 { 34 Serial.println("xl9535 not found"); 35 is_found = false; 36 } 37 38 return true; 39 } 40 41 void Arduino_XL9535SWSPI::beginWrite() 42 { 43 this->digitalWrite(_cs, 0); 44 } 45 46 void Arduino_XL9535SWSPI::endWrite() 47 { 48 this->digitalWrite(_cs, 1); 49 } 50 51 void Arduino_XL9535SWSPI::writeCommand(uint8_t c) 52 { 53 // D/C bit, command 54 this->digitalWrite(_mosi, 0); 55 this->digitalWrite(_sck, 0); 56 this->digitalWrite(_sck, 1); 57 58 uint8_t bit = 0x80; 59 while (bit) 60 { 61 if (c & bit) 62 { 63 this->digitalWrite(_mosi, 1); 64 } 65 else 66 { 67 this->digitalWrite(_mosi, 0); 68 } 69 this->digitalWrite(_sck, 0); 70 bit >>= 1; 71 this->digitalWrite(_sck, 1); 72 } 73 } 74 75 void Arduino_XL9535SWSPI::writeCommand16(uint16_t) 76 { 77 } 78 79 void Arduino_XL9535SWSPI::write(uint8_t d) 80 { 81 // D/C bit, data 82 this->digitalWrite(_mosi, 1); 83 this->digitalWrite(_sck, 0); 84 this->digitalWrite(_sck, 1); 85 86 uint8_t bit = 0x80; 87 while (bit) 88 { 89 if (d & bit) 90 { 91 this->digitalWrite(_mosi, 1); 92 } 93 else 94 { 95 this->digitalWrite(_mosi, 0); 96 } 97 this->digitalWrite(_sck, 0); 98 bit >>= 1; 99 this->digitalWrite(_sck, 1); 100 } 101 } 102 103 void Arduino_XL9535SWSPI::write16(uint16_t) 104 { 105 // not implemented 106 } 107 108 void Arduino_XL9535SWSPI::writeRepeat(uint16_t p, uint32_t len) 109 { 110 // not implemented 111 } 112 113 void Arduino_XL9535SWSPI::writePixels(uint16_t *data, uint32_t len) 114 { 115 // not implemented 116 } 117 118 #if !defined(LITTLE_FOOT_PRINT) 119 void Arduino_XL9535SWSPI::writeBytes(uint8_t *data, uint32_t len) 120 { 121 // not implemented 122 } 123 124 void Arduino_XL9535SWSPI::writePattern(uint8_t *data, uint8_t len, uint32_t repeat) 125 { 126 // not implemented 127 } 128 #endif // !defined(LITTLE_FOOT_PRINT) 129 130 void Arduino_XL9535SWSPI::writeRegister(uint8_t reg, uint8_t *data, uint8_t len) 131 { 132 _wire->beginTransmission(_address); 133 _wire->write(reg); 134 for (uint8_t i = 0; i < len; i++) 135 { 136 _wire->write(data[i]); 137 } 138 _wire->endTransmission(); 139 } 140 141 uint8_t Arduino_XL9535SWSPI::readRegister(uint8_t reg, uint8_t *data, uint8_t len) 142 { 143 _wire->beginTransmission(_address); 144 _wire->write(reg); 145 _wire->endTransmission(); 146 _wire->requestFrom(_address, len); 147 uint8_t index = 0; 148 while (index < len) 149 data[index++] = _wire->read(); 150 return 0; 151 } 152 153 void Arduino_XL9535SWSPI::pinMode(uint8_t pin, uint8_t mode) 154 { 155 if (is_found) 156 { 157 uint8_t port = 0; 158 if (pin > 7) 159 { 160 this->readRegister(XL9535_CONFIG_PORT_1_REG, &port, 1); 161 if (mode == OUTPUT) 162 { 163 port = port & (~(1 << (pin - 10))); 164 } 165 else 166 { 167 port = port | (1 << (pin - 10)); 168 } 169 this->writeRegister(XL9535_CONFIG_PORT_1_REG, &port, 1); 170 } 171 else 172 { 173 this->readRegister(XL9535_CONFIG_PORT_0_REG, &port, 1); 174 if (mode == OUTPUT) 175 { 176 port = port & (~(1 << pin)); 177 } 178 else 179 { 180 port = port | (1 << pin); 181 } 182 this->writeRegister(XL9535_CONFIG_PORT_0_REG, &port, 1); 183 } 184 } 185 else 186 { 187 Serial.println("xl9535 not found"); 188 } 189 } 190 void Arduino_XL9535SWSPI::pinMode8(uint8_t port, uint8_t pin, uint8_t mode) 191 { 192 if (is_found) 193 { 194 uint8_t _pin = (mode != OUTPUT) ? pin : ~pin; 195 if (port) 196 { 197 this->writeRegister(XL9535_CONFIG_PORT_1_REG, &_pin, 1); 198 } 199 else 200 { 201 this->writeRegister(XL9535_CONFIG_PORT_0_REG, &_pin, 1); 202 } 203 } 204 else 205 { 206 Serial.println("xl9535 not found"); 207 } 208 } 209 210 void Arduino_XL9535SWSPI::digitalWrite(uint8_t pin, uint8_t val) 211 { 212 if (is_found) 213 { 214 uint8_t port = 0; 215 uint8_t reg_data = 0; 216 if (pin > 7) 217 { 218 this->readRegister(XL9535_OUTPUT_PORT_1_REG, ®_data, 1); 219 reg_data = reg_data & (~(1 << (pin - 10))); 220 port = reg_data | val << (pin - 10); 221 this->writeRegister(XL9535_OUTPUT_PORT_1_REG, &port, 1); 222 } 223 else 224 { 225 this->readRegister(XL9535_OUTPUT_PORT_0_REG, ®_data, 1); 226 reg_data = reg_data & (~(1 << pin)); 227 port = reg_data | val << pin; 228 this->writeRegister(XL9535_OUTPUT_PORT_0_REG, &port, 1); 229 } 230 } 231 else 232 { 233 Serial.println("xl9535 not found"); 234 } 235 } 236 237 int Arduino_XL9535SWSPI::digitalRead(uint8_t pin) 238 { 239 if (is_found) 240 { 241 int state = 0; 242 uint8_t port = 0; 243 if (pin > 7) 244 { 245 this->readRegister(XL9535_INPUT_PORT_1_REG, &port, 1); 246 state = port & (pin - 10) ? 1 : 0; 247 } 248 else 249 { 250 this->readRegister(XL9535_INPUT_PORT_0_REG, &port, 1); 251 state = port & pin ? 1 : 0; 252 } 253 return state; 254 } 255 else 256 { 257 Serial.println("xl9535 not found"); 258 } 259 return 0; 260 }