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@5:2c60f4aafce6, 2014-05-22 (annotated)
- Committer:
- displaymodule
- Date:
- Thu May 22 09:35:12 2014 +0000
- Revision:
- 5:2c60f4aafce6
- Parent:
- 3:02c19cbc707c
- Child:
- 6:fde03297017b
Removed debug prints in touch controller code which decreased speed.
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 | 0:d6ff5fa503e8 | 15 | |
displaymodule | 0:d6ff5fa503e8 | 16 | #if defined(DM_TOOLCHAIN_ARDUINO) |
displaymodule | 0:d6ff5fa503e8 | 17 | // disp - which display is used |
displaymodule | 0:d6ff5fa503e8 | 18 | // cs - pin number CS (Chip Select, SS) |
displaymodule | 0:d6ff5fa503e8 | 19 | // irq - pin number IRQ (set to -1 if you don't want to use) |
displaymodule | 0:d6ff5fa503e8 | 20 | // hardwareSpi - set to 1 if you want to use hardware Spi, 0 otherwise |
displaymodule | 0:d6ff5fa503e8 | 21 | // (note, if you use hardware Spi on arduino, clk, mosi and miso will be disregarded) |
displaymodule | 0:d6ff5fa503e8 | 22 | // clk - pin number CLK (SLK) |
displaymodule | 0:d6ff5fa503e8 | 23 | // mosi - pin number MOSI |
displaymodule | 0:d6ff5fa503e8 | 24 | // miso - pin number MISO |
displaymodule | 0:d6ff5fa503e8 | 25 | // width - width of display (default 240) |
displaymodule | 0:d6ff5fa503e8 | 26 | // height - height of display (default 320) |
displaymodule | 0:d6ff5fa503e8 | 27 | DmTouch::DmTouch(Display disp, int8_t cs, int8_t irq, int8_t hardwareSpi, int8_t clk, int8_t mosi, int8_t miso, uint16_t width, uint16_t height) : _width(width), _height(height) |
displaymodule | 0:d6ff5fa503e8 | 28 | { |
displaymodule | 0:d6ff5fa503e8 | 29 | _hardwareSpi = hardwareSpi; |
displaymodule | 0:d6ff5fa503e8 | 30 | _cs = cs; |
displaymodule | 0:d6ff5fa503e8 | 31 | _irq = irq; |
displaymodule | 0:d6ff5fa503e8 | 32 | _clk = clk; |
displaymodule | 0:d6ff5fa503e8 | 33 | _mosi = mosi; |
displaymodule | 0:d6ff5fa503e8 | 34 | _miso = miso; |
displaymodule | 0:d6ff5fa503e8 | 35 | |
displaymodule | 0:d6ff5fa503e8 | 36 | #elif defined (DM_TOOLCHAIN_MBED) |
displaymodule | 0:d6ff5fa503e8 | 37 | // disp - which display is used |
displaymodule | 0:d6ff5fa503e8 | 38 | DmTouch::DmTouch(Display disp, bool hardwareSPI) |
displaymodule | 0:d6ff5fa503e8 | 39 | { |
displaymodule | 0:d6ff5fa503e8 | 40 | _hardwareSpi = hardwareSPI; |
displaymodule | 0:d6ff5fa503e8 | 41 | switch (disp) { |
displaymodule | 0:d6ff5fa503e8 | 42 | // Display with 40-pin connector on top of adapter board |
displaymodule | 0:d6ff5fa503e8 | 43 | case DmTouch::DM_TFT28_103: |
displaymodule | 0:d6ff5fa503e8 | 44 | case DmTouch::DM_TFT24_104: |
displaymodule | 0:d6ff5fa503e8 | 45 | _cs = D8; |
displaymodule | 0:d6ff5fa503e8 | 46 | _irq = D10; |
displaymodule | 0:d6ff5fa503e8 | 47 | _clk = A1; |
displaymodule | 0:d6ff5fa503e8 | 48 | _mosi = A0; |
displaymodule | 0:d6ff5fa503e8 | 49 | _miso = D9; |
displaymodule | 0:d6ff5fa503e8 | 50 | _width = 240; |
displaymodule | 0:d6ff5fa503e8 | 51 | _height = 320; |
displaymodule | 0:d6ff5fa503e8 | 52 | break; |
displaymodule | 3:02c19cbc707c | 53 | |
displaymodule | 0:d6ff5fa503e8 | 54 | case DmTouch::DM_TFT28_105: |
displaymodule | 0:d6ff5fa503e8 | 55 | _cs = D4; |
displaymodule | 0:d6ff5fa503e8 | 56 | _irq = D2; |
displaymodule | 0:d6ff5fa503e8 | 57 | _clk = D13; |
displaymodule | 0:d6ff5fa503e8 | 58 | _mosi = D11; |
displaymodule | 0:d6ff5fa503e8 | 59 | _miso = D12; |
displaymodule | 0:d6ff5fa503e8 | 60 | _width = 240; |
displaymodule | 0:d6ff5fa503e8 | 61 | _height = 320; |
displaymodule | 0:d6ff5fa503e8 | 62 | break; |
displaymodule | 0:d6ff5fa503e8 | 63 | |
displaymodule | 0:d6ff5fa503e8 | 64 | case DmTouch::DM_TFT35_107: |
displaymodule | 0:d6ff5fa503e8 | 65 | default: |
displaymodule | 0:d6ff5fa503e8 | 66 | _cs = D4; |
displaymodule | 0:d6ff5fa503e8 | 67 | _irq = D2; |
displaymodule | 0:d6ff5fa503e8 | 68 | _clk = D13; |
displaymodule | 0:d6ff5fa503e8 | 69 | _mosi = D11; |
displaymodule | 0:d6ff5fa503e8 | 70 | _miso = D12; |
displaymodule | 0:d6ff5fa503e8 | 71 | _width = 320; |
displaymodule | 0:d6ff5fa503e8 | 72 | _height = 240; |
displaymodule | 0:d6ff5fa503e8 | 73 | break; |
displaymodule | 0:d6ff5fa503e8 | 74 | } |
displaymodule | 0:d6ff5fa503e8 | 75 | #endif |
displaymodule | 0:d6ff5fa503e8 | 76 | |
displaymodule | 0:d6ff5fa503e8 | 77 | // Calibration data for the different displays |
displaymodule | 0:d6ff5fa503e8 | 78 | switch (disp) { |
displaymodule | 0:d6ff5fa503e8 | 79 | case DmTouch::DM_TFT28_103: |
displaymodule | 0:d6ff5fa503e8 | 80 | _calibLowX = 220; |
displaymodule | 0:d6ff5fa503e8 | 81 | _calibLowY = 220; |
displaymodule | 0:d6ff5fa503e8 | 82 | _calibModifierX = 15.25; |
displaymodule | 0:d6ff5fa503e8 | 83 | _calibModifierY = 11.16; |
displaymodule | 0:d6ff5fa503e8 | 84 | _calibInvertedTouch = false; |
displaymodule | 0:d6ff5fa503e8 | 85 | break; |
displaymodule | 0:d6ff5fa503e8 | 86 | |
displaymodule | 0:d6ff5fa503e8 | 87 | case DmTouch::DM_TFT24_104: |
displaymodule | 0:d6ff5fa503e8 | 88 | _calibLowX = 210; |
displaymodule | 0:d6ff5fa503e8 | 89 | _calibLowY = 280; |
displaymodule | 0:d6ff5fa503e8 | 90 | _calibModifierX = 15; |
displaymodule | 0:d6ff5fa503e8 | 91 | _calibModifierY = 11.24; |
displaymodule | 0:d6ff5fa503e8 | 92 | _calibInvertedTouch = true; |
displaymodule | 0:d6ff5fa503e8 | 93 | break; |
displaymodule | 0:d6ff5fa503e8 | 94 | |
displaymodule | 3:02c19cbc707c | 95 | case DmTouch::DM_TFT35_107: |
displaymodule | 0:d6ff5fa503e8 | 96 | _calibLowX = 912; |
displaymodule | 0:d6ff5fa503e8 | 97 | _calibLowY = 422 ; |
displaymodule | 0:d6ff5fa503e8 | 98 | _calibModifierX = 3.25; |
displaymodule | 0:d6ff5fa503e8 | 99 | _calibModifierY = 1.175; |
displaymodule | 0:d6ff5fa503e8 | 100 | _calibInvertedTouch = false; |
displaymodule | 0:d6ff5fa503e8 | 101 | break; |
displaymodule | 3:02c19cbc707c | 102 | |
displaymodule | 0:d6ff5fa503e8 | 103 | case DmTouch::DM_TFT28_105: |
displaymodule | 0:d6ff5fa503e8 | 104 | default: |
displaymodule | 0:d6ff5fa503e8 | 105 | _calibLowX = 260; |
displaymodule | 0:d6ff5fa503e8 | 106 | _calibLowY = 220; |
displaymodule | 0:d6ff5fa503e8 | 107 | _calibModifierX = 14.42; |
displaymodule | 0:d6ff5fa503e8 | 108 | _calibModifierY = 10.78; |
displaymodule | 0:d6ff5fa503e8 | 109 | _calibInvertedTouch = false; |
displaymodule | 0:d6ff5fa503e8 | 110 | break; |
displaymodule | 0:d6ff5fa503e8 | 111 | } |
displaymodule | 0:d6ff5fa503e8 | 112 | } |
displaymodule | 0:d6ff5fa503e8 | 113 | |
displaymodule | 0:d6ff5fa503e8 | 114 | void DmTouch::init() { |
displaymodule | 0:d6ff5fa503e8 | 115 | #if defined (DM_TOOLCHAIN_ARDUINO) |
displaymodule | 0:d6ff5fa503e8 | 116 | pinMode(_cs, OUTPUT); |
displaymodule | 0:d6ff5fa503e8 | 117 | _pinCS = portOutputRegister(digitalPinToPort(_cs)); |
displaymodule | 0:d6ff5fa503e8 | 118 | _bitmaskCS = digitalPinToBitMask(_cs); |
displaymodule | 0:d6ff5fa503e8 | 119 | |
displaymodule | 0:d6ff5fa503e8 | 120 | if (_hardwareSpi) { |
displaymodule | 0:d6ff5fa503e8 | 121 | SPI.begin(); |
displaymodule | 0:d6ff5fa503e8 | 122 | SPI.setClockDivider(SPI_CLOCK_DIV2); |
displaymodule | 0:d6ff5fa503e8 | 123 | SPI.setBitOrder(MSBFIRST); |
displaymodule | 0:d6ff5fa503e8 | 124 | SPI.setDataMode(SPI_MODE0); |
displaymodule | 0:d6ff5fa503e8 | 125 | _spiSettings = SPCR; |
displaymodule | 0:d6ff5fa503e8 | 126 | } |
displaymodule | 0:d6ff5fa503e8 | 127 | else { |
displaymodule | 0:d6ff5fa503e8 | 128 | pinMode(_clk, OUTPUT); |
displaymodule | 0:d6ff5fa503e8 | 129 | pinMode(_mosi, OUTPUT); |
displaymodule | 0:d6ff5fa503e8 | 130 | pinMode(_miso, INPUT); |
displaymodule | 0:d6ff5fa503e8 | 131 | _pinCLK = portOutputRegister(digitalPinToPort(_clk)); |
displaymodule | 0:d6ff5fa503e8 | 132 | _bitmaskCLK = digitalPinToBitMask(_clk); |
displaymodule | 0:d6ff5fa503e8 | 133 | _pinMOSI = portOutputRegister(digitalPinToPort(_mosi)); |
displaymodule | 0:d6ff5fa503e8 | 134 | _bitmaskMOSI = digitalPinToBitMask(_mosi); |
displaymodule | 0:d6ff5fa503e8 | 135 | _pinMISO = portInputRegister(digitalPinToPort(_miso)); |
displaymodule | 0:d6ff5fa503e8 | 136 | _bitmaskMISO = digitalPinToBitMask(_miso); |
displaymodule | 0:d6ff5fa503e8 | 137 | } |
displaymodule | 0:d6ff5fa503e8 | 138 | #elif defined (DM_TOOLCHAIN_MBED) |
displaymodule | 0:d6ff5fa503e8 | 139 | _pinCS = new DigitalOut((PinName)_cs); |
displaymodule | 0:d6ff5fa503e8 | 140 | if (_hardwareSpi) { |
displaymodule | 0:d6ff5fa503e8 | 141 | sbi(_pinCS, _bitmaskCS); |
displaymodule | 0:d6ff5fa503e8 | 142 | _spi = new SPI((PinName)_mosi, (PinName)_miso, (PinName)_clk); |
displaymodule | 0:d6ff5fa503e8 | 143 | _spi->format(8,0); |
displaymodule | 0:d6ff5fa503e8 | 144 | _spi->frequency(2000000); // Max SPI speed |
displaymodule | 0:d6ff5fa503e8 | 145 | //cbi(_pinCS, _bitmaskCS); |
displaymodule | 0:d6ff5fa503e8 | 146 | } else { |
displaymodule | 0:d6ff5fa503e8 | 147 | _pinCLK = new DigitalOut((PinName)_clk); |
displaymodule | 3:02c19cbc707c | 148 | _pinMISO = new DigitalIn((PinName)_miso); |
displaymodule | 0:d6ff5fa503e8 | 149 | _pinMOSI = new DigitalOut((PinName)_mosi); |
displaymodule | 3:02c19cbc707c | 150 | sbi(_pinCLK, _bitmaskCLK); |
displaymodule | 0:d6ff5fa503e8 | 151 | } |
displaymodule | 0:d6ff5fa503e8 | 152 | #endif |
displaymodule | 0:d6ff5fa503e8 | 153 | |
displaymodule | 0:d6ff5fa503e8 | 154 | if (_irq != -1) { // We will use Touch IRQ |
displaymodule | 0:d6ff5fa503e8 | 155 | enableIrq(); |
displaymodule | 0:d6ff5fa503e8 | 156 | } |
displaymodule | 0:d6ff5fa503e8 | 157 | } |
displaymodule | 0:d6ff5fa503e8 | 158 | |
displaymodule | 0:d6ff5fa503e8 | 159 | void DmTouch::enableIrq() { |
displaymodule | 0:d6ff5fa503e8 | 160 | #if defined (DM_TOOLCHAIN_ARDUINO) |
displaymodule | 0:d6ff5fa503e8 | 161 | pinMode(_irq, INPUT); |
displaymodule | 0:d6ff5fa503e8 | 162 | _pinIrq = portInputRegister(digitalPinToPort(_irq)); |
displaymodule | 0:d6ff5fa503e8 | 163 | _bitmaskIrq = digitalPinToBitMask(_irq); |
displaymodule | 0:d6ff5fa503e8 | 164 | #elif defined (DM_TOOLCHAIN_MBED) |
displaymodule | 0:d6ff5fa503e8 | 165 | _pinIrq = new DigitalIn((PinName)_irq); |
displaymodule | 0:d6ff5fa503e8 | 166 | _pinIrq->mode(PullUp); |
displaymodule | 0:d6ff5fa503e8 | 167 | #endif |
displaymodule | 0:d6ff5fa503e8 | 168 | |
displaymodule | 0:d6ff5fa503e8 | 169 | cbi(_pinCS, _bitmaskCS); |
displaymodule | 0:d6ff5fa503e8 | 170 | spiWrite(0x80); // Enable PENIRQ |
displaymodule | 0:d6ff5fa503e8 | 171 | sbi(_pinCS, _bitmaskCS); |
displaymodule | 0:d6ff5fa503e8 | 172 | } |
displaymodule | 0:d6ff5fa503e8 | 173 | |
displaymodule | 0:d6ff5fa503e8 | 174 | void DmTouch::spiWrite(uint8_t data) { |
displaymodule | 0:d6ff5fa503e8 | 175 | #if defined (DM_TOOLCHAIN_ARDUINO) |
displaymodule | 0:d6ff5fa503e8 | 176 | if (_hardwareSpi) { |
displaymodule | 0:d6ff5fa503e8 | 177 | SPCR = _spiSettings; // SPI Control Register |
displaymodule | 0:d6ff5fa503e8 | 178 | SPDR = data; // SPI Data Register |
displaymodule | 0:d6ff5fa503e8 | 179 | while(!(SPSR & _BV(SPIF))); // SPI Status Register Wait for transmission to finish |
displaymodule | 0:d6ff5fa503e8 | 180 | } |
displaymodule | 0:d6ff5fa503e8 | 181 | else { |
displaymodule | 0:d6ff5fa503e8 | 182 | uint8_t count=0; |
displaymodule | 0:d6ff5fa503e8 | 183 | uint8_t temp; |
displaymodule | 0:d6ff5fa503e8 | 184 | unsigned nop; |
displaymodule | 0:d6ff5fa503e8 | 185 | temp=data; // really needed? |
displaymodule | 0:d6ff5fa503e8 | 186 | cbi(_pinCLK, _bitmaskCLK); |
displaymodule | 0:d6ff5fa503e8 | 187 | for(count=0;count<8;count++) { |
displaymodule | 0:d6ff5fa503e8 | 188 | if(temp&0x80) { |
displaymodule | 0:d6ff5fa503e8 | 189 | sbi(_pinMOSI, _bitmaskMOSI); |
displaymodule | 0:d6ff5fa503e8 | 190 | } |
displaymodule | 0:d6ff5fa503e8 | 191 | else { |
displaymodule | 0:d6ff5fa503e8 | 192 | cbi(_pinMOSI, _bitmaskMOSI); |
displaymodule | 0:d6ff5fa503e8 | 193 | } |
displaymodule | 0:d6ff5fa503e8 | 194 | |
displaymodule | 0:d6ff5fa503e8 | 195 | temp=temp<<1; |
displaymodule | 0:d6ff5fa503e8 | 196 | |
displaymodule | 0:d6ff5fa503e8 | 197 | pulse_low(_pinCLK, _bitmaskCLK); |
displaymodule | 0:d6ff5fa503e8 | 198 | } |
displaymodule | 0:d6ff5fa503e8 | 199 | } |
displaymodule | 0:d6ff5fa503e8 | 200 | #elif defined (DM_TOOLCHAIN_MBED) |
displaymodule | 0:d6ff5fa503e8 | 201 | if (_hardwareSpi) { |
displaymodule | 0:d6ff5fa503e8 | 202 | _spi->write(data); |
displaymodule | 0:d6ff5fa503e8 | 203 | } |
displaymodule | 0:d6ff5fa503e8 | 204 | else { |
displaymodule | 0:d6ff5fa503e8 | 205 | uint8_t count=0; |
displaymodule | 0:d6ff5fa503e8 | 206 | uint8_t temp = data; |
displaymodule | 0:d6ff5fa503e8 | 207 | for(count=0;count<8;count++) { |
displaymodule | 0:d6ff5fa503e8 | 208 | if(temp&0x80) { |
displaymodule | 0:d6ff5fa503e8 | 209 | sbi(_pinMOSI, _bitmaskMOSI); |
displaymodule | 0:d6ff5fa503e8 | 210 | } |
displaymodule | 0:d6ff5fa503e8 | 211 | else { |
displaymodule | 0:d6ff5fa503e8 | 212 | cbi(_pinMOSI, _bitmaskMOSI); |
displaymodule | 0:d6ff5fa503e8 | 213 | } |
displaymodule | 0:d6ff5fa503e8 | 214 | |
displaymodule | 0:d6ff5fa503e8 | 215 | temp=temp<<1; |
displaymodule | 0:d6ff5fa503e8 | 216 | |
displaymodule | 0:d6ff5fa503e8 | 217 | pulse_low(_pinCLK, _bitmaskCLK); |
displaymodule | 0:d6ff5fa503e8 | 218 | } |
displaymodule | 0:d6ff5fa503e8 | 219 | } |
displaymodule | 0:d6ff5fa503e8 | 220 | #endif |
displaymodule | 0:d6ff5fa503e8 | 221 | } |
displaymodule | 0:d6ff5fa503e8 | 222 | |
displaymodule | 0:d6ff5fa503e8 | 223 | uint8_t DmTouch::spiRead() {// Only used for Hardware SPI |
displaymodule | 0:d6ff5fa503e8 | 224 | #if defined (DM_TOOLCHAIN_ARDUINO) |
displaymodule | 0:d6ff5fa503e8 | 225 | uint8_t data; |
displaymodule | 0:d6ff5fa503e8 | 226 | SPCR = _spiSettings; |
displaymodule | 0:d6ff5fa503e8 | 227 | spiWrite(0x00); |
displaymodule | 0:d6ff5fa503e8 | 228 | data = SPDR; |
displaymodule | 0:d6ff5fa503e8 | 229 | |
displaymodule | 0:d6ff5fa503e8 | 230 | return data; |
displaymodule | 0:d6ff5fa503e8 | 231 | #elif defined (DM_TOOLCHAIN_MBED) |
displaymodule | 0:d6ff5fa503e8 | 232 | if (_hardwareSpi) { |
displaymodule | 0:d6ff5fa503e8 | 233 | return _spi->write(0x00); // dummy byte to read |
displaymodule | 0:d6ff5fa503e8 | 234 | } else { |
displaymodule | 0:d6ff5fa503e8 | 235 | uint8_t count=0; |
displaymodule | 0:d6ff5fa503e8 | 236 | uint8_t temp=0; |
displaymodule | 3:02c19cbc707c | 237 | cbi(_pinMOSI, _bitmaskMOSI); // same as using 0x00 as dummy byte |
displaymodule | 0:d6ff5fa503e8 | 238 | for(count=0;count<8;count++) { |
displaymodule | 0:d6ff5fa503e8 | 239 | |
displaymodule | 0:d6ff5fa503e8 | 240 | pulse_low(_pinCLK, _bitmaskCLK); |
displaymodule | 0:d6ff5fa503e8 | 241 | temp = temp<<1; |
displaymodule | 0:d6ff5fa503e8 | 242 | temp |= _pinMISO->read(); |
displaymodule | 0:d6ff5fa503e8 | 243 | } |
displaymodule | 0:d6ff5fa503e8 | 244 | return temp; |
displaymodule | 0:d6ff5fa503e8 | 245 | } |
displaymodule | 0:d6ff5fa503e8 | 246 | #endif |
displaymodule | 0:d6ff5fa503e8 | 247 | } |
displaymodule | 0:d6ff5fa503e8 | 248 | |
displaymodule | 0:d6ff5fa503e8 | 249 | uint16_t DmTouch::readData12(uint8_t command) { |
displaymodule | 0:d6ff5fa503e8 | 250 | uint8_t temp = 0; |
displaymodule | 0:d6ff5fa503e8 | 251 | uint16_t value = 0; |
displaymodule | 0:d6ff5fa503e8 | 252 | |
displaymodule | 0:d6ff5fa503e8 | 253 | spiWrite(command); // Send command |
displaymodule | 0:d6ff5fa503e8 | 254 | //-------------- |
displaymodule | 0:d6ff5fa503e8 | 255 | #if defined (DM_TOOLCHAIN_ARDUINO) |
displaymodule | 0:d6ff5fa503e8 | 256 | if (_hardwareSpi) { |
displaymodule | 0:d6ff5fa503e8 | 257 | // We use 7-bits from the first byte and 5-bit from the second byte |
displaymodule | 0:d6ff5fa503e8 | 258 | temp = spiRead(); |
displaymodule | 0:d6ff5fa503e8 | 259 | value = temp<<8; |
displaymodule | 0:d6ff5fa503e8 | 260 | temp = spiRead(); |
displaymodule | 0:d6ff5fa503e8 | 261 | value |= temp; |
displaymodule | 0:d6ff5fa503e8 | 262 | value >>=3; |
displaymodule | 0:d6ff5fa503e8 | 263 | value &= 0xFFF; |
displaymodule | 0:d6ff5fa503e8 | 264 | } |
displaymodule | 0:d6ff5fa503e8 | 265 | else { |
displaymodule | 0:d6ff5fa503e8 | 266 | pulse_high(_pinCLK, _bitmaskCLK); |
displaymodule | 0:d6ff5fa503e8 | 267 | unsigned nop; |
displaymodule | 0:d6ff5fa503e8 | 268 | uint8_t count=0; |
displaymodule | 0:d6ff5fa503e8 | 269 | for(count=0;count<12;count++) { |
displaymodule | 0:d6ff5fa503e8 | 270 | value<<=1; |
displaymodule | 0:d6ff5fa503e8 | 271 | pulse_high(_pinCLK, _bitmaskCLK); |
displaymodule | 0:d6ff5fa503e8 | 272 | if ( gbi(_pinMISO, _bitmaskMISO) ) { |
displaymodule | 0:d6ff5fa503e8 | 273 | value++; |
displaymodule | 0:d6ff5fa503e8 | 274 | } |
displaymodule | 0:d6ff5fa503e8 | 275 | } |
displaymodule | 0:d6ff5fa503e8 | 276 | } |
displaymodule | 0:d6ff5fa503e8 | 277 | #elif defined (DM_TOOLCHAIN_MBED) |
displaymodule | 0:d6ff5fa503e8 | 278 | // We use 7-bits from the first byte and 5-bit from the second byte |
displaymodule | 0:d6ff5fa503e8 | 279 | temp = spiRead(); |
displaymodule | 0:d6ff5fa503e8 | 280 | value = temp<<8; |
displaymodule | 0:d6ff5fa503e8 | 281 | temp = spiRead(); |
displaymodule | 0:d6ff5fa503e8 | 282 | value |= temp; |
displaymodule | 0:d6ff5fa503e8 | 283 | value >>=3; |
displaymodule | 0:d6ff5fa503e8 | 284 | value &= 0xFFF; |
displaymodule | 0:d6ff5fa503e8 | 285 | #endif |
displaymodule | 0:d6ff5fa503e8 | 286 | return value; |
displaymodule | 0:d6ff5fa503e8 | 287 | } |
displaymodule | 0:d6ff5fa503e8 | 288 | |
displaymodule | 0:d6ff5fa503e8 | 289 | void DmTouch::readTouchData(uint16_t& posX, uint16_t& posY, bool& touching) { |
displaymodule | 0:d6ff5fa503e8 | 290 | #if defined (DM_TOOLCHAIN_MBED) |
displaymodule | 0:d6ff5fa503e8 | 291 | if (!isTouched()) { |
displaymodule | 0:d6ff5fa503e8 | 292 | touching = false; |
displaymodule | 0:d6ff5fa503e8 | 293 | return; |
displaymodule | 0:d6ff5fa503e8 | 294 | } |
displaymodule | 0:d6ff5fa503e8 | 295 | //touching = true; |
displaymodule | 0:d6ff5fa503e8 | 296 | #endif |
displaymodule | 0:d6ff5fa503e8 | 297 | unsigned int TP_X, TP_Y; |
displaymodule | 0:d6ff5fa503e8 | 298 | cbi(_pinCS, _bitmaskCS); |
displaymodule | 0:d6ff5fa503e8 | 299 | |
displaymodule | 0:d6ff5fa503e8 | 300 | TP_X = readData12(0xD0); |
displaymodule | 0:d6ff5fa503e8 | 301 | TP_Y = readData12(0x90); |
displaymodule | 0:d6ff5fa503e8 | 302 | |
displaymodule | 0:d6ff5fa503e8 | 303 | sbi(_pinCS, _bitmaskCS); |
displaymodule | 0:d6ff5fa503e8 | 304 | |
displaymodule | 0:d6ff5fa503e8 | 305 | // Serial.print("Raw X: "); |
displaymodule | 0:d6ff5fa503e8 | 306 | // Serial.println(TP_X); |
displaymodule | 0:d6ff5fa503e8 | 307 | // Serial.print("Raw Y: "); |
displaymodule | 0:d6ff5fa503e8 | 308 | // Serial.println(TP_Y); |
displaymodule | 0:d6ff5fa503e8 | 309 | |
displaymodule | 0:d6ff5fa503e8 | 310 | // Convert raw data to screen positions |
displaymodule | 0:d6ff5fa503e8 | 311 | if (_calibInvertedTouch) { |
displaymodule | 0:d6ff5fa503e8 | 312 | posX=_width-((TP_X-_calibLowX)/_calibModifierX); |
displaymodule | 0:d6ff5fa503e8 | 313 | posY=_height-((TP_Y-_calibLowY)/_calibModifierY); |
displaymodule | 0:d6ff5fa503e8 | 314 | } else { |
displaymodule | 0:d6ff5fa503e8 | 315 | posX=((TP_X-_calibLowX)/_calibModifierX); |
displaymodule | 0:d6ff5fa503e8 | 316 | posY=((TP_Y-_calibLowY)/_calibModifierY); |
displaymodule | 0:d6ff5fa503e8 | 317 | } |
displaymodule | 5:2c60f4aafce6 | 318 | //printf("Raw: %5u, %5u, Calibrated: %3u, %u\n", TP_X, TP_Y, posX, posY); |
displaymodule | 0:d6ff5fa503e8 | 319 | |
displaymodule | 0:d6ff5fa503e8 | 320 | //#if defined (DM_TOOLCHAIN_ARDUINO) |
displaymodule | 0:d6ff5fa503e8 | 321 | if (posX >= 0 && posX <= _width && posY >= 0 && posY <= _height) { |
displaymodule | 0:d6ff5fa503e8 | 322 | touching = true; |
displaymodule | 0:d6ff5fa503e8 | 323 | } else { |
displaymodule | 0:d6ff5fa503e8 | 324 | touching = false; |
displaymodule | 0:d6ff5fa503e8 | 325 | } |
displaymodule | 0:d6ff5fa503e8 | 326 | //#endif |
displaymodule | 0:d6ff5fa503e8 | 327 | } |
displaymodule | 0:d6ff5fa503e8 | 328 | |
displaymodule | 0:d6ff5fa503e8 | 329 | uint8_t DmTouch::isTouched() { |
displaymodule | 0:d6ff5fa503e8 | 330 | #if defined (DM_TOOLCHAIN_ARDUINO) |
displaymodule | 0:d6ff5fa503e8 | 331 | if (_irq == -1) { |
displaymodule | 0:d6ff5fa503e8 | 332 | uint16_t posX, posY; |
displaymodule | 0:d6ff5fa503e8 | 333 | bool touched; |
displaymodule | 0:d6ff5fa503e8 | 334 | readTouchData(posX, posY, touched); |
displaymodule | 0:d6ff5fa503e8 | 335 | return touched; |
displaymodule | 0:d6ff5fa503e8 | 336 | } |
displaymodule | 0:d6ff5fa503e8 | 337 | |
displaymodule | 0:d6ff5fa503e8 | 338 | if ( !gbi(_pinIrq, _bitmaskIrq) ) { |
displaymodule | 0:d6ff5fa503e8 | 339 | return true; |
displaymodule | 0:d6ff5fa503e8 | 340 | } |
displaymodule | 0:d6ff5fa503e8 | 341 | |
displaymodule | 0:d6ff5fa503e8 | 342 | return false; |
displaymodule | 0:d6ff5fa503e8 | 343 | #elif defined (DM_TOOLCHAIN_MBED) |
displaymodule | 0:d6ff5fa503e8 | 344 | return (*_pinIrq == 0); |
displaymodule | 0:d6ff5fa503e8 | 345 | #endif |
displaymodule | 0:d6ff5fa503e8 | 346 | } |