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

FT6206.cpp

Committer:
JackB
Date:
2015-03-23
Revision:
3:8e16a8987166
Parent:
2:35e21af6733c
Child:
4:b9ff3c020e7f

File content as of revision 3:8e16a8987166:

/*************************************************** 
  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
 ****************************************************/
/*
Usage:

#include "mbed.h"
#include "SPI_TFT_ILI9341.h"
#include "FT6206.h"
#include "Arial12x12.h"

#define PIN_XP          A3
#define PIN_XM          A1
#define PIN_YP          A2
#define PIN_YM          A0
#define PIN_SCLK        D13
#define PIN_MISO        D12
#define PIN_MOSI        D11
#define PIN_CS_TFT      D10  // chip select pin
#define PIN_DC_TFT      D9   // data/command select pin.
#define PIN_RESET_TFT   D8
//#define PIN_BL_TFT      D7
#define PIN_CS_SD       D4

#define PORTRAIT        0
#define LANDSCAPE       1

#define PIN_SCL_FT6206  P0_28
#define PIN_SDA_FT6206  P0_27
#define PIN_INT_FT6206  D7

SPI_TFT_ILI9341 TFT(PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_TFT, PIN_RESET_TFT, PIN_DC_TFT, "TFT"); // mosi, miso, sclk, cs, reset, dc 
FT6206 FT6206(PIN_SDA_FT6206, PIN_SCL_FT6206, PIN_INT_FT6206); // sda, scl, int

int main()
{
    while(1) {
//        if (FT6206.touched()) {
        if (FT6206.dataReceived()) {
            // Retrieve a point  
            TS_Point p = FT6206.getPoint();
            printf("Touch %3d %3d\n", p.x, p.y);
        }
    }
}
*/

#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));
}