forked from MarcoAntonini/RepRapLcd4D
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTouchCal.4dg
295 lines (239 loc) · 8.89 KB
/
TouchCal.4dg
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#platform "uLCD-32PT_GFX2"
#inherit "4DGL_16bitColours.fnc"
// This programs is used to calibrate the touch screen.
// Touchscreen calibration is provided as it is sometimes necessary to
// re-calibrate the touch screen for various reasons:-
// 1] Initial calibration after manufacture to offset for manufacturing differences.
// 2] Aging - touch screen paramaters may change due to drift and/or component aging.
// 3] Parallax issues for different viewing angles or orientation.
// 4] User preferences, depending on how you hold you scribe etc.
// Usually, after initial calibration the touch screen values will rethe same for long periods of time.
// It is easiest to just run the program as a RAM download as calibration is not often required.
// Just download the program to RAM from the workshop and follow the on-screen prompts. It is necessary
// to press firmly and accurately for approx 1 second on the sample points as sampling/averaging process can take place.
// Don't load this program to FLASH from the Workshop else it will be stuck as the main program and loop.
// If preferred, it could be saved to FAT16 as an executable and may be called upon as required from a menu of some kind.
// This program can also be simplified / beautified for individual requirements.
var X1, Y1;
var avgX,avgX2, avgY, avgY2, n, k, state, count;
var x1,y1,x2,y2, Xspan, Yspan, Xload, Yload;
//==============================================
// take the average of 256 samples
//==============================================
func scanTouch()
for(k:=0;k<16;k++)
for(n:=0;n<16;n++)
avgX+=peekW(TOUCH_RAW_X)>>2;
avgY+=peekW(TOUCH_RAW_Y)>>2;
next
avgX2+=avgX/16;
avgY2+=avgY/16;
avgX:=0;
avgY:=0;
next
X1 := avgX2/16;
Y1 := avgY2/16;
avgX2:=0;
avgY2:=0;
avgX:=0;
avgY:=0;
endfunc
//==============================================
// Draw first calibration point, wait for
// a stable sample.
//==============================================
func getDot1()
var Xsample,Ysample;
// gfx_MoveTo(70,284);
// print(" "); // clear dot 2 text if present
gfx_Hline(32,24-12,24+12,WHITE);
gfx_Vline(24,32-12,32+12,WHITE);
gfx_CircleFilled(24,32,3,WHITE);
gfx_Circle(24,32,12,WHITE);
repeat
gfx_MoveTo(50,210);
if(peekW(SYSTEM_TIMER_LO) & 512)
gfx_Set(SCREEN_MODE,LANDSCAPE);
print("<<< Touch Centre");
gfx_Set(SCREEN_MODE,PORTRAIT);
else
gfx_Set(SCREEN_MODE,LANDSCAPE);
print(" ");
gfx_Set(SCREEN_MODE,PORTRAIT);
endif
until (touch_Get(TOUCH_STATUS) == TOUCH_PRESSED);
gfx_MoveTo(50,210);
gfx_Set(SCREEN_MODE,LANDSCAPE);
print("<<< Sampling....");
gfx_Set(SCREEN_MODE,PORTRAIT);
repeat
scanTouch();
repeat // wait for stable reading
Xsample := X1;
Ysample := Y1;
scanTouch();
until(X1==Xsample && Y1 ==Ysample);
until(X1 > 0x40 && Y1 >= 0x40);
gfx_MoveTo(50,210);
gfx_Set(SCREEN_MODE,LANDSCAPE);
print("OK \n");
gfx_Set(SCREEN_MODE,PORTRAIT);
while(touch_Get(TOUCH_STATUS) != TOUCH_RELEASED); // wait for touch release
endfunc
//==============================================
// Draw second calibration point, wait for
// a stable sample.
//==============================================
func getDot2()
var Xsample,Ysample;
gfx_Hline(320-32,240-24-12,240-24+12,WHITE);
gfx_Vline(240-24,320-32-12,320-32+12,WHITE);
gfx_CircleFilled(240-24,320-32,3,WHITE);
gfx_Circle(240-24,320-32,12,WHITE);
repeat
gfx_MoveTo(140,17);
if(peekW(SYSTEM_TIMER_LO) & 512)
gfx_Set(SCREEN_MODE,LANDSCAPE);
print("Touch Centre >>>");
gfx_Set(SCREEN_MODE,PORTRAIT);
else
gfx_Set(SCREEN_MODE,LANDSCAPE);
print(" ");
gfx_Set(SCREEN_MODE,PORTRAIT);
endif
//scanTouch();
until (touch_Get(TOUCH_STATUS) == TOUCH_PRESSED);
gfx_Set(SCREEN_MODE,LANDSCAPE);
gfx_MoveTo(140,17);
print(" Sampling >>>");
gfx_Set(SCREEN_MODE,PORTRAIT);
repeat
scanTouch();
repeat // wait for stable reading
Xsample := X1;
Ysample := Y1;
scanTouch();
until(X1==Xsample && Y1 ==Ysample);
until(X1 > 0x200 && Y1 >= 0x200);
gfx_MoveTo(140,17);
gfx_Set(SCREEN_MODE,LANDSCAPE);
print(" OK");
gfx_Set(SCREEN_MODE,PORTRAIT);
while(touch_Get(TOUCH_STATUS) != TOUCH_RELEASED); // wait for touch release
endfunc
func main()
repeat
gfx_Cls();
redo:
txt_FGcolour(LIME);
touch_Set(TOUCH_ENABLE); // enable the touch screen
// display the current internal calibration values
gfx_Set(SCREEN_MODE,LANDSCAPE);
txt_MoveCursor(5,1);
print("Current Values");
print("\nTOUCH_XMINCAL=",[HEX] peekW(TOUCH_XMINCAL));
print("\nTOUCH_YMINCAL=",[HEX] peekW(TOUCH_YMINCAL));
print("\nTOUCH_XMAXCAL=",[HEX] peekW(TOUCH_XMAXCAL));
print("\nTOUCH_YMAXCAL=",[HEX] peekW(TOUCH_YMAXCAL));
gfx_Set(SCREEN_MODE,PORTRAIT);
// SAMPLE TLC
getDot1();
x1 := X1;
y1 := Y1;
pause(200);
// SAMPLE BRC
getDot2();
x2 := X1;
y2 := Y1;
// work out the ratiometric span
Xload := (1024 - x2) / 60;
Yload := (1024 - y2) / 80;
Xspan := x2-x1;
Yspan := y2-y1;
x1 -= Xspan/10;
x2 += Xspan/10;
y1 -= Yspan/10;
y2 += Yspan/10;
x1 -= Xload;
y1 -= Yload;
x2 += Xload;
y2 += Yload;
// poke the values
pokeW(TOUCH_XMINCAL, x1);
pokeW(TOUCH_YMINCAL, y1);
pokeW(TOUCH_XMAXCAL, x2);
pokeW(TOUCH_YMAXCAL, y2);
gfx_Set(SCREEN_MODE,LANDSCAPE);
// display new values
txt_MoveCursor(10,1);
print("New Values");
print("\nTOUCH_XMINCAL=",[HEX] peekW(TOUCH_XMINCAL));
print("\nTOUCH_YMINCAL=",[HEX] peekW(TOUCH_YMINCAL));
print("\nTOUCH_XMAXCAL=",[HEX] peekW(TOUCH_XMAXCAL));
print("\nTOUCH_YMAXCAL=",[HEX] peekW(TOUCH_YMAXCAL));
// save the touch settings back to non-volatile storage
if(sys_StoreTouchCalibration()) // returns true if the values were acceptable and were stored successfully
txt_FGcolour(YELLOW);
print("\nACCEPTED ");
else
txt_FGcolour(RED);
print("\nFAILED - REDO"); // nup, failed, do it again
goto redo;
endif
gfx_Set(SCREEN_MODE,PORTRAIT);
// ok success, do simple drawing function
// so you can test the alignmemn. Best way
// is to draw over the top of the tartgets.
draw();
// finished drawing
gfx_Cls();
txt_FGcolour(YELLOW);
gfx_Set(SCREEN_MODE,LANDSCAPE);
print("\nCalibration complete.");
print("\nTouch to exit...");
gfx_Set(SCREEN_MODE,PORTRAIT);
while(touch_Get(TOUCH_STATUS) != TOUCH_PRESSED); // we'll wait for a touch
SystemReset();
forever
endfunc
func draw()
var firstx, firsty, x, y, state;
txt_FGcolour(CYAN);
gfx_Set(SCREEN_MODE,LANDSCAPE);
gfx_MoveTo(2,2);
print("NOW DRAW ");
print("TO TEST....");
gfx_Set(SCREEN_MODE,PORTRAIT);
while(touch_Get(TOUCH_STATUS) != TOUCH_PRESSED); // we'll wait for a touch
gfx_CircleFilled(120,160,5,RED);
gfx_Circle(120,160,6,YELLOW);
firstx := touch_Get(TOUCH_GETX); // so we can get the first point
firsty := touch_Get(TOUCH_GETY);
txt_FGcolour(RED);
while(1)
gfx_MoveTo(173,115);
if(peekW(SYSTEM_TIMER_LO) & 512) // the flashing exit button
gfx_Set(SCREEN_MODE,LANDSCAPE);
print("<<< Exit");
gfx_Set(SCREEN_MODE,PORTRAIT);
else
gfx_Set(SCREEN_MODE,LANDSCAPE);
print(" ");
gfx_Set(SCREEN_MODE,PORTRAIT);
endif
state := touch_Get(TOUCH_STATUS); // look for any touch activity
x := touch_Get(TOUCH_GETX); // grab the x
y := touch_Get(TOUCH_GETY); // and the y coordinates of the touch
if(state == TOUCH_PRESSED) // if there's a press
if(x > 116 && x < 124 && y > 156 && y < 164) break; // exit button
firstx := x;
firsty := y;
endif
if(state == TOUCH_MOVING) // if there's movement
//gfx_PutPixel(x, y, LIGHTGREEN); // we'll could draw a pixel
gfx_Line(firstx, firsty, x, y, LIGHTGREEN); // but lines are much better
firstx := x;
firsty := y;
endif
wend
endfunc