From 664c8c59813715e3f6a6a77776a7d7da240dfb2c Mon Sep 17 00:00:00 2001 From: Jaret Burkett Date: Sat, 26 Sep 2015 09:13:32 -0500 Subject: [PATCH] Added Adafruit Touchscreen Library Added support for STM32 to the Adafruit Resistive Touchscreen library. --- .../Touch-Screen-Library_STM/README.txt | 6 + .../TouchScreen_STM.cpp | 310 ++++++++++++++++++ .../TouchScreen_STM.h | 40 +++ .../touchscreendemo/touchscreendemo.pde | 35 ++ .../touchscreendemoshield.ino | 37 +++ 5 files changed, 428 insertions(+) create mode 100644 STM32F1/libraries/Touch-Screen-Library_STM/README.txt create mode 100644 STM32F1/libraries/Touch-Screen-Library_STM/TouchScreen_STM.cpp create mode 100644 STM32F1/libraries/Touch-Screen-Library_STM/TouchScreen_STM.h create mode 100644 STM32F1/libraries/Touch-Screen-Library_STM/examples/touchscreendemo/touchscreendemo.pde create mode 100644 STM32F1/libraries/Touch-Screen-Library_STM/examples/touchscreendemoshield/touchscreendemoshield.ino diff --git a/STM32F1/libraries/Touch-Screen-Library_STM/README.txt b/STM32F1/libraries/Touch-Screen-Library_STM/README.txt new file mode 100644 index 0000000..9bdedda --- /dev/null +++ b/STM32F1/libraries/Touch-Screen-Library_STM/README.txt @@ -0,0 +1,6 @@ +This is the 4-wire resistive touch screen firmware for Arduino. Works with all Arduinos and the Mega + + +To install, click DOWNLOAD SOURCE in the top right corner, and rename the uncompressed folder "TouchScreen". See our tutorial at http://www.ladyada.net/library/arduino/libraries.html on Arduino Library installation + +Ported to the STM32 by Jaret Burkett https://github.com/jaretburkett \ No newline at end of file diff --git a/STM32F1/libraries/Touch-Screen-Library_STM/TouchScreen_STM.cpp b/STM32F1/libraries/Touch-Screen-Library_STM/TouchScreen_STM.cpp new file mode 100644 index 0000000..0cddc44 --- /dev/null +++ b/STM32F1/libraries/Touch-Screen-Library_STM/TouchScreen_STM.cpp @@ -0,0 +1,310 @@ +// Touch screen library with X Y and Z (pressure) readings as well +// as oversampling to avoid 'bouncing' +// (c) ladyada / adafruit +// Code under MIT License + +// Ported to STM32 by Jaret Burkett https://github.com/jaretburkett + +#include "pins_arduino.h" +#include "wiring_private.h" +#ifdef __AVR + #include +#elif defined(ESP8266) + #include +#elif defined (__STM32F1__) + #include + #include +#endif +#include "TouchScreen_STM.h" + +// increase or decrease the touchscreen oversampling. This is a little different than you make think: +// 1 is no oversampling, whatever data we get is immediately returned +// 2 is double-sampling and we only return valid data if both points are the same +// 3+ uses insert sort to get the median value. +// We found 2 is precise yet not too slow so we suggest sticking with it! + +#define NUMSAMPLES 2 + +TSPoint::TSPoint(void) { + x = y = 0; +} + +TSPoint::TSPoint(int16_t x0, int16_t y0, int16_t z0) { + x = x0; + y = y0; + z = z0; +} + +bool TSPoint::operator==(TSPoint p1) { + return ((p1.x == x) && (p1.y == y) && (p1.z == z)); +} + +bool TSPoint::operator!=(TSPoint p1) { + return ((p1.x != x) || (p1.y != y) || (p1.z != z)); +} + +#if (NUMSAMPLES > 2) +static void insert_sort(int array[], uint8_t size) { + uint8_t j; + int save; + + for (int i = 1; i < size; i++) { + save = array[i]; + for (j = i; j >= 1 && save < array[j - 1]; j--) + array[j] = array[j - 1]; + array[j] = save; + } +} +#endif + +TSPoint TouchScreen::getPoint(void) { + int x, y, z; + int samples[NUMSAMPLES]; + uint8_t i, valid; + + #if !defined (__STM32F1__) + uint8_t xp_port = digitalPinToPort(_xp); + uint8_t yp_port = digitalPinToPort(_yp); + uint8_t xm_port = digitalPinToPort(_xm); + uint8_t ym_port = digitalPinToPort(_ym); + + uint8_t xp_pin = digitalPinToBitMask(_xp); + uint8_t yp_pin = digitalPinToBitMask(_yp); + uint8_t xm_pin = digitalPinToBitMask(_xm); + uint8_t ym_pin = digitalPinToBitMask(_ym); + #endif + + valid = 1; + + pinMode(_yp, INPUT); + pinMode(_ym, INPUT); + + #if !defined (__STM32F1__) + *portOutputRegister(yp_port) &= ~yp_pin; + *portOutputRegister(ym_port) &= ~ym_pin; + + pinMode(_xp, OUTPUT); + pinMode(_xm, OUTPUT); + + *portOutputRegister(xp_port) |= xp_pin; + *portOutputRegister(xm_port) &= ~xm_pin; + #else + digitalWrite(_yp, LOW); + digitalWrite(_ym, LOW); + + pinMode(_xp, OUTPUT); + pinMode(_xm, OUTPUT); + + digitalWrite(_xp, HIGH); + digitalWrite(_xm, LOW); + #endif + + for (i=0; i 2 + insert_sort(samples, NUMSAMPLES); +#endif +#if NUMSAMPLES == 2 + if (samples[0] != samples[1]) { valid = 0; } +#endif + +#if !defined (__STM32F1__) + x = (1023-samples[NUMSAMPLES/2]); +#else + x = (4095-samples[NUMSAMPLES/2]); +#endif + + pinMode(_xp, INPUT); + pinMode(_xm, INPUT); + #if !defined (__STM32F1__) + *portOutputRegister(xp_port) &= ~xp_pin; + #else + digitalWrite(_xp, LOW); + #endif + + pinMode(_yp, OUTPUT); + + #if !defined (__STM32F1__) + *portOutputRegister(yp_port) |= yp_pin; + #else + digitalWrite(_yp, HIGH); + #endif + + pinMode(_ym, OUTPUT); + + for (i=0; i 2 + insert_sort(samples, NUMSAMPLES); +#endif +#if NUMSAMPLES == 2 + if (samples[0] != samples[1]) { valid = 0; } +#endif + +#if !defined (__STM32F1__) + y = (1023-samples[NUMSAMPLES/2]); +#else + y = (4095-samples[NUMSAMPLES/2]); +#endif + + pinMode(_xp, OUTPUT); + #if !defined (__STM32F1__) + // Set X+ to ground + *portOutputRegister(xp_port) &= ~xp_pin; + + // Set Y- to VCC + *portOutputRegister(ym_port) |= ym_pin; + + // Hi-Z X- and Y+ + *portOutputRegister(yp_port) &= ~yp_pin; + #else + // Set X+ to ground + digitalWrite(_xp, LOW); + + // Set Y- to VCC + digitalWrite(_ym, HIGH); + + // Hi-Z X- and Y+ + digitalWrite(_yp, LOW); + #endif + + //digitalWrite(_yp, LOW); + pinMode(_yp, INPUT); + + int z1 = analogRead(_xm); + int z2 = analogRead(_yp); + + if (_rxplate != 0) { + // now read the x + float rtouch; + rtouch = z2; + rtouch /= z1; + rtouch -= 1; + rtouch *= x; + rtouch *= _rxplate; +#if !defined (__STM32F1__) + rtouch /= 1024; +#else + rtouch /= 4095; +#endif + + z = rtouch; + } else { +#if !defined (__STM32F1__) + z = (1023-(z2-z1)); +#else + z = (4095-(z2-z1)); +#endif + } + + if (! valid) { + z = 0; + } + + return TSPoint(x, y, z); +} + +TouchScreen::TouchScreen(uint8_t xp, uint8_t yp, uint8_t xm, uint8_t ym) { + _yp = yp; + _xm = xm; + _ym = ym; + _xp = xp; + _rxplate = 0; + pressureThreshhold = 10; +} + + +TouchScreen::TouchScreen(uint8_t xp, uint8_t yp, uint8_t xm, uint8_t ym, + uint16_t rxplate) { + _yp = yp; + _xm = xm; + _ym = ym; + _xp = xp; + _rxplate = rxplate; + + pressureThreshhold = 10; +} + +int TouchScreen::readTouchX(void) { + pinMode(_yp, INPUT); + pinMode(_ym, INPUT); + digitalWrite(_yp, LOW); + digitalWrite(_ym, LOW); + + pinMode(_xp, OUTPUT); + digitalWrite(_xp, HIGH); + pinMode(_xm, OUTPUT); + digitalWrite(_xm, LOW); + +#if !defined (__STM32F1__) + return (1023-analogRead(_yp)); +#else + return (4095-analogRead(_yp)); +#endif + +} + + +int TouchScreen::readTouchY(void) { + pinMode(_xp, INPUT); + pinMode(_xm, INPUT); + digitalWrite(_xp, LOW); + digitalWrite(_xm, LOW); + + pinMode(_yp, OUTPUT); + digitalWrite(_yp, HIGH); + pinMode(_ym, OUTPUT); + digitalWrite(_ym, LOW); + +#if !defined (__STM32F1__) + return (1023-analogRead(_xm)); +#else + return (4095-analogRead(_xm)); +#endif +} + + +uint16_t TouchScreen::pressure(void) { + // Set X+ to ground + pinMode(_xp, OUTPUT); + digitalWrite(_xp, LOW); + + // Set Y- to VCC + pinMode(_ym, OUTPUT); + digitalWrite(_ym, HIGH); + + // Hi-Z X- and Y+ + digitalWrite(_xm, LOW); + pinMode(_xm, INPUT); + digitalWrite(_yp, LOW); + pinMode(_yp, INPUT); + + int z1 = analogRead(_xm); + int z2 = analogRead(_yp); + + if (_rxplate != 0) { + // now read the x + float rtouch; + rtouch = z2; + rtouch /= z1; + rtouch -= 1; + rtouch *= readTouchX(); + rtouch *= _rxplate; +#if !defined (__STM32F1__) + rtouch /= 1024; +#else + rtouch /= 4095; +#endif + + return rtouch; + } else { +#if !defined (__STM32F1__) + return (1023-(z2-z1)); +#else + return (4095-(z2-z1)); +#endif + } +} diff --git a/STM32F1/libraries/Touch-Screen-Library_STM/TouchScreen_STM.h b/STM32F1/libraries/Touch-Screen-Library_STM/TouchScreen_STM.h new file mode 100644 index 0000000..da7fb90 --- /dev/null +++ b/STM32F1/libraries/Touch-Screen-Library_STM/TouchScreen_STM.h @@ -0,0 +1,40 @@ +// Touch screen library with X Y and Z (pressure) readings as well +// as oversampling to avoid 'bouncing' +// (c) ladyada / adafruit +// Code under MIT License + +// Ported to STM32 by Jaret Burkett https://github.com/jaretburkett + +#ifndef _ADAFRUIT_TOUCHSCREEN_STM_H_ +#define _ADAFRUIT_TOUCHSCREEN_STM_H_ +#include + +class TSPoint { + public: + TSPoint(void); + TSPoint(int16_t x, int16_t y, int16_t z); + + bool operator==(TSPoint); + bool operator!=(TSPoint); + + int16_t x, y, z; +}; + +class TouchScreen { + public: + TouchScreen(uint8_t xp, uint8_t yp, uint8_t xm, uint8_t ym); + TouchScreen(uint8_t xp, uint8_t yp, uint8_t xm, uint8_t ym, uint16_t rx); + + bool isTouching(void); + uint16_t pressure(void); + int readTouchY(); + int readTouchX(); + TSPoint getPoint(); + int16_t pressureThreshhold; + +private: + uint8_t _yp, _ym, _xm, _xp; + uint16_t _rxplate; +}; + +#endif diff --git a/STM32F1/libraries/Touch-Screen-Library_STM/examples/touchscreendemo/touchscreendemo.pde b/STM32F1/libraries/Touch-Screen-Library_STM/examples/touchscreendemo/touchscreendemo.pde new file mode 100644 index 0000000..590691f --- /dev/null +++ b/STM32F1/libraries/Touch-Screen-Library_STM/examples/touchscreendemo/touchscreendemo.pde @@ -0,0 +1,35 @@ +// Touch screen library with X Y and Z (pressure) readings as well +// as oversampling to avoid 'bouncing' +// This demo code returns raw readings, public domain + +#include +#include + +#define YP 8 // must be an analog pin +#define XM 9 // must be an analog pin +#define YM 10 // can be a digital pin +#define XP 11 // can be a digital pin + +// For better pressure precision, we need to know the resistance +// between X+ and X- Use any multimeter to read it +// For the one we're using, its 300 ohms across the X plate +TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300); + +void setup(void) { + Serial.begin(9600); +} + +void loop(void) { + // a point object holds x y and z coordinates + TSPoint p = ts.getPoint(); + + // we have some minimum pressure we consider 'valid' + // pressure of 0 means no pressing! + if (p.z > ts.pressureThreshhold) { + Serial.print("X = "); Serial.print(p.x); + Serial.print("\tY = "); Serial.print(p.y); + Serial.print("\tPressure = "); Serial.println(p.z); + } + + delay(100); +} diff --git a/STM32F1/libraries/Touch-Screen-Library_STM/examples/touchscreendemoshield/touchscreendemoshield.ino b/STM32F1/libraries/Touch-Screen-Library_STM/examples/touchscreendemoshield/touchscreendemoshield.ino new file mode 100644 index 0000000..9abd508 --- /dev/null +++ b/STM32F1/libraries/Touch-Screen-Library_STM/examples/touchscreendemoshield/touchscreendemoshield.ino @@ -0,0 +1,37 @@ +// Touch screen library with X Y and Z (pressure) readings as well +// as oversampling to avoid 'bouncing' +// This demo code returns raw readings, public domain + +#include +#include + +// These are the pins for the shield! +#define YP 8 // must be an analog pin +#define XM 9 // must be an analog pin +#define YM 10 // can be a digital pin +#define XP 11 // can be a digital pin + +#define MINPRESSURE 10 +#define MAXPRESSURE 1000 + +// For better pressure precision, we need to know the resistance +// between X+ and X- Use any multimeter to read it +// For the one we're using, its 300 ohms across the X plate +TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300); + +void setup(void) { + Serial.begin(9600); +} + +void loop(void) { + // a point object holds x y and z coordinates + TSPoint p = ts.getPoint(); + + // we have some minimum pressure we consider 'valid' + // pressure of 0 means no pressing! + if (p.z > MINPRESSURE && p.z < MAXPRESSURE) { + Serial.print("X = "); Serial.print(p.x); + Serial.print("\tY = "); Serial.print(p.y); + Serial.print("\tPressure = "); Serial.println(p.z); + } +}