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

ModulesCSTSelf.tpp (3613B)

      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      ModulesCSTSelf.tpp
     26  * @author    Micky (513673326@qq.com)
     27  * @date      2022-10-24
     28  *
     29  */
     30 
     31 #if defined(ARDUINO)
     32 #include <Arduino.h>
     33 #endif
     34 #include "REG/CSTSelfConstants.h"
     35 #include "TouchLibCommon.tpp"
     36 #include "TouchLibInterface.hpp"
     37 
     38 class TouchLibCSTSelf : public TouchLibCommon<TouchLibCSTSelf>, public TouchLibInterface, public TouchLibGesture {
     39   friend class TouchLibCommon<TouchLibCSTSelf>;
     40 
     41 public:
     42 #if defined(ARDUINO)
     43   TouchLibCSTSelf(TwoWire &w, int sda = SDA, int scl = SCL, uint8_t addr = CTS826_SLAVE_ADDRESS, int rst = -1) {
     44     __wire = &w;
     45     __sda = sda;
     46     __scl = scl;
     47     __addr = addr;
     48     __rst = rst;
     49   }
     50 #endif
     51 
     52   TouchLibCSTSelf() {
     53 #if defined(ARDUINO)
     54     __wire = &Wire;
     55     __sda = SDA;
     56     __scl = SCL;
     57     __rst = -1;
     58 #endif
     59     __addr = CTS826_SLAVE_ADDRESS;
     60   }
     61 
     62   ~TouchLibCSTSelf() {
     63     log_i("~TouchLibCSTSelf");
     64     deinit();
     65   }
     66 
     67   bool init() { return begin(); }
     68 
     69   void deinit() { end(); }
     70 
     71   bool enableSleep() { return this->writeRegister(SLEEP_REG, (uint8_t)(0x03)); }
     72 
     73   bool read() {
     74     this->readRegister(WORK_MODE_REG, raw_data, sizeof(raw_data));
     75     return raw_data[TOUCH_NUM_REG] > 0;
     76   }
     77 
     78   uint8_t getPointNum() { return raw_data[TOUCH_NUM_REG] & 0x0f; }
     79 
     80   TP_Point getPoint(uint8_t n) {
     81     TP_Point t;
     82     switch (n) {
     83     case 0:
     84       t.state = raw_data[TOUCH1_XH_REG] >> 6;
     85       t.x = COMBINE_H4L8(TOUCH1_XH_REG, TOUCH1_XL_REG);
     86       t.y = COMBINE_H4L8(TOUCH1_YH_REG, TOUCH1_YL_REG);
     87       break;
     88     case 1:
     89       t.state = raw_data[TOUCH2_XH_REG] >> 6;
     90       t.x = COMBINE_H4L8(TOUCH2_XH_REG, TOUCH2_XL_REG);
     91       t.y = COMBINE_H4L8(TOUCH2_YH_REG, TOUCH2_YL_REG);
     92       break;
     93     default:
     94       log_i("The parameter range of getPoint is between 0 and 1.");
     95       break;
     96     }
     97     t.id = n;
     98 
     99     if (rotation == 0) {
    100     } else if (rotation == 1) {
    101       uint16_t tmp = t.x;
    102       t.x = t.y;
    103       t.y = tmp;
    104     }
    105     return t;
    106   }
    107 
    108   void setRotation(uint8_t r) { rotation = r % 4; }
    109 
    110   uint8_t getRotation() { return rotation; }
    111 
    112   bool isEnableGesture() { return this->readRegister(GES_STATE_REG); }
    113 
    114   bool enableGesture() {
    115     uint8_t t = 0x01;
    116     return this->writeRegister(GES_STATE_REG, t);
    117   }
    118 
    119   bool disableGesture() {
    120     uint8_t t = 0x00;
    121     return this->writeRegister(GES_STATE_REG, t);
    122   }
    123 
    124   uint8_t getGesture(void) { return this->readRegister(GESTURE_ID_REG); }
    125 
    126 protected:
    127   bool initImpl() { return true; }
    128   uint8_t raw_data[13] = {0};
    129   uint8_t rotation = 0;
    130 };