Display Module / DmTftLibrary

Dependents:   dm_bubbles dm_calc dm_paint dm_sdcard_with_adapter ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DmTpFt6x06.cpp Source File

DmTpFt6x06.cpp

00001 /**********************************************************************************************
00002  Copyright (c) 2015 DisplayModule. All rights reserved.
00003 
00004  Redistribution and use of this source code, part of this source code or any compiled binary
00005  based on this source code is permitted as long as the above copyright notice and following
00006  disclaimer is retained.
00007 
00008  DISCLAIMER:
00009  THIS SOFTWARE IS SUPPLIED "AS IS" WITHOUT ANY WARRANTIES AND SUPPORT. DISPLAYMODULE ASSUMES
00010  NO RESPONSIBILITY OR LIABILITY FOR THE USE OF THE SOFTWARE.
00011  ********************************************************************************************/
00012 // Tested with FT6X06
00013 
00014 #include "DmTpFt6x06.h"
00015 //#define log(...) Serial.println(__VA_ARGS__)
00016 #define log(...)
00017 
00018 #if defined(DM_TOOLCHAIN_ARDUINO)
00019 // disp        - which display is used
00020 // useIrq      - Enable IRQ or disable IRQ
00021 DmTpFt6x06::DmTpFt6x06(Display disp, bool useIrq)
00022 #elif defined (DM_TOOLCHAIN_MBED)
00023 // disp        - which display is used
00024 // &i2c        - I2C class
00025 // useIrq      - Enable IRQ or disable IRQ
00026 DmTpFt6x06::DmTpFt6x06(Display disp,  I2C &i2c, bool useIrq)
00027 #endif
00028 {
00029     switch (disp) {
00030     case DmTpFt6x06::DM_TFT28_116:
00031         _irq = D2;
00032         break;          
00033     default:
00034         _irq = D2;      
00035         break;
00036     }
00037 
00038     if (!useIrq) {
00039         _irq = NC;
00040     }
00041 #if defined (DM_TOOLCHAIN_MBED)
00042    _i2c = &i2c;
00043 #endif     
00044 }
00045 
00046 void DmTpFt6x06::init() {
00047     log("enter init()");
00048     
00049 #if defined (DM_TOOLCHAIN_ARDUINO)
00050     Wire.begin();   
00051     Serial.print("Panel ID: "); Serial.println(readRegister8(FT6x06_FOCALTECH_ID));
00052     Serial.print("Chip ID: "); Serial.println(readRegister8(FT6x06_CHIPER));
00053     Serial.print("Firmware V: "); Serial.println(readRegister8(FT6x06_FIRMID));
00054     Serial.print("Point Rate Hz: "); Serial.println(readRegister8(FT6x06_PERIODACTIVE));
00055     Serial.print("Threshold: "); Serial.println(readRegister8(FT6x06_TH_GROUP));      
00056 #elif defined (DM_TOOLCHAIN_MBED)
00057     _i2c->frequency(400000);
00058 #endif
00059     if (_irq != -1) { // We will use Touch IRQ
00060         enableIrq();
00061     }
00062 
00063     
00064     log("exit init()");
00065 }
00066 
00067 void DmTpFt6x06::enableIrq() {
00068     log("enter enableIrq()");   
00069     
00070 #if defined (DM_TOOLCHAIN_ARDUINO)
00071     pinMode(_irq, INPUT);
00072     _pinIrq = portInputRegister(digitalPinToPort(_irq));
00073     _bitmaskIrq  = digitalPinToBitMask(_irq);
00074 #elif defined (DM_TOOLCHAIN_MBED)
00075     _pinIrq = new DigitalIn((PinName)_irq);
00076     _pinIrq->mode(PullUp);
00077 #endif  
00078  
00079     log("exit enableIrq()");    
00080 }
00081 
00082 uint8_t DmTpFt6x06::readRegister8(uint8_t reg) {
00083 #if defined (DM_TOOLCHAIN_ARDUINO)    
00084     uint8_t x ;
00085     Wire.beginTransmission(FT6x06_ADDR);
00086     Wire.write((byte)reg);
00087     Wire.endTransmission();
00088     Wire.beginTransmission(FT6x06_ADDR);
00089     Wire.requestFrom((byte)FT6x06_ADDR, (byte)1);
00090     x = Wire.read();
00091     Wire.endTransmission();
00092     return x;
00093 #elif defined (DM_TOOLCHAIN_MBED)
00094     char buf[1] = {reg}, data = 0;   
00095     _i2c->write(FT6x06_ADDR, buf, 1, true);
00096     _i2c->read(FT6x06_ADDR, &data, 1);     
00097     return data;
00098 #endif      
00099 }
00100 
00101 void DmTpFt6x06::writeRegister8(uint8_t reg, uint8_t val) {
00102 #if defined (DM_TOOLCHAIN_ARDUINO)      
00103     Wire.beginTransmission(FT6x06_ADDR);
00104     Wire.write((byte)reg);
00105     Wire.write((byte)val);
00106     Wire.endTransmission();
00107 #elif defined (DM_TOOLCHAIN_MBED)    
00108     char buf[2] = {reg, val};
00109     _i2c->write(FT6x06_ADDR, buf, 2);
00110 #endif  
00111 }
00112 
00113 
00114 bool DmTpFt6x06::isTouched() {
00115     if (_irq == -1) {
00116         uint8_t val;
00117         val = readRegister8(FT6x06_TD_STATUS);
00118         if ((val == 1) || (val == 2)) return true;
00119         return false;
00120     }
00121     
00122 #if defined (DM_TOOLCHAIN_ARDUINO)    
00123     if ( !gbi(_pinIrq, _bitmaskIrq) ) {
00124         return true;
00125     }
00126     return false;
00127 #elif defined (DM_TOOLCHAIN_MBED)
00128     if (!_pinIrq->read()) {
00129         return true;
00130     }
00131     return false;
00132 #endif
00133 }
00134 
00135 
00136 void DmTpFt6x06::readTouchData(uint16_t& posX, uint16_t& posY, bool& touching) {
00137 #if defined (DM_TOOLCHAIN_ARDUINO)    
00138     uint8_t buf[16];
00139     Wire.beginTransmission(FT6x06_ADDR);
00140     Wire.write((byte)0);  
00141     Wire.endTransmission();
00142     Wire.beginTransmission(FT6x06_ADDR);
00143     Wire.requestFrom((byte)FT6x06_ADDR, (byte)16);
00144     for (uint8_t i=0; i<16; i++)
00145         buf[i] = Wire.read();
00146     Wire.endTransmission();  
00147 
00148     posX = buf[3] & 0x0F;
00149     posX <<= 8;
00150     posX |= buf[4]; 
00151     posY = buf[5] & 0x0F;
00152     posY <<= 8;
00153     posY |= buf[6]; 
00154 #elif defined (DM_TOOLCHAIN_MBED)
00155     posX = readRegister8(0x03) & 0x0F;
00156     posX <<= 8;
00157     posX |= readRegister8(0x04); 
00158     posY = readRegister8(0x05) & 0x0F;
00159     posY <<= 8;
00160     posY |= readRegister8(0x06); 
00161     
00162     posX = map(posX, 0, 240, 240, 0);
00163     posY = map(posY, 0, 320, 320, 0);
00164 #endif
00165     touching = isTouched();
00166 }
00167 
00168 bool DmTpFt6x06::readTouchData(uint16_t& posX, uint16_t& posY) {
00169 #if defined (DM_TOOLCHAIN_ARDUINO)   
00170     uint8_t buf[16];
00171     Wire.beginTransmission(FT6x06_ADDR);
00172     Wire.write((byte)0);    
00173     Wire.endTransmission();
00174     Wire.beginTransmission(FT6x06_ADDR);
00175     Wire.requestFrom((byte)FT6x06_ADDR, (byte)16);
00176     for (uint8_t i=0; i<16; i++)
00177         buf[i] = Wire.read();
00178     Wire.endTransmission();  
00179 
00180     if(buf[2] > 2){
00181         posX = 0;
00182         posY = 0;
00183         return false;
00184         }  
00185     posX = buf[3] & 0x0F;
00186     posX <<= 8;
00187     posX |= buf[4]; 
00188     posY = buf[5] & 0x0F;
00189     posY <<= 8;
00190     posY |= buf[6]; 
00191     return true;
00192 #elif defined (DM_TOOLCHAIN_MBED)
00193     if(readRegister8(0x03) > 2){
00194         posX = 0;
00195         posY = 0;
00196         return false;
00197         } 
00198 
00199     posX = readRegister8(0x03) & 0x0F;
00200     posX <<= 8;
00201     posX |= readRegister8(0x04); 
00202     posY = readRegister8(0x05) & 0x0F;
00203     posY <<= 8;
00204     posY |= readRegister8(0x06); 
00205     
00206     posX = map(posX, 0, 240, 240, 0);
00207     posY = map(posY, 0, 320, 320, 0);
00208     return true;
00209 #endif    
00210 }
00211 
00212 long DmTpFt6x06::map(long x, long in_min, long in_max, long out_min, long out_max)
00213 {
00214   return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
00215 }