1b443b1fc387df62ad135b809741268e12d0f97f
[fw/altos] / src / stm-vga / ao_demo.c
1 /*
2  * Copyright © 2011 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include "ao.h"
20 #include <ao_exti.h>
21 #include <ao_event.h>
22 #include <ao_quadrature.h>
23 #include <ao_button.h>
24 #include <ao_boot.h>
25 #include <ao_vga.h>
26 #include <ao_ps2.h>
27 #include <ao_console.h>
28
29 struct ao_task ball_task;
30 struct ao_task ps2_task;
31
32 #define BALL_WIDTH      5
33 #define BALL_HEIGHT     5
34
35 static int      ball_x;
36 static int      ball_y;
37 static int      ball_dx, ball_dy;
38
39 uint8_t         ball_enable;
40
41 void
42 ao_ball(void)
43 {
44         ball_dx = 1;
45         ball_dy = 1;
46         ball_x = 0;
47         ball_y = 0;
48         for (;;) {
49                 while (!ball_enable)
50                         ao_sleep(&ball_enable);
51                 for (;;) {
52                         ao_line(&ao_vga_bitmap,
53                                 -100, -100, ball_x*2, ball_y*2,
54                                 1, AO_XOR);
55                         ao_text(&ao_vga_bitmap,
56                                 ball_x, ball_y - 10,
57                                 "Hello, Bdale!",
58                                 1, AO_XOR);
59                         ao_rect(&ao_vga_bitmap,
60                                 ball_x, ball_y,
61                                 BALL_WIDTH,
62                                 BALL_HEIGHT,
63                                 1,
64                                 AO_XOR);
65                         ao_delay(AO_MS_TO_TICKS(10));
66                         ao_rect(&ao_vga_bitmap,
67                                 ball_x, ball_y,
68                                 BALL_WIDTH,
69                                 BALL_HEIGHT,
70                                 1,
71                                 AO_XOR);
72                         ao_text(&ao_vga_bitmap,
73                                 ball_x, ball_y - 10,
74                                 "Hello, Bdale!",
75                                 1, AO_XOR);
76                         ao_line(&ao_vga_bitmap,
77                                 -100, -100, ball_x*2, ball_y*2,
78                                 1, AO_XOR);
79                         if (!ball_enable)
80                                 break;
81                         ball_x += ball_dx;
82                         ball_y += ball_dy;
83                         if (ball_x + BALL_WIDTH > AO_VGA_WIDTH) {
84                                 ball_x = AO_VGA_WIDTH - BALL_WIDTH;
85                                 ball_dx = -ball_dx;
86                         }
87                         if (ball_x < 0) {
88                                 ball_x = -ball_x;
89                                 ball_dx = -ball_dx;
90                         }
91                         if (ball_y + BALL_HEIGHT > AO_VGA_HEIGHT) {
92                                 ball_y = AO_VGA_HEIGHT - BALL_HEIGHT;
93                                 ball_dy = -ball_dy;
94                         }
95                         if (ball_y < 0) {
96                                 ball_y = -ball_y;
97                                 ball_dy = -ball_dy;
98                         }
99                 }
100         }
101 }
102
103 void
104 ao_ps2(void)
105 {
106         uint8_t leds = 0;
107         for (;;) {
108                 uint8_t b = ao_ps2_get();
109                 printf ("%02x\n", b);
110                 flush();
111                 if (b == 0x14) {
112                         leds ^= 4;
113                         ao_ps2_put(0xed);
114                         if (ao_ps2_get() == 0xfa)
115                                 ao_ps2_put(leds);
116                 }
117         }
118 }
119
120 static void
121 ao_fb_init(void)
122 {
123         ao_rect(&ao_vga_bitmap,
124                 0, 0, AO_VGA_WIDTH, AO_VGA_HEIGHT,
125                 1, AO_COPY);
126
127         ao_rect(&ao_vga_bitmap,
128                 10, 10, 10, 10,
129                 0, AO_COPY);
130
131         ao_rect(&ao_vga_bitmap,
132                 AO_VGA_WIDTH - 20, 10, 10, 10,
133                 0, AO_COPY);
134
135         ao_rect(&ao_vga_bitmap,
136                 10, AO_VGA_HEIGHT - 20, 10, 10,
137                 0, AO_COPY);
138
139         ao_rect(&ao_vga_bitmap,
140                 AO_VGA_WIDTH - 20, AO_VGA_HEIGHT - 20, 10, 10,
141                 0, AO_COPY);
142
143         ao_text(&ao_vga_bitmap,
144                 20, 100,
145                 "Hello, Bdale!",
146                 0, AO_COPY);
147
148         ao_text(&ao_vga_bitmap,
149                 1, ao_font.ascent,
150                 "UL",
151                 0, AO_COPY);
152
153         ao_text(&ao_vga_bitmap,
154                 1, AO_VGA_HEIGHT - ao_font.descent,
155                 "BL",
156                 0, AO_COPY);
157 }
158
159 static void
160 ao_video_toggle(void)
161 {
162         ao_cmd_decimal();
163         if (ao_cmd_lex_i)
164                 ao_fb_init();
165         ao_vga_enable(ao_cmd_lex_i);
166 }
167
168 static void
169 ao_ball_toggle(void)
170 {
171         ao_cmd_decimal();
172         ball_enable = ao_cmd_lex_i;
173         ao_wakeup(&ball_enable);
174 }
175
176 static void
177 ao_ps2_read_keys(void)
178 {
179         char    c;
180
181         for (;;) {
182                 c = ao_ps2_getchar();
183                 printf("%02x %c\n", c, ' ' <= c && c < 0x7f ? c : '.');
184                 flush();
185                 if (c == ' ')
186                         break;
187         }
188 }
189
190 static void
191 ao_console_send(void)
192 {
193         char    c;
194
195         while ((c = getchar()) != '~') {
196                 ao_console_putchar(c);
197                 flush();
198         }
199 }
200
201 __code struct ao_cmds ao_demo_cmds[] = {
202         { ao_video_toggle, "V\0Toggle video" },
203         { ao_ball_toggle, "B\0Toggle ball" },
204         { ao_ps2_read_keys, "K\0Read keys from keyboard" },
205         { ao_console_send, "C\0Send data to console, end with ~" },
206         { 0, NULL }
207 };
208
209 int
210 main(void)
211 {
212         ao_clock_init();
213
214         ao_task_init();
215
216         ao_led_init(LEDS_AVAILABLE);
217         ao_led_on(AO_LED_GREEN);
218         ao_led_off(AO_LED_BLUE);
219         ao_timer_init();
220         ao_dma_init();
221         ao_cmd_init();
222         ao_vga_init();
223         ao_usb_init();
224         ao_exti_init();
225         ao_ps2_init();
226         ao_console_init();
227
228         ao_add_task(&ball_task, ao_ball, "ball");
229         ao_cmd_register(&ao_demo_cmds[0]);
230
231         ao_start_scheduler();
232         return 0;
233 }