altos/stm-vga: Fix DMA reset to load scanline each time
[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_solid(AO_ALLONES,
50                                  AO_ALLONES,
51                                  ao_vga_fb + ball_y * AO_VGA_STRIDE,
52                                  AO_VGA_STRIDE,
53                                  ball_x,
54                                  BALL_WIDTH,
55                                  BALL_HEIGHT);
56                         ao_delay(AO_MS_TO_TICKS(10));
57                         ao_solid(AO_ALLONES,
58                                  AO_ALLONES,
59                                  ao_vga_fb + ball_y * AO_VGA_STRIDE,
60                                  AO_VGA_STRIDE,
61                                  ball_x,
62                                  BALL_WIDTH,
63                                  BALL_HEIGHT);
64                         if (!ball_enable)
65                                 break;
66                         ball_x += ball_dx;
67                         ball_y += ball_dy;
68                         if (ball_x + BALL_WIDTH > AO_VGA_WIDTH) {
69                                 ball_x = AO_VGA_WIDTH - BALL_WIDTH;
70                                 ball_dx = -ball_dx;
71                         }
72                         if (ball_x < 0) {
73                                 ball_x = -ball_x;
74                                 ball_dx = -ball_dx;
75                         }
76                         if (ball_y + BALL_HEIGHT > AO_VGA_HEIGHT) {
77                                 ball_y = AO_VGA_HEIGHT - BALL_HEIGHT;
78                                 ball_dy = -ball_dy;
79                         }
80                         if (ball_y < 0) {
81                                 ball_y = -ball_y;
82                                 ball_dy = -ball_dy;
83                         }
84                 }
85         }
86 }
87
88 static void
89 ao_video_toggle(void)
90 {
91         ao_cmd_decimal();
92         ao_vga_enable(ao_cmd_lex_i);
93 }
94
95 static void
96 ao_ball_toggle(void)
97 {
98         ao_cmd_decimal();
99         ball_enable = ao_cmd_lex_i;
100         ao_wakeup(&ball_enable);
101 }
102
103 __code struct ao_cmds ao_demo_cmds[] = {
104         { ao_video_toggle, "V\0Toggle video" },
105         { ao_ball_toggle, "B\0Toggle ball" },
106         { 0, NULL }
107 };
108
109 int
110 main(void)
111 {
112         ao_clock_init();
113
114         ao_task_init();
115
116         ao_led_init(LEDS_AVAILABLE);
117         ao_led_on(AO_LED_GREEN);
118         ao_led_off(AO_LED_BLUE);
119         ao_timer_init();
120         ao_dma_init();
121         ao_cmd_init();
122         ao_vga_init();
123         ao_usb_init();
124
125         ao_add_task(&ball_task, ao_ball, "ball");
126         ao_cmd_register(&ao_demo_cmds[0]);
127
128         ao_start_scheduler();
129         return 0;
130 }