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

Files at this revision

API Documentation at this revision

Comitter:
davidprentice
Date:
Mon Apr 26 15:15:38 2021 +0000
Child:
1:849734501e5a
Commit message:
initial commit

Changed in this revision

TouchScreen_kbv_mbed.cpp Show annotated file Show diff for this revision Revisions of this file
TouchScreen_kbv_mbed.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TouchScreen_kbv_mbed.cpp	Mon Apr 26 15:15:38 2021 +0000
@@ -0,0 +1,191 @@
+// Touch screen library with X Y and Z (pressure) readings as well
+// as oversampling to avoid 'bouncing'
+// adapted from (c) ladyada / adafruit
+
+#include "Arduino.h"
+
+#include "TouchScreen_kbv_mbed.h"
+
+#define NUMSAMPLES 3  //.kbv
+#if defined(__STM32F1__) || defined(ESP32) || defined(___arm__)  //Maple core
+#define ADC_ADJUST >>2
+#else
+#define ADC_ADJUST
+#endif
+
+TSPoint_kbv::TSPoint_kbv(void)
+{
+    x = y = 0;
+}
+
+TSPoint_kbv::TSPoint_kbv(int16_t x0, int16_t y0, int16_t z0)
+{
+    x = x0;
+    y = y0;
+    z = z0;
+}
+
+bool TSPoint_kbv::operator==(TSPoint_kbv p1)
+{
+    return  ((p1.x == x) && (p1.y == y) && (p1.z == z));
+}
+
+bool TSPoint_kbv::operator!=(TSPoint_kbv p1)
+{
+    return  ((p1.x != x) || (p1.y != y) || (p1.z != z));
+}
+
+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;
+    }
+}
+
+uint16_t TouchScreen_kbv::analogRead(PinName p)
+{
+    AnalogIn _adc(p);
+    return _adc.read_u16() >> 6;
+}
+
+void TouchScreen_kbv::pinModeVal(PinName p, uint8_t mode, uint8_t val)
+{
+    DigitalInOut _out(p);
+    if (mode) {
+        _out.output();
+        _out.write(val);
+    } else {
+        _out.input();
+        _out.mode(PullNone);
+    }
+}
+
+TSPoint_kbv TouchScreen_kbv::getPoint(void)
+{
+    int x, y, z;
+    int samples[NUMSAMPLES];
+    uint8_t i, valid = 1;
+    DigitalInOut _xpout(_xp), _xmout(_xm), _ypout(_yp), _ymout(_ym);
+#if 1
+    pinModeVal(_yp, 0, 0);
+    pinModeVal(_ym, 0, 0);
+    pinModeVal(_xp, 1, 1);
+    pinModeVal(_xm, 1, 0);
+#else
+    _ypout.input();
+    _ymout.input();
+    _ypout.mode(PullNone);
+    _ymout.mode(PullNone);
+
+    _xpout.output();
+    _xmout.output();
+    _xpout.write(1);
+    _xmout.write(0);
+#endif
+    for (i = 0; i < NUMSAMPLES; i++) {
+        samples[i] = analogRead(_yp) ADC_ADJUST;
+    }
+    insert_sort(samples, NUMSAMPLES);
+    x = (1023 - samples[NUMSAMPLES / 2]); //choose median
+
+    pinModeVal(_xp, 0, 0);
+    pinModeVal(_xm, 0, 0);
+    pinModeVal(_yp, 1, 1);
+    pinModeVal(_ym, 1, 0);
+
+    for (i = 0; i < NUMSAMPLES; i++) {
+        samples[i] = analogRead(_xm) ADC_ADJUST;
+    }
+
+    insert_sort(samples, NUMSAMPLES);
+
+    y = (1023 - samples[NUMSAMPLES / 2]);
+
+    pinModeVal(_xp, 1, 0); // Set X+ to ground
+    pinModeVal(_xm, 0, 0); // Hi-Z
+    pinModeVal(_yp, 0, 0); // Hi-Z
+    pinModeVal(_ym, 1, 1); // Set Y- to VCC
+
+    int z1 = analogRead(_xm) ADC_ADJUST;
+    int z2 = analogRead(_yp) ADC_ADJUST;
+
+    z = (1023 - (z2 - z1));
+
+    return TSPoint_kbv(x, y, z);  //XM, YP still in ANALOG mode
+}
+
+
+TouchScreen_kbv::TouchScreen_kbv(PinName xp, PinName yp, PinName xm, PinName ym, uint16_t rxplate)
+{
+    _yp = yp;
+    _xm = xm;
+    _ym = ym;
+    _xp = xp;
+    _rxplate = rxplate;
+
+    pressureThreshhold = 10;
+}
+
+int TouchScreen_kbv::readTouchX(void)
+{
+    DigitalInOut _xpout(_xp), _xmout(_xm), _ypout(_yp), _ymout(_ym);
+
+    _ypout.input();
+    _ymout.input();
+    _ypout.mode(PullNone);
+    _ymout.mode(PullNone);
+
+    _xpout.output();
+    _xmout.output();
+    _xpout.write(1);
+    _xmout.write(0);
+
+    return (1023 - (analogRead(_yp)) ADC_ADJUST);
+}
+
+
+int TouchScreen_kbv::readTouchY(void)
+{
+    DigitalInOut _xpout(_xp), _xmout(_xm), _ypout(_yp), _ymout(_ym);
+    _xpout.input();
+    _xmout.input();
+    _xpout.mode(PullNone);
+    _xmout.mode(PullNone);
+    _ypout.output();
+    _ymout.output();
+    _ypout.write(1);
+    _ymout.write(0);
+
+    return (1023 - (analogRead(_xm)) ADC_ADJUST);
+}
+
+
+uint16_t TouchScreen_kbv::pressure(void)
+{
+    DigitalInOut _xpout(_xp), _xmout(_xm), _ypout(_yp), _ymout(_ym);
+    // Set X+ to ground
+    _xpout.output();
+    _xpout.write(0);
+
+    // Set Y- to VCC
+    _ymout.output();         //.kbv
+    _ymout.write(1);
+    // Hi-Z X- and Y+
+    _xmout.write(0);   //.kbv
+    _xmout.input();      //.kbv
+    _ypout.write(0);
+    _ypout.input();
+
+    int z1 = analogRead(_xm) ADC_ADJUST;
+    int z2 = analogRead(_yp) ADC_ADJUST;
+
+    return (1023 - (z2 - z1));
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TouchScreen_kbv_mbed.h	Mon Apr 26 15:15:38 2021 +0000
@@ -0,0 +1,41 @@
+// Touch screen library with X Y and Z (pressure) readings as well
+// as oversampling to avoid 'bouncing'
+// (c) ladyada / adafruit
+// Code under MIT License
+
+#ifndef _TOUCHSCREEN_KBV_MBED_H_
+#define _TOUCHSCREEN_KBV_MBED_H_
+#include <stdint.h>
+
+class TSPoint_kbv {
+    public:
+        TSPoint_kbv(void);
+        TSPoint_kbv(int16_t x, int16_t y, int16_t z);
+
+        bool operator==(TSPoint_kbv);
+        bool operator!=(TSPoint_kbv);
+
+        int16_t x, y, z;
+};
+
+class TouchScreen_kbv {
+    public:
+        TouchScreen_kbv(PinName xp, PinName yp, PinName xm, PinName ym, uint16_t rx = 0);
+
+//        bool isTouching(void);
+        uint16_t pressure(void);
+        int readTouchY();
+        int readTouchX();
+        TSPoint_kbv getPoint();
+        int16_t pressureThreshhold;
+
+    private:
+        uint16_t analogRead(PinName p);
+        void pinModeVal(PinName p, uint8_t mode, uint8_t val); 
+        PinName _yp, _ym, _xm, _xp;
+        uint16_t _rxplate;
+};
+
+#endif
+
+