IoAbstraction
Loading...
Searching...
No Matches
ResistiveTouchScreen.h
Go to the documentation of this file.
1
2#ifndef IOA_RESISTIVETOUCHSCREEN_H
3#define IOA_RESISTIVETOUCHSCREEN_H
4
5#include "PlatformDetermination.h"
7#include <TaskManagerIO.h>
8
13
14#ifndef TOUCH_THRESHOLD
15#define TOUCH_THRESHOLD 0.05F
16#endif
17
18#define TOUCH_ORIENTATION_BIT_SWAP 0
19#define TOUCH_ORIENTATION_BIT_INV_X 1
20#define TOUCH_ORIENTATION_BIT_INV_Y 2
21
22namespace iotouch {
30 private:
31 uint16_t flags;
32 public:
39 TouchOrientationSettings(bool swapAxisXY, bool xIsInverted, bool yIsInverted) {
40 flags = 0;
41 bitWrite(flags, TOUCH_ORIENTATION_BIT_SWAP, swapAxisXY);
42 bitWrite(flags, TOUCH_ORIENTATION_BIT_INV_X, xIsInverted);
43 bitWrite(flags, TOUCH_ORIENTATION_BIT_INV_Y, yIsInverted);
44 }
46 TouchOrientationSettings& operator= (const TouchOrientationSettings& other) = default;
47
48 bool isOrientationSwapped() const { return bitRead(flags, TOUCH_ORIENTATION_BIT_SWAP);}
49 bool isXInverted() const { return bitRead(flags, TOUCH_ORIENTATION_BIT_INV_X);}
50 bool isYInverted() const { return bitRead(flags, TOUCH_ORIENTATION_BIT_INV_Y);}
51 };
52
54 enum AccelerationMode: uint8_t {
55 WAITING,
56 ACCELERATING,
57 NEVER_ACCELERATES
58 };
59
64 class AccelerationHandler {
65 private:
66 uint8_t minTicks;
67 uint8_t ticks;
68 uint8_t accel;
70 public:
71 AccelerationHandler(uint8_t minTicks, bool accelerate) : minTicks(minTicks) {
72 mode = accelerate ? WAITING : NEVER_ACCELERATES;
73 }
74
75 void reset() {
76 if(mode == ACCELERATING) mode = WAITING;
77 }
78
79 bool tick();
80 };
81
86 class CalibrationHandler {
87 private:
88 float minX, maxX;
89 float minY, maxY;
90 bool calibrationOn;
91
92 public:
93 CalibrationHandler() = default;
94 CalibrationHandler(const CalibrationHandler& other) = default;
95 CalibrationHandler& operator=(const CalibrationHandler& other) = default;
96
97 void setCalibrationValues(float mnX, float mxX, float mnY, float mxY);
98
99 void enableCalibration(bool state) {
100 calibrationOn = state;
101 }
102
103 float calibrateX(float rawValue, bool isInverted) const;
104
105 float calibrateY(float rawValue, bool isInverted) const;
106
107 float getMinX() const { return minX;}
108 float getMinY() const { return minY;}
109 float getMaxX() const { return maxX;}
110 float getMaxY() const { return maxY;}
111 void setXPosition(float x, bool isMax);
112 void setYPosition(float y, bool isMax);
113 };
114
126
127#define portableFloatAbs(x) ((x)<0.0F?-(x):(x))
128
135 public:
136 virtual ~TouchInterrogator() = default;
145 virtual TouchState internalProcessTouch(float* ptrX, float* ptrY, const TouchOrientationSettings& settings, const CalibrationHandler& calib)=0;
146 };
147
148 class TouchInterrogator;
149
150 class TouchScreenManager : public Executable {
151 public:
152 private:
153 AccelerationHandler accelerationHandler;
154 CalibrationHandler calibrator;
155 TouchInterrogator* touchInterrogator;
156 TouchState touchMode;
157 TouchOrientationSettings orientation;
158 bool usedForScrolling = false;
159 public:
160 explicit TouchScreenManager(TouchInterrogator* interrogator, const TouchOrientationSettings& orientationSettings) :
161 accelerationHandler(10, true), calibrator(),
162 touchInterrogator(interrogator), touchMode(NOT_TOUCHED), orientation(orientationSettings) {}
163
164 void start() {
165 touchMode = NOT_TOUCHED;
166 taskManager.execute(this);
167 }
168
169 void setUsedForScrolling(bool scrolling) {
170 usedForScrolling = scrolling;
171 }
172
173 void calibrateMinMaxValues(float xmin, float xmax, float ymin, float ymax) {
174 calibrator.setCalibrationValues(xmin, xmax, ymin, ymax);
175 }
176
177 void setCalibration(const CalibrationHandler& other) { calibrator = other; }
178
179 void enableCalibration(bool ena) {
180 calibrator.enableCalibration(ena);
181 }
182
183 TouchOrientationSettings changeOrientation(const TouchOrientationSettings& newOrientation);
184
185 TouchOrientationSettings getOrientation() { return orientation; }
186
187 void exec() override;
188
197 virtual void sendEvent(float locationX, float locationY, float touchPressure, TouchState touched) = 0;
198 };
199
212 class ResistiveTouchInterrogator : public TouchInterrogator {
213 private:
214 pinid_t xpPin, xnPinAdc, ypPinAdc, ynPin;
215 public:
216
217 ResistiveTouchInterrogator(pinid_t xpPin, pinid_t xnPin, pinid_t ypPin, pinid_t ynPin)
218 : xpPin(xpPin), xnPinAdc(xnPin), ypPinAdc(ypPin), ynPin(ynPin) {}
219
220 TouchState internalProcessTouch(float* ptrX, float* ptrY, const TouchOrientationSettings& rotation, const CalibrationHandler& calibrator) override;
221
222 };
223
228 class ValueStoringResistiveTouchScreen : public TouchScreenManager {
229 private:
230 float lastX, lastY, touchPressure;
231 TouchState touchState;
232 public:
233 ValueStoringResistiveTouchScreen(TouchInterrogator& interrogator, const TouchOrientationSettings& rotation)
234 : TouchScreenManager(&interrogator, rotation), lastX(0.0F), lastY(0.0F), touchState(NOT_TOUCHED) {
235 }
236
237 void sendEvent(float locationX, float locationY, float pressure, TouchState touched) override;
238
239 float getTouchPressure() const { return touchPressure; }
240 float getLastX() const { return lastX; }
241 float getLastY() const { return lastY; }
242 bool isPressed() const { return touchState == TOUCHED; }
243 TouchState getTouchState() const { return touchState; }
244 };
245}
246
247#endif //IOA_RESISTIVETOUCHSCREEN_H
Contains a series of helper classes for dealing with analog devices, these are compatible across a wi...
TouchState
Definition ResistiveTouchScreen.h:116
@ TOUCH_DEBOUNCE
Definition ResistiveTouchScreen.h:124
@ TOUCHED
Definition ResistiveTouchScreen.h:120
@ NOT_TOUCHED
Definition ResistiveTouchScreen.h:118
@ HELD
Definition ResistiveTouchScreen.h:122
AccelerationMode
Definition ResistiveTouchScreen.h:54
Definition ResistiveTouchScreen.h:64
Definition ResistiveTouchScreen.h:86
TouchState internalProcessTouch(float *ptrX, float *ptrY, const TouchOrientationSettings &rotation, const CalibrationHandler &calibrator) override
Definition ResistiveTouchScreen.cpp:103
Definition ResistiveTouchScreen.h:134
virtual TouchState internalProcessTouch(float *ptrX, float *ptrY, const TouchOrientationSettings &settings, const CalibrationHandler &calib)=0
Definition ResistiveTouchScreen.h:29
TouchOrientationSettings(bool swapAxisXY, bool xIsInverted, bool yIsInverted)
Definition ResistiveTouchScreen.h:39
virtual void sendEvent(float locationX, float locationY, float touchPressure, TouchState touched)=0
void sendEvent(float locationX, float locationY, float pressure, TouchState touched) override
Definition ResistiveTouchScreen.cpp:159