-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUI.cpp
More file actions
205 lines (146 loc) · 4.73 KB
/
Copy pathUI.cpp
File metadata and controls
205 lines (146 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <EEPROM.h>
#include "UI.h"
#include <ChibiOS_AVR.h>
#include "SPFD5408_Adafruit_GFX.h"
#include "SPFD5408_Adafruit_TFTLCD.h"
#include "SPFD5408_TouchScreen.h"
#include "GFX.h"
#include "GUI.h"
#include "Application.h"
// *** SPFD5408 change -- End
#if defined(__SAM3X8E__)
#undef __FlashStringHelper::F(string_literal)
#define F(string_literal) string_literal
#endif
// When using the BREAKOUT BOARD only, use these 8 data lines to the LCD:
// For the Arduino Uno, Duemilanove, Diecimila, etc.:
// D0 connects to digital pin 8 (Notice these are
// D1 connects to digital pin 9 NOT in order!)
// D2 connects to digital pin 2
// D3 connects to digital pin 3
// D4 connects to digital pin 4
// D5 connects to digital pin 5
// D6 connects to digital pin 6
// D7 connects to digital pin 7
/////////////////////////////////////////////////////
// PF, old screen
// #define YP A1 // must be an analog pin, use "An" notation!
// #define XM A2 // must be an analog pin, use "An" notation!
// #define YM 7 // can be a digital pin
// #define XP 6 // can be a digital pin
// PF, new screen
#define YP A3 // must be an analog pin, use "An" notation!
#define XM A2 // must be an analog pin, use "An" notation!
#define YM 9 // can be a digital pin
#define XP 8 // can be a digital pin
// Original values
//#define TS_MINX 150
//#define TS_MINY 120
//#define TS_MAXX 920
//#define TS_MAXY 940
// Calibrate values
#define TS_MINX 125
#define TS_MINY 85
#define TS_MAXX 965
#define TS_MAXY 905
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
// optional
#define LCD_RESET A4
// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
#define SENSITIVITY 300
static Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
static GFX gfx(&tft, 240,320);
static TouchScreen ts = TouchScreen(XP, YP, XM, YM, SENSITIVITY);
#define MINPRESSURE 100 // inital 200-700
#define MAXPRESSURE 700
TSPoint getPoint() {
TSPoint p;
p= ts.getPoint();
pinMode(XM, OUTPUT); //Pins configures again for TFT control
pinMode(YP, OUTPUT);
return p;
}
bool isTouched(TSPoint p) {
return !((p.z < MINPRESSURE )|| (p.z > MAXPRESSURE));
}
TSPoint waitOneTouch() {
TSPoint p;
do {
p= ts.getPoint();
pinMode(XM, OUTPUT); //Pins configures again for TFT control
pinMode(YP, OUTPUT);
chThdSleepMicroseconds(1000);
} while((p.z < MINPRESSURE )|| (p.z > MAXPRESSURE));
return p;
}
void InitUI() {
tft.begin(0x9341); // SDFP5408
tft.setRotation(90);
}
void drawCross(uint16_t x, uint16_t y, uint16_t color) {
gfx.drawLine(x - 10, y - 10, x + 10, y + 10, color);
gfx.drawLine(x - 10, y + 10, x + 10, y - 10, color);
}
TSPoint readPoint(int &adr) {
int16_t x;
int16_t y;
EEPROM.get(adr,x);
adr += sizeof(int16_t);
EEPROM.get(adr, y);
adr += sizeof(int16_t);
return TSPoint(x, y, 0);
}
void writePoint(int &adr, TSPoint t) {
EEPROM.put(adr, t.x);
adr += sizeof(int16_t);
EEPROM.put(adr, t.y);
adr += sizeof(int16_t);
}
#define MAGIC_NUMBER 126
void RunUI() {
GUI::InitGFX(&gfx);
TSPoint first;
TSPoint second;
if (EEPROM.read(0) != MAGIC_NUMBER) {
// calibration
drawCross(10,10, RED);
first = waitOneTouch();
chThdSleepMilliseconds(1000);
drawCross(230,310, RED);
second = waitOneTouch();
int adr = 1;
writePoint(adr,first);
writePoint(adr,second);
EEPROM.write(0,(char)MAGIC_NUMBER);
} else
{
// already calibrated
int adr = 1;
first = readPoint(adr);
second = readPoint(adr);
}
GUI::Scale xc = GUI::Scale(first.x,10, second.x, 230);
GUI::Scale yc = GUI::Scale(first.y,10, second.y, 310);
long ticks = millis();
App::mainApp.startup();
while(true) {
chThdSleepMicroseconds(10000);
TSPoint t = getPoint();
if (isTouched(t)) {
GUI::TouchMessage m( NULL, xc.convert(t.x), yc.convert(t.y)) ;
App::mainApp.dispatchMessage(&m);
//gfx.drawPixel(50 +random(100), 50 + random(100),random(100)); // RED
}
if (millis() - ticks > 100) {
GUI::TimerMessage m(millis() - ticks);
App::mainApp.dispatchMessage(&m);
ticks = millis();
}
// gfx.drawPixel(50 +random(100), 50 + random(100),random(100)); // RED
}
}