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

ModulesZTW622.tpp (6444B)

      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      TouchLibZTW622.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/ZTW622Constants.h"
     35 #include "TouchLibCommon.tpp"
     36 #include "TouchLibInterface.hpp"
     37 
     38 class TouchLibZTW622 : public TouchLibCommon<TouchLibZTW622>, public TouchLibInterface {
     39   friend class TouchLibCommon<TouchLibZTW622>;
     40 
     41 public:
     42 #if defined(ARDUINO)
     43   TouchLibZTW622(TwoWire &w, int sda = SDA, int scl = SCL, uint8_t addr = ZTW622_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   TouchLibZTW622() {
     53 #if defined(ARDUINO)
     54     __wire = &Wire;
     55     __sda = SDA;
     56     __scl = SCL;
     57     __rst = -1;
     58 #endif
     59     __addr = ZTW622_SLAVE_ADDRESS;
     60   }
     61 
     62   ~TouchLibZTW622() {
     63     log_i("~TouchLibZTW622");
     64     deinit();
     65   }
     66 
     67   bool init() {
     68     int ret = 0;
     69     begin();
     70     ret = this->writeRegister((uint16_t)(0x000C), (uint16_t)(0x0001));
     71     if (ret) {
     72       log_i("power sequence error (vendor cmd enable)");
     73       return false;
     74     }
     75     /* Read the value of the 0xCC00 address, the ID of the chip */
     76     ret = this->readRegister((uint16_t)(0xCC00), (uint8_t *)&chip_code, 2);
     77     if (ret) {
     78       log_i("fail to read chip code");
     79       return false;
     80     }
     81     log_i("chip code = 0x%x\n", chip_code);
     82 
     83     ret = this->writeRegister((uint8_t)(0xC0), (uint8_t)(0x04));
     84     if (ret) {
     85       log_i("power sequence error (intn clear)");
     86       return false;
     87     }
     88 
     89     ret = this->writeRegister((uint16_t)(0xC002), (uint16_t)(0x0001));
     90     if (ret) {
     91       log_i("power sequence error (nvm init)");
     92       return false;
     93     }
     94 
     95     ret = this->writeRegister((uint16_t)(0xC001), (uint16_t)(0x0001));
     96     if (ret) {
     97       log_i("power sequence error (program start)");
     98       return false;
     99     }
    100     delay(150);
    101 
    102     ret = this->readRegister(ZINITIX_FIRMWARE_VERSION, (uint8_t *)&firmware_version, 2);
    103     if (ret) {
    104       log_i("fail to read FIRMWARE_VERSION");
    105       return false;
    106     }
    107     /* 0x07 version cannot wake up from sleep */
    108     if (firmware_version != 0x07)
    109       ztw622_has_sleep_function = true;
    110     log_i("touch FW version = %d\r\n", firmware_version);
    111 
    112     ret = this->readRegister(ZINITIX_DATA_VERSION_REG, (uint8_t *)&reg_data_version, 2);
    113     if (ret) {
    114       log_i("fail to read reg version");
    115       return false;
    116     }
    117     log_i("touch register version = %d\r\n", reg_data_version);
    118 
    119     if (reg_data_version >= 3)
    120       ztw622_has_palm_coverage_detection = true;
    121 
    122     ret = this->writeRegister(ZINITIX_INITIAL_TOUCH_MODE, (uint16_t)(0x0001));
    123     if (ret) {
    124       log_i("fail to write inital touch mode");
    125       return false;
    126     }
    127     ret = this->writeRegister(ZINITIX_TOUCH_MODE, (uint16_t)(0x0001));
    128     if (ret) {
    129       log_i("fail to write touh mode");
    130       return false;
    131     }
    132     ret = this->writeRegister(ZINITIX_SUPPORTED_FINGER_NUM, (uint16_t)(0x0001));
    133     if (ret) {
    134       log_i("fail to write finger num");
    135       return false;
    136     }
    137 
    138     ret = this->writeRegister(ZINITIX_X_RESOLUTION, (uint16_t)(0x0001));
    139     if (ret) {
    140       log_i("fail to write finger num");
    141       return false;
    142     }
    143     ret = this->writeRegister(ZINITIX_Y_RESOLUTION, (uint16_t)(0x0001));
    144     if (ret) {
    145       log_i("fail to write finger num");
    146       return false;
    147     }
    148     ret = this->writeRegister((uint8_t)(0x00), (uint8_t)(0x06));
    149     if (ret) {
    150       log_i("fail to write finger num");
    151       return false;
    152     }
    153     for (int i = 0; i < 10; i++) {
    154       this->writeRegister((uint8_t)(0x00), (uint8_t)(0x03));
    155       delay(10);
    156     }
    157 
    158     return 1;
    159   }
    160 
    161   void deinit() { end(); }
    162 
    163   bool enableSleep() { return 1; }
    164 
    165   bool read() {
    166     this->readRegister(ZINITIX_POINT_STATUS_REG, raw_data, sizeof(raw_data));
    167 
    168     // for (int i = 0; i < 16; i++) {
    169     //   Serial.printf("[%d]0x%02X ", i, raw_data[i]);
    170     // }
    171     // Serial.println();
    172 
    173     // this->readRegister(POINT_COUNT_TIMESTAMP_REG, &raw_data[2], 2);
    174 
    175     // if ((raw_data[3] << 8 | raw_data[2]) != 0) {
    176     // this->writeRegister((uint8_t)(ZINITIX_CLEAR_INT_CMD >> 8), (uint8_t)(ZINITIX_CLEAR_INT_CMD & 0xFF));
    177     // return 1;
    178     // }
    179     // return 0;
    180     return (raw_data[1] & BIT_POINT_DETECTED);
    181   }
    182 
    183   uint8_t getPointNum() { return (raw_data[3] << 8 | raw_data[2]); }
    184 
    185   TP_Point getPoint(uint8_t n) {
    186     if (n > 4) {
    187       log_i("The parameter range of getPoint is between 0 and 4.");
    188       return TP_Point(0, 0, 0, 0, 0, 0);
    189     }
    190 
    191     TP_Point t;
    192     uint16_t point_reg[5] = ZINITIX_POINTS_REG;
    193     uint16_t offset = point_reg[n] - ZINITIX_POINT_STATUS_REG;
    194 
    195     t.id = n;
    196     t.x = raw_data[offset] + (raw_data[offset + 1] << 8);
    197     t.y = raw_data[offset + 2] + (raw_data[offset + 3] << 8);
    198     t.pressure = raw_data[offset + 4];
    199     t.state = raw_data[offset + 5];
    200 
    201     if (rotation == 0) {
    202     } else if (rotation == 1) {
    203       uint16_t tmp = t.x;
    204       t.x = t.y;
    205       t.y = tmp;
    206     }
    207     return t;
    208   }
    209   void setRotation(uint8_t r) { rotation = r % 4; }
    210 
    211   uint8_t getRotation() { return 1; }
    212 
    213 protected:
    214   bool initImpl() { return true; }
    215   uint8_t raw_data[33] = {0};
    216   uint8_t rotation = 0;
    217   bool ztw622_has_sleep_function = false;
    218   bool ztw622_has_palm_coverage_detection = false;
    219   uint16_t chip_code = 0;
    220   uint16_t firmware_version = 0;
    221   uint16_t reg_data_version = 0;
    222 };