Caiou
Dependents: LCD_Proj LCD_imagetest LCD_Proj_rtc
Fork of DmTftLibrary by
DmTpFt6x06.cpp@17:67d1b918afe2, 2015-12-21 (annotated)
- Committer:
- Nikolas
- Date:
- Mon Dec 21 14:29:20 2015 +0000
- Revision:
- 17:67d1b918afe2
- Parent:
- 14:2db7065edbec
,jggfjlf;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
displaymodule | 13:6ff2649b6c27 | 1 | /********************************************************************************************** |
displaymodule | 13:6ff2649b6c27 | 2 | Copyright (c) 2015 DisplayModule. All rights reserved. |
displaymodule | 13:6ff2649b6c27 | 3 | |
displaymodule | 13:6ff2649b6c27 | 4 | Redistribution and use of this source code, part of this source code or any compiled binary |
displaymodule | 13:6ff2649b6c27 | 5 | based on this source code is permitted as long as the above copyright notice and following |
displaymodule | 13:6ff2649b6c27 | 6 | disclaimer is retained. |
displaymodule | 13:6ff2649b6c27 | 7 | |
displaymodule | 13:6ff2649b6c27 | 8 | DISCLAIMER: |
displaymodule | 13:6ff2649b6c27 | 9 | THIS SOFTWARE IS SUPPLIED "AS IS" WITHOUT ANY WARRANTIES AND SUPPORT. DISPLAYMODULE ASSUMES |
displaymodule | 13:6ff2649b6c27 | 10 | NO RESPONSIBILITY OR LIABILITY FOR THE USE OF THE SOFTWARE. |
displaymodule | 13:6ff2649b6c27 | 11 | ********************************************************************************************/ |
displaymodule | 13:6ff2649b6c27 | 12 | // Tested with FT6X06 |
displaymodule | 13:6ff2649b6c27 | 13 | |
displaymodule | 13:6ff2649b6c27 | 14 | #include "DmTpFt6x06.h" |
displaymodule | 13:6ff2649b6c27 | 15 | //#define log(...) Serial.println(__VA_ARGS__) |
displaymodule | 13:6ff2649b6c27 | 16 | #define log(...) |
displaymodule | 13:6ff2649b6c27 | 17 | |
displaymodule | 13:6ff2649b6c27 | 18 | #if defined(DM_TOOLCHAIN_ARDUINO) |
displaymodule | 13:6ff2649b6c27 | 19 | // disp - which display is used |
displaymodule | 13:6ff2649b6c27 | 20 | // useIrq - Enable IRQ or disable IRQ |
displaymodule | 13:6ff2649b6c27 | 21 | DmTpFt6x06::DmTpFt6x06(Display disp, bool useIrq) |
displaymodule | 13:6ff2649b6c27 | 22 | #elif defined (DM_TOOLCHAIN_MBED) |
displaymodule | 13:6ff2649b6c27 | 23 | // disp - which display is used |
displaymodule | 13:6ff2649b6c27 | 24 | // &i2c - I2C class |
displaymodule | 13:6ff2649b6c27 | 25 | // useIrq - Enable IRQ or disable IRQ |
displaymodule | 13:6ff2649b6c27 | 26 | DmTpFt6x06::DmTpFt6x06(Display disp, I2C &i2c, bool useIrq) |
displaymodule | 13:6ff2649b6c27 | 27 | #endif |
displaymodule | 13:6ff2649b6c27 | 28 | { |
displaymodule | 13:6ff2649b6c27 | 29 | switch (disp) { |
displaymodule | 13:6ff2649b6c27 | 30 | case DmTpFt6x06::DM_TFT28_116: |
displaymodule | 13:6ff2649b6c27 | 31 | _irq = D2; |
displaymodule | 13:6ff2649b6c27 | 32 | break; |
displaymodule | 13:6ff2649b6c27 | 33 | default: |
displaymodule | 13:6ff2649b6c27 | 34 | _irq = D2; |
displaymodule | 13:6ff2649b6c27 | 35 | break; |
displaymodule | 13:6ff2649b6c27 | 36 | } |
displaymodule | 13:6ff2649b6c27 | 37 | |
displaymodule | 13:6ff2649b6c27 | 38 | if (!useIrq) { |
displaymodule | 14:2db7065edbec | 39 | _irq = NC; |
displaymodule | 13:6ff2649b6c27 | 40 | } |
displaymodule | 13:6ff2649b6c27 | 41 | #if defined (DM_TOOLCHAIN_MBED) |
displaymodule | 13:6ff2649b6c27 | 42 | _i2c = &i2c; |
displaymodule | 13:6ff2649b6c27 | 43 | #endif |
displaymodule | 13:6ff2649b6c27 | 44 | } |
displaymodule | 13:6ff2649b6c27 | 45 | |
displaymodule | 13:6ff2649b6c27 | 46 | void DmTpFt6x06::init() { |
displaymodule | 13:6ff2649b6c27 | 47 | log("enter init()"); |
displaymodule | 13:6ff2649b6c27 | 48 | |
displaymodule | 13:6ff2649b6c27 | 49 | #if defined (DM_TOOLCHAIN_ARDUINO) |
displaymodule | 13:6ff2649b6c27 | 50 | Wire.begin(); |
displaymodule | 13:6ff2649b6c27 | 51 | Serial.print("Panel ID: "); Serial.println(readRegister8(FT6x06_FOCALTECH_ID)); |
displaymodule | 13:6ff2649b6c27 | 52 | Serial.print("Chip ID: "); Serial.println(readRegister8(FT6x06_CHIPER)); |
displaymodule | 13:6ff2649b6c27 | 53 | Serial.print("Firmware V: "); Serial.println(readRegister8(FT6x06_FIRMID)); |
displaymodule | 13:6ff2649b6c27 | 54 | Serial.print("Point Rate Hz: "); Serial.println(readRegister8(FT6x06_PERIODACTIVE)); |
displaymodule | 13:6ff2649b6c27 | 55 | Serial.print("Threshold: "); Serial.println(readRegister8(FT6x06_TH_GROUP)); |
displaymodule | 13:6ff2649b6c27 | 56 | #elif defined (DM_TOOLCHAIN_MBED) |
displaymodule | 13:6ff2649b6c27 | 57 | _i2c->frequency(400000); |
displaymodule | 13:6ff2649b6c27 | 58 | #endif |
displaymodule | 13:6ff2649b6c27 | 59 | if (_irq != -1) { // We will use Touch IRQ |
displaymodule | 13:6ff2649b6c27 | 60 | enableIrq(); |
displaymodule | 13:6ff2649b6c27 | 61 | } |
displaymodule | 13:6ff2649b6c27 | 62 | |
displaymodule | 13:6ff2649b6c27 | 63 | |
displaymodule | 13:6ff2649b6c27 | 64 | log("exit init()"); |
displaymodule | 13:6ff2649b6c27 | 65 | } |
displaymodule | 13:6ff2649b6c27 | 66 | |
displaymodule | 13:6ff2649b6c27 | 67 | void DmTpFt6x06::enableIrq() { |
displaymodule | 13:6ff2649b6c27 | 68 | log("enter enableIrq()"); |
displaymodule | 13:6ff2649b6c27 | 69 | |
displaymodule | 13:6ff2649b6c27 | 70 | #if defined (DM_TOOLCHAIN_ARDUINO) |
displaymodule | 13:6ff2649b6c27 | 71 | pinMode(_irq, INPUT); |
displaymodule | 13:6ff2649b6c27 | 72 | _pinIrq = portInputRegister(digitalPinToPort(_irq)); |
displaymodule | 13:6ff2649b6c27 | 73 | _bitmaskIrq = digitalPinToBitMask(_irq); |
displaymodule | 13:6ff2649b6c27 | 74 | #elif defined (DM_TOOLCHAIN_MBED) |
displaymodule | 13:6ff2649b6c27 | 75 | _pinIrq = new DigitalIn((PinName)_irq); |
displaymodule | 13:6ff2649b6c27 | 76 | _pinIrq->mode(PullUp); |
displaymodule | 13:6ff2649b6c27 | 77 | #endif |
displaymodule | 13:6ff2649b6c27 | 78 | |
displaymodule | 13:6ff2649b6c27 | 79 | log("exit enableIrq()"); |
displaymodule | 13:6ff2649b6c27 | 80 | } |
displaymodule | 13:6ff2649b6c27 | 81 | |
displaymodule | 13:6ff2649b6c27 | 82 | uint8_t DmTpFt6x06::readRegister8(uint8_t reg) { |
displaymodule | 13:6ff2649b6c27 | 83 | #if defined (DM_TOOLCHAIN_ARDUINO) |
displaymodule | 13:6ff2649b6c27 | 84 | uint8_t x ; |
displaymodule | 13:6ff2649b6c27 | 85 | Wire.beginTransmission(FT6x06_ADDR); |
displaymodule | 13:6ff2649b6c27 | 86 | Wire.write((byte)reg); |
displaymodule | 13:6ff2649b6c27 | 87 | Wire.endTransmission(); |
displaymodule | 13:6ff2649b6c27 | 88 | Wire.beginTransmission(FT6x06_ADDR); |
displaymodule | 13:6ff2649b6c27 | 89 | Wire.requestFrom((byte)FT6x06_ADDR, (byte)1); |
displaymodule | 13:6ff2649b6c27 | 90 | x = Wire.read(); |
displaymodule | 13:6ff2649b6c27 | 91 | Wire.endTransmission(); |
displaymodule | 13:6ff2649b6c27 | 92 | return x; |
displaymodule | 13:6ff2649b6c27 | 93 | #elif defined (DM_TOOLCHAIN_MBED) |
displaymodule | 13:6ff2649b6c27 | 94 | char buf[1] = {reg}, data = 0; |
displaymodule | 13:6ff2649b6c27 | 95 | _i2c->write(FT6x06_ADDR, buf, 1, true); |
displaymodule | 13:6ff2649b6c27 | 96 | _i2c->read(FT6x06_ADDR, &data, 1); |
displaymodule | 13:6ff2649b6c27 | 97 | return data; |
displaymodule | 13:6ff2649b6c27 | 98 | #endif |
displaymodule | 13:6ff2649b6c27 | 99 | } |
displaymodule | 13:6ff2649b6c27 | 100 | |
displaymodule | 13:6ff2649b6c27 | 101 | void DmTpFt6x06::writeRegister8(uint8_t reg, uint8_t val) { |
displaymodule | 13:6ff2649b6c27 | 102 | #if defined (DM_TOOLCHAIN_ARDUINO) |
displaymodule | 13:6ff2649b6c27 | 103 | Wire.beginTransmission(FT6x06_ADDR); |
displaymodule | 13:6ff2649b6c27 | 104 | Wire.write((byte)reg); |
displaymodule | 13:6ff2649b6c27 | 105 | Wire.write((byte)val); |
displaymodule | 13:6ff2649b6c27 | 106 | Wire.endTransmission(); |
displaymodule | 13:6ff2649b6c27 | 107 | #elif defined (DM_TOOLCHAIN_MBED) |
displaymodule | 13:6ff2649b6c27 | 108 | char buf[2] = {reg, val}; |
displaymodule | 13:6ff2649b6c27 | 109 | _i2c->write(FT6x06_ADDR, buf, 2); |
displaymodule | 13:6ff2649b6c27 | 110 | #endif |
displaymodule | 13:6ff2649b6c27 | 111 | } |
displaymodule | 13:6ff2649b6c27 | 112 | |
displaymodule | 13:6ff2649b6c27 | 113 | |
displaymodule | 13:6ff2649b6c27 | 114 | bool DmTpFt6x06::isTouched() { |
displaymodule | 13:6ff2649b6c27 | 115 | if (_irq == -1) { |
displaymodule | 13:6ff2649b6c27 | 116 | uint8_t val; |
displaymodule | 13:6ff2649b6c27 | 117 | val = readRegister8(FT6x06_TD_STATUS); |
displaymodule | 13:6ff2649b6c27 | 118 | if ((val == 1) || (val == 2)) return true; |
displaymodule | 13:6ff2649b6c27 | 119 | return false; |
displaymodule | 13:6ff2649b6c27 | 120 | } |
displaymodule | 13:6ff2649b6c27 | 121 | |
displaymodule | 13:6ff2649b6c27 | 122 | #if defined (DM_TOOLCHAIN_ARDUINO) |
displaymodule | 13:6ff2649b6c27 | 123 | if ( !gbi(_pinIrq, _bitmaskIrq) ) { |
displaymodule | 13:6ff2649b6c27 | 124 | return true; |
displaymodule | 13:6ff2649b6c27 | 125 | } |
displaymodule | 13:6ff2649b6c27 | 126 | return false; |
displaymodule | 13:6ff2649b6c27 | 127 | #elif defined (DM_TOOLCHAIN_MBED) |
displaymodule | 13:6ff2649b6c27 | 128 | if (!_pinIrq->read()) { |
displaymodule | 13:6ff2649b6c27 | 129 | return true; |
displaymodule | 13:6ff2649b6c27 | 130 | } |
displaymodule | 13:6ff2649b6c27 | 131 | return false; |
displaymodule | 13:6ff2649b6c27 | 132 | #endif |
displaymodule | 13:6ff2649b6c27 | 133 | } |
displaymodule | 13:6ff2649b6c27 | 134 | |
displaymodule | 13:6ff2649b6c27 | 135 | |
displaymodule | 13:6ff2649b6c27 | 136 | void DmTpFt6x06::readTouchData(uint16_t& posX, uint16_t& posY, bool& touching) { |
displaymodule | 13:6ff2649b6c27 | 137 | #if defined (DM_TOOLCHAIN_ARDUINO) |
displaymodule | 13:6ff2649b6c27 | 138 | uint8_t buf[16]; |
displaymodule | 13:6ff2649b6c27 | 139 | Wire.beginTransmission(FT6x06_ADDR); |
displaymodule | 13:6ff2649b6c27 | 140 | Wire.write((byte)0); |
displaymodule | 13:6ff2649b6c27 | 141 | Wire.endTransmission(); |
displaymodule | 13:6ff2649b6c27 | 142 | Wire.beginTransmission(FT6x06_ADDR); |
displaymodule | 13:6ff2649b6c27 | 143 | Wire.requestFrom((byte)FT6x06_ADDR, (byte)16); |
displaymodule | 13:6ff2649b6c27 | 144 | for (uint8_t i=0; i<16; i++) |
displaymodule | 13:6ff2649b6c27 | 145 | buf[i] = Wire.read(); |
displaymodule | 13:6ff2649b6c27 | 146 | Wire.endTransmission(); |
displaymodule | 13:6ff2649b6c27 | 147 | |
displaymodule | 13:6ff2649b6c27 | 148 | posX = buf[3] & 0x0F; |
displaymodule | 13:6ff2649b6c27 | 149 | posX <<= 8; |
displaymodule | 13:6ff2649b6c27 | 150 | posX |= buf[4]; |
displaymodule | 13:6ff2649b6c27 | 151 | posY = buf[5] & 0x0F; |
displaymodule | 13:6ff2649b6c27 | 152 | posY <<= 8; |
displaymodule | 13:6ff2649b6c27 | 153 | posY |= buf[6]; |
displaymodule | 13:6ff2649b6c27 | 154 | #elif defined (DM_TOOLCHAIN_MBED) |
displaymodule | 13:6ff2649b6c27 | 155 | posX = readRegister8(0x03) & 0x0F; |
displaymodule | 13:6ff2649b6c27 | 156 | posX <<= 8; |
displaymodule | 13:6ff2649b6c27 | 157 | posX |= readRegister8(0x04); |
displaymodule | 13:6ff2649b6c27 | 158 | posY = readRegister8(0x05) & 0x0F; |
displaymodule | 13:6ff2649b6c27 | 159 | posY <<= 8; |
displaymodule | 13:6ff2649b6c27 | 160 | posY |= readRegister8(0x06); |
displaymodule | 13:6ff2649b6c27 | 161 | |
displaymodule | 13:6ff2649b6c27 | 162 | posX = map(posX, 0, 240, 240, 0); |
displaymodule | 13:6ff2649b6c27 | 163 | posY = map(posY, 0, 320, 320, 0); |
displaymodule | 13:6ff2649b6c27 | 164 | #endif |
displaymodule | 13:6ff2649b6c27 | 165 | touching = isTouched(); |
displaymodule | 13:6ff2649b6c27 | 166 | } |
displaymodule | 13:6ff2649b6c27 | 167 | |
displaymodule | 13:6ff2649b6c27 | 168 | bool DmTpFt6x06::readTouchData(uint16_t& posX, uint16_t& posY) { |
displaymodule | 13:6ff2649b6c27 | 169 | #if defined (DM_TOOLCHAIN_ARDUINO) |
displaymodule | 13:6ff2649b6c27 | 170 | uint8_t buf[16]; |
displaymodule | 13:6ff2649b6c27 | 171 | Wire.beginTransmission(FT6x06_ADDR); |
displaymodule | 13:6ff2649b6c27 | 172 | Wire.write((byte)0); |
displaymodule | 13:6ff2649b6c27 | 173 | Wire.endTransmission(); |
displaymodule | 13:6ff2649b6c27 | 174 | Wire.beginTransmission(FT6x06_ADDR); |
displaymodule | 13:6ff2649b6c27 | 175 | Wire.requestFrom((byte)FT6x06_ADDR, (byte)16); |
displaymodule | 13:6ff2649b6c27 | 176 | for (uint8_t i=0; i<16; i++) |
displaymodule | 13:6ff2649b6c27 | 177 | buf[i] = Wire.read(); |
displaymodule | 13:6ff2649b6c27 | 178 | Wire.endTransmission(); |
displaymodule | 13:6ff2649b6c27 | 179 | |
displaymodule | 13:6ff2649b6c27 | 180 | if(buf[2] > 2){ |
displaymodule | 13:6ff2649b6c27 | 181 | posX = 0; |
displaymodule | 13:6ff2649b6c27 | 182 | posY = 0; |
displaymodule | 13:6ff2649b6c27 | 183 | return false; |
displaymodule | 13:6ff2649b6c27 | 184 | } |
displaymodule | 13:6ff2649b6c27 | 185 | posX = buf[3] & 0x0F; |
displaymodule | 13:6ff2649b6c27 | 186 | posX <<= 8; |
displaymodule | 13:6ff2649b6c27 | 187 | posX |= buf[4]; |
displaymodule | 13:6ff2649b6c27 | 188 | posY = buf[5] & 0x0F; |
displaymodule | 13:6ff2649b6c27 | 189 | posY <<= 8; |
displaymodule | 13:6ff2649b6c27 | 190 | posY |= buf[6]; |
displaymodule | 13:6ff2649b6c27 | 191 | return true; |
displaymodule | 13:6ff2649b6c27 | 192 | #elif defined (DM_TOOLCHAIN_MBED) |
displaymodule | 13:6ff2649b6c27 | 193 | if(readRegister8(0x03) > 2){ |
displaymodule | 13:6ff2649b6c27 | 194 | posX = 0; |
displaymodule | 13:6ff2649b6c27 | 195 | posY = 0; |
displaymodule | 13:6ff2649b6c27 | 196 | return false; |
displaymodule | 13:6ff2649b6c27 | 197 | } |
displaymodule | 13:6ff2649b6c27 | 198 | |
displaymodule | 13:6ff2649b6c27 | 199 | posX = readRegister8(0x03) & 0x0F; |
displaymodule | 13:6ff2649b6c27 | 200 | posX <<= 8; |
displaymodule | 13:6ff2649b6c27 | 201 | posX |= readRegister8(0x04); |
displaymodule | 13:6ff2649b6c27 | 202 | posY = readRegister8(0x05) & 0x0F; |
displaymodule | 13:6ff2649b6c27 | 203 | posY <<= 8; |
displaymodule | 13:6ff2649b6c27 | 204 | posY |= readRegister8(0x06); |
displaymodule | 13:6ff2649b6c27 | 205 | |
displaymodule | 13:6ff2649b6c27 | 206 | posX = map(posX, 0, 240, 240, 0); |
displaymodule | 13:6ff2649b6c27 | 207 | posY = map(posY, 0, 320, 320, 0); |
displaymodule | 13:6ff2649b6c27 | 208 | return true; |
displaymodule | 13:6ff2649b6c27 | 209 | #endif |
displaymodule | 13:6ff2649b6c27 | 210 | } |
displaymodule | 13:6ff2649b6c27 | 211 | |
displaymodule | 13:6ff2649b6c27 | 212 | long DmTpFt6x06::map(long x, long in_min, long in_max, long out_min, long out_max) |
displaymodule | 13:6ff2649b6c27 | 213 | { |
displaymodule | 13:6ff2649b6c27 | 214 | return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; |
displaymodule | 13:6ff2649b6c27 | 215 | } |