FT6206 Library for Adafruit 2.8" TFT Touch Shield for Arduino w/Capacitive Touch

Dependents:   ArchPro_TFT ATT_AWS_IoT_demo_v06 ArchPro_TFT TermProject

Revision:
0:d146e986a07f
Child:
1:43274ec89d1a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FT6206.cpp	Mon Mar 23 19:49:52 2015 +0000
@@ -0,0 +1,169 @@
+/*************************************************** 
+  This is a library for the Adafruit Capacitive Touch Screens
+
+  ----> http://www.adafruit.com/products/1947
+ 
+  Check out the links above for our tutorials and wiring diagrams
+  This chipset uses I2C to communicate
+
+  Adafruit invests time and resources providing this open source code,
+  please support Adafruit and open-source hardware by purchasing
+  products from Adafruit!
+
+  Written by Limor Fried/Ladyada for Adafruit Industries.
+  MIT license, all text above must be included in any redistribution
+ ****************************************************/
+
+#include "FT6206.h"
+
+FT6206::FT6206(PinName sda, PinName scl, PinName interrupt) : m_i2c(sda, scl), m_interrupt(interrupt)
+{
+    m_addr = (FT6206_ADDR << 1);
+    m_i2c.frequency(FT6206_I2C_FREQUENCY);
+    m_interrupt.mode(PullUp);
+}
+
+
+/**************************************************************************/
+/*! 
+    @brief  Setups the HW
+*/
+/**************************************************************************/
+bool FT6206::begin(uint8_t threshhold) {
+////  Wire.begin();
+
+  // change threshhold to be higher/lower
+  writeRegister8(FT6206_REG_THRESHHOLD, threshhold);
+  
+  if (readRegister8(FT6206_REG_VENDID) != 17)
+    return false;
+  
+  if (readRegister8(FT6206_REG_CHIPID) != 6)
+    return false;
+/*
+  printf("Vend ID: %d\n", readRegister8(FT6206_REG_VENDID));
+  printf("Chip ID: %d\n", readRegister8(FT6206_REG_CHIPID));
+  printf("Firm V: %d\n", readRegister8(FT6206_REG_FIRMVERS));
+  printf("Rate Hz: %d\n", readRegister8(FT6206_REG_POINTRATE));
+  printf("Thresh: %d\n", readRegister8(FT6206_REG_THRESHHOLD));
+*/
+  // dump all registers
+/*
+    for (int16_t i=0; i<0x20; i++) {
+        printf("I2C $%02x = 0x %02x\n", i, readRegister8(i));
+    }
+*/
+    return true;
+}
+
+bool FT6206::touched(void) {
+    uint8_t n = readRegister8(FT6206_REG_NUMTOUCHES);
+    if ((n == 1) || (n == 2))
+        return true;
+    return false;
+}
+
+/*****************************/
+
+void FT6206::readData(uint16_t *x, uint16_t *y) {
+    char i2cdat[16];
+
+    m_i2c.write(m_addr, 0x00, 1);
+    m_i2c.read(m_addr, i2cdat, 16);
+
+/*
+    for (int16_t i=0; i<0x20; i++) {
+        printf("I2C %02x = %02x\n", i, i2cdat[i]);;
+    }
+*/
+
+    touches = i2cdat[0x02];
+    
+    // printf("touches: %d\n", touches);
+    if (touches > 2) {
+        touches = 0;
+        *x = *y = 0;
+    }
+    if (touches == 0) {
+        *x = *y = 0;
+        return;
+    }
+
+/*
+    if (touches == 2) Serial.print('2');
+    for (uint8_t i=0; i<16; i++) {
+        printf("%02x ", i2cdat[i]);
+    }
+    printf("\n");
+*/
+
+/*
+    printf("\n");
+    if (i2cdat[0x01] != 0x00) {
+        printf("Gesture #%d\n", i2cdat[0x01]); 
+    }
+*/
+
+    //printf("# Touches: %d", touches);
+    for (uint8_t i=0; i<2; i++) {
+      touchY[i] = i2cdat[0x03 + i*6] & 0x0F;
+      touchY[i] <<= 8;
+      touchY[i] |= i2cdat[0x04 + i*6]; 
+      touchX[i] = i2cdat[0x05 + i*6] & 0x0F;
+      touchX[i] <<= 8;
+      touchX[i] |= i2cdat[0x06 + i*6];
+      touchID[i] = i2cdat[0x05 + i*6] >> 4;
+    }
+/*
+    for (uint8_t i=0; i<touches; i++) {
+      printf("ID #%d (%d, %d", touchID[i], touchX[i], touchY[i]);
+      printf(")\n");
+    }
+*/
+    *x = ILI9341_TFTWIDTH - touchX[0];
+    *y = touchY[0];
+}
+
+TS_Point FT6206::getPoint(void) {
+    uint16_t x, y;
+    readData(&x, &y);
+    return TS_Point(x, y, 1);
+}
+
+char FT6206::readRegister8(char reg) {
+    char val;
+    m_i2c.write(m_addr, &reg, 1);
+    m_i2c.read(m_addr, &val, 1);
+    return val;
+}
+
+void FT6206::writeRegister8(char reg, char val) {
+    char data[2];
+    data[0] = reg;
+    data[1] = val;
+    m_i2c.write((int)FT6206_ADDR, data, 2);
+}
+
+char FT6206::dataReceived(void) {
+    return !m_interrupt;
+}
+
+/****************/
+
+TS_Point::TS_Point(void) {
+    x = y = 0;
+}
+
+TS_Point::TS_Point(int16_t x0, int16_t y0, int16_t z0) {
+    x = x0;
+    y = y0;
+    z = z0;
+}
+
+bool TS_Point::operator==(TS_Point p1) {
+    return  ((p1.x == x) && (p1.y == y) && (p1.z == z));
+}
+
+bool TS_Point::operator!=(TS_Point p1) {
+    return  ((p1.x != x) || (p1.y != y) || (p1.z != z));
+}