]> rtime.felk.cvut.cz Git - eurobot/public.git/blob - src/disp-4dgl/control_screen.4dg
disp: added main fsm state vizualization
[eurobot/public.git] / src / disp-4dgl / control_screen.4dg
1 #platform "uOLED-32028-PMD3T"
2
3 #inherit "4DGL_16bitColours.fnc"
4 //#inherit "4DGL-System.fnc"
5 \r
6 #constant MSG_BUFF_LEN 10\r
7 \r
8 #constant BT_LIFT_CALIB 0\r
9 #constant BT_PUSHER_IN 1\r
10 #constant BT_PUSHER_OUT 2\r
11 #constant BT_LIFT_UP 3\r
12 #constant BT_LIFT_DOWN 4\r
13 #constant BT_CMU_INIT 5\r
14 #constant BT_START 6\r
15 #constant BT_COLOR 7\r
16 #constant BT_INIT 9\r
17 #constant BT_END 10\r
18 #constant BT_RESTART 11\r
19 \r
20 #constant X_OFFSET 0\r
21 #constant Y_OFFSET 4\r
22 \r
23 #constant MSG_START 150\r
24 #constant MSG_TERM_LF 10\r
25 \r
26 #constant MSG_LIFT_CAL 151\r
27 #constant MSG_PUSHER_IN 152\r
28 #constant MSG_PUSHER_OUT 153\r
29 #constant MSG_LIFT_UP 154\r
30 #constant MSG_LIFT_DOWN 155\r
31 #constant MSG_CMU_INIT 156\r
32 #constant MSG_START_ACT 157\r
33 #constant MSG_COLOR 158\r
34 #constant MSG_INIT 160\r
35 #constant MSG_STATUS_REQ 183\r
36 #constant MSG_RESTART 162\r
37 #constant MSG_SWITCH_TO_CONTROL_DONE 186\r
38 \r
39 #constant COLOR_RED 1\r
40 #constant COLOR_GREEN 0\r
41 \r
42 var msg_buff[MSG_BUFF_LEN];\r
43 var msg_rcvd[MSG_BUFF_LEN];\r
44 var bt_y[20];\r
45 var bt_x[20];\r
46 var out_buff[4];\r
47 var msg_len;\r
48 var current_color;\r
49 var button_color;\r
50 \r
51 func send_msg()\r
52     var idx;\r
53     idx := 0;\r
54     \r
55     repeat\r
56         serout(out_buff[idx]);\r
57         idx++;\r
58     until(idx==msg_len)\r
59 endfunc\r
60 \r
61 func act_lift_calib()\r
62     out_buff[0]:=MSG_START;\r
63     out_buff[1]:=MSG_LIFT_CAL;\r
64     out_buff[2]:=MSG_TERM_LF;\r
65     msg_len:=3;\r
66     send_msg();\r
67 endfunc\r
68 \r
69 func act_pusher_in()\r
70     out_buff[0]:=MSG_START;\r
71     out_buff[1]:=MSG_PUSHER_IN;\r
72     out_buff[2]:=MSG_TERM_LF;\r
73     msg_len:=3;\r
74     send_msg();\r
75 endfunc\r
76 \r
77 func act_pusher_out()\r
78     out_buff[0]:=MSG_START;\r
79     out_buff[1]:=MSG_PUSHER_OUT;\r
80     out_buff[2]:=MSG_TERM_LF;\r
81     msg_len:=3;\r
82     send_msg();\r
83 endfunc\r
84 \r
85 func act_lift_up()\r
86     out_buff[0]:=MSG_START;\r
87     out_buff[1]:=MSG_LIFT_UP;\r
88     out_buff[2]:=MSG_TERM_LF;\r
89     msg_len:=3;\r
90     send_msg();\r
91 endfunc\r
92 \r
93 func act_lift_down()\r
94     out_buff[0]:=MSG_START;\r
95     out_buff[1]:=MSG_LIFT_DOWN;\r
96     out_buff[2]:=MSG_TERM_LF;\r
97     msg_len:=3;\r
98     send_msg();\r
99 endfunc\r
100 \r
101 func act_cmu_init()\r
102     out_buff[0]:=MSG_START;\r
103     out_buff[1]:=MSG_CMU_INIT;\r
104     out_buff[2]:=MSG_TERM_LF;\r
105     msg_len:=3;\r
106     send_msg();\r
107 endfunc\r
108 \r
109 func act_start()\r
110     out_buff[0]:=MSG_START;\r
111     out_buff[1]:=MSG_START_ACT;\r
112     out_buff[2]:=MSG_TERM_LF;\r
113     msg_len:=3;\r
114     send_msg();\r
115 endfunc\r
116 \r
117 func act_init()\r
118     out_buff[0]:=MSG_START;\r
119     out_buff[1]:=MSG_INIT;\r
120     out_buff[2]:=MSG_TERM_LF;\r
121     msg_len:=3;\r
122     send_msg();\r
123 endfunc\r
124 \r
125 func act_status()\r
126     out_buff[0]:=MSG_START;\r
127     out_buff[1]:=MSG_STATUS_REQ;\r
128     out_buff[2]:=MSG_TERM_LF;\r
129     msg_len:=3;\r
130     send_msg();\r
131 endfunc\r
132 \r
133 func act_restart()\r
134     out_buff[0]:=MSG_START;\r
135     out_buff[1]:=MSG_RESTART;\r
136     out_buff[2]:=MSG_TERM_LF;\r
137     msg_len:=3;\r
138     send_msg();\r
139 endfunc\r
140 \r
141 func act_color()\r
142     if(current_color==COLOR_RED)\r
143         current_color := COLOR_GREEN;\r
144         button_color := GREEN;\r
145     else\r
146         if(current_color==COLOR_GREEN)\r
147             current_color := COLOR_RED;\r
148             button_color := RED;\r
149         endif\r
150     endif\r
151 \r
152     out_buff[0]:=MSG_START;\r
153     out_buff[1]:=MSG_COLOR;\r
154     out_buff[2]:=current_color;\r
155     out_buff[3]:=10;\r
156     msg_len:=4;\r
157     send_msg();\r
158 endfunc\r
159 \r
160 func lift_calib_bt(var state)\r
161     gfx_Button(state, X_OFFSET, bt_y[BT_LIFT_CALIB], BLUE, YELLOW, FONT2, 2, 2, "   LIFT CAL   ");\r
162 endfunc\r
163 \r
164 func pusher_in_bt(var state)\r
165     gfx_Button(state, X_OFFSET, bt_y[BT_PUSHER_IN], BLUE, YELLOW, FONT2, 2, 2, "PSH IN");\r
166 endfunc\r
167 \r
168 func pusher_out_bt(var state)\r
169     gfx_Button(state, bt_x[BT_PUSHER_OUT], bt_y[BT_PUSHER_IN], BLUE, YELLOW, FONT2, 2, 2, "PSH OUT");\r
170 endfunc\r
171 \r
172 func lift_up_bt(var state)\r
173     gfx_Button(state, X_OFFSET, bt_y[BT_LIFT_UP], BLUE, YELLOW, FONT2, 2, 2, "LFT UP");\r
174 endfunc\r
175 \r
176 func lift_down_bt(var state)\r
177     gfx_Button(state, bt_x[BT_LIFT_DOWN], bt_y[BT_LIFT_UP], BLUE, YELLOW, FONT2, 2, 2, "LFT DWN");\r
178 endfunc\r
179 \r
180 func cmu_init_bt(var state)\r
181     gfx_Button(state, X_OFFSET, bt_y[BT_CMU_INIT], BLUE, YELLOW, FONT2, 2, 2, "   CMU INIT   ");\r
182 endfunc\r
183 \r
184 func start_bt(var state)\r
185     gfx_Button(state, X_OFFSET, bt_y[BT_START], ORANGE, WHITE, FONT2, 2, 2, "   !!START!!  ");\r
186 endfunc\r
187 \r
188 func color_bt(var state)\r
189     gfx_Button(state, X_OFFSET, bt_y[BT_COLOR], button_color, YELLOW, FONT2, 2, 2, "     COLOR    ");\r
190 endfunc\r
191 \r
192 func restart_bt(var state)\r
193     gfx_Button(state, X_OFFSET, bt_y[BT_RESTART], GRAY, YELLOW, FONT2, 2, 2, "  RESTART ALL ");\r
194 endfunc\r
195 \r
196 func init_bt(var state)\r
197     gfx_Button(state, X_OFFSET, bt_y[BT_INIT], GRAY, YELLOW, FONT2, 2, 2, "     EXIT     ");\r
198 endfunc\r
199 \r
200 func status_bt(var state)\r
201     gfx_Button(state, 0, 290, GRAY, WHITE, FONT3, 1, 1, "            STATUS          ");\r
202 endfunc\r
203 \r
204 \r
205 func draw_control()\r
206     bt_y[BT_LIFT_CALIB]:=Y_OFFSET;\r
207     lift_calib_bt(UP);\r
208     \r
209     bt_y[BT_PUSHER_IN]:=gfx_Get(3)+4;\r
210     pusher_in_bt(UP);\r
211     \r
212     bt_x[BT_PUSHER_OUT]:=gfx_Get(2)+4;\r
213     bt_y[BT_PUSHER_OUT]:=gfx_Get(3)+4;\r
214     pusher_out_bt(UP);\r
215     \r
216     bt_y[BT_LIFT_UP]:=gfx_Get(3)+4;\r
217     lift_up_bt(UP);\r
218     \r
219     bt_x[BT_LIFT_DOWN]:=gfx_Get(2)+4;\r
220     bt_y[BT_LIFT_DOWN]:=gfx_Get(3)+4;\r
221     lift_down_bt(UP);\r
222     \r
223     bt_y[BT_CMU_INIT]:=gfx_Get(3)+4;\r
224     cmu_init_bt(UP);\r
225 \r
226     bt_y[BT_START]:=gfx_Get(3)+4;\r
227     start_bt(UP);\r
228     \r
229     bt_y[BT_COLOR]:=gfx_Get(3)+4;\r
230     color_bt(UP);\r
231 \r
232     bt_y[BT_RESTART]:=gfx_Get(3)+4;\r
233     restart_bt(UP);\r
234     \r
235     bt_y[BT_INIT]:=gfx_Get(3)+4;\r
236     init_bt(UP);\r
237     \r
238     bt_y[BT_END]:=gfx_Get(3);\r
239     \r
240     status_bt(UP);\r
241 endfunc\r
242 \r
243 func touchscreen()\r
244 var col, state, x, y;\r
245     state := touch_Get(TOUCH_STATUS);               // get touchscreen status\r
246         \r
247         if(state == NOTOUCH)\r
248             return;\r
249         endif\r
250         //-----------------------------------------------------------------------------------------\r
251         if(state == TOUCH_PRESS)                        // if there's a press\r
252             x := touch_Get(TOUCH_GETX);                          \r
253             y := touch_Get(TOUCH_GETY);\r
254             \r
255             if((y > bt_y[BT_LIFT_CALIB])&&(y<bt_y[BT_PUSHER_IN]))\r
256                 lift_calib_bt(DOWN);\r
257             endif            \r
258             if((y > bt_y[BT_PUSHER_IN])&&(y<bt_y[BT_PUSHER_OUT]))\r
259                 if(x < bt_x[BT_PUSHER_OUT])\r
260                     pusher_in_bt(DOWN);\r
261                 else\r
262                     pusher_out_bt(DOWN);\r
263                 endif\r
264             endif            \r
265             if((y > bt_y[BT_LIFT_UP])&&(y<bt_y[BT_LIFT_DOWN]))\r
266                 if(x < bt_x[BT_LIFT_DOWN])\r
267                     lift_up_bt(DOWN);\r
268                 else\r
269                     lift_down_bt(DOWN);\r
270                 endif\r
271             endif            \r
272             if((y > bt_y[BT_CMU_INIT])&&(y<bt_y[BT_START]))\r
273                 cmu_init_bt(DOWN);\r
274             endif            \r
275             if((y > bt_y[BT_START])&&(y<bt_y[BT_COLOR]))\r
276                 start_bt(DOWN);\r
277             endif            \r
278             if((y > bt_y[BT_COLOR])&&(y<bt_y[BT_RESTART]))\r
279                 color_bt(DOWN);\r
280             endif            \r
281             if((y > bt_y[BT_RESTART])&&(y<bt_y[BT_INIT]))\r
282                 restart_bt(DOWN);\r
283             endif            \r
284             if((y > bt_y[BT_INIT])&&(y<bt_y[BT_END]))\r
285                 init_bt(DOWN);\r
286             endif            \r
287             if((y > 290))\r
288                 status_bt(DOWN);\r
289             endif            \r
290 \r
291         endif\r
292         if(state == TOUCH_RELEASE)                        // if there's a press\r
293             x := touch_Get(TOUCH_GETX);                          \r
294             y := touch_Get(TOUCH_GETY);\r
295             if((y > bt_y[BT_LIFT_CALIB])&&(y<bt_y[BT_PUSHER_IN]))\r
296                 lift_calib_bt(UP);\r
297                 act_lift_calib();\r
298             endif            \r
299             if((y > bt_y[BT_PUSHER_IN])&&(y<bt_y[BT_PUSHER_OUT]))\r
300                 if(x < bt_x[BT_PUSHER_OUT])\r
301                     pusher_in_bt(UP);\r
302                     act_pusher_in();\r
303                 else\r
304                     pusher_out_bt(UP);\r
305                     act_pusher_out();\r
306                 endif\r
307             endif            \r
308             if((y > bt_y[BT_LIFT_UP])&&(y<bt_y[BT_LIFT_DOWN]))\r
309                 if(x < bt_x[BT_LIFT_DOWN])\r
310                     lift_up_bt(UP);\r
311                     act_lift_up();\r
312                 else\r
313                     lift_down_bt(UP);\r
314                     act_lift_down();\r
315                 endif\r
316             endif            \r
317             if((y > bt_y[BT_LIFT_DOWN])&&(y<bt_y[BT_CMU_INIT]))\r
318                 lift_down_bt(UP);\r
319                 act_lift_down();\r
320             endif            \r
321             if((y > bt_y[BT_CMU_INIT])&&(y<bt_y[BT_START]))\r
322                 cmu_init_bt(UP);\r
323                 act_cmu_init();\r
324             endif            \r
325             if((y > bt_y[BT_START])&&(y<bt_y[BT_COLOR]))\r
326                 start_bt(UP);\r
327                 act_start();\r
328             endif       \r
329             if((y > bt_y[BT_COLOR])&&(y<bt_y[BT_RESTART]))\r
330                 act_color();\r
331                 color_bt(UP);                \r
332             endif       \r
333             if((y > bt_y[BT_RESTART])&&(y<bt_y[BT_INIT]))\r
334                 restart_bt(UP);\r
335                 act_restart();\r
336             endif       \r
337             if((y > bt_y[BT_INIT])&&(y<bt_y[BT_END]))\r
338                 init_bt(UP);\r
339                 act_init();\r
340             endif            \r
341             if((y > 290))\r
342                 status_bt(UP);\r
343                 act_status();\r
344             endif            \r
345         endif\r
346 endfunc\r
347 \r
348 func load_status()\r
349     file_Run("STATUS.4XE",0);\r
350 endfunc\r
351 \r
352 func process_msg()\r
353     if(msg_rcvd[0]==200)\r
354         if(msg_rcvd[2]==1)\r
355             load_status();\r
356         endif\r
357     endif\r
358     if(msg_rcvd[0]=='c')\r
359         if(msg_rcvd[1]==COLOR_RED)\r
360             current_color := COLOR_RED;\r
361             button_color := RED;\r
362         else\r
363             if(msg_rcvd[1]==COLOR_GREEN)\r
364                 current_color := COLOR_GREEN;\r
365                 button_color := GREEN;\r
366             endif\r
367         endif\r
368         color_bt(UP);\r
369     endif\r
370 endfunc\r
371 \r
372 func get_msg()\r
373     var in, idx, bad, msg_len;\r
374 \r
375     idx:=0;\r
376     bad:=0;    \r
377     in:=serin();\r
378     \r
379     repeat\r
380         in:=serin();\r
381         if(in!=-1)\r
382             msg_rcvd[idx] := in;\r
383             idx++;\r
384         else\r
385             break;\r
386         endif\r
387     until(idx==MSG_BUFF_LEN);\r
388 \r
389     msg_len := idx;\r
390     com_Init(msg_buff, MSG_BUFF_LEN, ':');\r
391 \r
392     process_msg();\r
393     return 0; \r
394 endfunc\r
395
396 func main()\r
397     var idx;\r
398     setbaud(8); // 19200 baud\r
399     com_Init(msg_buff, MSG_BUFF_LEN, ':');\r
400 \r
401     out_buff[0]:=MSG_START;\r
402     out_buff[1]:=MSG_SWITCH_TO_CONTROL_DONE;\r
403     out_buff[2]:=MSG_TERM_LF;\r
404     idx:=0;\r
405     repeat\r
406         serout(out_buff[idx]);\r
407         idx++;\r
408     until(idx==3)  \r
409 \r
410     touch_Set(TOUCH_ENABLE); // enable TS\r
411     touch_Set(TOUCH_REGIONDEFAULT); // reset touch area to fullscreen\r
412     \r
413     current_color:=COLOR_GREEN;\r
414     button_color:=GREEN;\r
415     draw_control();\r
416     repeat // the main application loop\r
417         touchscreen();\r
418         if(com_Full()==1)\r
419             get_msg();\r
420         endif\r
421     forever\r
422 endfunc\r
423 \r
424 \r
425 \r
426 \r
427 \r
428 \r
429 \r
430 \r
431 \r
432 \r