IoAbstraction
Toggle main menu visibility
Loading...
Searching...
No Matches
src
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"
6
#include "
AnalogDeviceAbstraction.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
22
namespace
iotouch {
29
class
TouchOrientationSettings
{
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
}
45
TouchOrientationSettings
(
const
TouchOrientationSettings
& other) =
default
;
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;
69
AccelerationMode
mode;
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
116
enum
TouchState
: uint8_t {
118
NOT_TOUCHED
,
120
TOUCHED
,
122
HELD
,
124
TOUCH_DEBOUNCE
125
};
126
127
#define portableFloatAbs(x) ((x)<0.0F?-(x):(x))
128
134
class
TouchInterrogator
{
135
public
:
144
virtual
TouchState
internalProcessTouch
(
float
* ptrX,
float
* ptrY,
const
TouchOrientationSettings
& settings,
const
CalibrationHandler
& calib)=0;
145
};
146
147
class
TouchInterrogator
;
148
149
class
TouchScreenManager :
public
Executable {
150
public
:
151
private
:
152
AccelerationHandler
accelerationHandler;
153
CalibrationHandler
calibrator;
154
TouchInterrogator
* touchInterrogator;
155
TouchState
touchMode;
156
TouchOrientationSettings
orientation;
157
bool
usedForScrolling =
false
;
158
public
:
159
explicit
TouchScreenManager(
TouchInterrogator
* interrogator,
const
TouchOrientationSettings
& orientationSettings) :
160
accelerationHandler(10,
true
), calibrator(),
161
touchInterrogator(interrogator), touchMode(
NOT_TOUCHED
), orientation(orientationSettings) {}
162
163
void
start() {
164
touchMode =
NOT_TOUCHED
;
165
taskManager.execute(
this
);
166
}
167
168
void
setUsedForScrolling(
bool
scrolling) {
169
usedForScrolling = scrolling;
170
}
171
172
void
calibrateMinMaxValues(
float
xmin,
float
xmax,
float
ymin,
float
ymax) {
173
calibrator.setCalibrationValues(xmin, xmax, ymin, ymax);
174
}
175
176
void
setCalibration(
const
CalibrationHandler
& other) { calibrator = other; }
177
178
void
enableCalibration(
bool
ena) {
179
calibrator.enableCalibration(ena);
180
}
181
182
TouchOrientationSettings
changeOrientation(
const
TouchOrientationSettings
& newOrientation);
183
184
TouchOrientationSettings
getOrientation() {
return
orientation; }
185
186
void
exec()
override
;
187
196
virtual
void
sendEvent
(
float
locationX,
float
locationY,
float
touchPressure,
TouchState
touched) = 0;
197
};
198
211
class
ResistiveTouchInterrogator :
public
TouchInterrogator
{
212
private
:
213
pinid_t xpPin, xnPinAdc, ypPinAdc, ynPin;
214
public
:
215
216
ResistiveTouchInterrogator(pinid_t xpPin, pinid_t xnPin, pinid_t ypPin, pinid_t ynPin)
217
: xpPin(xpPin), xnPinAdc(xnPin), ypPinAdc(ypPin), ynPin(ynPin) {}
218
219
TouchState
internalProcessTouch
(
float
* ptrX,
float
* ptrY,
const
TouchOrientationSettings
& rotation,
const
CalibrationHandler
& calibrator)
override
;
220
221
};
222
227
class
ValueStoringResistiveTouchScreen :
public
TouchScreenManager {
228
private
:
229
float
lastX, lastY, touchPressure;
230
TouchState
touchState;
231
public
:
232
ValueStoringResistiveTouchScreen(
TouchInterrogator
& interrogator,
const
TouchOrientationSettings
& rotation)
233
: TouchScreenManager(&interrogator, rotation), lastX(0.0F), lastY(0.0F), touchState(
NOT_TOUCHED
) {
234
}
235
236
void
sendEvent
(
float
locationX,
float
locationY,
float
pressure,
TouchState
touched)
override
;
237
238
float
getTouchPressure()
const
{
return
touchPressure; }
239
float
getLastX()
const
{
return
lastX; }
240
float
getLastY()
const
{
return
lastY; }
241
bool
isPressed()
const
{
return
touchState ==
TOUCHED
; }
242
TouchState
getTouchState()
const
{
return
touchState; }
243
};
244
}
245
246
#endif
//IOA_RESISTIVETOUCHSCREEN_H
AnalogDeviceAbstraction.h
Contains a series of helper classes for dealing with analog devices, these are compatible across a wi...
iotouch::TouchState
TouchState
Definition
ResistiveTouchScreen.h:116
iotouch::TOUCH_DEBOUNCE
@ TOUCH_DEBOUNCE
Definition
ResistiveTouchScreen.h:124
iotouch::TOUCHED
@ TOUCHED
Definition
ResistiveTouchScreen.h:120
iotouch::NOT_TOUCHED
@ NOT_TOUCHED
Definition
ResistiveTouchScreen.h:118
iotouch::HELD
@ HELD
Definition
ResistiveTouchScreen.h:122
iotouch::AccelerationMode
AccelerationMode
Definition
ResistiveTouchScreen.h:54
iotouch::AccelerationHandler
Definition
ResistiveTouchScreen.h:64
iotouch::CalibrationHandler
Definition
ResistiveTouchScreen.h:86
iotouch::ResistiveTouchInterrogator::internalProcessTouch
TouchState internalProcessTouch(float *ptrX, float *ptrY, const TouchOrientationSettings &rotation, const CalibrationHandler &calibrator) override
Definition
ResistiveTouchScreen.cpp:103
iotouch::TouchInterrogator
Definition
ResistiveTouchScreen.h:134
iotouch::TouchInterrogator::internalProcessTouch
virtual TouchState internalProcessTouch(float *ptrX, float *ptrY, const TouchOrientationSettings &settings, const CalibrationHandler &calib)=0
iotouch::TouchOrientationSettings
Definition
ResistiveTouchScreen.h:29
iotouch::TouchOrientationSettings::TouchOrientationSettings
TouchOrientationSettings(bool swapAxisXY, bool xIsInverted, bool yIsInverted)
Definition
ResistiveTouchScreen.h:39
iotouch::TouchScreenManager::sendEvent
virtual void sendEvent(float locationX, float locationY, float touchPressure, TouchState touched)=0
iotouch::ValueStoringResistiveTouchScreen::sendEvent
void sendEvent(float locationX, float locationY, float pressure, TouchState touched) override
Definition
ResistiveTouchScreen.cpp:159