altos/test: Adjust CRC error rate after FEC fix
[fw/altos] / src / cortexelf-v1 / ao_cortexelf.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_profile.h>
22 #if HAS_STACK_GUARD
23 #include <ao_mpu.h>
24 #endif
25 #include <ao_ps2.h>
26 #include <ao_vga.h>
27 #include <ao_console.h>
28 #include <ao_sdcard.h>
29 #include <ao_fat.h>
30 #include <ao_scheme.h>
31 #include <ao_button.h>
32 #include <ao_event.h>
33 #include <ao_as1107.h>
34 #include <ao_hex.h>
35 #include <ao_1802.h>
36
37 struct ao_task ball_task;
38
39 #define BALL_WIDTH      5
40 #define BALL_HEIGHT     5
41
42 static int      ball_x;
43 static int      ball_y;
44 static int      ball_dx, ball_dy;
45
46 uint8_t         ball_enable;
47
48 void
49 ao_ball(void)
50 {
51         ball_dx = 1;
52         ball_dy = 1;
53         ball_x = 0;
54         ball_y = 0;
55         for (;;) {
56                 while (!ball_enable)
57                         ao_sleep(&ball_enable);
58                 for (;;) {
59                         ao_line(&ao_vga_bitmap,
60                                 -100, -100, ball_x*2, ball_y*2,
61                                 1, AO_XOR);
62                         ao_text(&ao_vga_bitmap,
63                                 ball_x, ball_y - 10,
64                                 "Hello, Bdale!",
65                                 1, AO_XOR);
66                         ao_rect(&ao_vga_bitmap,
67                                 ball_x, ball_y,
68                                 BALL_WIDTH,
69                                 BALL_HEIGHT,
70                                 1,
71                                 AO_XOR);
72                         ao_delay(AO_MS_TO_TICKS(10));
73                         ao_rect(&ao_vga_bitmap,
74                                 ball_x, ball_y,
75                                 BALL_WIDTH,
76                                 BALL_HEIGHT,
77                                 1,
78                                 AO_XOR);
79                         ao_text(&ao_vga_bitmap,
80                                 ball_x, ball_y - 10,
81                                 "Hello, Bdale!",
82                                 1, AO_XOR);
83                         ao_line(&ao_vga_bitmap,
84                                 -100, -100, ball_x*2, ball_y*2,
85                                 1, AO_XOR);
86                         if (!ball_enable)
87                                 break;
88                         ball_x += ball_dx;
89                         ball_y += ball_dy;
90                         if (ball_x + BALL_WIDTH > AO_VGA_WIDTH) {
91                                 ball_x = AO_VGA_WIDTH - BALL_WIDTH;
92                                 ball_dx = -ball_dx;
93                         }
94                         if (ball_x < 0) {
95                                 ball_x = -ball_x;
96                                 ball_dx = -ball_dx;
97                         }
98                         if (ball_y + BALL_HEIGHT > AO_VGA_HEIGHT) {
99                                 ball_y = AO_VGA_HEIGHT - BALL_HEIGHT;
100                                 ball_dy = -ball_dy;
101                         }
102                         if (ball_y < 0) {
103                                 ball_y = -ball_y;
104                                 ball_dy = -ball_dy;
105                         }
106                 }
107         }
108 }
109
110 static void
111 ao_fb_init(void)
112 {
113         ao_rect(&ao_vga_bitmap,
114                 0, 0, AO_VGA_WIDTH, AO_VGA_HEIGHT,
115                 1, AO_COPY);
116
117         ao_rect(&ao_vga_bitmap,
118                 10, 10, 10, 10,
119                 0, AO_COPY);
120
121         ao_rect(&ao_vga_bitmap,
122                 AO_VGA_WIDTH - 20, 10, 10, 10,
123                 0, AO_COPY);
124
125         ao_rect(&ao_vga_bitmap,
126                 10, AO_VGA_HEIGHT - 20, 10, 10,
127                 0, AO_COPY);
128
129         ao_rect(&ao_vga_bitmap,
130                 AO_VGA_WIDTH - 20, AO_VGA_HEIGHT - 20, 10, 10,
131                 0, AO_COPY);
132
133         ao_text(&ao_vga_bitmap,
134                 20, 100,
135                 "Hello, Bdale!",
136                 0, AO_COPY);
137
138         ao_text(&ao_vga_bitmap,
139                 1, ao_font.ascent,
140                 "UL",
141                 0, AO_COPY);
142
143         ao_text(&ao_vga_bitmap,
144                 1, AO_VGA_HEIGHT - ao_font.descent,
145                 "BL",
146                 0, AO_COPY);
147 }
148
149 static void
150 ao_video_toggle(void)
151 {
152         uint16_t r = ao_cmd_decimal();
153         if (r)
154                 ao_fb_init();
155         ao_vga_enable(r)
156 }
157
158 static void
159 ao_ball_toggle(void)
160 {
161         ball_enable = ao_cmd_decimal();
162         ao_wakeup(&ball_enable);
163 }
164
165 static void
166 ao_ps2_read_keys(void)
167 {
168         char    c;
169
170         for (;;) {
171                 c = ao_ps2_getchar();
172                 printf("%02x %c\n", c, ' ' <= c && c < 0x7f ? c : '.');
173                 flush();
174                 if (c == ' ')
175                         break;
176         }
177 }
178
179 static void
180 ao_console_send(void)
181 {
182         char    c;
183
184         while ((c = getchar()) != '~') {
185                 ao_console_putchar(c);
186                 flush();
187         }
188 }
189
190 static void scheme_cmd() {
191         ao_scheme_read_eval_print();
192 }
193
194 static void
195 ao_serial_blather(void)
196 {
197         char c;
198
199         while ((c = getchar()) != '~') {
200                 ao_serial1_putchar(c);
201                 ao_serial2_putchar(c);
202         }
203 }
204
205 static void
206 led_cmd(void)
207 {
208         uint8_t start;
209         uint8_t value;
210
211         start = ao_cmd_decimal();
212         value = ao_cmd_hex();
213         if (ao_cmd_status != ao_cmd_success)
214                 return;
215         ao_as1107_write_8(start, value);
216 }
217
218 const struct ao_cmds ao_demo_cmds[] = {
219         { ao_video_toggle, "V\0Toggle video" },
220         { ao_ball_toggle, "B\0Toggle ball" },
221         { ao_ps2_read_keys, "K\0Read keys from keyboard" },
222         { ao_console_send, "C\0Send data to console, end with ~" },
223         { ao_serial_blather, "S\0Blather on serial ports briefly" },
224         { scheme_cmd, "l\0Run scheme interpreter" },
225         { led_cmd, "L start value\0Show value (byte) at digit start" },
226         { 0, NULL }
227 };
228
229 static struct ao_task event_task;
230
231 static void
232 ao_event_loop(void)
233 {
234         for (;;) {
235                 struct ao_event ev;
236
237                 ao_event_get(&ev);
238                 printf("type %d uint %d tick %d value %d\n",
239                        ev.type, ev.unit, ev.tick, ev.value);
240                 flush();
241         }
242 }
243
244 int
245 main(void)
246 {
247         ao_clock_init();
248
249 #if HAS_STACK_GUARD
250         ao_mpu_init();
251 #endif
252
253         ao_task_init();
254         ao_serial_init();
255         ao_timer_init();
256
257         ao_spi_init();
258         ao_dma_init();
259         ao_exti_init();
260
261         ao_sdcard_init();
262         ao_fat_init();
263
264         ao_ps2_init();
265         ao_vga_init();
266         ao_console_init();
267
268         ao_cmd_init();
269
270         ao_usb_init();
271
272         ao_button_init();
273
274         ao_as1107_init();
275         ao_matrix_init();
276         ao_1802_init();
277
278         ao_hex_init();
279
280         ao_config_init();
281
282         ao_add_task(&ball_task, ao_ball, "ball");
283         ao_add_task(&event_task, ao_event_loop, "events");
284         ao_cmd_register(&ao_demo_cmds[0]);
285
286         ao_start_scheduler();
287         return 0;
288 }