TouchScreenAdapter v0.5.0
TouchScreenAdapter is a library providing a unified touchscreen API.
AdafruitResistiveTouchScreen.h
1
9#ifndef AdafruitResistiveTouchScreen_h
10#define AdafruitResistiveTouchScreen_h
11
12//Only create adapapter if Adafruit's TouchScreen is already included.
13#ifdef _ADAFRUIT_TOUCHSCREEN_H_
14
15#include <Arduino.h>
16
17#include "TouchScreenAdapter.h"
18
19
32
33public:
34
46 AdafruitResistiveTouchScreen(uint8_t pinXPos, uint8_t pinYPos, uint8_t pinXNeg,uint8_t pinYNeg, uint16_t ohms=300, uint16_t displayWidth=240, uint16_t displayHeight=320 )
47 {
52 touchscreen = new TouchScreen(pinXPos, pinYPos, pinXNeg, pinYNeg, ohms);
53 }
54
60 delete touchscreen; //Safe to delete even if null;
61 }
62
63
72 TSPoint tsPoint = touchscreen->getPoint();
73 //Serial.printf("TSPoint X: %3i, Y: %3i, Z: %3i \n", tsPoint.x, tsPoint.y, tsPoint.x);
75 // The resistive touch panel will irregularly report touch when at rest
76 // I suspect it is the flexible membrane 'relaxing' or a read error in the TouchScreen lib,
77 // so we have to chck if Z, X and Y are within the expected resistance range.
78 if ( tsPoint.z > (int16_t)minRawZ && tsPoint.z < (int16_t)maxRawZ
79 && tsPoint.x > (int16_t)minRawX && tsPoint.x < (int16_t)maxRawX
80 && tsPoint.y > (int16_t)minRawY && tsPoint.y < (int16_t)maxRawY ) {
81 touchpoint.z = constrain(map(tsPoint.z, maxRawZ, minRawZ, 1, 255), 1, 255);
82 uint16_t x = rotation % 2 ? tsPoint.y : tsPoint.x;
83 uint16_t y = rotation % 2 ? tsPoint.x : tsPoint.y;
84 if ( rotation == 2 || rotation == 3 ) {
85 touchpoint.x = constrain(map(x, maxRawX, minRawX, 0, displayWidth), 0, displayWidth-1);
86 } else {
87 touchpoint.x = constrain(map(x, minRawX, maxRawX, 0, displayWidth), 0, displayWidth-1);
88 }
89 if ( rotation == 1 || rotation == 2 ) {
90 touchpoint.y = constrain(map(y, maxRawY, minRawY, 0, displayHeight), 0, displayHeight-1);
91 } else {
92 touchpoint.y = constrain(map(y, minRawY, maxRawY, 0, displayHeight), 0, displayHeight-1);
93 }
94 }
95 return touchpoint;
96 }
97
98
109 TSPoint tsPoint = touchscreen->getPoint();
111 touchpoint.x = tsPoint.x;
112 touchpoint.y = tsPoint.y;
113 touchpoint.z = tsPoint.z;
114 return touchpoint;
115 }
116
123 bool begin(void) { return true; }
124
125 // Not implemented becuse the Adfruit TouchScreen library doesn't use the pressueThreshold (or pressureThreshhold :-)
126 // void setPressureThreshold(uint8_t pressure) {
127 // touchscreen->pressureThreshhold = pressure;
128 // }
129
135 void setMinRawY(uint16_t limit) { minRawY = limit; }
136
142 void setMinRawX(uint16_t limit) { minRawX = limit; }
143
149 void setMaxRawX(uint16_t limit) { maxRawX = limit; }
150
156 void setMaxRawY(uint16_t limit) { maxRawY = limit; }
157
163 void setMinRawZ(uint16_t limit) { minRawZ = limit; }
164
170 void setMaxRawZ(uint16_t limit) { maxRawZ = limit; }
171
172protected:
173
174private:
175
176 //Have to use heap or Teensy on PlatformIO will fail to link. :-/
177 //https://forum.pjrc.com/index.php?threads/multiple-definition-of-error-when-using-std-function-with-argument-teensy41-platformio.74505/
178 TouchScreen *touchscreen = nullptr;
179
180 uint16_t minRawY = 100;
181 uint16_t maxRawY = 900;
182 uint16_t minRawX = 100;
183 uint16_t maxRawX = 900;
184 uint16_t minRawZ = 1;
185 uint16_t maxRawZ = 1023;
186 uint8_t zThreshold = 10;
187};
188
189
190#endif
191#endif
Concrete implementation of TouchScreenAdapter for Adafruit's resistive TouchScreen class.
Definition: AdafruitResistiveTouchScreen.h:31
bool begin(void)
Empty begin() implementation.
Definition: AdafruitResistiveTouchScreen.h:123
TouchScreenAdapter::TouchPoint getTouchPointRaw(void)
Get the raw TouchPoint struct.
Definition: AdafruitResistiveTouchScreen.h:108
void setMaxRawX(uint16_t limit)
Set the maximum raw X value.
Definition: AdafruitResistiveTouchScreen.h:149
void setMaxRawZ(uint16_t limit)
Set the maximum raw Z value.
Definition: AdafruitResistiveTouchScreen.h:170
void setMaxRawY(uint16_t limit)
Set the maximum raw Y value.
Definition: AdafruitResistiveTouchScreen.h:156
void setMinRawY(uint16_t limit)
Set the minimun raw Y value.
Definition: AdafruitResistiveTouchScreen.h:135
~AdafruitResistiveTouchScreen()
Destroy the AdafruitResistiveTouchScreen object.
Definition: AdafruitResistiveTouchScreen.h:59
void setMinRawZ(uint16_t limit)
Set the maximum raw Z value.
Definition: AdafruitResistiveTouchScreen.h:163
void setMinRawX(uint16_t limit)
Set the minimun raw X value.
Definition: AdafruitResistiveTouchScreen.h:142
TouchScreenAdapter::TouchPoint getTouchPoint(void)
Get the TouchPoint struct.
Definition: AdafruitResistiveTouchScreen.h:71
AdafruitResistiveTouchScreen(uint8_t pinXPos, uint8_t pinYPos, uint8_t pinXNeg, uint8_t pinYNeg, uint16_t ohms=300, uint16_t displayWidth=240, uint16_t displayHeight=320)
Construct a new Resistive Touch Screen object.
Definition: AdafruitResistiveTouchScreen.h:46
A lightweight abstract Abstract class for touch screen panels.
Definition: TouchScreenAdapter.h:19
uint16_t displayHeight
The (optionally rotated) height of the display in pixels.
Definition: TouchScreenAdapter.h:162
uint16_t displayWidth
The (optionally rotated) width of the display in pixels.
Definition: TouchScreenAdapter.h:161
uint8_t rotation
The current rotation of the touch panel. Default is 0 (native orientation).
Definition: TouchScreenAdapter.h:156
uint16_t DISPLAY_HEIGHT
The non-rotated height of the display in pixels.
Definition: TouchScreenAdapter.h:159
uint16_t DISPLAY_WIDTH
The non-rotated width of the display in pixels.
Definition: TouchScreenAdapter.h:158
A consistent struct containing points X & Y plus Z as pressure.
Definition: TouchScreenAdapter.h:27
uint16_t x
Mapped 0 to screen width -1.
Definition: TouchScreenAdapter.h:47
uint16_t y
Mapped 0 to screen height -1.
Definition: TouchScreenAdapter.h:48
uint16_t z
Mapped 0-255 representing pressure (0 = not touched). is uint16_t because raw is odten > 255.
Definition: TouchScreenAdapter.h:49