@xfpd Especially For You
LCD_1inch28_Touch_t9.ino
//*******************************************************************************************
// Activate only the touch screen on a 1.28" TFT (the model without the integrated ESP32)
// This file will only work with Arduino from the AVR family (UNO, Nano, Mega etc...)
// THIS WILL NOT WORK ON ESP32 !!!
// Only connect 6 wires to activate ONLY the touch sensor
// then follow instructions on serial monitor (baud = 115,200)
// GND --> GND
// VIN --> 3.3V
// TP_RST_PIN --> 4
// TP_INT_PIN --> 3
// TP_SDA --> SDA
// TP_SCL --> SCL
//*******************************************************************************************
#include "Touch_Driver.h"
UBYTE flag = 0,flgh = 0;
unsigned int timeToSwipe=100;
bool swipe=0;
bool touching=0;
bool up=0,down=0, left=0, right=0, longPress=0;
int16_t x=0,y=0;
// ******************************************************
// SETUP
// ******************************************************
void setup()
{
Config_Init();
delay(1000);
Touch_1IN28_XY XY;
Serial.println("Setup started");
XY.mode = 2;
XY.x_point = 0;
XY.y_point = 0;
if(Touch_1IN28_init(XY.mode) == true)
Serial.println("Touch_1IN28_init - OK!");
else
Serial.println("Touch_1IN28_init - NOT OK! (but it will still work)");
attachInterrupt(1,Touch_INT_callback,LOW);
pinMode(TP_INT_PIN, INPUT_PULLUP);
DEV_Delay_ms(500);
Serial.println("Setup ended");
Serial.println();
Serial.println("Try touching a point, swiping left, right, up or down and long pressing");
}
// ******************************************************
// LOOP
// ******************************************************
void loop()
{
XY = Touch_1IN28_Get_Point();
if ((XY.x_point>0 && XY.x_point<250) && (XY.y_point>0 && XY.y_point<250)){
x= XY.x_point;
y= XY.y_point;
}
if (flag == 1){
touching=1;
for(int i=0;i<timeToSwipe;i++){
delay(1);
if (DEV_I2C_Read_Byte(address,0x01) == UP) {
resetTouch();
flag = 0;
swipe=1;
up=1;
}
if (DEV_I2C_Read_Byte(address,0x01) == Down) {
resetTouch();
flag = 0;
swipe=1;
down=1;
}
if (DEV_I2C_Read_Byte(address,0x01) == LEFT) {
resetTouch();
flag = 0;
swipe=1;
left=1;
}
if (DEV_I2C_Read_Byte(address,0x01) == RIGHT) {
resetTouch();
flag = 0;
swipe=1;
right=1;
}
if (DEV_I2C_Read_Byte(address,0x01) == LONG_PRESS) {
resetTouch();
flag = 0;
swipe=1;
longPress=1;
}
}
flag = 0;
}
if (touching){
touching=0;
if (!swipe){
//XY = Touch_1IN28_Get_Point();
Serial.print(" x: ");
Serial.print(x);
Serial.print(" y: ");
Serial.println(y);
flag = 0;
}
else if(up){
Serial.println("** Swipe UP");
up=0;
swipe=0;
}
else if(down){
Serial.println("** Swipe DOWN");
down=0;
swipe=0;
}
else if(left){
Serial.println("** Swipe LEFT");
left=0;
swipe=0;
}
else if(right){
Serial.println("** Swipe Right");
right=0;
swipe=0;
}
else if(longPress){
Serial.println("** Long press");
longPress=0;
swipe=0;
}
}
}
// ******************************************************
// resetTouch
// ******************************************************
void resetTouch(){
DEV_Digital_Write(TP_RST_PIN, 0);
DEV_Delay_ms(10);
DEV_Digital_Write(TP_RST_PIN, 1);
DEV_Delay_ms(50);
}
// ******************************************************
// Touch_INT_callback
// ******************************************************
void Touch_INT_callback()
{
flag = TOUCH_IRQ;
}
DEV_Config.cpp
/*****************************************************************************
* | File : DEV_Config.c
* | Author : Waveshare team
* | Function : Hardware underlying interface
* | Info :
* Used to shield the underlying layers of each master
* and enhance portability
*----------------
* | This version: V1.0
* | Date : 2018-11-22
* | Info :
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documnetation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
******************************************************************************/
#include "DEV_Config.h"
void GPIO_Init()
{
pinMode(TP_RST_PIN, OUTPUT);
}
void Config_Init()
{
GPIO_Init();
//Serial
Serial.begin(115200);
//spi
SPI.setDataMode(SPI_MODE3);
SPI.setBitOrder(MSBFIRST);
SPI.setClockDivider(SPI_CLOCK_DIV2);
SPI.begin();
//i2c
Wire.begin();
}
UBYTE DEV_I2C_Read_Byte(UBYTE DevAddr, UBYTE RegAddr)
{
UBYTE value;
Wire.beginTransmission(DevAddr);
Wire.write((byte)RegAddr);
Wire.endTransmission();
Wire.requestFrom(DevAddr, (byte)1);
value = Wire.read();
return value;
}
void DEV_I2C_Read_nByte(UBYTE DevAddr,UBYTE Cmd, UBYTE *data, UBYTE num)
{
Wire.beginTransmission(DevAddr);
Wire.write(Cmd);
// Wire.endTransmission();
Wire.requestFrom(DevAddr, num);
UBYTE i = 0;
for(i = 0; i < num; i++) {
data[i] = Wire.read();
}
Wire.endTransmission();
}
void DEV_I2C_Write_Byte(UBYTE DevAddr, UBYTE RegAddr, UBYTE value)
{
Wire.beginTransmission(DevAddr);
Wire.write(RegAddr);
Wire.write(value);
Wire.endTransmission();
}
DEV_Config.h
/*****************************************************************************
* | File : DEV_Config.c
* | Author : Waveshare team
* | Function : Hardware underlying interface
* | Info :
* Used to shield the underlying layers of each master
* and enhance portability
*----------------
* | This version: V1.0
* | Date : 2018-11-22
* | Info :
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documnetation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
******************************************************************************/
#ifndef _DEV_CONFIG_H_
#define _DEV_CONFIG_H_
#include <stdint.h>
#include <stdio.h>
#include <SPI.h>
#include <Wire.h>
#include "Debug.h"
#include <avr/pgmspace.h>
#define UBYTE uint8_t
#define UWORD uint16_t
#define UDOUBLE uint32_t
/**
* GPIO config
**/
#define TP_RST_PIN 4
#define TP_INT_PIN 3
/**
* GPIO read and write
**/
#define DEV_Digital_Write(_pin, _value) digitalWrite(_pin, _value == 0? LOW:HIGH)
#define DEV_Digital_Read(_pin) digitalRead(_pin)
/**
* SPI
**/
#define DEV_SPI_WRITE(_dat) SPI.transfer(_dat)
/**
* delay x ms
**/
#define DEV_Delay_ms(__xms) delay(__xms)
/**
* PWM_BL
**/
#define DEV_Set_BL(_Pin, _Value) analogWrite(_Pin, _Value)
/*-----------------------------------------------------------------------------*/
void Config_Init();
uint8_t DEV_I2C_Read_Byte(uint8_t DevAddr, uint8_t RegAddr);
void DEV_I2C_Write_Byte(uint8_t DevAddr, uint8_t RegAddr, uint8_t value);
void DEV_I2C_Read_nByte(UBYTE DevAddr,UBYTE Cmd, UBYTE *data, UBYTE num);
#endif
Debug.h
/*****************************************************************************
* | File : Debug.h
* | Author : Waveshare team
* | Function : debug with prntf
* | Info :
* Image scanning
* Please use progressive scanning to generate images or fonts
*----------------
* | This version: V1.0
* | Date : 2018-01-11
* | Info : Basic version
*
******************************************************************************/
#ifndef __DEBUG_H
#define __DEBUG_H
#include "stdio.h"
#define DEV_DEBUG 1
#if DEV_DEBUG
#define Debug(__info,...) printf("Debug : " __info,##__VA_ARGS__)
#else
#define DEBUG(__info,...)
#endif
#endif
Touch_Driver.cpp
/*****************************************************************************
* | File : Touch_1IN28.c
* | Author : Waveshare team
* | Function : Hardware underlying interface
* | Info :
* Used to shield the underlying layers of each master
* and enhance portability
*----------------
* | This version: V1.0
* | Date : 2022-12-02
* | Info : Basic version
*
******************************************************************************/
#include "Touch_Driver.h"
Touch_1IN28_XY XY;
/******************************************************************************
function : read ID 读取ID
parameter: CST816T : 0xB5
******************************************************************************/
UBYTE Touch_1IN28_WhoAmI()
{
Serial.print("Who am I: ");
Serial.println(DEV_I2C_Read_Byte(address,0xA7));
if (DEV_I2C_Read_Byte(address,0xA7) == 0xB5)
return true;
else
return false;
}
/******************************************************************************
function : reset touch 复位触摸
parameter:
******************************************************************************/
void Touch_1IN28_Reset()
{
DEV_Digital_Write(TP_RST_PIN, 0);
DEV_Delay_ms(100);
DEV_Digital_Write(TP_RST_PIN, 1);
DEV_Delay_ms(100);
}
/******************************************************************************
function : Read software version number 读取软件版本号
parameter:
******************************************************************************/
UBYTE Touch_1IN28_Read_Revision()
{
return DEV_I2C_Read_Byte(address,0xA9);
}
/******************************************************************************
function : exit sleep mode 退出休眠模式
parameter:
******************************************************************************/
void Touch_1IN28_Stop_Sleep()
{
DEV_I2C_Write_Byte(address,DisAutoSleep,0x01);
}
/******************************************************************************
function : Set touch mode 设置触摸模式
parameter:
mode = 0 gestures mode
mode = 1 point mode
mode = 2 mixed mode
******************************************************************************/
void Touch_1IN28_Set_Mode(UBYTE mode)
{
if (mode == 1)
{
DEV_I2C_Write_Byte(address,IrqCtl,0X41);
DEV_I2C_Write_Byte(address,NorScanPer,0X01);//Normal fast detection cycle unit 10ms
DEV_I2C_Write_Byte(address,IrqPluseWidth,0x0f); //Interrupt low pulse output width 1.5MS
}
else if(mode == 2)
DEV_I2C_Write_Byte(address,IrqCtl,0X71);
else
{
DEV_I2C_Write_Byte(address,IrqCtl,0X11);
DEV_I2C_Write_Byte(address,NorScanPer,0X01);
DEV_I2C_Write_Byte(address,IrqPluseWidth,0x01);//Interrupt low pulse output width 1.5MS
DEV_I2C_Write_Byte(address,MotionMask,EnDClick);//Enable double-tap mode
}
}
/******************************************************************************
function : wake up touchscreen 唤醒触摸屏
parameter:
******************************************************************************/
void Touch_1IN28_Wake_up()
{
DEV_Digital_Write(TP_RST_PIN, 0);
DEV_Delay_ms(10);
DEV_Digital_Write(TP_RST_PIN, 1);
DEV_Delay_ms(50);
DEV_I2C_Write_Byte(address,0xFE,0x01);
}
/******************************************************************************
function : screen initialization 屏幕初始化
parameter:
******************************************************************************/
UBYTE Touch_1IN28_init(UBYTE mode)
{
UBYTE bRet,Rev;
Touch_1IN28_Reset();
bRet = Touch_1IN28_WhoAmI();
if (bRet)
{
// Serial.println("Success:Detected CST816T.");
Rev = Touch_1IN28_Read_Revision();
// Serial.print("CST816T Revision = ");
// Serial.println(Rev);
Touch_1IN28_Stop_Sleep();
}
else
{
Serial.println("Error: Not Detected CST816T.");
return false;
}
XY.mode = mode;
Touch_1IN28_Set_Mode(mode);
XY.x_point = 0;
XY.y_point = 0;
return true;
}
/******************************************************************************
function : Get the corresponding point coordinates 获取对应的点坐标
parameter:
******************************************************************************/
Touch_1IN28_XY Touch_1IN28_Get_Point()
{
UBYTE data[4];
DEV_I2C_Read_nByte(address, 0x03, data, 4);
XY.x_point = ((data[0] & 0x0f)<<8) + data[1];
XY.y_point = ((data[2] & 0x0f)<<8) + data[3];
return XY;
}
Touch_Driver.h
/*****************************************************************************
* | File : Touch_Driver.h
* | Author : Waveshare team
* | Function : Hardware underlying interface
* | Info :
* Used to shield the underlying layers of each master
* and enhance portability
*----------------
* | This version: V1.0
* | Date : 2022-12-02
* | Info : Basic version
*
******************************************************************************/
#ifndef __Touch_DRIVER_H
#define __Touch_DRIVER_H
#include "DEV_Config.h"
#include <stdlib.h> //itoa()
#include <stdio.h>
#define address 0x15 //slave address
#define GESTUREID 0x01 //gesture code
#define None 0x00 //no gesture
#define UP 0x02 //slide up
#define Down 0x01 //slide down
#define LEFT 0x03 //Swipe left
#define RIGHT 0x04 //Swipe right
#define CLICK 0x05 //click
#define DOUBLE_CLICK 0x0B//double click
#define LONG_PRESS 0x0C //Press
#define FingerNum 0X02
#define XposH 0x03 //X coordinate high 4 digits
#define XposL 0x04 //The lower 8 bits of the X coordinate
#define YposH 0x05 //High 4 digits of Y coordinate
#define YposL 0x06 //The lower 8 bits of the Y coordinate
#define BPC0H 0xB0 //High 8 bits of BPC0 value
#define BPC0L 0xB1 //The lower 8 bits of the BPC0 value
#define BPC1H 0xB2 //The upper 8 bits of the BPC1 value
#define BPC1L 0xB3 //The lower 8 bits of the BPC1 value
#define ChipID 0xA7 //Chip model
#define ProjID 0xA8 //Project Number
#define FwVersion 0xA9 //software version number
#define MotionMask 0xEC
#define EnConLR 0x04 //Enable continuous left and right swipe actions
#define EnConUD 0x02 //Enable continuous up and down sliding action
#define EnDClick 0x01 //Enable double-click action
#define IrqPluseWidth 0xED //Interrupt low pulse output width unit 0.1ms, optional value: 1~200, default value is 10
#define NorScanPer 0xEE //Normal fast detection cycle unit 10ms, optional value: 1~30, default value is 1
#define MotionSlAngle 0xEF //Gesture detection sliding partition angle control Angle=tan(c)*10
//c is the angle based on the positive direction of the x-axis
#define LpScanRaw1H 0xF0 //Low power consumption scans the upper 8 bits of the reference value of channel 1
#define LpScanRaw1L 0xF1 //Low power scan the lower 8 bits of the reference value of channel 1
#define LpScanRaw2H 0xF2 //Low power consumption scans the upper 8 bits of the reference value of channel 1
#define LpScanRaw2L 0xF3 //Low power scan the lower 8 bits of the reference value of channel 1
#define LpAutoWakeTime 0xF4 //Auto-recalibration cycle at low power consumption Unit: 1 minute, optional value: 1~5. The default value is 5
#define LpScanTH 0xF5 //Low power scan wake-up threshold. The smaller the value, the more sensitive it is. Available values: 1-255. The default value is 48
#define LpScanWin 0xF6 //Low power scan range. The larger the value, the more sensitive and the higher the power consumption. Available values: 0, 1, 2, 3. The default value is 3
#define LpScanFreq 0xF7 //Low power consumption scan frequency The smaller the more sensitive the optional value: 1~255. The default value is 7
#define LpScanIdac 0xF8 //Low power consumption scanning current The smaller the more sensitive Optional value: 1~255
#define AutoSleepTime 0xF9 //When there is no touch within x seconds, it will automatically enter the low power consumption mode. The unit is 1S, and the default value is 2S.
#define IrqCtl 0xFA
#define EnTest 0x80 //Interrupt pin test, automatically sends out low pulses periodically after enabling
#define EnTouch 0x40 //When a touch is detected, periodically emit a low pulse
#define EnChange 0x20 //Sends a low pulse when a touch state change is detected
#define EnMotion 0x10 //When a gesture is detected, emit a low pulse
#define OnceWLP 0x00 //The long press gesture only sends out a low pulse signal
#define AutoReset 0xFB //When there is a touch but no valid gesture within x seconds, it will automatically reset. The unit 1S is not enabled when it is 0. The default is 5.
#define LongPressTime 0xFC //Press and hold for x seconds to reset automatically. Unit 1S is not enabled when it is 0. The default is 10.
#define IOCtl 0xFD //
#define SOFT_RST 0x04 //Enable soft reset
#define IIC_OD 0x02 //OD
#define En1v8 0x01 //1.8V
#define DisAutoSleep 0xFE //The default is 0, enabling automatic entry into low power consumption mode; non-zero value, prohibiting automatic entry into low power consumption mode
typedef struct{
UBYTE mode;
UBYTE Gesture;
UWORD color;
UWORD x_point;
UWORD y_point;
}Touch_1IN28_XY;
extern Touch_1IN28_XY XY;
typedef enum {
TOUCH_INIT = 0,
TOUCH_IRQ,
TOUCH_FUNCTION,
TOUCH_DRAW,
TOUCH_OUT_GESTURE,
TOUCH_NO_DRAW,
} Touch_STATE;
UBYTE Touch_1IN28_init(UBYTE mode);
Touch_1IN28_XY Touch_1IN28_Get_Point();
#endif