|
| 1 | +// Touch screen library with X Y and Z (pressure) readings as well |
| 2 | +// as oversampling to avoid 'bouncing' |
| 3 | +// (c) ladyada / adafruit |
| 4 | +// Code under MIT License |
| 5 | + |
| 6 | +// Ported to STM32 by Jaret Burkett https://github.com/jaretburkett |
| 7 | + |
| 8 | +#include "pins_arduino.h" |
| 9 | +#include "wiring_private.h" |
| 10 | +#ifdef __AVR |
| 11 | + #include <avr/pgmspace.h> |
| 12 | +#elif defined(ESP8266) |
| 13 | + #include <pgmspace.h> |
| 14 | +#elif defined (__STM32F1__) |
| 15 | + #include <avr/pgmspace.h> |
| 16 | + #include <WProgram.h> |
| 17 | +#endif |
| 18 | +#include "TouchScreen_STM.h" |
| 19 | + |
| 20 | +// increase or decrease the touchscreen oversampling. This is a little different than you make think: |
| 21 | +// 1 is no oversampling, whatever data we get is immediately returned |
| 22 | +// 2 is double-sampling and we only return valid data if both points are the same |
| 23 | +// 3+ uses insert sort to get the median value. |
| 24 | +// We found 2 is precise yet not too slow so we suggest sticking with it! |
| 25 | + |
| 26 | +#define NUMSAMPLES 2 |
| 27 | + |
| 28 | +TSPoint::TSPoint(void) { |
| 29 | + x = y = 0; |
| 30 | +} |
| 31 | + |
| 32 | +TSPoint::TSPoint(int16_t x0, int16_t y0, int16_t z0) { |
| 33 | + x = x0; |
| 34 | + y = y0; |
| 35 | + z = z0; |
| 36 | +} |
| 37 | + |
| 38 | +bool TSPoint::operator==(TSPoint p1) { |
| 39 | + return ((p1.x == x) && (p1.y == y) && (p1.z == z)); |
| 40 | +} |
| 41 | + |
| 42 | +bool TSPoint::operator!=(TSPoint p1) { |
| 43 | + return ((p1.x != x) || (p1.y != y) || (p1.z != z)); |
| 44 | +} |
| 45 | + |
| 46 | +#if (NUMSAMPLES > 2) |
| 47 | +static void insert_sort(int array[], uint8_t size) { |
| 48 | + uint8_t j; |
| 49 | + int save; |
| 50 | + |
| 51 | + for (int i = 1; i < size; i++) { |
| 52 | + save = array[i]; |
| 53 | + for (j = i; j >= 1 && save < array[j - 1]; j--) |
| 54 | + array[j] = array[j - 1]; |
| 55 | + array[j] = save; |
| 56 | + } |
| 57 | +} |
| 58 | +#endif |
| 59 | + |
| 60 | +TSPoint TouchScreen::getPoint(void) { |
| 61 | + int x, y, z; |
| 62 | + int samples[NUMSAMPLES]; |
| 63 | + uint8_t i, valid; |
| 64 | + |
| 65 | + #if !defined (__STM32F1__) |
| 66 | + uint8_t xp_port = digitalPinToPort(_xp); |
| 67 | + uint8_t yp_port = digitalPinToPort(_yp); |
| 68 | + uint8_t xm_port = digitalPinToPort(_xm); |
| 69 | + uint8_t ym_port = digitalPinToPort(_ym); |
| 70 | + |
| 71 | + uint8_t xp_pin = digitalPinToBitMask(_xp); |
| 72 | + uint8_t yp_pin = digitalPinToBitMask(_yp); |
| 73 | + uint8_t xm_pin = digitalPinToBitMask(_xm); |
| 74 | + uint8_t ym_pin = digitalPinToBitMask(_ym); |
| 75 | + #endif |
| 76 | + |
| 77 | + valid = 1; |
| 78 | + |
| 79 | + pinMode(_yp, INPUT); |
| 80 | + pinMode(_ym, INPUT); |
| 81 | + |
| 82 | + #if !defined (__STM32F1__) |
| 83 | + *portOutputRegister(yp_port) &= ~yp_pin; |
| 84 | + *portOutputRegister(ym_port) &= ~ym_pin; |
| 85 | + |
| 86 | + pinMode(_xp, OUTPUT); |
| 87 | + pinMode(_xm, OUTPUT); |
| 88 | + |
| 89 | + *portOutputRegister(xp_port) |= xp_pin; |
| 90 | + *portOutputRegister(xm_port) &= ~xm_pin; |
| 91 | + #else |
| 92 | + digitalWrite(_yp, LOW); |
| 93 | + digitalWrite(_ym, LOW); |
| 94 | + |
| 95 | + pinMode(_xp, OUTPUT); |
| 96 | + pinMode(_xm, OUTPUT); |
| 97 | + |
| 98 | + digitalWrite(_xp, HIGH); |
| 99 | + digitalWrite(_xm, LOW); |
| 100 | + #endif |
| 101 | + |
| 102 | + for (i=0; i<NUMSAMPLES; i++) { |
| 103 | + samples[i] = analogRead(_yp); |
| 104 | + } |
| 105 | +#if NUMSAMPLES > 2 |
| 106 | + insert_sort(samples, NUMSAMPLES); |
| 107 | +#endif |
| 108 | +#if NUMSAMPLES == 2 |
| 109 | + if (samples[0] != samples[1]) { valid = 0; } |
| 110 | +#endif |
| 111 | + |
| 112 | +#if !defined (__STM32F1__) |
| 113 | + x = (1023-samples[NUMSAMPLES/2]); |
| 114 | +#else |
| 115 | + x = (4095-samples[NUMSAMPLES/2]); |
| 116 | +#endif |
| 117 | + |
| 118 | + pinMode(_xp, INPUT); |
| 119 | + pinMode(_xm, INPUT); |
| 120 | + #if !defined (__STM32F1__) |
| 121 | + *portOutputRegister(xp_port) &= ~xp_pin; |
| 122 | + #else |
| 123 | + digitalWrite(_xp, LOW); |
| 124 | + #endif |
| 125 | + |
| 126 | + pinMode(_yp, OUTPUT); |
| 127 | + |
| 128 | + #if !defined (__STM32F1__) |
| 129 | + *portOutputRegister(yp_port) |= yp_pin; |
| 130 | + #else |
| 131 | + digitalWrite(_yp, HIGH); |
| 132 | + #endif |
| 133 | + |
| 134 | + pinMode(_ym, OUTPUT); |
| 135 | + |
| 136 | + for (i=0; i<NUMSAMPLES; i++) { |
| 137 | + samples[i] = analogRead(_xm); |
| 138 | + } |
| 139 | + |
| 140 | +#if NUMSAMPLES > 2 |
| 141 | + insert_sort(samples, NUMSAMPLES); |
| 142 | +#endif |
| 143 | +#if NUMSAMPLES == 2 |
| 144 | + if (samples[0] != samples[1]) { valid = 0; } |
| 145 | +#endif |
| 146 | + |
| 147 | +#if !defined (__STM32F1__) |
| 148 | + y = (1023-samples[NUMSAMPLES/2]); |
| 149 | +#else |
| 150 | + y = (4095-samples[NUMSAMPLES/2]); |
| 151 | +#endif |
| 152 | + |
| 153 | + pinMode(_xp, OUTPUT); |
| 154 | + #if !defined (__STM32F1__) |
| 155 | + // Set X+ to ground |
| 156 | + *portOutputRegister(xp_port) &= ~xp_pin; |
| 157 | + |
| 158 | + // Set Y- to VCC |
| 159 | + *portOutputRegister(ym_port) |= ym_pin; |
| 160 | + |
| 161 | + // Hi-Z X- and Y+ |
| 162 | + *portOutputRegister(yp_port) &= ~yp_pin; |
| 163 | + #else |
| 164 | + // Set X+ to ground |
| 165 | + digitalWrite(_xp, LOW); |
| 166 | + |
| 167 | + // Set Y- to VCC |
| 168 | + digitalWrite(_ym, HIGH); |
| 169 | + |
| 170 | + // Hi-Z X- and Y+ |
| 171 | + digitalWrite(_yp, LOW); |
| 172 | + #endif |
| 173 | + |
| 174 | + //digitalWrite(_yp, LOW); |
| 175 | + pinMode(_yp, INPUT); |
| 176 | + |
| 177 | + int z1 = analogRead(_xm); |
| 178 | + int z2 = analogRead(_yp); |
| 179 | + |
| 180 | + if (_rxplate != 0) { |
| 181 | + // now read the x |
| 182 | + float rtouch; |
| 183 | + rtouch = z2; |
| 184 | + rtouch /= z1; |
| 185 | + rtouch -= 1; |
| 186 | + rtouch *= x; |
| 187 | + rtouch *= _rxplate; |
| 188 | +#if !defined (__STM32F1__) |
| 189 | + rtouch /= 1024; |
| 190 | +#else |
| 191 | + rtouch /= 4095; |
| 192 | +#endif |
| 193 | + |
| 194 | + z = rtouch; |
| 195 | + } else { |
| 196 | +#if !defined (__STM32F1__) |
| 197 | + z = (1023-(z2-z1)); |
| 198 | +#else |
| 199 | + z = (4095-(z2-z1)); |
| 200 | +#endif |
| 201 | + } |
| 202 | + |
| 203 | + if (! valid) { |
| 204 | + z = 0; |
| 205 | + } |
| 206 | + |
| 207 | + return TSPoint(x, y, z); |
| 208 | +} |
| 209 | + |
| 210 | +TouchScreen::TouchScreen(uint8_t xp, uint8_t yp, uint8_t xm, uint8_t ym) { |
| 211 | + _yp = yp; |
| 212 | + _xm = xm; |
| 213 | + _ym = ym; |
| 214 | + _xp = xp; |
| 215 | + _rxplate = 0; |
| 216 | + pressureThreshhold = 10; |
| 217 | +} |
| 218 | + |
| 219 | + |
| 220 | +TouchScreen::TouchScreen(uint8_t xp, uint8_t yp, uint8_t xm, uint8_t ym, |
| 221 | + uint16_t rxplate) { |
| 222 | + _yp = yp; |
| 223 | + _xm = xm; |
| 224 | + _ym = ym; |
| 225 | + _xp = xp; |
| 226 | + _rxplate = rxplate; |
| 227 | + |
| 228 | + pressureThreshhold = 10; |
| 229 | +} |
| 230 | + |
| 231 | +int TouchScreen::readTouchX(void) { |
| 232 | + pinMode(_yp, INPUT); |
| 233 | + pinMode(_ym, INPUT); |
| 234 | + digitalWrite(_yp, LOW); |
| 235 | + digitalWrite(_ym, LOW); |
| 236 | + |
| 237 | + pinMode(_xp, OUTPUT); |
| 238 | + digitalWrite(_xp, HIGH); |
| 239 | + pinMode(_xm, OUTPUT); |
| 240 | + digitalWrite(_xm, LOW); |
| 241 | + |
| 242 | +#if !defined (__STM32F1__) |
| 243 | + return (1023-analogRead(_yp)); |
| 244 | +#else |
| 245 | + return (4095-analogRead(_yp)); |
| 246 | +#endif |
| 247 | + |
| 248 | +} |
| 249 | + |
| 250 | + |
| 251 | +int TouchScreen::readTouchY(void) { |
| 252 | + pinMode(_xp, INPUT); |
| 253 | + pinMode(_xm, INPUT); |
| 254 | + digitalWrite(_xp, LOW); |
| 255 | + digitalWrite(_xm, LOW); |
| 256 | + |
| 257 | + pinMode(_yp, OUTPUT); |
| 258 | + digitalWrite(_yp, HIGH); |
| 259 | + pinMode(_ym, OUTPUT); |
| 260 | + digitalWrite(_ym, LOW); |
| 261 | + |
| 262 | +#if !defined (__STM32F1__) |
| 263 | + return (1023-analogRead(_xm)); |
| 264 | +#else |
| 265 | + return (4095-analogRead(_xm)); |
| 266 | +#endif |
| 267 | +} |
| 268 | + |
| 269 | + |
| 270 | +uint16_t TouchScreen::pressure(void) { |
| 271 | + // Set X+ to ground |
| 272 | + pinMode(_xp, OUTPUT); |
| 273 | + digitalWrite(_xp, LOW); |
| 274 | + |
| 275 | + // Set Y- to VCC |
| 276 | + pinMode(_ym, OUTPUT); |
| 277 | + digitalWrite(_ym, HIGH); |
| 278 | + |
| 279 | + // Hi-Z X- and Y+ |
| 280 | + digitalWrite(_xm, LOW); |
| 281 | + pinMode(_xm, INPUT); |
| 282 | + digitalWrite(_yp, LOW); |
| 283 | + pinMode(_yp, INPUT); |
| 284 | + |
| 285 | + int z1 = analogRead(_xm); |
| 286 | + int z2 = analogRead(_yp); |
| 287 | + |
| 288 | + if (_rxplate != 0) { |
| 289 | + // now read the x |
| 290 | + float rtouch; |
| 291 | + rtouch = z2; |
| 292 | + rtouch /= z1; |
| 293 | + rtouch -= 1; |
| 294 | + rtouch *= readTouchX(); |
| 295 | + rtouch *= _rxplate; |
| 296 | +#if !defined (__STM32F1__) |
| 297 | + rtouch /= 1024; |
| 298 | +#else |
| 299 | + rtouch /= 4095; |
| 300 | +#endif |
| 301 | + |
| 302 | + return rtouch; |
| 303 | + } else { |
| 304 | +#if !defined (__STM32F1__) |
| 305 | + return (1023-(z2-z1)); |
| 306 | +#else |
| 307 | + return (4095-(z2-z1)); |
| 308 | +#endif |
| 309 | + } |
| 310 | +} |
0 commit comments