IoAbstraction
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 
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 {
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 
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 
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 {
125  };
126 
127 #define portableFloatAbs(x) ((x)<0.0F?-(x):(x))
128 
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 
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 
228  private:
229  float lastX, lastY, touchPressure;
230  TouchState touchState;
231  public:
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
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
Definition: ResistiveTouchScreen.h:211
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
Definition: ResistiveTouchScreen.h:149
virtual void sendEvent(float locationX, float locationY, float touchPressure, TouchState touched)=0
Definition: ResistiveTouchScreen.h:227
void sendEvent(float locationX, float locationY, float pressure, TouchState touched) override
Definition: ResistiveTouchScreen.cpp:159