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 |
TouchLibCommon.tpp (9392B)
1 /** 2 * 3 * @license MIT License 4 * 5 * Copyright (c) 2022 Micky 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a copy 8 * of this software and associated documentation files (the "Software"), to deal 9 * in the Software without restriction, including without limitation the rights 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 * copies of the Software, and to permit persons to whom the Software is 12 * furnished to do so, subject to the following conditions: 13 * 14 * The above copyright notice and this permission notice shall be included in all 15 * copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 * SOFTWARE. 24 * 25 * @file TouchCommon.tpp 26 * @author Micky (513673326@qq.com) 27 * @date 2022-10-24 28 * 29 */ 30 31 #pragma once 32 33 #if defined(ARDUINO) 34 #include <Wire.h> 35 #endif 36 37 #ifdef _BV 38 #undef _BV 39 #endif 40 #define _BV(b) (1ULL << (uint64_t)(b)) 41 42 #ifndef constrain 43 #define constrain(amt, low, high) ((amt) < (low) ? (low) : ((amt) > (high) ? (high) : (amt))) 44 #endif 45 46 #define COMBINE_H8L4_H(h, l) (raw_data[h & 0xFF] << 4 | raw_data[l & 0xFF] >> 4) 47 #define COMBINE_H8L4_L(h, l) (raw_data[h & 0xFF] << 4 | raw_data[l & 0xFF] & 0xF) 48 49 #define COMBINE_H4L8(h, l) ((raw_data[h] & 0x0F) << 8 | raw_data[l]) 50 51 #define IS_BIT_SET(val, mask) (((val) & (mask)) == (mask)) 52 53 #if !defined(ARDUINO) 54 #define log_e(...) 55 #define log_i(...) 56 #define log_d(...) 57 58 #define LOW 0x0 59 #define HIGH 0x1 60 61 // GPIO FUNCTIONS 62 #define INPUT 0x01 63 #define OUTPUT 0x03 64 #define PULLUP 0x04 65 #define INPUT_PULLUP 0x05 66 #define PULLDOWN 0x08 67 #define INPUT_PULLDOWN 0x09 68 69 #define RISING 0x01 70 #define FALLING 0x02 71 72 #endif 73 74 template <class chipType> class TouchLibCommon { 75 typedef int (*iic_fptr_t)(uint8_t devAddr, uint16_t regAddr, uint8_t *data, uint8_t len); 76 77 public: 78 #if defined(ARDUINO) 79 bool begin(TwoWire &w, int sda, int scl, uint8_t addr, int rst) { 80 if (__has_init) 81 return thisChip().initImpl(); 82 __has_init = true; 83 __wire = &w; 84 __wire->begin(sda, scl); 85 __addr = addr; 86 __rst = rst; 87 return thisChip().initImpl(); 88 } 89 #endif 90 91 bool begin(uint8_t addr, int rst, iic_fptr_t readRegCallback, iic_fptr_t writeRegCallback) { 92 if (__has_init) 93 return thisChip().initImpl(); 94 __has_init = true; 95 thisReadRegCallback = readRegCallback; 96 thisWriteRegCallback = writeRegCallback; 97 __addr = addr; 98 __rst = rst; 99 return thisChip().initImpl(); 100 } 101 102 // private: 103 protected: 104 int readRegister(uint8_t reg) { 105 uint8_t val = 0; 106 if (thisReadRegCallback != nullptr) { 107 if (thisReadRegCallback(__addr, reg, &val, 1) != 0) { 108 return 0; 109 } 110 return val; 111 } 112 #if defined(ARDUINO) 113 if (__wire) { 114 __wire->beginTransmission(__addr); 115 __wire->write(reg); 116 if (__wire->endTransmission() != 0) { 117 return -1; 118 } 119 __wire->requestFrom(__addr, 1U); 120 return __wire->read(); 121 } 122 #endif 123 return -1; 124 } 125 int readRegister(uint16_t reg) { 126 uint8_t val = 0; 127 if (thisReadRegCallback != nullptr) { 128 if (thisReadRegCallback(__addr, reg, &val, 1) != 0) { 129 return 0; 130 } 131 return val; 132 } 133 #if defined(ARDUINO) 134 if (__wire) { 135 __wire->beginTransmission(__addr); 136 __wire->write((uint8_t)(reg >> 8)); 137 __wire->write((uint8_t)(reg & 0xff)); 138 if (__wire->endTransmission() != 0) { 139 return -1; 140 } 141 __wire->requestFrom(__addr, 1U); 142 return __wire->read(); 143 } 144 #endif 145 return -1; 146 } 147 int writeRegister(uint8_t reg, uint8_t val) { 148 if (thisWriteRegCallback) { 149 return thisWriteRegCallback(__addr, reg, &val, 1); 150 } 151 #if defined(ARDUINO) 152 if (__wire) { 153 __wire->beginTransmission(__addr); 154 __wire->write(reg); 155 __wire->write(val); 156 return (__wire->endTransmission() == 0) ? 0 : -1; 157 } 158 #endif 159 return -1; 160 } 161 162 int writeRegister(uint16_t reg, uint8_t val) { 163 if (thisWriteRegCallback) { 164 return thisWriteRegCallback(__addr, reg, &val, 1); 165 } 166 #if defined(ARDUINO) 167 if (__wire) { 168 __wire->beginTransmission(__addr); 169 __wire->write(reg >> 8); 170 __wire->write(reg & 0xff); 171 __wire->write(val); 172 return (__wire->endTransmission() == 0) ? 0 : -1; 173 } 174 #endif 175 return -1; 176 } 177 178 int writeRegister(uint16_t reg, uint16_t val) { 179 if (thisWriteRegCallback) { 180 return thisWriteRegCallback(__addr, reg, (uint8_t *)&val, 2); 181 } 182 #if defined(ARDUINO) 183 if (__wire) { 184 __wire->beginTransmission(__addr); 185 __wire->write(reg >> 8); 186 __wire->write(reg & 0xff); 187 __wire->write(val >> 8); 188 __wire->write(val & 0xff); 189 return (__wire->endTransmission() == 0) ? 0 : -1; 190 } 191 #endif 192 return -1; 193 } 194 195 int readRegister(uint8_t reg, uint8_t *buf, uint8_t lenght) { 196 if (thisReadRegCallback) { 197 return thisReadRegCallback(__addr, reg, buf, lenght); 198 } 199 #if defined(ARDUINO) 200 if (__wire) { 201 __wire->beginTransmission(__addr); 202 __wire->write(reg); 203 if (__wire->endTransmission() != 0) { 204 return -1; 205 } 206 __wire->requestFrom(__addr, lenght); 207 return __wire->readBytes(buf, lenght) == lenght ? 0 : -1; 208 } 209 #endif 210 return -1; 211 } 212 213 int readRegister(uint16_t reg, uint8_t *buf, uint8_t lenght) { 214 if (thisReadRegCallback) { 215 return thisReadRegCallback(__addr, reg, buf, lenght); 216 } 217 #if defined(ARDUINO) 218 if (__wire) { 219 __wire->beginTransmission(__addr); 220 __wire->write(reg >> 8); 221 __wire->write(reg & 0xff); 222 if (__wire->endTransmission() != 0) { 223 return -1; 224 } 225 __wire->requestFrom(__addr, lenght); 226 return __wire->readBytes(buf, lenght) == lenght ? 0 : -1; 227 } 228 #endif 229 return -1; 230 } 231 232 int writeRegister(uint8_t reg, uint8_t *buf, uint8_t lenght) { 233 if (thisWriteRegCallback) { 234 return thisWriteRegCallback(__addr, reg, buf, lenght); 235 } 236 #if defined(ARDUINO) 237 if (__wire) { 238 __wire->beginTransmission(__addr); 239 __wire->write(reg); 240 __wire->write(buf, lenght); 241 return (__wire->endTransmission() == 0) ? 0 : -1; 242 } 243 #endif 244 return -1; 245 } 246 247 bool inline clrRegisterBit(uint8_t registers, uint8_t bit) { 248 int val = readRegister(registers); 249 if (val == -1) { 250 return false; 251 } 252 return writeRegister(registers, (val & (~_BV(bit)))) == 0; 253 } 254 255 bool inline setRegisterBit(uint8_t registers, uint8_t bit) { 256 int val = readRegister(registers); 257 if (val == -1) { 258 return false; 259 } 260 return writeRegister(registers, (val | (_BV(bit)))) == 0; 261 } 262 263 bool inline getRegisterBit(uint8_t registers, uint8_t bit) { 264 int val = readRegister(registers); 265 if (val == -1) { 266 return false; 267 } 268 return val & _BV(bit); 269 } 270 271 uint16_t inline readRegisterH8L4(uint8_t highReg, uint8_t lowReg) { 272 int h8 = readRegister(highReg); 273 int l4 = readRegister(lowReg); 274 if (h8 == -1 || l4 == -1) 275 return 0; 276 return (h8 << 4) | (l4 & 0x0F); 277 } 278 279 uint16_t inline readRegisterH8L5(uint8_t highReg, uint8_t lowReg) { 280 int h8 = readRegister(highReg); 281 int l5 = readRegister(lowReg); 282 if (h8 == -1 || l5 == -1) 283 return 0; 284 return (h8 << 5) | (l5 & 0x1F); 285 } 286 287 uint16_t inline readRegisterH6L8(uint8_t highReg, uint8_t lowReg) { 288 int h6 = readRegister(highReg); 289 int l8 = readRegister(lowReg); 290 if (h6 == -1 || l8 == -1) 291 return 0; 292 return ((h6 & 0x3F) << 8) | l8; 293 } 294 295 uint16_t inline readRegisterH5L8(uint8_t highReg, uint8_t lowReg) { 296 int h5 = readRegister(highReg); 297 int l8 = readRegister(lowReg); 298 if (h5 == -1 || l8 == -1) 299 return 0; 300 return ((h5 & 0x1F) << 8) | l8; 301 } 302 303 /* 304 * CRTP Helper 305 */ 306 protected: 307 bool begin() { 308 #if defined(ARDUINO) 309 if (__has_init) 310 return thisChip().initImpl(); 311 __has_init = true; 312 log_i("SDA:%d SCL:%d RST:%d", __sda, __scl, __rst); 313 if (__rst != -1) { 314 pinMode(__rst, OUTPUT); 315 digitalWrite(__rst, 0); 316 delay(200); 317 digitalWrite(__rst, 1); 318 delay(200); 319 } 320 __wire->begin(__sda, __scl); 321 __wire->beginTransmission(__addr); 322 uint8_t error = Wire.endTransmission(); 323 if (error != 0) 324 return false; 325 #endif /*ARDUINO*/ 326 return thisChip().initImpl(); 327 } 328 329 void end() { 330 #if defined(ARDUINO) 331 if (__wire) { 332 #if defined(ESP_IDF_VERSION) 333 #if ESP_IDF_VERSION > ESP_IDF_VERSION_VAL(4, 4, 0) 334 __wire->end(); 335 #endif /*ESP_IDF_VERSION*/ 336 #endif /*ESP_IDF_VERSION*/ 337 } 338 #endif /*ARDUINO*/ 339 } 340 341 inline const chipType &thisChip() const { return static_cast<const chipType &>(*this); } 342 343 inline chipType &thisChip() { return static_cast<chipType &>(*this); } 344 345 protected: 346 bool __has_init = false; 347 #if defined(ARDUINO) 348 TwoWire *__wire = NULL; 349 #endif 350 int __sda = -1; 351 int __scl = -1; 352 int __rst = -1; 353 uint8_t __addr = 0xFF; 354 iic_fptr_t thisReadRegCallback = NULL; 355 iic_fptr_t thisWriteRegCallback = NULL; 356 };