use red LED to indicate system startup
[fw/altos] / ao_test.c
1 /*
2  * Copyright © 2009 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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include "ao.h"
19
20 struct ao_task __xdata blink_0_task;
21 struct ao_task __xdata blink_1_task;
22 struct ao_task __xdata wakeup_task;
23 struct ao_task __xdata beep_task;
24 struct ao_task __xdata echo_task;
25
26 void delay(int n) __reentrant
27 {
28         uint8_t j = 0;
29         while (--n)
30                 while (--j)
31                         ao_yield();
32 }
33
34 static __xdata uint8_t blink_chan;
35
36 void
37 blink_0(void)
38 {
39         uint8_t b = 0;
40         for (;;) {
41                 b = 1 - b;
42                 if (b)
43                         ao_led_on(AO_LED_GREEN);
44                 else
45                         ao_led_off(AO_LED_GREEN);
46                 ao_sleep(&blink_chan);
47         }
48 }
49
50 void
51 blink_1(void)
52 {
53         static __xdata struct ao_adc adc;
54
55         for (;;) {
56                 ao_sleep(&ao_adc_ring);
57                 ao_adc_get(&adc);
58                 if (adc.accel < 15900)
59                         ao_led_on(AO_LED_RED);
60                 else
61                         ao_led_off(AO_LED_RED);
62         }
63 }
64
65 void
66 wakeup(void)
67 {
68         for (;;) {
69                 ao_delay(AO_MS_TO_TICKS(100));
70                 ao_wakeup(&blink_chan);
71         }
72 }
73
74 void
75 beep(void)
76 {
77         static __xdata struct ao_adc adc;
78
79         for (;;) {
80                 ao_delay(AO_SEC_TO_TICKS(1));
81                 ao_adc_get(&adc);
82                 if (adc.temp > 7400)
83                         ao_beep_for(AO_BEEP_LOW, AO_MS_TO_TICKS(50));
84         }
85 }
86
87 void
88 echo(void)
89 {
90         uint8_t c;
91         for (;;) {
92                 ao_usb_flush();
93                 c = ao_usb_getchar();
94                 ao_usb_putchar(c);
95                 if (c == '\r')
96                         ao_usb_putchar('\n');
97         }
98 }
99
100 void
101 main(void)
102 {
103         CLKCON = 0;
104         while (!(SLEEP & SLEEP_XOSC_STB))
105                 ;
106
107 //      ao_add_task(&blink_0_task, blink_0);
108 //      ao_add_task(&blink_1_task, blink_1);
109 //      ao_add_task(&wakeup_task, wakeup);
110 //      ao_add_task(&beep_task, beep);
111         ao_add_task(&echo_task, echo);
112         ao_timer_init();
113         ao_adc_init();
114         ao_beep_init();
115         ao_led_init();
116         ao_usb_init();
117         
118         ao_start_scheduler();
119 }