altos/draw: Add a reasonable API for drawing, add lines.
[fw/altos] / src / stm-vga / ao_demo.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_event.h>
22 #include <ao_quadrature.h>
23 #include <ao_button.h>
24 #include <ao_boot.h>
25 #include <ao_vga.h>
26
27 struct ao_task ball_task;
28
29 #define BALL_WIDTH      5
30 #define BALL_HEIGHT     5
31
32 static int      ball_x;
33 static int      ball_y;
34 static int      ball_dx, ball_dy;
35
36 uint8_t         ball_enable;
37
38 void
39 ao_ball(void)
40 {
41         ball_dx = 1;
42         ball_dy = 1;
43         ball_x = 0;
44         ball_y = 0;
45         for (;;) {
46                 while (!ball_enable)
47                         ao_sleep(&ball_enable);
48                 for (;;) {
49                         ao_line(&ao_vga_bitmap,
50                                 -100, -100, ball_x*2, ball_y*2,
51                                 1, AO_XOR);
52                         ao_text(&ao_vga_bitmap,
53                                 ball_x, ball_y - 10,
54                                 "Hello, Bdale!",
55                                 1, AO_XOR);
56                         ao_rect(&ao_vga_bitmap,
57                                 ball_x, ball_y,
58                                 BALL_WIDTH,
59                                 BALL_HEIGHT,
60                                 1,
61                                 AO_XOR);
62                         ao_delay(AO_MS_TO_TICKS(10));
63                         ao_rect(&ao_vga_bitmap,
64                                 ball_x, ball_y,
65                                 BALL_WIDTH,
66                                 BALL_HEIGHT,
67                                 1,
68                                 AO_XOR);
69                         ao_text(&ao_vga_bitmap,
70                                 ball_x, ball_y - 10,
71                                 "Hello, Bdale!",
72                                 1, AO_XOR);
73                         ao_line(&ao_vga_bitmap,
74                                 -100, -100, ball_x*2, ball_y*2,
75                                 1, AO_XOR);
76                         if (!ball_enable)
77                                 break;
78                         ball_x += ball_dx;
79                         ball_y += ball_dy;
80                         if (ball_x + BALL_WIDTH > AO_VGA_WIDTH) {
81                                 ball_x = AO_VGA_WIDTH - BALL_WIDTH;
82                                 ball_dx = -ball_dx;
83                         }
84                         if (ball_x < 0) {
85                                 ball_x = -ball_x;
86                                 ball_dx = -ball_dx;
87                         }
88                         if (ball_y + BALL_HEIGHT > AO_VGA_HEIGHT) {
89                                 ball_y = AO_VGA_HEIGHT - BALL_HEIGHT;
90                                 ball_dy = -ball_dy;
91                         }
92                         if (ball_y < 0) {
93                                 ball_y = -ball_y;
94                                 ball_dy = -ball_dy;
95                         }
96                 }
97         }
98 }
99
100 static void
101 ao_fb_init(void)
102 {
103         ao_rect(&ao_vga_bitmap,
104                 0, 0, AO_VGA_WIDTH, AO_VGA_HEIGHT,
105                 1, AO_COPY);
106
107         ao_rect(&ao_vga_bitmap,
108                 10, 10, 10, 10,
109                 0, AO_COPY);
110
111         ao_rect(&ao_vga_bitmap,
112                 AO_VGA_WIDTH - 20, 10, 10, 10,
113                 0, AO_COPY);
114
115         ao_rect(&ao_vga_bitmap,
116                 10, AO_VGA_HEIGHT - 20, 10, 10,
117                 0, AO_COPY);
118
119         ao_rect(&ao_vga_bitmap,
120                 AO_VGA_WIDTH - 20, AO_VGA_HEIGHT - 20, 10, 10,
121                 0, AO_COPY);
122
123         ao_text(&ao_vga_bitmap,
124                 20, 100,
125                 "Hello, Bdale!",
126                 0, AO_COPY);
127
128         ao_text(&ao_vga_bitmap,
129                 1, ao_font.ascent,
130                 "UL",
131                 0, AO_COPY);
132
133         ao_text(&ao_vga_bitmap,
134                 1, AO_VGA_HEIGHT - ao_font.descent,
135                 "BL",
136                 0, AO_COPY);
137 }
138
139 static void
140 ao_video_toggle(void)
141 {
142         ao_cmd_decimal();
143         if (ao_cmd_lex_i)
144                 ao_fb_init();
145         ao_vga_enable(ao_cmd_lex_i);
146 }
147
148 static void
149 ao_ball_toggle(void)
150 {
151         ao_cmd_decimal();
152         ball_enable = ao_cmd_lex_i;
153         ao_wakeup(&ball_enable);
154 }
155
156 __code struct ao_cmds ao_demo_cmds[] = {
157         { ao_video_toggle, "V\0Toggle video" },
158         { ao_ball_toggle, "B\0Toggle ball" },
159         { 0, NULL }
160 };
161
162 int
163 main(void)
164 {
165         ao_clock_init();
166
167         ao_task_init();
168
169         ao_led_init(LEDS_AVAILABLE);
170         ao_led_on(AO_LED_GREEN);
171         ao_led_off(AO_LED_BLUE);
172         ao_timer_init();
173         ao_dma_init();
174         ao_cmd_init();
175         ao_vga_init();
176         ao_usb_init();
177
178         ao_add_task(&ball_task, ao_ball, "ball");
179         ao_cmd_register(&ao_demo_cmds[0]);
180
181         ao_start_scheduler();
182         return 0;
183 }