Resistive TouchScreen e.g. for MCUFRIEND Display Shields
Dependents: TFT_Touch_botao_v1 TFT_Touch_exemplo5_git_touch TESTE_1 TFT_Touch_exemplo6_git_touch_button_3_ ... more
TouchScreen_kbv_mbed.cpp@2:f23dd0aaa0ec, 2021-05-17 (annotated)
- Committer:
- davidprentice
- Date:
- Mon May 17 08:10:08 2021 +0000
- Revision:
- 2:f23dd0aaa0ec
- Parent:
- 1:849734501e5a
set output() when leaving getPoint(). not for readTouchX() etc
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
davidprentice | 0:84f0f29e4695 | 1 | // Touch screen library with X Y and Z (pressure) readings as well |
davidprentice | 0:84f0f29e4695 | 2 | // as oversampling to avoid 'bouncing' |
davidprentice | 0:84f0f29e4695 | 3 | // adapted from (c) ladyada / adafruit |
davidprentice | 0:84f0f29e4695 | 4 | |
davidprentice | 0:84f0f29e4695 | 5 | #include "Arduino.h" |
davidprentice | 0:84f0f29e4695 | 6 | |
davidprentice | 0:84f0f29e4695 | 7 | #include "TouchScreen_kbv_mbed.h" |
davidprentice | 0:84f0f29e4695 | 8 | |
davidprentice | 0:84f0f29e4695 | 9 | #define NUMSAMPLES 3 //.kbv |
davidprentice | 1:849734501e5a | 10 | #if defined(__STM32F1__) || defined(ESP32) //Maple core |
davidprentice | 0:84f0f29e4695 | 11 | #define ADC_ADJUST >>2 |
davidprentice | 0:84f0f29e4695 | 12 | #else |
davidprentice | 0:84f0f29e4695 | 13 | #define ADC_ADJUST |
davidprentice | 0:84f0f29e4695 | 14 | #endif |
davidprentice | 0:84f0f29e4695 | 15 | |
davidprentice | 0:84f0f29e4695 | 16 | TSPoint_kbv::TSPoint_kbv(void) |
davidprentice | 0:84f0f29e4695 | 17 | { |
davidprentice | 0:84f0f29e4695 | 18 | x = y = 0; |
davidprentice | 0:84f0f29e4695 | 19 | } |
davidprentice | 0:84f0f29e4695 | 20 | |
davidprentice | 0:84f0f29e4695 | 21 | TSPoint_kbv::TSPoint_kbv(int16_t x0, int16_t y0, int16_t z0) |
davidprentice | 0:84f0f29e4695 | 22 | { |
davidprentice | 0:84f0f29e4695 | 23 | x = x0; |
davidprentice | 0:84f0f29e4695 | 24 | y = y0; |
davidprentice | 0:84f0f29e4695 | 25 | z = z0; |
davidprentice | 0:84f0f29e4695 | 26 | } |
davidprentice | 0:84f0f29e4695 | 27 | |
davidprentice | 0:84f0f29e4695 | 28 | bool TSPoint_kbv::operator==(TSPoint_kbv p1) |
davidprentice | 0:84f0f29e4695 | 29 | { |
davidprentice | 0:84f0f29e4695 | 30 | return ((p1.x == x) && (p1.y == y) && (p1.z == z)); |
davidprentice | 0:84f0f29e4695 | 31 | } |
davidprentice | 0:84f0f29e4695 | 32 | |
davidprentice | 0:84f0f29e4695 | 33 | bool TSPoint_kbv::operator!=(TSPoint_kbv p1) |
davidprentice | 0:84f0f29e4695 | 34 | { |
davidprentice | 0:84f0f29e4695 | 35 | return ((p1.x != x) || (p1.y != y) || (p1.z != z)); |
davidprentice | 0:84f0f29e4695 | 36 | } |
davidprentice | 0:84f0f29e4695 | 37 | |
davidprentice | 0:84f0f29e4695 | 38 | static void insert_sort(int array[], uint8_t size) |
davidprentice | 0:84f0f29e4695 | 39 | { |
davidprentice | 0:84f0f29e4695 | 40 | uint8_t j; |
davidprentice | 0:84f0f29e4695 | 41 | int save; |
davidprentice | 0:84f0f29e4695 | 42 | |
davidprentice | 0:84f0f29e4695 | 43 | for (int i = 1; i < size; i++) { |
davidprentice | 0:84f0f29e4695 | 44 | save = array[i]; |
davidprentice | 0:84f0f29e4695 | 45 | for (j = i; j >= 1 && save < array[j - 1]; j--) |
davidprentice | 0:84f0f29e4695 | 46 | array[j] = array[j - 1]; |
davidprentice | 0:84f0f29e4695 | 47 | array[j] = save; |
davidprentice | 0:84f0f29e4695 | 48 | } |
davidprentice | 0:84f0f29e4695 | 49 | } |
davidprentice | 0:84f0f29e4695 | 50 | |
davidprentice | 1:849734501e5a | 51 | // ***** conditional for MBED ***** |
davidprentice | 1:849734501e5a | 52 | #if defined(__MBED__) |
davidprentice | 1:849734501e5a | 53 | TouchScreen_kbv::TouchScreen_kbv(PinName xp, PinName yp, PinName xm, PinName ym, uint16_t rxplate) |
davidprentice | 1:849734501e5a | 54 | { |
davidprentice | 1:849734501e5a | 55 | _yp = yp; |
davidprentice | 1:849734501e5a | 56 | _xm = xm; |
davidprentice | 1:849734501e5a | 57 | _ym = ym; |
davidprentice | 1:849734501e5a | 58 | _xp = xp; |
davidprentice | 1:849734501e5a | 59 | _rxplate = rxplate; |
davidprentice | 1:849734501e5a | 60 | |
davidprentice | 1:849734501e5a | 61 | pressureThreshhold = 10; |
davidprentice | 1:849734501e5a | 62 | } |
davidprentice | 1:849734501e5a | 63 | |
davidprentice | 0:84f0f29e4695 | 64 | uint16_t TouchScreen_kbv::analogRead(PinName p) |
davidprentice | 0:84f0f29e4695 | 65 | { |
davidprentice | 0:84f0f29e4695 | 66 | AnalogIn _adc(p); |
davidprentice | 1:849734501e5a | 67 | return _adc.read_u16() >> 6; // MBED gives 16-bit |
davidprentice | 0:84f0f29e4695 | 68 | } |
davidprentice | 0:84f0f29e4695 | 69 | |
davidprentice | 0:84f0f29e4695 | 70 | void TouchScreen_kbv::pinModeVal(PinName p, uint8_t mode, uint8_t val) |
davidprentice | 0:84f0f29e4695 | 71 | { |
davidprentice | 0:84f0f29e4695 | 72 | DigitalInOut _out(p); |
davidprentice | 0:84f0f29e4695 | 73 | if (mode) { |
davidprentice | 0:84f0f29e4695 | 74 | _out.output(); |
davidprentice | 0:84f0f29e4695 | 75 | _out.write(val); |
davidprentice | 0:84f0f29e4695 | 76 | } else { |
davidprentice | 0:84f0f29e4695 | 77 | _out.input(); |
davidprentice | 0:84f0f29e4695 | 78 | _out.mode(PullNone); |
davidprentice | 0:84f0f29e4695 | 79 | } |
davidprentice | 0:84f0f29e4695 | 80 | } |
davidprentice | 1:849734501e5a | 81 | #else |
davidprentice | 1:849734501e5a | 82 | TouchScreen_kbv::TouchScreen_kbv(uint8_t xp, uint8_t yp, uint8_t xm, uint8_t ym, uint16_t rxplate) |
davidprentice | 1:849734501e5a | 83 | { |
davidprentice | 1:849734501e5a | 84 | _yp = yp; |
davidprentice | 1:849734501e5a | 85 | _xm = xm; |
davidprentice | 1:849734501e5a | 86 | _ym = ym; |
davidprentice | 1:849734501e5a | 87 | _xp = xp; |
davidprentice | 1:849734501e5a | 88 | _rxplate = rxplate; |
davidprentice | 1:849734501e5a | 89 | |
davidprentice | 1:849734501e5a | 90 | pressureThreshhold = 10; |
davidprentice | 1:849734501e5a | 91 | } |
davidprentice | 1:849734501e5a | 92 | |
davidprentice | 1:849734501e5a | 93 | void TouchScreen_kbv::pinModeVal(uint8_t p, uint8_t mode, uint8_t val) |
davidprentice | 1:849734501e5a | 94 | { |
davidprentice | 1:849734501e5a | 95 | pinMode(p, mode); |
davidprentice | 1:849734501e5a | 96 | digitalWrite(p, val); |
davidprentice | 1:849734501e5a | 97 | } |
davidprentice | 1:849734501e5a | 98 | #endif |
davidprentice | 1:849734501e5a | 99 | // ******** end of conditional **** |
davidprentice | 0:84f0f29e4695 | 100 | |
davidprentice | 0:84f0f29e4695 | 101 | TSPoint_kbv TouchScreen_kbv::getPoint(void) |
davidprentice | 0:84f0f29e4695 | 102 | { |
davidprentice | 0:84f0f29e4695 | 103 | int x, y, z; |
davidprentice | 0:84f0f29e4695 | 104 | int samples[NUMSAMPLES]; |
davidprentice | 0:84f0f29e4695 | 105 | uint8_t i, valid = 1; |
davidprentice | 1:849734501e5a | 106 | |
davidprentice | 0:84f0f29e4695 | 107 | pinModeVal(_yp, 0, 0); |
davidprentice | 0:84f0f29e4695 | 108 | pinModeVal(_ym, 0, 0); |
davidprentice | 0:84f0f29e4695 | 109 | pinModeVal(_xp, 1, 1); |
davidprentice | 0:84f0f29e4695 | 110 | pinModeVal(_xm, 1, 0); |
davidprentice | 0:84f0f29e4695 | 111 | |
davidprentice | 0:84f0f29e4695 | 112 | for (i = 0; i < NUMSAMPLES; i++) { |
davidprentice | 0:84f0f29e4695 | 113 | samples[i] = analogRead(_yp) ADC_ADJUST; |
davidprentice | 0:84f0f29e4695 | 114 | } |
davidprentice | 0:84f0f29e4695 | 115 | insert_sort(samples, NUMSAMPLES); |
davidprentice | 0:84f0f29e4695 | 116 | x = (1023 - samples[NUMSAMPLES / 2]); //choose median |
davidprentice | 0:84f0f29e4695 | 117 | |
davidprentice | 0:84f0f29e4695 | 118 | pinModeVal(_xp, 0, 0); |
davidprentice | 0:84f0f29e4695 | 119 | pinModeVal(_xm, 0, 0); |
davidprentice | 0:84f0f29e4695 | 120 | pinModeVal(_yp, 1, 1); |
davidprentice | 0:84f0f29e4695 | 121 | pinModeVal(_ym, 1, 0); |
davidprentice | 0:84f0f29e4695 | 122 | |
davidprentice | 0:84f0f29e4695 | 123 | for (i = 0; i < NUMSAMPLES; i++) { |
davidprentice | 0:84f0f29e4695 | 124 | samples[i] = analogRead(_xm) ADC_ADJUST; |
davidprentice | 0:84f0f29e4695 | 125 | } |
davidprentice | 0:84f0f29e4695 | 126 | |
davidprentice | 0:84f0f29e4695 | 127 | insert_sort(samples, NUMSAMPLES); |
davidprentice | 0:84f0f29e4695 | 128 | |
davidprentice | 0:84f0f29e4695 | 129 | y = (1023 - samples[NUMSAMPLES / 2]); |
davidprentice | 0:84f0f29e4695 | 130 | |
davidprentice | 0:84f0f29e4695 | 131 | pinModeVal(_xp, 1, 0); // Set X+ to ground |
davidprentice | 0:84f0f29e4695 | 132 | pinModeVal(_xm, 0, 0); // Hi-Z |
davidprentice | 0:84f0f29e4695 | 133 | pinModeVal(_yp, 0, 0); // Hi-Z |
davidprentice | 0:84f0f29e4695 | 134 | pinModeVal(_ym, 1, 1); // Set Y- to VCC |
davidprentice | 0:84f0f29e4695 | 135 | |
davidprentice | 0:84f0f29e4695 | 136 | int z1 = analogRead(_xm) ADC_ADJUST; |
davidprentice | 0:84f0f29e4695 | 137 | int z2 = analogRead(_yp) ADC_ADJUST; |
davidprentice | 0:84f0f29e4695 | 138 | |
davidprentice | 0:84f0f29e4695 | 139 | z = (1023 - (z2 - z1)); |
davidprentice | 0:84f0f29e4695 | 140 | |
davidprentice | 1:849734501e5a | 141 | // *** these two lines would ensure XM, YP are in Digital mode *** |
davidprentice | 2:f23dd0aaa0ec | 142 | pinModeVal(_xm, 1, 1); // OUTPUT HIGH |
davidprentice | 2:f23dd0aaa0ec | 143 | pinModeVal(_yp, 1, 1); // OUTPUT HIGH |
davidprentice | 0:84f0f29e4695 | 144 | |
davidprentice | 1:849734501e5a | 145 | return TSPoint_kbv(x, y, z); //XM, YP still in ANALOG mode |
davidprentice | 0:84f0f29e4695 | 146 | } |
davidprentice | 0:84f0f29e4695 | 147 | |
davidprentice | 0:84f0f29e4695 | 148 | int TouchScreen_kbv::readTouchX(void) |
davidprentice | 0:84f0f29e4695 | 149 | { |
davidprentice | 1:849734501e5a | 150 | pinModeVal(_yp, 0, 0); |
davidprentice | 1:849734501e5a | 151 | pinModeVal(_ym, 0, 0); |
davidprentice | 1:849734501e5a | 152 | pinModeVal(_xp, 1, 1); |
davidprentice | 1:849734501e5a | 153 | pinModeVal(_xm, 1, 0); |
davidprentice | 0:84f0f29e4695 | 154 | |
davidprentice | 0:84f0f29e4695 | 155 | return (1023 - (analogRead(_yp)) ADC_ADJUST); |
davidprentice | 0:84f0f29e4695 | 156 | } |
davidprentice | 0:84f0f29e4695 | 157 | |
davidprentice | 0:84f0f29e4695 | 158 | int TouchScreen_kbv::readTouchY(void) |
davidprentice | 0:84f0f29e4695 | 159 | { |
davidprentice | 1:849734501e5a | 160 | pinModeVal(_xp, 0, 0); |
davidprentice | 1:849734501e5a | 161 | pinModeVal(_xm, 0, 0); |
davidprentice | 1:849734501e5a | 162 | pinModeVal(_yp, 1, 1); |
davidprentice | 1:849734501e5a | 163 | pinModeVal(_ym, 1, 0); |
davidprentice | 0:84f0f29e4695 | 164 | |
davidprentice | 0:84f0f29e4695 | 165 | return (1023 - (analogRead(_xm)) ADC_ADJUST); |
davidprentice | 0:84f0f29e4695 | 166 | } |
davidprentice | 0:84f0f29e4695 | 167 | |
davidprentice | 0:84f0f29e4695 | 168 | uint16_t TouchScreen_kbv::pressure(void) |
davidprentice | 0:84f0f29e4695 | 169 | { |
davidprentice | 1:849734501e5a | 170 | pinModeVal(_xp, 1, 0); // Set X+ to ground |
davidprentice | 1:849734501e5a | 171 | pinModeVal(_xm, 0, 0); // Hi-Z |
davidprentice | 1:849734501e5a | 172 | pinModeVal(_yp, 0, 0); // Hi-Z |
davidprentice | 1:849734501e5a | 173 | pinModeVal(_ym, 1, 1); // Set Y- to VCC |
davidprentice | 0:84f0f29e4695 | 174 | |
davidprentice | 0:84f0f29e4695 | 175 | int z1 = analogRead(_xm) ADC_ADJUST; |
davidprentice | 0:84f0f29e4695 | 176 | int z2 = analogRead(_yp) ADC_ADJUST; |
davidprentice | 0:84f0f29e4695 | 177 | |
davidprentice | 0:84f0f29e4695 | 178 | return (1023 - (z2 - z1)); |
davidprentice | 0:84f0f29e4695 | 179 | } |