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

ModulesCSTMutual.tpp (4516B)

      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      TouchLibCSTMutual.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/CSTMutualConstants.h"
     35 #include "TouchLibCommon.tpp"
     36 #include "TouchLibInterface.hpp"
     37 
     38 class TouchLibCSTMutual : public TouchLibCommon<TouchLibCSTMutual>, public TouchLibInterface
     39 {
     40   friend class TouchLibCommon<TouchLibCSTMutual>;
     41 
     42 public:
     43 #if defined(ARDUINO)
     44   TouchLibCSTMutual(TwoWire &w, int sda = SDA, int scl = SCL, uint8_t addr = CTS328_SLAVE_ADDRESS, int rst = -1)
     45   {
     46     __wire = &w;
     47     __sda = sda;
     48     __scl = scl;
     49     __addr = addr;
     50     __rst = rst;
     51   }
     52 #endif
     53 
     54   TouchLibCSTMutual()
     55   {
     56 #if defined(ARDUINO)
     57     __wire = &Wire;
     58     __sda = SDA;
     59     __scl = SCL;
     60     __rst = -1;
     61 #endif
     62     __addr = CTS328_SLAVE_ADDRESS;
     63   }
     64 
     65   ~TouchLibCSTMutual()
     66   {
     67     log_i("~TouchLibCSTMutual");
     68     deinit();
     69   }
     70 
     71   bool init() { return begin(); }
     72 
     73   void deinit() { end(); }
     74 
     75   bool enableSleep() { return this->writeRegister((uint8_t)(CHIP_DEEP_SLEEP_REG >> 8), (uint8_t)(CHIP_DEEP_SLEEP_REG & 0xFF)); }
     76 
     77   bool read()
     78   {
     79     this->readRegister(MODE_NORMAL_0_REG, raw_data, sizeof(raw_data));
     80     this->writeRegister(MODE_NORMAL_0_REG, (uint8_t)0xAB); // sync signal
     81     // return raw_data[MODE_NORMAL_6_REG & 0xFF] == 0XAB ? 1 : 0;
     82     return ((raw_data[MODE_NORMAL_5_REG & 0xff] & 0x0f) != 0 ? 1 : 0);
     83   }
     84 
     85   uint8_t getPointNum() { return raw_data[MODE_NORMAL_5_REG & 0xff] & 0x0f; }
     86 
     87   TP_Point getPoint(uint8_t n)
     88   {
     89     TP_Point t;
     90     switch (n)
     91     {
     92     case 0:
     93       t.state = raw_data[MODE_NORMAL_0_REG & 0xFF] & 0xF;
     94       t.x = COMBINE_H8L4_H(MODE_NORMAL_1_REG, MODE_NORMAL_3_REG);
     95       t.y = COMBINE_H8L4_L(MODE_NORMAL_2_REG, MODE_NORMAL_3_REG);
     96       t.pressure = raw_data[MODE_NORMAL_4_REG & 0xF];
     97       break;
     98     case 1:
     99       t.state = raw_data[MODE_NORMAL_7_REG & 0xFF] & 0xF;
    100       t.x = COMBINE_H8L4_H(MODE_NORMAL_8_REG, MODE_NORMAL_10_REG);
    101       t.y = COMBINE_H8L4_L(MODE_NORMAL_9_REG, MODE_NORMAL_10_REG);
    102       t.pressure = raw_data[MODE_NORMAL_11_REG & 0xF];
    103       break;
    104     case 2:
    105       t.state = raw_data[MODE_NORMAL_12_REG & 0xFF] & 0xF;
    106       t.x = COMBINE_H8L4_H(MODE_NORMAL_13_REG, MODE_NORMAL_15_REG);
    107       t.y = COMBINE_H8L4_L(MODE_NORMAL_14_REG, MODE_NORMAL_15_REG);
    108       t.pressure = raw_data[MODE_NORMAL_16_REG & 0xF];
    109       break;
    110     case 3:
    111       t.state = raw_data[MODE_NORMAL_17_REG & 0xFF] & 0xF;
    112       t.x = COMBINE_H8L4_H(MODE_NORMAL_18_REG, MODE_NORMAL_20_REG);
    113       t.y = COMBINE_H8L4_L(MODE_NORMAL_19_REG, MODE_NORMAL_20_REG);
    114       t.pressure = raw_data[MODE_NORMAL_21_REG & 0xF];
    115       break;
    116     case 4:
    117       t.state = raw_data[MODE_NORMAL_22_REG & 0xFF] & 0xF;
    118       t.x = COMBINE_H8L4_H(MODE_NORMAL_23_REG, MODE_NORMAL_25_REG);
    119       t.y = COMBINE_H8L4_L(MODE_NORMAL_24_REG, MODE_NORMAL_25_REG);
    120       t.pressure = raw_data[MODE_NORMAL_26_REG & 0xF];
    121       break;
    122     default:
    123       log_i("The parameter range of getPoint is between 0 and 4.");
    124       break;
    125     }
    126     t.id = n;
    127 
    128     if (rotation == 0)
    129     {
    130     }
    131     else if (rotation == 1)
    132     {
    133       uint16_t tmp = t.x;
    134       t.x = t.y;
    135       t.y = tmp;
    136     }
    137     return t;
    138   }
    139 
    140   void setRotation(uint8_t r) { rotation = r % 4; }
    141 
    142   uint8_t getRotation() { return rotation; }
    143 
    144 protected:
    145   bool initImpl() { return true; }
    146   uint8_t raw_data[27] = {0};
    147   uint8_t rotation = 0;
    148 };