Skip to content

Commit

Permalink
Unification of touch interfaces class
Browse files Browse the repository at this point in the history
  • Loading branch information
lewisxhe committed Sep 21, 2023
1 parent 87011e4 commit fc3f087
Show file tree
Hide file tree
Showing 8 changed files with 265 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void setup()

void loop()
{
if (touch.getTouched()) {
if (touch.isPressed()) {
uint8_t point = touch.getPoint(x, y, 5);
Serial.print("Point:"); Serial.println(point);
uint8_t touched = touch.getPoint(x, y, 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ void setup()
void loop()
{

if (touch.getTouched()) {
if (touch.isPressed()) {
uint8_t point = touch.getPoint(x, y, 5);
Serial.print("Point:"); Serial.println(point);
uint8_t touched = touch.getPoint(x, y, 2);
Expand Down
7 changes: 5 additions & 2 deletions src/TouchDrvCHSC5816.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ class TouchDrvCHSC5816 :
if (y_array) {
*y_array = (unsigned int)(touch.rp.y_h4 << 8) | touch.rp.y_l8;
}

updateXY(1, x_array, y_array);

return 1;
}

Expand All @@ -171,9 +174,9 @@ class TouchDrvCHSC5816 :
return false;
}

uint8_t getChipID()
uint32_t getChipID()
{
return false;
return 0x00;
}

const char *getModelName()
Expand Down
45 changes: 4 additions & 41 deletions src/TouchDrvCSTXXX.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class TouchDrvCSTXXX :
TouchDrvCSTXXX(TwoWire &w,
int sda = DEFAULT_SDA,
int scl = DEFAULT_SCL,
uint8_t addr = CSTXXX_SLAVE_ADDRESS): __swapXY(false), __mirrorX(false), __mirrorY(false), __resX(0), __resY(0)
uint8_t addr = CSTXXX_SLAVE_ADDRESS)
{
__wire = & w;
__sda = sda;
Expand All @@ -58,7 +58,7 @@ class TouchDrvCSTXXX :
}
#endif

TouchDrvCSTXXX(): __swapXY(false), __mirrorX(false), __mirrorY(false), __resX(0), __resY(0)
TouchDrvCSTXXX()
{
#if defined(ARDUINO)
__wire = &Wire;
Expand Down Expand Up @@ -185,7 +185,7 @@ class TouchDrvCSTXXX :
return false;
}

uint8_t getChipID()
uint32_t getChipID()
{
return __chipID;
}
Expand Down Expand Up @@ -288,44 +288,8 @@ class TouchDrvCSTXXX :
this->user_data = user_data;
}


void setSwapXY(bool swap)
{
__swapXY = swap;
}

void setMirrorXY(bool mirrorX, bool mirrorY)
{
__mirrorX = mirrorX;
__mirrorY = mirrorY;
}

void setMaxCoordinates(uint16_t x, uint16_t y)
{
__xMax = x;
__yMax = y;
}


private:

void updateXY(uint8_t pointNum, int16_t *xBuffer, int16_t *yBuffer)
{
for (int i = 0; i < pointNum; ++i) {
if (__swapXY) {
uint16_t tmp = xBuffer[i];
xBuffer[i] = yBuffer[i];
yBuffer[i] = tmp;
}
if (__mirrorX && __xMax ) {
xBuffer[i] = __xMax - xBuffer[i];
}
if (__mirrorY && __yMax) {
yBuffer[i] = __yMax - yBuffer[i];
}
}
}

uint8_t updateCST226SE()
{
uint8_t buffer[CST226SE_BUFFER_NUM];
Expand Down Expand Up @@ -591,8 +555,7 @@ class TouchDrvCSTXXX :
home_button_callback_t __homeButtonCb = NULL;
void *user_data = NULL;
TouchData report;
uint16_t __resX, __resY, __xMax, __yMax;
bool __swapXY, __mirrorX, __mirrorY;

};


Expand Down
136 changes: 100 additions & 36 deletions src/TouchDrvFT6X36.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@


#include "REG/FT6X36Constants.h"
#include "TouchDrvInterface.hpp"
#include "SensorCommon.tpp"

class TouchDrvFT6X36 :
public TouchDrvInterface,
public SensorCommon<TouchDrvFT6X36>
{
friend class SensorCommon<TouchDrvFT6X36>;
Expand Down Expand Up @@ -100,9 +102,11 @@ class TouchDrvFT6X36 :
}
#endif

bool init()
bool init(int rst, int irq)
{
return begin();
__rst = rst;
__irq = irq;
return initImpl();
}

void deinit()
Expand All @@ -116,7 +120,7 @@ class TouchDrvFT6X36 :
}

// Obtaining gestures depends on whether the built-in firmware of the chip has this function
GesTrue getGesture(void)
uint8_t getGesture()
{
int val = readRegister(FT6X36_REG_GEST);
switch (val) {
Expand Down Expand Up @@ -194,23 +198,23 @@ class TouchDrvFT6X36 :
writeRegister(FT6X36_REG_INT_STATUS, 0);
}

uint8_t getPoint(int16_t *x, int16_t *y, uint8_t size = 1)
uint8_t getPoint(int16_t *x_array, int16_t *y_array, uint8_t size = 1)
{
uint8_t touchedID;
uint8_t buffer[16];

if (x == NULL || y == NULL || size == 0)
if (!x_array || !y_array || !size)
return 0;

if (readRegister(FT6X36_REG_MODE, buffer, 16) == -1) {
return false;
if (readRegister(FT6X36_REG_MODE, buffer, 16) == DEV_WIRE_ERR) {
return 0;
}

uint8_t mode = buffer[0];
//REG 0x01
uint8_t gestrue = buffer[1];
//REG 0x02
uint8_t touched = buffer[2] & 0x0F;
uint8_t point = buffer[2] & 0x0F;

//REG 0x03 ~ 0x04
uint8_t eventFlag = (buffer[3] & 0xC0) >> 6;
Expand All @@ -219,24 +223,24 @@ class TouchDrvFT6X36 :
uint16_t posY = ((buffer[5] & 0x0F) << 8) | buffer[6] ;


if (touched == 0) {
return false;
if (point == 0) {
return 0;
}

x[0] = posX;
y[0] = posY;
x_array[0] = posX;
y_array[0] = posY;

#ifdef LOG_PORT
LOG_PORT.println("----------------------------------------------------------------------------");
LOG_PORT.println("Touched Gesture EvenFlag [0]PosX [0]PosY [1]PosX [1]PosY");
LOG_PORT.print(touched); LOG_PORT.print("\t");
LOG_PORT.print(point); LOG_PORT.print("\t");
LOG_PORT.print(gestrue); LOG_PORT.print("\t");
LOG_PORT.print(eventFlag); LOG_PORT.print("\t");
LOG_PORT.print(posX); LOG_PORT.print("\t");
LOG_PORT.print(posY); LOG_PORT.print("\t");
#endif

if (touched == 2) {
if (point == 2) {
//REG 0x09 ~ 0x0A
posX = ((buffer[9] & 0x0F) << 8) | buffer[10];
//REG 0x0B ~ 0x0C
Expand All @@ -248,27 +252,57 @@ class TouchDrvFT6X36 :
#endif

if (size == 2) {
x[1] = posX;
y[1] = posY;
x_array[1] = posX;
y_array[1] = posY;
}
}
#ifdef LOG_PORT
LOG_PORT.println();
#endif
return touched;
updateXY(point, x_array, y_array);

return point;
}

uint8_t getTouched()
bool isPressed()
{
return readRegister(FT6X36_REG_STATUS);
if (__irq != SENSOR_PIN_NONE) {
return digitalRead(__irq) == LOW;
}
return readRegister(FT6X36_REG_STATUS) & 0x0F;
}

void setPowerMode(PowerMode mode)
{
writeRegister(FT6X36_REG_POWER_MODE, mode);
}

uint8_t getChipID(void)
void sleep()
{
writeRegister(FT6X36_REG_POWER_MODE, PMODE_DEEPSLEEP);
}

bool writeConfig(uint8_t *data, uint32_t size)
{
return false;
}

void wakeup()
{
reset();
}

void idle()
{

}

uint8_t getSupportTouchPoint()
{
return 1;
}

uint32_t getChipID(void)
{
return readRegister(FT6X36_REG_CHIPID);
}
Expand All @@ -294,46 +328,74 @@ class TouchDrvFT6X36 :
}
}

bool enableInterrupt()
{
return false;
}

bool disableInterrupt()
{
return false;
}

bool getResolution(int16_t *x, int16_t *y)
{
return false;
}

void reset()
{
if (__rst != SENSOR_PIN_NONE) {
digitalWrite(__rst, HIGH);
delay(10);
digitalWrite(__rst, LOW);
delay(30);
digitalWrite(__rst, HIGH);
delay(5);
}
}

void setPins(int rst, int irq)
{
__irq = irq;
__rst = rst;
}

private:
bool initImpl()
{
uint8_t vendId = readRegister(FT6X36_REG_VENDOR1_ID);
chipID = readRegister(FT6X36_REG_CHIPID);

#ifdef LOG_PORT
LOG_PORT.print("Vend ID: 0x");
LOG_PORT.println(vendId, HEX);
LOG_PORT.print("Chip ID: 0x");
LOG_PORT.println(chipID, HEX);
LOG_PORT.print("Firm V: "); LOG_PORT.println(readRegister(FT6X36_REG_FIRMVERS));
LOG_PORT.print("Point Rate Hz: ");
LOG_PORT.println(readRegister(FT6X36_REG_PERIODACTIVE));
LOG_PORT.print("Thresh: ");
LOG_PORT.println(readRegister(FT6X36_REG_THRESHHOLD));
#endif
log_i("Vend ID: 0x%X", vendId);
log_i("Chip ID: 0x%X", chipID);
log_i("Firm Version: 0x%X", readRegister(FT6X36_REG_FIRMVERS));
log_i("Point Rate Hz: %u", readRegister(FT6X36_REG_PERIODACTIVE));
log_i("Thresh : %u", readRegister(FT6X36_REG_THRESHHOLD));

// change threshhold to be higher/lower
writeRegister(FT6X36_REG_THRESHHOLD, 60);

if (vendId != FT6X36_VENDID) {
LOG("Vendor id is not match!");
log_e("Vendor id is not match!");
return false;
}
if ((chipID != FT6206_CHIPID) &&
(chipID != FT6236_CHIPID) &&
(chipID != FT6236U_CHIPID) &&
(chipID != FT3267_CHIPID)
) {
LOG("Vendor id is not match!");
LOG("ChipID:0x%x should be 0x06 or 0x36 or 0x64\n", chipID);
log_e("Vendor id is not match!");
log_e("ChipID:0x%x should be 0x06 or 0x36 or 0x64\n", chipID);
return false;
}

uint16_t version = getLibraryVersion();
LOG("Chip library version : 0x%x\n", version);
log_i("Chip library version : 0x%x\n", version);

// This register describes period of monitor status, it should not less than 30.
uint8_t val = readRegister(FT6X36_REG_PERIODMONITOR);
LOG("Chip period of monitor status : 0x%x\n", val);
log_i("Chip period of monitor status : 0x%x\n", val);

// This register describes the period of active status, it should not less than 12

Expand All @@ -348,6 +410,8 @@ class TouchDrvFT6X36 :

protected:
uint8_t chipID;
int __rst = SENSOR_PIN_NONE;
int __irq = SENSOR_PIN_NONE;
};


Expand Down
Loading

0 comments on commit fc3f087

Please sign in to comment.