cortexelf-v1: Add ps/2 and vga with graphics
[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
29 struct ao_task ball_task;
30
31 #define BALL_WIDTH      5
32 #define BALL_HEIGHT     5
33
34 static int      ball_x;
35 static int      ball_y;
36 static int      ball_dx, ball_dy;
37
38 uint8_t         ball_enable;
39
40 void
41 ao_ball(void)
42 {
43         ball_dx = 1;
44         ball_dy = 1;
45         ball_x = 0;
46         ball_y = 0;
47         for (;;) {
48                 while (!ball_enable)
49                         ao_sleep(&ball_enable);
50                 for (;;) {
51                         ao_line(&ao_vga_bitmap,
52                                 -100, -100, ball_x*2, ball_y*2,
53                                 1, AO_XOR);
54                         ao_text(&ao_vga_bitmap,
55                                 ball_x, ball_y - 10,
56                                 "Hello, Bdale!",
57                                 1, AO_XOR);
58                         ao_rect(&ao_vga_bitmap,
59                                 ball_x, ball_y,
60                                 BALL_WIDTH,
61                                 BALL_HEIGHT,
62                                 1,
63                                 AO_XOR);
64                         ao_delay(AO_MS_TO_TICKS(10));
65                         ao_rect(&ao_vga_bitmap,
66                                 ball_x, ball_y,
67                                 BALL_WIDTH,
68                                 BALL_HEIGHT,
69                                 1,
70                                 AO_XOR);
71                         ao_text(&ao_vga_bitmap,
72                                 ball_x, ball_y - 10,
73                                 "Hello, Bdale!",
74                                 1, AO_XOR);
75                         ao_line(&ao_vga_bitmap,
76                                 -100, -100, ball_x*2, ball_y*2,
77                                 1, AO_XOR);
78                         if (!ball_enable)
79                                 break;
80                         ball_x += ball_dx;
81                         ball_y += ball_dy;
82                         if (ball_x + BALL_WIDTH > AO_VGA_WIDTH) {
83                                 ball_x = AO_VGA_WIDTH - BALL_WIDTH;
84                                 ball_dx = -ball_dx;
85                         }
86                         if (ball_x < 0) {
87                                 ball_x = -ball_x;
88                                 ball_dx = -ball_dx;
89                         }
90                         if (ball_y + BALL_HEIGHT > AO_VGA_HEIGHT) {
91                                 ball_y = AO_VGA_HEIGHT - BALL_HEIGHT;
92                                 ball_dy = -ball_dy;
93                         }
94                         if (ball_y < 0) {
95                                 ball_y = -ball_y;
96                                 ball_dy = -ball_dy;
97                         }
98                 }
99         }
100 }
101
102 static void
103 ao_fb_init(void)
104 {
105         ao_rect(&ao_vga_bitmap,
106                 0, 0, AO_VGA_WIDTH, AO_VGA_HEIGHT,
107                 1, AO_COPY);
108
109         ao_rect(&ao_vga_bitmap,
110                 10, 10, 10, 10,
111                 0, AO_COPY);
112
113         ao_rect(&ao_vga_bitmap,
114                 AO_VGA_WIDTH - 20, 10, 10, 10,
115                 0, AO_COPY);
116
117         ao_rect(&ao_vga_bitmap,
118                 10, AO_VGA_HEIGHT - 20, 10, 10,
119                 0, AO_COPY);
120
121         ao_rect(&ao_vga_bitmap,
122                 AO_VGA_WIDTH - 20, AO_VGA_HEIGHT - 20, 10, 10,
123                 0, AO_COPY);
124
125         ao_text(&ao_vga_bitmap,
126                 20, 100,
127                 "Hello, Bdale!",
128                 0, AO_COPY);
129
130         ao_text(&ao_vga_bitmap,
131                 1, ao_font.ascent,
132                 "UL",
133                 0, AO_COPY);
134
135         ao_text(&ao_vga_bitmap,
136                 1, AO_VGA_HEIGHT - ao_font.descent,
137                 "BL",
138                 0, AO_COPY);
139 }
140
141 static void
142 ao_video_toggle(void)
143 {
144         ao_cmd_decimal();
145         if (ao_cmd_lex_i)
146                 ao_fb_init();
147         ao_vga_enable(ao_cmd_lex_i);
148 }
149
150 static void
151 ao_ball_toggle(void)
152 {
153         ao_cmd_decimal();
154         ball_enable = ao_cmd_lex_i;
155         ao_wakeup(&ball_enable);
156 }
157
158 static void
159 ao_ps2_read_keys(void)
160 {
161         char    c;
162
163         for (;;) {
164                 c = ao_ps2_getchar();
165                 printf("%02x %c\n", c, ' ' <= c && c < 0x7f ? c : '.');
166                 flush();
167                 if (c == ' ')
168                         break;
169         }
170 }
171
172 static void
173 ao_console_send(void)
174 {
175         char    c;
176
177         while ((c = getchar()) != '~') {
178                 ao_console_putchar(c);
179                 flush();
180         }
181 }
182
183 __code struct ao_cmds ao_demo_cmds[] = {
184         { ao_video_toggle, "V\0Toggle video" },
185         { ao_ball_toggle, "B\0Toggle ball" },
186         { ao_ps2_read_keys, "K\0Read keys from keyboard" },
187         { ao_console_send, "C\0Send data to console, end with ~" },
188         { 0, NULL }
189 };
190
191 int
192 main(void)
193 {
194         ao_clock_init();
195
196 #if HAS_STACK_GUARD
197         ao_mpu_init();
198 #endif
199
200         ao_task_init();
201         ao_serial_init();
202         ao_timer_init();
203
204         ao_spi_init();
205         ao_dma_init();
206         ao_exti_init();
207
208         ao_ps2_init();
209         ao_vga_init();
210         ao_console_init();
211
212         ao_cmd_init();
213
214         ao_usb_init();
215
216         ao_config_init();
217
218         ao_add_task(&ball_task, ao_ball, "ball");
219         ao_cmd_register(&ao_demo_cmds[0]);
220
221         ao_start_scheduler();
222         return 0;
223 }