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