Browse Source

add touch

quangthai 1 năm trước cách đây
mục cha
commit
deaaf8e04a

+ 115 - 0
export/SquareLine_Project_test/ui/CST816D.cpp

@@ -0,0 +1,115 @@
+#include "CST816D.h"
+
+CST816D::CST816D(int8_t sda_pin, int8_t scl_pin, int8_t rst_pin, int8_t int_pin)
+{
+    _sda = sda_pin;
+    _scl = scl_pin;
+    _rst = rst_pin;
+    _int = int_pin;
+}
+
+void CST816D::begin(void)
+{
+    // Initialize I2C
+    if (_sda != -1 && _scl != -1)
+    {
+        Wire.begin(_sda, _scl);
+    }
+    else
+    {
+        Wire.begin();
+    }
+
+    // Int Pin Configuration
+    if (_int != -1)
+    {
+        pinMode(_int, OUTPUT);
+        digitalWrite(_int, HIGH); //高电平
+        delay(1);
+        digitalWrite(_int, LOW); //低电平
+        delay(1);
+    }
+
+    // Reset Pin Configuration
+    if (_rst != -1)
+    {
+        pinMode(_rst, OUTPUT);
+        digitalWrite(_rst, LOW);
+        delay(10);
+        digitalWrite(_rst, HIGH);
+        delay(300);
+    }
+
+    // Initialize Touch
+    i2c_write(0xFE, 0XFF); //禁止自动进入低功耗模式。
+}
+
+bool CST816D::getTouch(uint16_t *x, uint16_t *y, uint8_t *gesture)
+{
+    bool FingerIndex = false;
+    FingerIndex = (bool)i2c_read(0x02);
+
+    *gesture = i2c_read(0x01);
+    if (!(*gesture == SlideUp || *gesture == SlideDown))
+    {
+        *gesture = None;
+    }
+
+    uint8_t data[4];
+    i2c_read_continuous(0x03,data,4);
+    *x = ((data[0] & 0x0f) << 8) | data[1];
+    *y = ((data[2] & 0x0f) << 8) | data[3];
+
+   // *x=240-*x;
+
+    return FingerIndex;
+}
+
+uint8_t CST816D::i2c_read(uint8_t addr)
+{
+    uint8_t rdData;
+    uint8_t rdDataCount;
+    do
+    {
+        Wire.beginTransmission(I2C_ADDR_CST816D);
+        Wire.write(addr);
+        Wire.endTransmission(false); // Restart
+        rdDataCount = Wire.requestFrom(I2C_ADDR_CST816D, 1);
+    } while (rdDataCount == 0);
+    while (Wire.available())
+    {
+        rdData = Wire.read();
+    }
+    return rdData;
+}
+
+uint8_t CST816D::i2c_read_continuous(uint8_t addr, uint8_t *data, uint32_t length)
+{
+  Wire.beginTransmission(I2C_ADDR_CST816D);
+  Wire.write(addr);
+  if ( Wire.endTransmission(true))return -1;
+  Wire.requestFrom(I2C_ADDR_CST816D, length);
+  for (int i = 0; i < length; i++) {
+    *data++ = Wire.read();
+  }
+  return 0;
+}
+
+void CST816D::i2c_write(uint8_t addr, uint8_t data)
+{
+    Wire.beginTransmission(I2C_ADDR_CST816D);
+    Wire.write(addr);
+    Wire.write(data);
+    Wire.endTransmission();
+}
+
+uint8_t CST816D::i2c_write_continuous(uint8_t addr, const uint8_t *data, uint32_t length)
+{
+  Wire.beginTransmission(I2C_ADDR_CST816D);
+  Wire.write(addr);
+  for (int i = 0; i < length; i++) {
+    Wire.write(*data++);
+  }
+  if ( Wire.endTransmission(true))return -1;
+  return 0;
+}

+ 42 - 0
export/SquareLine_Project_test/ui/CST816D.h

@@ -0,0 +1,42 @@
+#ifndef _CST816D_H
+#define _CST816D_H
+
+#include <Wire.h>
+
+#define I2C_ADDR_CST816D 0x15
+
+//手势
+enum GESTURE
+{
+    None = 0x00,       //无手势
+    SlideDown = 0x01,  //向下滑动
+    SlideUp = 0x02,    //向上滑动
+    SlideLeft = 0x03,  //向左滑动
+    SlideRight = 0x04, //向右滑动
+    SingleTap = 0x05,  //单击
+    DoubleTap = 0x0B,  //双击
+    LongPress = 0x0C   //长按
+};
+
+/**************************************************************************/
+/*!
+    @brief  CST816D I2C CTP controller driver
+*/
+/**************************************************************************/
+class CST816D
+{
+public:
+    CST816D(int8_t sda_pin = -1, int8_t scl_pin = -1, int8_t rst_pin = -1, int8_t int_pin = -1);
+
+    void begin(void);
+    bool getTouch(uint16_t *x, uint16_t *y, uint8_t *gesture);
+
+private:
+    int8_t _sda, _scl, _rst, _int;
+
+    uint8_t i2c_read(uint8_t addr);
+    uint8_t i2c_read_continuous(uint8_t addr, uint8_t *data, uint32_t length);
+    void i2c_write(uint8_t addr, uint8_t data);
+    uint8_t i2c_write_continuous(uint8_t addr, const uint8_t *data, uint32_t length);
+};
+#endif

+ 10 - 4
export/SquareLine_Project_test/ui/ui.ino

@@ -1,8 +1,14 @@
 #include <lvgl.h>
 #include <TFT_eSPI.h>
 #include <ui.h>
+#include "CST816D.h"
 
 /*Don't forget to set Sketchbook location in File/Preferencesto the path of your UI project (the parent foder of this INO file)*/
+#define TFT_BLK 3
+#define I2C_SDA 4
+#define I2C_SCL 5
+#define TP_INT 0
+#define TP_RST 1
 
 /*Change to your screen resolution*/
 static const uint16_t screenWidth  = 240;
@@ -10,9 +16,8 @@ static const uint16_t screenHeight = 240;
 
 static lv_disp_draw_buf_t draw_buf;
 static lv_color_t buf[ screenWidth * screenHeight / 10 ];
-
+CST816D touch(I2C_SDA, I2C_SCL, TP_RST, TP_INT);
 TFT_eSPI tft = TFT_eSPI(screenWidth, screenHeight); /* TFT instance */
-#define TFT_BLK 3
 #if LV_USE_LOG != 0
 /* Serial debugging */
 void my_print(const char * buf)
@@ -40,8 +45,9 @@ void my_disp_flush( lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *colo
 void my_touchpad_read( lv_indev_drv_t * indev_driver, lv_indev_data_t * data )
 {
     uint16_t touchX = 0, touchY = 0;
+    uint8_t gesture;
 
-    bool touched = false;//tft.getTouch( &touchX, &touchY, 600 );
+    bool touched = touch.getTouch( &touchX, &touchY, &gesture );
 
     if( !touched )
     {
@@ -80,7 +86,7 @@ void setup()
 #if LV_USE_LOG != 0
     lv_log_register_print_cb( my_print ); /* register print function for debugging */
 #endif
-
+    touch.begin();
     tft.begin();          /* TFT init */
     tft.setRotation( 3 ); /* Landscape orientation, flipped */