Modified version of the DmTftLibrary, optimized for the LPC4088 Experiment Base Board
Dependents: lpc4088_ebb_dm_calc lpc4088_ebb_dm_bubbles
Fork of DmTftLibrary by
DmTouch.cpp@8:b24f01d148c4, 2014-07-07 (annotated)
- Committer:
- displaymodule
- Date:
- Mon Jul 07 10:38:09 2014 +0000
- Revision:
- 8:b24f01d148c4
- Parent:
- 7:6cd8c36cbdb3
- Child:
- 9:bac5439e3c1c
Removed dependency on the T_IRQ pin.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
displaymodule | 0:d6ff5fa503e8 | 1 | /********************************************************************************************** |
displaymodule | 0:d6ff5fa503e8 | 2 | Copyright (c) 2014 DisplayModule. All rights reserved. |
displaymodule | 0:d6ff5fa503e8 | 3 | |
displaymodule | 0:d6ff5fa503e8 | 4 | Redistribution and use of this source code, part of this source code or any compiled binary |
displaymodule | 0:d6ff5fa503e8 | 5 | based on this source code is permitted as long as the above copyright notice and following |
displaymodule | 0:d6ff5fa503e8 | 6 | disclaimer is retained. |
displaymodule | 0:d6ff5fa503e8 | 7 | |
displaymodule | 0:d6ff5fa503e8 | 8 | DISCLAIMER: |
displaymodule | 0:d6ff5fa503e8 | 9 | THIS SOFTWARE IS SUPPLIED "AS IS" WITHOUT ANY WARRANTIES AND SUPPORT. DISPLAYMODULE ASSUMES |
displaymodule | 0:d6ff5fa503e8 | 10 | NO RESPONSIBILITY OR LIABILITY FOR THE USE OF THE SOFTWARE. |
displaymodule | 0:d6ff5fa503e8 | 11 | ********************************************************************************************/ |
displaymodule | 0:d6ff5fa503e8 | 12 | // Tested with Xpt2046 |
displaymodule | 0:d6ff5fa503e8 | 13 | |
displaymodule | 0:d6ff5fa503e8 | 14 | #include "DmTouch.h" |
displaymodule | 7:6cd8c36cbdb3 | 15 | #include "DmTouchCalibration.h" |
displaymodule | 7:6cd8c36cbdb3 | 16 | |
displaymodule | 7:6cd8c36cbdb3 | 17 | #define MEASUREMENTS 10 |
displaymodule | 0:d6ff5fa503e8 | 18 | |
displaymodule | 0:d6ff5fa503e8 | 19 | #if defined(DM_TOOLCHAIN_ARDUINO) |
displaymodule | 0:d6ff5fa503e8 | 20 | // disp - which display is used |
displaymodule | 7:6cd8c36cbdb3 | 21 | // spiMode - How to read SPI-data, Software, Hardware or Auto |
displaymodule | 7:6cd8c36cbdb3 | 22 | // useIrq - Enable IRQ or disable IRQ |
displaymodule | 7:6cd8c36cbdb3 | 23 | DmTouch::DmTouch(Display disp, SpiMode spiMode, bool useIrq) |
displaymodule | 0:d6ff5fa503e8 | 24 | |
displaymodule | 0:d6ff5fa503e8 | 25 | #elif defined (DM_TOOLCHAIN_MBED) |
displaymodule | 0:d6ff5fa503e8 | 26 | // disp - which display is used |
displaymodule | 7:6cd8c36cbdb3 | 27 | // spiMode - How to read SPI-data, Software, Hardware or Auto |
displaymodule | 7:6cd8c36cbdb3 | 28 | DmTouch::DmTouch(Display disp, SpiMode spiMode) |
displaymodule | 7:6cd8c36cbdb3 | 29 | #endif |
displaymodule | 0:d6ff5fa503e8 | 30 | { |
displaymodule | 0:d6ff5fa503e8 | 31 | switch (disp) { |
displaymodule | 0:d6ff5fa503e8 | 32 | // Display with 40-pin connector on top of adapter board |
displaymodule | 0:d6ff5fa503e8 | 33 | case DmTouch::DM_TFT28_103: |
displaymodule | 0:d6ff5fa503e8 | 34 | case DmTouch::DM_TFT24_104: |
displaymodule | 0:d6ff5fa503e8 | 35 | _cs = D8; |
displaymodule | 0:d6ff5fa503e8 | 36 | _irq = D10; |
displaymodule | 0:d6ff5fa503e8 | 37 | _clk = A1; |
displaymodule | 0:d6ff5fa503e8 | 38 | _mosi = A0; |
displaymodule | 0:d6ff5fa503e8 | 39 | _miso = D9; |
displaymodule | 7:6cd8c36cbdb3 | 40 | _hardwareSpi = false; |
displaymodule | 0:d6ff5fa503e8 | 41 | break; |
displaymodule | 3:02c19cbc707c | 42 | |
displaymodule | 0:d6ff5fa503e8 | 43 | case DmTouch::DM_TFT28_105: |
displaymodule | 0:d6ff5fa503e8 | 44 | _cs = D4; |
displaymodule | 0:d6ff5fa503e8 | 45 | _irq = D2; |
displaymodule | 0:d6ff5fa503e8 | 46 | _clk = D13; |
displaymodule | 0:d6ff5fa503e8 | 47 | _mosi = D11; |
displaymodule | 0:d6ff5fa503e8 | 48 | _miso = D12; |
displaymodule | 7:6cd8c36cbdb3 | 49 | _hardwareSpi = true; |
displaymodule | 0:d6ff5fa503e8 | 50 | break; |
displaymodule | 0:d6ff5fa503e8 | 51 | |
displaymodule | 0:d6ff5fa503e8 | 52 | case DmTouch::DM_TFT35_107: |
displaymodule | 0:d6ff5fa503e8 | 53 | default: |
displaymodule | 0:d6ff5fa503e8 | 54 | _cs = D4; |
displaymodule | 0:d6ff5fa503e8 | 55 | _irq = D2; |
displaymodule | 0:d6ff5fa503e8 | 56 | _clk = D13; |
displaymodule | 0:d6ff5fa503e8 | 57 | _mosi = D11; |
displaymodule | 0:d6ff5fa503e8 | 58 | _miso = D12; |
displaymodule | 7:6cd8c36cbdb3 | 59 | _hardwareSpi = true; |
displaymodule | 0:d6ff5fa503e8 | 60 | break; |
displaymodule | 0:d6ff5fa503e8 | 61 | } |
displaymodule | 7:6cd8c36cbdb3 | 62 | |
displaymodule | 7:6cd8c36cbdb3 | 63 | if (spiMode == DmTouch::Hardware) { |
displaymodule | 7:6cd8c36cbdb3 | 64 | _hardwareSpi = true; |
displaymodule | 7:6cd8c36cbdb3 | 65 | } else if (spiMode == DmTouch::Software) { |
displaymodule | 7:6cd8c36cbdb3 | 66 | _hardwareSpi = false; |
displaymodule | 7:6cd8c36cbdb3 | 67 | } |
displaymodule | 7:6cd8c36cbdb3 | 68 | |
displaymodule | 7:6cd8c36cbdb3 | 69 | #if defined(DM_TOOLCHAIN_ARDUINO) |
displaymodule | 7:6cd8c36cbdb3 | 70 | if (!useIrq) { |
displaymodule | 7:6cd8c36cbdb3 | 71 | _irq = -1; |
displaymodule | 7:6cd8c36cbdb3 | 72 | } |
displaymodule | 8:b24f01d148c4 | 73 | #elif defined (DM_TOOLCHAIN_MBED) |
displaymodule | 8:b24f01d148c4 | 74 | _irq = -1; |
displaymodule | 0:d6ff5fa503e8 | 75 | #endif |
displaymodule | 7:6cd8c36cbdb3 | 76 | |
displaymodule | 7:6cd8c36cbdb3 | 77 | setCalibrationMatrix(DmTouchCalibration::getDefaultCalibrationData(disp)); |
displaymodule | 0:d6ff5fa503e8 | 78 | |
displaymodule | 7:6cd8c36cbdb3 | 79 | _samplesPerMeasurement = 3; |
displaymodule | 0:d6ff5fa503e8 | 80 | } |
displaymodule | 0:d6ff5fa503e8 | 81 | |
displaymodule | 0:d6ff5fa503e8 | 82 | void DmTouch::init() { |
displaymodule | 0:d6ff5fa503e8 | 83 | #if defined (DM_TOOLCHAIN_ARDUINO) |
displaymodule | 0:d6ff5fa503e8 | 84 | pinMode(_cs, OUTPUT); |
displaymodule | 0:d6ff5fa503e8 | 85 | _pinCS = portOutputRegister(digitalPinToPort(_cs)); |
displaymodule | 0:d6ff5fa503e8 | 86 | _bitmaskCS = digitalPinToBitMask(_cs); |
displaymodule | 0:d6ff5fa503e8 | 87 | |
displaymodule | 0:d6ff5fa503e8 | 88 | if (_hardwareSpi) { |
displaymodule | 0:d6ff5fa503e8 | 89 | SPI.begin(); |
displaymodule | 7:6cd8c36cbdb3 | 90 | SPI.setClockDivider(SPI_CLOCK_DIV32); |
displaymodule | 0:d6ff5fa503e8 | 91 | SPI.setBitOrder(MSBFIRST); |
displaymodule | 0:d6ff5fa503e8 | 92 | SPI.setDataMode(SPI_MODE0); |
displaymodule | 0:d6ff5fa503e8 | 93 | _spiSettings = SPCR; |
displaymodule | 0:d6ff5fa503e8 | 94 | } |
displaymodule | 0:d6ff5fa503e8 | 95 | else { |
displaymodule | 0:d6ff5fa503e8 | 96 | pinMode(_clk, OUTPUT); |
displaymodule | 0:d6ff5fa503e8 | 97 | pinMode(_mosi, OUTPUT); |
displaymodule | 0:d6ff5fa503e8 | 98 | pinMode(_miso, INPUT); |
displaymodule | 0:d6ff5fa503e8 | 99 | _pinCLK = portOutputRegister(digitalPinToPort(_clk)); |
displaymodule | 0:d6ff5fa503e8 | 100 | _bitmaskCLK = digitalPinToBitMask(_clk); |
displaymodule | 0:d6ff5fa503e8 | 101 | _pinMOSI = portOutputRegister(digitalPinToPort(_mosi)); |
displaymodule | 0:d6ff5fa503e8 | 102 | _bitmaskMOSI = digitalPinToBitMask(_mosi); |
displaymodule | 0:d6ff5fa503e8 | 103 | _pinMISO = portInputRegister(digitalPinToPort(_miso)); |
displaymodule | 0:d6ff5fa503e8 | 104 | _bitmaskMISO = digitalPinToBitMask(_miso); |
displaymodule | 0:d6ff5fa503e8 | 105 | } |
displaymodule | 0:d6ff5fa503e8 | 106 | #elif defined (DM_TOOLCHAIN_MBED) |
displaymodule | 0:d6ff5fa503e8 | 107 | _pinCS = new DigitalOut((PinName)_cs); |
displaymodule | 0:d6ff5fa503e8 | 108 | if (_hardwareSpi) { |
displaymodule | 0:d6ff5fa503e8 | 109 | sbi(_pinCS, _bitmaskCS); |
displaymodule | 0:d6ff5fa503e8 | 110 | _spi = new SPI((PinName)_mosi, (PinName)_miso, (PinName)_clk); |
displaymodule | 0:d6ff5fa503e8 | 111 | _spi->format(8,0); |
displaymodule | 0:d6ff5fa503e8 | 112 | _spi->frequency(2000000); // Max SPI speed |
displaymodule | 0:d6ff5fa503e8 | 113 | //cbi(_pinCS, _bitmaskCS); |
displaymodule | 0:d6ff5fa503e8 | 114 | } else { |
displaymodule | 0:d6ff5fa503e8 | 115 | _pinCLK = new DigitalOut((PinName)_clk); |
displaymodule | 3:02c19cbc707c | 116 | _pinMISO = new DigitalIn((PinName)_miso); |
displaymodule | 0:d6ff5fa503e8 | 117 | _pinMOSI = new DigitalOut((PinName)_mosi); |
displaymodule | 3:02c19cbc707c | 118 | sbi(_pinCLK, _bitmaskCLK); |
displaymodule | 0:d6ff5fa503e8 | 119 | } |
displaymodule | 0:d6ff5fa503e8 | 120 | #endif |
displaymodule | 0:d6ff5fa503e8 | 121 | |
displaymodule | 0:d6ff5fa503e8 | 122 | if (_irq != -1) { // We will use Touch IRQ |
displaymodule | 0:d6ff5fa503e8 | 123 | enableIrq(); |
displaymodule | 0:d6ff5fa503e8 | 124 | } |
displaymodule | 0:d6ff5fa503e8 | 125 | } |
displaymodule | 0:d6ff5fa503e8 | 126 | |
displaymodule | 0:d6ff5fa503e8 | 127 | void DmTouch::enableIrq() { |
displaymodule | 0:d6ff5fa503e8 | 128 | #if defined (DM_TOOLCHAIN_ARDUINO) |
displaymodule | 0:d6ff5fa503e8 | 129 | pinMode(_irq, INPUT); |
displaymodule | 0:d6ff5fa503e8 | 130 | _pinIrq = portInputRegister(digitalPinToPort(_irq)); |
displaymodule | 0:d6ff5fa503e8 | 131 | _bitmaskIrq = digitalPinToBitMask(_irq); |
displaymodule | 0:d6ff5fa503e8 | 132 | |
displaymodule | 0:d6ff5fa503e8 | 133 | cbi(_pinCS, _bitmaskCS); |
displaymodule | 0:d6ff5fa503e8 | 134 | spiWrite(0x80); // Enable PENIRQ |
displaymodule | 0:d6ff5fa503e8 | 135 | sbi(_pinCS, _bitmaskCS); |
displaymodule | 8:b24f01d148c4 | 136 | #endif |
displaymodule | 0:d6ff5fa503e8 | 137 | } |
displaymodule | 0:d6ff5fa503e8 | 138 | |
displaymodule | 0:d6ff5fa503e8 | 139 | void DmTouch::spiWrite(uint8_t data) { |
displaymodule | 7:6cd8c36cbdb3 | 140 | if (_hardwareSpi) { |
displaymodule | 0:d6ff5fa503e8 | 141 | #if defined (DM_TOOLCHAIN_ARDUINO) |
displaymodule | 0:d6ff5fa503e8 | 142 | SPCR = _spiSettings; // SPI Control Register |
displaymodule | 0:d6ff5fa503e8 | 143 | SPDR = data; // SPI Data Register |
displaymodule | 0:d6ff5fa503e8 | 144 | while(!(SPSR & _BV(SPIF))); // SPI Status Register Wait for transmission to finish |
displaymodule | 0:d6ff5fa503e8 | 145 | #elif defined (DM_TOOLCHAIN_MBED) |
displaymodule | 0:d6ff5fa503e8 | 146 | _spi->write(data); |
displaymodule | 7:6cd8c36cbdb3 | 147 | #endif |
displaymodule | 0:d6ff5fa503e8 | 148 | } |
displaymodule | 0:d6ff5fa503e8 | 149 | else { |
displaymodule | 0:d6ff5fa503e8 | 150 | uint8_t count=0; |
displaymodule | 0:d6ff5fa503e8 | 151 | uint8_t temp = data; |
displaymodule | 7:6cd8c36cbdb3 | 152 | delay(1); |
displaymodule | 7:6cd8c36cbdb3 | 153 | cbi(_pinCLK, _bitmaskCLK); |
displaymodule | 0:d6ff5fa503e8 | 154 | for(count=0;count<8;count++) { |
displaymodule | 0:d6ff5fa503e8 | 155 | if(temp&0x80) { |
displaymodule | 0:d6ff5fa503e8 | 156 | sbi(_pinMOSI, _bitmaskMOSI); |
displaymodule | 0:d6ff5fa503e8 | 157 | } |
displaymodule | 0:d6ff5fa503e8 | 158 | else { |
displaymodule | 0:d6ff5fa503e8 | 159 | cbi(_pinMOSI, _bitmaskMOSI); |
displaymodule | 0:d6ff5fa503e8 | 160 | } |
displaymodule | 0:d6ff5fa503e8 | 161 | |
displaymodule | 0:d6ff5fa503e8 | 162 | temp=temp<<1; |
displaymodule | 0:d6ff5fa503e8 | 163 | |
displaymodule | 6:fde03297017b | 164 | slow_pulse_low(_pinCLK, _bitmaskCLK); |
displaymodule | 0:d6ff5fa503e8 | 165 | } |
displaymodule | 0:d6ff5fa503e8 | 166 | } |
displaymodule | 0:d6ff5fa503e8 | 167 | } |
displaymodule | 0:d6ff5fa503e8 | 168 | |
displaymodule | 0:d6ff5fa503e8 | 169 | uint8_t DmTouch::spiRead() {// Only used for Hardware SPI |
displaymodule | 0:d6ff5fa503e8 | 170 | #if defined (DM_TOOLCHAIN_ARDUINO) |
displaymodule | 0:d6ff5fa503e8 | 171 | uint8_t data; |
displaymodule | 0:d6ff5fa503e8 | 172 | SPCR = _spiSettings; |
displaymodule | 0:d6ff5fa503e8 | 173 | spiWrite(0x00); |
displaymodule | 0:d6ff5fa503e8 | 174 | data = SPDR; |
displaymodule | 0:d6ff5fa503e8 | 175 | |
displaymodule | 0:d6ff5fa503e8 | 176 | return data; |
displaymodule | 0:d6ff5fa503e8 | 177 | #elif defined (DM_TOOLCHAIN_MBED) |
displaymodule | 0:d6ff5fa503e8 | 178 | if (_hardwareSpi) { |
displaymodule | 0:d6ff5fa503e8 | 179 | return _spi->write(0x00); // dummy byte to read |
displaymodule | 0:d6ff5fa503e8 | 180 | } else { |
displaymodule | 0:d6ff5fa503e8 | 181 | uint8_t count=0; |
displaymodule | 0:d6ff5fa503e8 | 182 | uint8_t temp=0; |
displaymodule | 7:6cd8c36cbdb3 | 183 | cbi(_pinCLK, _bitmaskCLK); |
displaymodule | 3:02c19cbc707c | 184 | cbi(_pinMOSI, _bitmaskMOSI); // same as using 0x00 as dummy byte |
displaymodule | 0:d6ff5fa503e8 | 185 | for(count=0;count<8;count++) { |
displaymodule | 0:d6ff5fa503e8 | 186 | |
displaymodule | 0:d6ff5fa503e8 | 187 | pulse_low(_pinCLK, _bitmaskCLK); |
displaymodule | 0:d6ff5fa503e8 | 188 | temp = temp<<1; |
displaymodule | 0:d6ff5fa503e8 | 189 | temp |= _pinMISO->read(); |
displaymodule | 0:d6ff5fa503e8 | 190 | } |
displaymodule | 0:d6ff5fa503e8 | 191 | return temp; |
displaymodule | 0:d6ff5fa503e8 | 192 | } |
displaymodule | 0:d6ff5fa503e8 | 193 | #endif |
displaymodule | 0:d6ff5fa503e8 | 194 | } |
displaymodule | 0:d6ff5fa503e8 | 195 | |
displaymodule | 0:d6ff5fa503e8 | 196 | uint16_t DmTouch::readData12(uint8_t command) { |
displaymodule | 0:d6ff5fa503e8 | 197 | uint8_t temp = 0; |
displaymodule | 0:d6ff5fa503e8 | 198 | uint16_t value = 0; |
displaymodule | 0:d6ff5fa503e8 | 199 | |
displaymodule | 0:d6ff5fa503e8 | 200 | spiWrite(command); // Send command |
displaymodule | 7:6cd8c36cbdb3 | 201 | |
displaymodule | 0:d6ff5fa503e8 | 202 | #if defined (DM_TOOLCHAIN_ARDUINO) |
displaymodule | 0:d6ff5fa503e8 | 203 | if (_hardwareSpi) { |
displaymodule | 0:d6ff5fa503e8 | 204 | // We use 7-bits from the first byte and 5-bit from the second byte |
displaymodule | 0:d6ff5fa503e8 | 205 | temp = spiRead(); |
displaymodule | 0:d6ff5fa503e8 | 206 | value = temp<<8; |
displaymodule | 0:d6ff5fa503e8 | 207 | temp = spiRead(); |
displaymodule | 7:6cd8c36cbdb3 | 208 | |
displaymodule | 7:6cd8c36cbdb3 | 209 | value |= temp; |
displaymodule | 0:d6ff5fa503e8 | 210 | value >>=3; |
displaymodule | 0:d6ff5fa503e8 | 211 | value &= 0xFFF; |
displaymodule | 7:6cd8c36cbdb3 | 212 | } else { |
displaymodule | 0:d6ff5fa503e8 | 213 | pulse_high(_pinCLK, _bitmaskCLK); |
displaymodule | 0:d6ff5fa503e8 | 214 | unsigned nop; |
displaymodule | 0:d6ff5fa503e8 | 215 | uint8_t count=0; |
displaymodule | 0:d6ff5fa503e8 | 216 | for(count=0;count<12;count++) { |
displaymodule | 0:d6ff5fa503e8 | 217 | value<<=1; |
displaymodule | 0:d6ff5fa503e8 | 218 | pulse_high(_pinCLK, _bitmaskCLK); |
displaymodule | 0:d6ff5fa503e8 | 219 | if ( gbi(_pinMISO, _bitmaskMISO) ) { |
displaymodule | 0:d6ff5fa503e8 | 220 | value++; |
displaymodule | 0:d6ff5fa503e8 | 221 | } |
displaymodule | 0:d6ff5fa503e8 | 222 | } |
displaymodule | 0:d6ff5fa503e8 | 223 | } |
displaymodule | 0:d6ff5fa503e8 | 224 | #elif defined (DM_TOOLCHAIN_MBED) |
displaymodule | 0:d6ff5fa503e8 | 225 | // We use 7-bits from the first byte and 5-bit from the second byte |
displaymodule | 0:d6ff5fa503e8 | 226 | temp = spiRead(); |
displaymodule | 0:d6ff5fa503e8 | 227 | value = temp<<8; |
displaymodule | 0:d6ff5fa503e8 | 228 | temp = spiRead(); |
displaymodule | 0:d6ff5fa503e8 | 229 | value |= temp; |
displaymodule | 0:d6ff5fa503e8 | 230 | value >>=3; |
displaymodule | 0:d6ff5fa503e8 | 231 | value &= 0xFFF; |
displaymodule | 0:d6ff5fa503e8 | 232 | #endif |
displaymodule | 0:d6ff5fa503e8 | 233 | return value; |
displaymodule | 0:d6ff5fa503e8 | 234 | } |
displaymodule | 0:d6ff5fa503e8 | 235 | |
displaymodule | 7:6cd8c36cbdb3 | 236 | void DmTouch::readRawData(uint16_t &x, uint16_t &y) { |
displaymodule | 7:6cd8c36cbdb3 | 237 | cbi(_pinCS, _bitmaskCS); |
displaymodule | 7:6cd8c36cbdb3 | 238 | x = readData12(0xD0); |
displaymodule | 7:6cd8c36cbdb3 | 239 | y = readData12(0x90); |
displaymodule | 7:6cd8c36cbdb3 | 240 | sbi(_pinCS, _bitmaskCS); |
displaymodule | 7:6cd8c36cbdb3 | 241 | } |
displaymodule | 7:6cd8c36cbdb3 | 242 | |
displaymodule | 7:6cd8c36cbdb3 | 243 | void DmTouch::readTouchData(uint16_t& posX, uint16_t& posY, bool& touching) { |
displaymodule | 7:6cd8c36cbdb3 | 244 | uint16_t touchX, touchY; |
displaymodule | 7:6cd8c36cbdb3 | 245 | getMiddleXY(touchX,touchY); |
displaymodule | 0:d6ff5fa503e8 | 246 | |
displaymodule | 7:6cd8c36cbdb3 | 247 | posX = getDisplayCoordinateX(touchX, touchY); |
displaymodule | 7:6cd8c36cbdb3 | 248 | posY = getDisplayCoordinateY(touchX, touchY); |
displaymodule | 7:6cd8c36cbdb3 | 249 | |
displaymodule | 8:b24f01d148c4 | 250 | #if defined (DM_TOOLCHAIN_ARDUINO) |
displaymodule | 7:6cd8c36cbdb3 | 251 | touching = isTouched(); |
displaymodule | 8:b24f01d148c4 | 252 | #elif defined (DM_TOOLCHAIN_MBED) |
displaymodule | 8:b24f01d148c4 | 253 | touching = (posX < 4096 && posY < 4096); |
displaymodule | 8:b24f01d148c4 | 254 | #endif |
displaymodule | 0:d6ff5fa503e8 | 255 | } |
displaymodule | 0:d6ff5fa503e8 | 256 | |
displaymodule | 7:6cd8c36cbdb3 | 257 | bool DmTouch::isSampleValid() { |
displaymodule | 7:6cd8c36cbdb3 | 258 | uint16_t sampleX,sampleY; |
displaymodule | 7:6cd8c36cbdb3 | 259 | readRawData(sampleX,sampleY); |
displaymodule | 7:6cd8c36cbdb3 | 260 | if (sampleX > 0 && sampleX < 4095 && sampleY > 0 && sampleY < 4095) { |
displaymodule | 7:6cd8c36cbdb3 | 261 | return true; |
displaymodule | 7:6cd8c36cbdb3 | 262 | } else { |
displaymodule | 7:6cd8c36cbdb3 | 263 | return false; |
displaymodule | 7:6cd8c36cbdb3 | 264 | } |
displaymodule | 7:6cd8c36cbdb3 | 265 | } |
displaymodule | 7:6cd8c36cbdb3 | 266 | |
displaymodule | 7:6cd8c36cbdb3 | 267 | bool DmTouch::isTouched() { |
displaymodule | 0:d6ff5fa503e8 | 268 | #if defined (DM_TOOLCHAIN_ARDUINO) |
displaymodule | 0:d6ff5fa503e8 | 269 | if (_irq == -1) { |
displaymodule | 7:6cd8c36cbdb3 | 270 | return isSampleValid(); |
displaymodule | 0:d6ff5fa503e8 | 271 | } |
displaymodule | 0:d6ff5fa503e8 | 272 | |
displaymodule | 0:d6ff5fa503e8 | 273 | if ( !gbi(_pinIrq, _bitmaskIrq) ) { |
displaymodule | 0:d6ff5fa503e8 | 274 | return true; |
displaymodule | 0:d6ff5fa503e8 | 275 | } |
displaymodule | 0:d6ff5fa503e8 | 276 | |
displaymodule | 0:d6ff5fa503e8 | 277 | return false; |
displaymodule | 0:d6ff5fa503e8 | 278 | #elif defined (DM_TOOLCHAIN_MBED) |
displaymodule | 8:b24f01d148c4 | 279 | return isSampleValid(); |
displaymodule | 0:d6ff5fa503e8 | 280 | #endif |
displaymodule | 0:d6ff5fa503e8 | 281 | } |
displaymodule | 7:6cd8c36cbdb3 | 282 | |
displaymodule | 7:6cd8c36cbdb3 | 283 | bool DmTouch::getMiddleXY(uint16_t &x, uint16_t &y) { |
displaymodule | 7:6cd8c36cbdb3 | 284 | bool haveAllMeasurements = true; |
displaymodule | 7:6cd8c36cbdb3 | 285 | uint16_t valuesX[MEASUREMENTS]; |
displaymodule | 7:6cd8c36cbdb3 | 286 | uint16_t valuesY[MEASUREMENTS]; |
displaymodule | 7:6cd8c36cbdb3 | 287 | uint8_t nbrOfMeasurements = 0; |
displaymodule | 7:6cd8c36cbdb3 | 288 | |
displaymodule | 7:6cd8c36cbdb3 | 289 | for (int i=0; i<MEASUREMENTS; i++) { |
displaymodule | 7:6cd8c36cbdb3 | 290 | getAverageXY(valuesX[i], valuesY[i]); |
displaymodule | 7:6cd8c36cbdb3 | 291 | nbrOfMeasurements++; |
displaymodule | 7:6cd8c36cbdb3 | 292 | if (!isTouched()) { |
displaymodule | 7:6cd8c36cbdb3 | 293 | haveAllMeasurements = false; |
displaymodule | 7:6cd8c36cbdb3 | 294 | break; |
displaymodule | 7:6cd8c36cbdb3 | 295 | } |
displaymodule | 7:6cd8c36cbdb3 | 296 | } |
displaymodule | 7:6cd8c36cbdb3 | 297 | x = calculateMiddleValue(valuesX, nbrOfMeasurements); |
displaymodule | 7:6cd8c36cbdb3 | 298 | y = calculateMiddleValue(valuesY, nbrOfMeasurements); |
displaymodule | 7:6cd8c36cbdb3 | 299 | |
displaymodule | 7:6cd8c36cbdb3 | 300 | return haveAllMeasurements; |
displaymodule | 7:6cd8c36cbdb3 | 301 | } |
displaymodule | 7:6cd8c36cbdb3 | 302 | |
displaymodule | 7:6cd8c36cbdb3 | 303 | void DmTouch::getAverageXY(uint16_t &x, uint16_t &y) { |
displaymodule | 7:6cd8c36cbdb3 | 304 | uint32_t sumX = 0; |
displaymodule | 7:6cd8c36cbdb3 | 305 | uint32_t sumY = 0; |
displaymodule | 7:6cd8c36cbdb3 | 306 | uint16_t sampleX,sampleY; |
displaymodule | 7:6cd8c36cbdb3 | 307 | readRawData(sampleX,sampleY); |
displaymodule | 7:6cd8c36cbdb3 | 308 | |
displaymodule | 7:6cd8c36cbdb3 | 309 | for (int i=0; i<_samplesPerMeasurement; i++) { |
displaymodule | 7:6cd8c36cbdb3 | 310 | readRawData(sampleX,sampleY); |
displaymodule | 7:6cd8c36cbdb3 | 311 | sumX += sampleX; |
displaymodule | 7:6cd8c36cbdb3 | 312 | sumY += sampleY; |
displaymodule | 7:6cd8c36cbdb3 | 313 | } |
displaymodule | 7:6cd8c36cbdb3 | 314 | |
displaymodule | 7:6cd8c36cbdb3 | 315 | x = (uint32_t)sumX/_samplesPerMeasurement; |
displaymodule | 7:6cd8c36cbdb3 | 316 | y = (uint32_t)sumY/_samplesPerMeasurement; |
displaymodule | 7:6cd8c36cbdb3 | 317 | } |
displaymodule | 7:6cd8c36cbdb3 | 318 | |
displaymodule | 7:6cd8c36cbdb3 | 319 | // Total number of samples = MEASUREMENTS * _samplesPerMeasurement |
displaymodule | 7:6cd8c36cbdb3 | 320 | void DmTouch::setPrecison(uint8_t samplesPerMeasurement) { |
displaymodule | 7:6cd8c36cbdb3 | 321 | _samplesPerMeasurement = samplesPerMeasurement; |
displaymodule | 7:6cd8c36cbdb3 | 322 | } |
displaymodule | 7:6cd8c36cbdb3 | 323 | |
displaymodule | 7:6cd8c36cbdb3 | 324 | void DmTouch::setCalibrationMatrix(CalibrationMatrix calibrationMatrix) { |
displaymodule | 7:6cd8c36cbdb3 | 325 | _calibrationMatrix = calibrationMatrix; |
displaymodule | 7:6cd8c36cbdb3 | 326 | } |
displaymodule | 7:6cd8c36cbdb3 | 327 | |
displaymodule | 7:6cd8c36cbdb3 | 328 | void DmTouch::waitForTouch() { |
displaymodule | 7:6cd8c36cbdb3 | 329 | while(!isTouched()) {} |
displaymodule | 7:6cd8c36cbdb3 | 330 | } |
displaymodule | 7:6cd8c36cbdb3 | 331 | |
displaymodule | 7:6cd8c36cbdb3 | 332 | void DmTouch::waitForTouchRelease() { |
displaymodule | 7:6cd8c36cbdb3 | 333 | while(isTouched()) {} |
displaymodule | 7:6cd8c36cbdb3 | 334 | } |
displaymodule | 7:6cd8c36cbdb3 | 335 | |
displaymodule | 7:6cd8c36cbdb3 | 336 | uint16_t DmTouch::getDisplayCoordinateX(uint16_t x_touch, uint16_t y_touch) { |
displaymodule | 7:6cd8c36cbdb3 | 337 | uint16_t Xd; |
displaymodule | 7:6cd8c36cbdb3 | 338 | float temp; |
displaymodule | 7:6cd8c36cbdb3 | 339 | temp = (_calibrationMatrix.a * x_touch + _calibrationMatrix.b * y_touch + _calibrationMatrix.c) / rescaleFactor(); |
displaymodule | 7:6cd8c36cbdb3 | 340 | Xd = (uint16_t)(temp); |
displaymodule | 7:6cd8c36cbdb3 | 341 | if (Xd > 60000) { |
displaymodule | 7:6cd8c36cbdb3 | 342 | Xd = 0; |
displaymodule | 7:6cd8c36cbdb3 | 343 | } |
displaymodule | 7:6cd8c36cbdb3 | 344 | return Xd; |
displaymodule | 7:6cd8c36cbdb3 | 345 | } |
displaymodule | 7:6cd8c36cbdb3 | 346 | |
displaymodule | 7:6cd8c36cbdb3 | 347 | uint16_t DmTouch::getDisplayCoordinateY(uint16_t x_touch, uint16_t y_touch) { |
displaymodule | 7:6cd8c36cbdb3 | 348 | uint16_t Yd; |
displaymodule | 7:6cd8c36cbdb3 | 349 | float temp; |
displaymodule | 7:6cd8c36cbdb3 | 350 | temp = (_calibrationMatrix.d * x_touch + _calibrationMatrix.e * y_touch + _calibrationMatrix.f) / rescaleFactor(); |
displaymodule | 7:6cd8c36cbdb3 | 351 | Yd = (uint16_t)(temp); |
displaymodule | 7:6cd8c36cbdb3 | 352 | if (Yd > 60000) { |
displaymodule | 7:6cd8c36cbdb3 | 353 | Yd = 0; |
displaymodule | 7:6cd8c36cbdb3 | 354 | } |
displaymodule | 7:6cd8c36cbdb3 | 355 | return Yd; |
displaymodule | 7:6cd8c36cbdb3 | 356 | } |
displaymodule | 7:6cd8c36cbdb3 | 357 | |
displaymodule | 7:6cd8c36cbdb3 | 358 | uint16_t DmTouch::calculateMiddleValue(uint16_t values[], uint8_t count) { |
displaymodule | 7:6cd8c36cbdb3 | 359 | uint16_t temp; |
displaymodule | 7:6cd8c36cbdb3 | 360 | |
displaymodule | 7:6cd8c36cbdb3 | 361 | for(uint8_t i=0; i<count-1; i++) { |
displaymodule | 7:6cd8c36cbdb3 | 362 | for(uint8_t j=i+1; j<count; j++) { |
displaymodule | 7:6cd8c36cbdb3 | 363 | if(values[j] < values[i]) { |
displaymodule | 7:6cd8c36cbdb3 | 364 | temp = values[i]; |
displaymodule | 7:6cd8c36cbdb3 | 365 | values[i] = values[j]; |
displaymodule | 7:6cd8c36cbdb3 | 366 | values[j] = temp; |
displaymodule | 7:6cd8c36cbdb3 | 367 | } |
displaymodule | 7:6cd8c36cbdb3 | 368 | } |
displaymodule | 7:6cd8c36cbdb3 | 369 | } |
displaymodule | 7:6cd8c36cbdb3 | 370 | |
displaymodule | 7:6cd8c36cbdb3 | 371 | if(count%2==0) { |
displaymodule | 7:6cd8c36cbdb3 | 372 | return((values[count/2] + values[count/2 - 1]) / 2.0); |
displaymodule | 7:6cd8c36cbdb3 | 373 | } else { |
displaymodule | 7:6cd8c36cbdb3 | 374 | return values[count/2]; |
displaymodule | 7:6cd8c36cbdb3 | 375 | } |
displaymodule | 7:6cd8c36cbdb3 | 376 | } |