|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <TouchScreen.h> |
| 4 | + |
| 5 | +// TODO: implement Touch on Arduino (based on TouchScreen) |
| 6 | + |
| 7 | +// TODO add into a config? |
| 8 | +#ifndef SCREEN_DEFAULT_CLK_ALPHA |
| 9 | +#define SCREEN_DEFAULT_CLK_ALPHA 10 |
| 10 | +#endif |
| 11 | + |
| 12 | +#ifndef SCREEN_DEFAULT_CLK_MAX |
| 13 | +#define SCREEN_DEFAULT_CLK_MAX 150 |
| 14 | +#endif |
| 15 | + |
| 16 | +#ifndef SCREEN_DEFAULT_CLK_MIN |
| 17 | +#define SCREEN_DEFAULT_CLK_MIN 30 |
| 18 | +#endif |
| 19 | + |
| 20 | +struct Size { |
| 21 | + int width = SCREEN_THEME_WIDTH; |
| 22 | + int height = SCREEN_THEME_HEIGHT; |
| 23 | + bool operator==(const Size& other) const { |
| 24 | + return |
| 25 | + width == other.width && |
| 26 | + height == other.height; |
| 27 | + } |
| 28 | + bool operator!=(const Size& other) const { |
| 29 | + return !(*this == other); |
| 30 | + } |
| 31 | +}; |
| 32 | + |
| 33 | +class Touch { |
| 34 | +public: |
| 35 | + |
| 36 | + struct Calibration { |
| 37 | + unsigned int tsMinX; |
| 38 | + unsigned int tsMinY; |
| 39 | + unsigned int tsMaxX; |
| 40 | + unsigned int tsMaxY; |
| 41 | + |
| 42 | + // touch |
| 43 | + double alpha; |
| 44 | + double cmax; |
| 45 | + double cmin; |
| 46 | + }; |
| 47 | + |
| 48 | +protected: |
| 49 | + Graphics& graphics; |
| 50 | + TouchScreen& TS; |
| 51 | + const uint8_t& yp; |
| 52 | + const uint8_t& xm; |
| 53 | + int rotation; |
| 54 | + |
| 55 | + Size size; |
| 56 | + Calibration calibration; |
| 57 | + int calibrating = 0; |
| 58 | + mutable double clkz = 0; |
| 59 | + |
| 60 | + TSPoint p; |
| 61 | + void read() { |
| 62 | + prevState = state; |
| 63 | + |
| 64 | + p = TS.getPoint(); |
| 65 | + pinMode(xm, OUTPUT); |
| 66 | + pinMode(yp, OUTPUT); |
| 67 | + |
| 68 | + // smooth EMA on sensor value |
| 69 | + clkz = (clkz * calibration.alpha + (double)p.z) / (calibration.alpha + 1); |
| 70 | + if (clkz > calibration.cmax) clkz = calibration.cmax; |
| 71 | + if (clkz < calibration.cmin) clkz = 0; |
| 72 | + state.z = clkz; |
| 73 | + |
| 74 | + switch (state.event) { |
| 75 | + case EVENT_IDLE: |
| 76 | + if (clkz >= calibration.cmax) state.event = EVENT_TOUCH; |
| 77 | + break; |
| 78 | + case EVENT_TOUCH: |
| 79 | + state.event = EVENT_MOVE; |
| 80 | + break; |
| 81 | + case EVENT_MOVE: |
| 82 | + if (clkz <= calibration.cmin) state.event = EVENT_RELEASE; |
| 83 | + break; |
| 84 | + case EVENT_RELEASE: |
| 85 | + state.event = EVENT_IDLE; |
| 86 | + break; |
| 87 | + |
| 88 | + default: |
| 89 | + state.event = EVENT_IDLE; |
| 90 | + break; |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + if (state.event == EVENT_TOUCH || (EVENT_MOVE && state.z == calibration.cmax)) { |
| 95 | + switch (rotation) { |
| 96 | + default: |
| 97 | + case 0: |
| 98 | + state.x = p.x; |
| 99 | + state.y = p.y; |
| 100 | + break; |
| 101 | + case 1: |
| 102 | + state.x = p.y; |
| 103 | + state.y = p.x; |
| 104 | + break; |
| 105 | + case 2: |
| 106 | + state.x = size.width - p.x; |
| 107 | + state.y = p.y; |
| 108 | + break; |
| 109 | + case 3: |
| 110 | + state.x = p.x; |
| 111 | + state.y = size.height - p.y; |
| 112 | + break; |
| 113 | + case 4: |
| 114 | + state.x = size.width - p.x; |
| 115 | + state.y = size.height - p.y; |
| 116 | + break; |
| 117 | + case 5: |
| 118 | + state.x = size.width - p.y; |
| 119 | + state.y = p.x; |
| 120 | + break; |
| 121 | + case 6: |
| 122 | + state.x = p.y; |
| 123 | + state.y = size.height - p.x; |
| 124 | + break; |
| 125 | + case 7: |
| 126 | + state.x = size.width - p.y; |
| 127 | + state.y = size.height - p.x; |
| 128 | + break; |
| 129 | + } |
| 130 | + if (!calibrating) { |
| 131 | + state.x = map(state.x, calibration.tsMaxX, calibration.tsMinX, size.width, 0); |
| 132 | + state.y = map(state.y, calibration.tsMaxY, calibration.tsMinY, size.height, 0); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + } |
| 137 | + |
| 138 | +public: |
| 139 | + struct State { |
| 140 | + int event; |
| 141 | + int x; |
| 142 | + int y; |
| 143 | + int z; |
| 144 | + }; |
| 145 | +protected: |
| 146 | + State state, prevState; |
| 147 | +public: |
| 148 | + Touch(TouchScreen& TS, const uint8_t& yp, const uint8_t& xm, const int rotation = 0): TS(TS), yp(yp), xm(xm), rotation(rotation) { |
| 149 | + |
| 150 | + state.event = EVENT_IDLE; |
| 151 | + state.x = -1; |
| 152 | + state.y = -1; |
| 153 | + } |
| 154 | + |
| 155 | + // TODO: add on desktop? |
| 156 | + void begin( |
| 157 | + int width, int height, unsigned int tsMinX, unsigned int tsMinY, unsigned int tsMaxX, unsigned int tsMaxY, |
| 158 | + const double alpha = SCREEN_DEFAULT_CLK_ALPHA, const double cmax = SCREEN_DEFAULT_CLK_MAX, const double cmin = SCREEN_DEFAULT_CLK_MIN |
| 159 | + ) { |
| 160 | + size.width = width; |
| 161 | + size.height = height; |
| 162 | + calibration.tsMinX = tsMinX; |
| 163 | + calibration.tsMinY = tsMinY; |
| 164 | + calibration.tsMaxX = tsMaxX; |
| 165 | + calibration.tsMaxY = tsMaxY; |
| 166 | + calibration.alpha = alpha; |
| 167 | + calibration.cmax = cmax; |
| 168 | + calibration.cmin = cmin; |
| 169 | + calibrating = 0; |
| 170 | + } |
| 171 | + |
| 172 | + Calibration& begin( |
| 173 | + int width, int height, Graphics& graphics, |
| 174 | + const double alpha = SCREEN_DEFAULT_CLK_ALPHA, const double cmax = SCREEN_DEFAULT_CLK_MAX, const double cmin = SCREEN_DEFAULT_CLK_MIN |
| 175 | + ) { |
| 176 | + size.width = width; |
| 177 | + size.height = height; |
| 178 | + calibrating = 1; |
| 179 | + calibration.alpha = alpha; |
| 180 | + calibration.cmax = cmax; |
| 181 | + calibration.cmin = cmin; |
| 182 | + |
| 183 | + Color textcolor = COLOR(EGA_WHITE); |
| 184 | + const char* text = "Calibration..."; |
| 185 | + |
| 186 | + while(calibrating) { |
| 187 | + read(); |
| 188 | + switch (calibrating) { |
| 189 | + case 1: |
| 190 | + graphics.fillScreen(EGA_BLACK); |
| 191 | + // const char* s = "Calibration..."; |
| 192 | + graphics.text(text, 0, 40, 2, textcolor); |
| 193 | + graphics.text("X <- Press here", 1, 1, 2, textcolor); |
| 194 | + calibrating = 2; |
| 195 | + break; |
| 196 | + case 2: |
| 197 | + // waiting for first point... |
| 198 | + if (state.event == EVENT_TOUCH) { |
| 199 | + calibration.tsMinX = state.x; |
| 200 | + calibration.tsMinY = state.y; |
| 201 | + |
| 202 | + graphics.fillScreen(EGA_BLACK); |
| 203 | + graphics.text(text, 0, 0, 2, textcolor); |
| 204 | + uint16_t w,h; |
| 205 | + const char* txt = "Press here -> X"; |
| 206 | + graphics.getTextBounds(txt, 2, w, h); |
| 207 | + graphics.text(txt, width-w, height-h, 2, textcolor); |
| 208 | + |
| 209 | + calibrating = 3; |
| 210 | + } |
| 211 | + break; |
| 212 | + |
| 213 | + case 3: |
| 214 | + // waiting for second point... |
| 215 | + if (state.event == EVENT_TOUCH) { |
| 216 | + calibration.tsMaxX = state.x; |
| 217 | + calibration.tsMaxY = state.y; |
| 218 | + calibrating = 0; |
| 219 | + } |
| 220 | + break; |
| 221 | + default: |
| 222 | + calibrating = 0; |
| 223 | + break; |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + graphics.fillScreen(COLOR(EGA_BLACK)); |
| 228 | + return calibration; |
| 229 | + } |
| 230 | + |
| 231 | + State& getState() { |
| 232 | + read(); |
| 233 | + return state; |
| 234 | + } |
| 235 | +}; |
0 commit comments