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

ModulesGT911.tpp (3873B)

      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      ModulesGT911.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/GT911Constants.h"
     35 #include "TouchLibCommon.tpp"
     36 #include "TouchLibInterface.hpp"
     37 
     38 class TouchLibGT911 : public TouchLibCommon<TouchLibGT911>, public TouchLibInterface
     39 {
     40     friend class TouchLibCommon<TouchLibGT911>;
     41 
     42 public:
     43 #if defined(ARDUINO)
     44     TouchLibGT911(TwoWire &w, int sda = SDA, int scl = SCL, uint8_t addr = GT911_SLAVE_ADDRESS2, int rst = -1)
     45     {
     46         __wire = &w;
     47         __sda = sda;
     48         __scl = scl;
     49         __addr = addr;
     50         __rst = rst;
     51     }
     52 #endif
     53 
     54     TouchLibGT911()
     55     {
     56 #if defined(ARDUINO)
     57         __wire = &Wire;
     58         __sda = SDA;
     59         __scl = SCL;
     60         __rst = -1;
     61 #endif
     62         __addr = GT911_SLAVE_ADDRESS2;
     63     }
     64 
     65     ~TouchLibGT911()
     66     {
     67         log_i("~TouchLibGT911");
     68         deinit();
     69     }
     70 
     71     bool init()
     72     {
     73         begin();
     74 
     75         this->writeRegister(GT911_COMMAND, (uint8_t)0x02); // software reset
     76         delay(200);
     77         int val = readRegister(GT911_MODULE_SWITCH_1);
     78         val &= 0XFC;
     79         val |= 0x03;
     80         this->writeRegister(GT911_MODULE_SWITCH_1, (uint8_t)val);
     81         delay(200);
     82         return  true;
     83     }
     84 
     85     void deinit()
     86     {
     87         end();
     88     }
     89 
     90     bool enableSleep()
     91     {
     92         this->writeRegister(GT911_COMMAND, (uint8_t)0x05);
     93         return 0;
     94     }
     95 
     96     bool read()
     97     {
     98         this->readRegister(GT911_POINT_INFO, raw_data, sizeof(raw_data));
     99         this->writeRegister(GT911_POINT_INFO, (uint8_t)0x00); // sync signal
    100         return (raw_data[0] & 0xF) != 0 ? true : false;
    101     }
    102 
    103     uint8_t getPointNum()
    104     {
    105         return raw_data[0] & 0xF;
    106     }
    107 
    108     TP_Point getPoint(uint8_t n)
    109     {
    110         if (n > 4) {
    111             log_i("The parameter range of getPoint is between 0 and 4.");
    112             return TP_Point(0, 0, 0, 0, 0, 0);
    113         }
    114 
    115         TP_Point t;
    116         uint16_t point_reg[5] = GT911_POINTS_REG;
    117         uint16_t offset = point_reg[n] - GT911_POINT_INFO;
    118 
    119         t.id = raw_data[offset];
    120         t.x = raw_data[offset + 1] + (raw_data[offset + 2] << 8);
    121         t.y = raw_data[offset + 3] + (raw_data[offset + 4] << 8);
    122         t.size = raw_data[offset + 5] | (raw_data[offset + 6] << 8);
    123 
    124         if (rotation == 0) {
    125         } else if (rotation == 1) {
    126             uint16_t tmp = t.x;
    127             t.x = t.y;
    128             t.y = tmp;
    129         }
    130         return t;
    131     }
    132 
    133     void setRotation(uint8_t r)
    134     {
    135         rotation = r % 4;
    136     }
    137 
    138     uint8_t getRotation()
    139     {
    140         return rotation;
    141     }
    142 
    143 protected:
    144     bool initImpl()
    145     {
    146         return true;
    147     }
    148     uint8_t raw_data[40] = {0};
    149     uint8_t rotation = 0;
    150 };