|
| 1 | +/** |
| 2 | + * |
| 3 | + * |
| 4 | + */ |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +#include "Arduino.h" |
| 9 | +#include "XPT2046_touch.h" |
| 10 | + |
| 11 | + |
| 12 | +/****************************************************************************/ |
| 13 | + XPT2046_touch::XPT2046_touch(uint8_t _cs_pin, SPIClass _spiChan) : cs_pin(_cs_pin), my_SPI(_spiChan){ |
| 14 | + } |
| 15 | + |
| 16 | + |
| 17 | +/****************************************************************************/ |
| 18 | + |
| 19 | + void XPT2046_touch::begin(){ |
| 20 | + pinMode(cs_pin, OUTPUT); |
| 21 | + digitalWrite(cs_pin, HIGH); |
| 22 | + my_SPI.begin(); |
| 23 | + } |
| 24 | + |
| 25 | + /****************************************************************************/ |
| 26 | + |
| 27 | + boolean XPT2046_touch::read_XY(uint16_t *xy){ |
| 28 | + int z1, z2, tmpH, tmpL; |
| 29 | + digitalWrite(cs_pin, LOW); |
| 30 | + |
| 31 | + //Check if touch screen is pressed. |
| 32 | + SPI.transfer(B10110011); // Z1 |
| 33 | + delay(10); |
| 34 | + tmpH = (my_SPI.transfer(0) << 5); |
| 35 | + tmpL = (my_SPI.transfer(0) >> 3); |
| 36 | + z1 = tmpH | tmpL; |
| 37 | + |
| 38 | + SPI.transfer(B11000011); // Z2 |
| 39 | + delay(10); |
| 40 | + tmpH = (my_SPI.transfer(0) << 5); |
| 41 | + tmpL = (my_SPI.transfer(0) >> 3); |
| 42 | + z2 = tmpH | tmpL; |
| 43 | + |
| 44 | + if((z2 - z1) < Z_THRESHOLD){ //If the touch screen is pressed, read the X,Y coordinates from XPT2046. |
| 45 | + my_SPI.transfer(B11010011); // X |
| 46 | + delay(10); |
| 47 | + tmpH = (my_SPI.transfer(0) << 5); |
| 48 | + tmpL = (my_SPI.transfer(0) >> 3); |
| 49 | + xy[0] = tmpH | tmpL; |
| 50 | + |
| 51 | + my_SPI.transfer(B10010011); // Y |
| 52 | + delay(10); |
| 53 | + tmpH = (my_SPI.transfer(0) << 5); |
| 54 | + tmpL = (my_SPI.transfer(0) >> 3); |
| 55 | + xy[1] = tmpH | tmpL; |
| 56 | + digitalWrite(cs_pin, HIGH); |
| 57 | + return true; |
| 58 | + } |
| 59 | + digitalWrite(cs_pin, HIGH); |
| 60 | + return false; |
| 61 | + } |
| 62 | + |
| 63 | + /****************************************************************************/ |
| 64 | + void XPT2046_touch::setButtonsNumber(byte columnButtons, byte rowButtons ){ |
| 65 | + _rowButtons = rowButtons; |
| 66 | + _columnButtons = columnButtons; |
| 67 | + } |
| 68 | + |
| 69 | + /****************************************************************************/ |
| 70 | + uint8_t XPT2046_touch::getButtonNumber(){ |
| 71 | + uint16_t xy[2]; |
| 72 | + uint8_t tmp, buttonNum; |
| 73 | + int div; |
| 74 | + |
| 75 | + if(read_XY(xy)){ |
| 76 | + |
| 77 | + div = (X_MAX + X_MIN) / (_columnButtons + 1); |
| 78 | + buttonNum = ((xy[1] / div)); |
| 79 | + |
| 80 | + div = (Y_MAX + Y_MIN) / _rowButtons; |
| 81 | + tmp = ((xy[0] / div)); |
| 82 | + |
| 83 | + return ((buttonNum * _rowButtons) + tmp + 1); //Return the button number. |
| 84 | + } |
| 85 | + |
| 86 | + return 0; //Touch screen is not pressed. |
| 87 | + } |
| 88 | +/****************************************************************************/ |
0 commit comments