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