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