diff --git a/STM32/libraries/BoardExamples/examples/Discovery746NG/TFT/TouchDisplay/TouchDisplay.ino b/STM32/libraries/BoardExamples/examples/Discovery746NG/TFT/TouchDisplay/TouchDisplay.ino new file mode 100644 index 0000000..cd32678 --- /dev/null +++ b/STM32/libraries/BoardExamples/examples/Discovery746NG/TFT/TouchDisplay/TouchDisplay.ino @@ -0,0 +1,85 @@ +/* + Touch example for STM32F476 Discvery + + June 2017, ChrisMicro + +*/ + +#include +#include "stm32_ub_touch_480x272.h" +#include "LTDC_F746_Discovery.h" + +LTDC_F746_Discovery tft; + +uint8_t UB_I2C3_ReadByte(uint8_t addressI2cDevice, uint8_t registerId) +{ + uint8_t result; + addressI2cDevice = addressI2cDevice >> 1; + + Wire.beginTransmission( addressI2cDevice ); + Wire.write( registerId ); + uint8_t error; + error = Wire.endTransmission(); + + Wire.requestFrom( addressI2cDevice, (uint8_t) 1 , (uint8_t) true ); + + while ( Wire.available() < 1 ); + + result = Wire.read() ; + + if (error)Serial.println("I2C error"); + + return result; +} + +void setup() +{ + Wire.stm32SetInstance(I2C3); + Wire.stm32SetSDA(PH8); + Wire.stm32SetSCL(PH7); + + Wire.begin(); + + UB_Touch_Init(); + + // The buffer is memory mapped + // You can directly draw on the display by writing to the buffer + uint16_t *buffer = (uint16_t *)malloc(LTDC_F746_ROKOTECH.width * LTDC_F746_ROKOTECH.height); + + tft.begin((uint16_t *)buffer); + tft.fillScreen(LTDC_BLACK); + //tft.setRotation(0); + tft.setCursor(0, 0); + tft.setTextColor(LTDC_GREEN); tft.setTextSize(3); + tft.println("STM32F746 Discovery"); + delay(3000); + tft.setTextColor(LTDC_YELLOW); tft.setTextSize(2); + +} + +void loop() +{ + if (UB_Touch_Read() == SUCCESS) + { + tft.setCursor(0, 0); + + tft.print(Touch_Data.xp); tft.print(" , "); tft.print(Touch_Data.yp); + tft.print(" contacts: "); tft.println( P_Touch_GetContacts() ); + + if( P_Touch_GetContacts() ) + { + tft.setCursor(Touch_Data.xp, Touch_Data.yp); + tft.print("touched"); + } + + //delay(100); + } else + { + tft.print("."); + delay(200); + } + + delay(100); + tft.fillScreen(LTDC_BLACK); + +} diff --git a/STM32/libraries/BoardExamples/examples/Discovery746NG/TFT/TouchDisplay/stm32_ub_touch_480x272.cpp b/STM32/libraries/BoardExamples/examples/Discovery746NG/TFT/TouchDisplay/stm32_ub_touch_480x272.cpp new file mode 100644 index 0000000..0a6f6e7 --- /dev/null +++ b/STM32/libraries/BoardExamples/examples/Discovery746NG/TFT/TouchDisplay/stm32_ub_touch_480x272.cpp @@ -0,0 +1,263 @@ +// driver found at +// http://mikrocontroller.bplaced.net/wordpress/?page_id=5329 +// datasheet: +// https://www.newhavendisplay.com/appnotes/datasheets/touchpanel/FT5336.pdf +//-------------------------------------------------------------- +// File : stm32_ub_touch_480x272.c +// Datum : 15.07.2015 +// Version : 1.1 +// Autor : UB +// EMail : mc-4u(@)t-online.de +// Web : www.mikrocontroller-4u.de +// CPU : STM32F746 +// IDE : OpenSTM32 +// GCC : 4.9 2015q2 +// Module : CubeHAL, STM32_UB_I2C3 +// Funktion : Touch-Funktionen +// Touch-Controller (Chip = FT5336GQQ) +// +// Hinweis : Settings : +// I2C-Slave-ADR = [0x70] +// FRQ-Max = 400 kHz +// I2C-3 [SCL=PH7, SDA=PH8] +// Interrupt-Pin = PI13 (wird nicht benutzt) +// +// im H-File einstellen ob Single- oder Multitouch +// benutzt werden soll +//-------------------------------------------------------------- + +//-------------------------------------------------------------- +// Includes +//-------------------------------------------------------------- +#include "stm32_ub_touch_480x272.h" +//#include "stm32_ub_i2c3.h" + +MultiTouch_Data_t MultiTouch_Data; +Touch_Data_t Touch_Data; + +//-------------------------------------------------------------- +// interne Funktionen +//-------------------------------------------------------------- +uint8_t P_Touch_ReadID(void); +uint8_t P_Touch_GetContacts(void); +uint8_t P_Touch_GetPositions(void); + +//-------------------------------------------------------------- +uint8_t FT5336_TOUCH_REG[5][4]; // fuer Register Adressen + + +//-------------------------------------------------------------- +// Init vom Touch +// Return_wert : +// -> ERROR , wenn Touch nicht gefunden wurde +// -> SUCCESS , wenn Touch OK +//-------------------------------------------------------------- +ErrorStatus UB_Touch_Init(void) +{ + uint8_t touch_id=0,n; + + Touch_Data.status=TOUCH_RELEASED; + Touch_Data.xp=0; + Touch_Data.yp=0; + + MultiTouch_Data.cnt=0; + for(n=0;n ERROR , wenn Touch nicht gefunden wurde +// -> SUCCESS , wenn Touch OK +// +// Touch_Data.status : [TOUCH_PRESSED, TOUCH_RELEASED] +// Touch_Data.xp : [0...479] +// Touch_Data.yp : [0...271] +// +// MultiTouch_Data.cnt : [0...5] +// MultiTouch_Data.p[0..4].xp : [0...479] +// MultiTouch_Data.p[0..4].yp : [0...271] +//-------------------------------------------------------------- +ErrorStatus UB_Touch_Read(void) +{ + uint8_t check,n; + + // init + Touch_Data.status=TOUCH_RELEASED; + Touch_Data.xp=0; + Touch_Data.yp=0; + + MultiTouch_Data.cnt=0; + for(n=0;nTOUCH_MAX_CONTACT) return ERROR; + + if(check>0) { + // wenn touch betaetigt + MultiTouch_Data.cnt=check; + Touch_Data.status=TOUCH_PRESSED; + check=P_Touch_GetPositions(); + if(check>TOUCH_MAX_CONTACT) { + // fehler beim lesen + Touch_Data.status=TOUCH_RELEASED; + Touch_Data.xp=0; + Touch_Data.yp=0; + + MultiTouch_Data.cnt=0; + for(n=0;n return = 0 +//-------------------------------------------------------------- +uint8_t P_Touch_ReadID(void) +{ + int16_t i2c_wert; + uint8_t n; + + for(n=0;n<5;n++) { + i2c_wert=UB_I2C3_ReadByte(FT5336_I2C_ADDR, FT5336_ID_REG); + if(i2c_wert>0) { + if((i2c_wert&0xFF)==FT5336_ID) return(FT5336_ID); + } + } + + return 0; +} + +//-------------------------------------------------------------- +// interne Funktion +// anzahl der Touch Kontakte ermitteln +// ret_wert : 0...5 = Anzahl der Kontakte +// 99 = Error +//-------------------------------------------------------------- +uint8_t P_Touch_GetContacts(void) +{ + uint8_t ret_wert=0; + int16_t i2c_wert; + + i2c_wert=UB_I2C3_ReadByte(FT5336_I2C_ADDR, FT5336_STATUS_REG); + if(i2c_wert<0) return 99; + + // wert maskieren + ret_wert = (uint8_t)(i2c_wert & FT5336_STATUS_MASK); + + if(ret_wert>TOUCH_MAX_CONTACT) return 0; + + #if USE_MULTITOUCH==0 + // bei Singletouch maximal ein Kontakt + if(ret_wert>1) ret_wert=1; + #endif + + return(ret_wert); +} + +//-------------------------------------------------------------- +// interne Funktion +// die Touch positionen von allen Kontakten ermitteln +// ret_wert : 0 = OK +// 99 = Error +//-------------------------------------------------------------- +// interne Funktion +uint8_t P_Touch_GetPositions(void) +{ + uint8_t n,adr,wert_lo,wert_hi; + int16_t i2c_wert; + uint16_t position; + + if(MultiTouch_Data.cnt==0) return 0; + + // alle daten einlesen + for(n=0;n=TOUCH_MAXY) position=(TOUCH_MAXY-1); + MultiTouch_Data.p[n].yp=position; + // y_lo + adr=FT5336_TOUCH_REG[n][2]; + i2c_wert=UB_I2C3_ReadByte(FT5336_I2C_ADDR, adr); + if(i2c_wert<0) return 99; + wert_lo = i2c_wert & FT5336_LO_MASK; + // y_hi + adr=FT5336_TOUCH_REG[n][3]; + i2c_wert=UB_I2C3_ReadByte(FT5336_I2C_ADDR, adr); + if(i2c_wert<0) return 99; + wert_hi = i2c_wert & FT5336_HI_MASK; + position=(wert_hi<<8)|wert_lo; + if(position>=TOUCH_MAXX) position=(TOUCH_MAXX-1); + MultiTouch_Data.p[n].xp=position; + } + + return 0; +} diff --git a/STM32/libraries/BoardExamples/examples/Discovery746NG/TFT/TouchDisplay/stm32_ub_touch_480x272.h b/STM32/libraries/BoardExamples/examples/Discovery746NG/TFT/TouchDisplay/stm32_ub_touch_480x272.h new file mode 100644 index 0000000..a78d82f --- /dev/null +++ b/STM32/libraries/BoardExamples/examples/Discovery746NG/TFT/TouchDisplay/stm32_ub_touch_480x272.h @@ -0,0 +1,144 @@ +//-------------------------------------------------------------- +// File : stm32_ub_touch_480x272.h +//-------------------------------------------------------------- + +//-------------------------------------------------------------- +#ifndef __STM32F7_UB_TOUCH_480x272_H +#define __STM32F7_UB_TOUCH_480x272_H + +//-------------------------------------------------------------- +// Includes +//-------------------------------------------------------------- +//#include "stm32_ub_system.h" +#include +//#include "stm32_ub_i2c3.h" +#include "Arduino.h" + +uint8_t UB_I2C3_ReadByte(uint8_t addressI2cDevice, uint8_t registerId); +/* +typedef enum { + TOUCH_PRESSED = 0, // Touch ist betaetigt + TOUCH_RELEASED = 1 // Touch ist nicht betaetigt +}ErrorStatus; +*/ +//-------------------------------------------------------------- +// define ob Multitouch benutzt werden soll oder nicht +// 1 = bis zu 5 Touch-Positionen gleichzeitig +// 0 = nur eine Touchposition +//-------------------------------------------------------------- +//#define USE_MULTITOUCH 1 // mit Multitouch +#define USE_MULTITOUCH 0 // nur Single-Touch + + +//-------------------------------------------------------------- +#define TOUCH_INIT_DELAY 200 // 200ms +#define TOUCH_MAX_CONTACT 5 // Multitouch + +//-------------------------------------------------------------- +#define TOUCH_MAXX 480 // Pixel in X +#define TOUCH_MAXY 272 // Pixel in Y + +//-------------------------------------------------------------- +// I2C-Adresse +//-------------------------------------------------------------- +#define FT5336_I2C_ADDR 0x70 + + + +//-------------------------------------------------------------- +// Register Adressen +//-------------------------------------------------------------- +#define FT5336_STATUS_REG ((uint8_t)0x02) + +#define FT5336_P1_XH_REG ((uint8_t)0x03) +#define FT5336_P1_XL_REG ((uint8_t)0x04) +#define FT5336_P1_YH_REG ((uint8_t)0x05) +#define FT5336_P1_YL_REG ((uint8_t)0x06) + +#define FT5336_P2_XH_REG ((uint8_t)0x09) +#define FT5336_P2_XL_REG ((uint8_t)0x0A) +#define FT5336_P2_YH_REG ((uint8_t)0x0B) +#define FT5336_P2_YL_REG ((uint8_t)0x0C) + +#define FT5336_P3_XH_REG ((uint8_t)0x0F) +#define FT5336_P3_XL_REG ((uint8_t)0x10) +#define FT5336_P3_YH_REG ((uint8_t)0x11) +#define FT5336_P3_YL_REG ((uint8_t)0x12) + +#define FT5336_P4_XH_REG ((uint8_t)0x15) +#define FT5336_P4_XL_REG ((uint8_t)0x16) +#define FT5336_P4_YH_REG ((uint8_t)0x17) +#define FT5336_P4_YL_REG ((uint8_t)0x18) + +#define FT5336_P5_XH_REG ((uint8_t)0x1B) +#define FT5336_P5_XL_REG ((uint8_t)0x1C) +#define FT5336_P5_YH_REG ((uint8_t)0x1D) +#define FT5336_P5_YL_REG ((uint8_t)0x1E) + +#define FT5336_ID_REG ((uint8_t)0xA8) + + + +//-------------------------------------------------------------- +// Status-ID +//-------------------------------------------------------------- +#define FT5336_ID 0x51 + +//-------------------------------------------------------------- +// Masken +//-------------------------------------------------------------- +#define FT5336_STATUS_MASK 0x0F +#define FT5336_LO_MASK 0xFF +#define FT5336_HI_MASK 0x0F + + +//----------------------------------------- +// Touch-Status +//----------------------------------------- +typedef enum { + TOUCH_PRESSED = 0, // Touch ist betaetigt + TOUCH_RELEASED = 1 // Touch ist nicht betaetigt +}Touch_Status_t; + + +//----------------------------------------- +// Globale Struktur der Touch-Daten +//----------------------------------------- +typedef struct { + Touch_Status_t status; + uint16_t xp; + uint16_t yp; +}Touch_Data_t; +extern Touch_Data_t Touch_Data; + + +//----------------------------------------- +// Touch-Position +//----------------------------------------- +typedef struct { + uint16_t xp; + uint16_t yp; +}TP_Point_t; + + +//----------------------------------------- +// Globale Struktur der Multi-Touch-Daten +//----------------------------------------- +typedef struct { + uint16_t cnt; + TP_Point_t p[TOUCH_MAX_CONTACT]; +}MultiTouch_Data_t; +//MultiTouch_Data_t MultiTouch_Data; + + +//-------------------------------------------------------------- +// Globale Funktionen +//-------------------------------------------------------------- +ErrorStatus UB_Touch_Init(void); +ErrorStatus UB_Touch_Read(void); +uint8_t P_Touch_GetContacts(void); + + +//-------------------------------------------------------------- +#endif // __STM32F7_UB_TOUCH_480x272_H +