cda889c6eef25c0eeb43d39a0bb19d36bb5e7707
[fw/altos] / src / nucleao-32 / ao_nucleo.c
1 /*
2  * Copyright © 2016 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
15 #include <ao.h>
16
17 static uint16_t blink_delay, blink_running;
18
19 static void blink(void) {
20         blink_running = 1;
21         while (blink_delay) {
22                 ao_led_on(AO_LED_GREEN);
23                 ao_delay(blink_delay);
24                 ao_led_off(AO_LED_GREEN);
25                 ao_delay(blink_delay);
26         }
27         blink_running = 0;
28         ao_wakeup(&blink_running);
29         ao_exit();
30 }
31
32 struct ao_task blink_task;
33
34 static void blink_cmd() {
35         ao_cmd_decimal();
36         blink_delay = ao_cmd_lex_i;
37         if (blink_delay && !blink_running)
38                 ao_add_task(&blink_task, blink, "blink");
39         if (!blink_delay)
40                 while (blink_running)
41                         ao_sleep(&blink_running);
42 }
43
44 static const struct ao_cmds blink_cmds[] = {
45         { blink_cmd,    "b <delay, 0 off>\0Blink the green LED" },
46         { 0, 0 }
47 };
48
49 void main(void)
50 {
51         ao_led_init(LEDS_AVAILABLE);
52         ao_clock_init();
53         ao_task_init();
54         ao_timer_init();
55         ao_dma_init();
56         ao_usb_init();
57         ao_serial_init();
58         ao_cmd_init();
59         ao_cmd_register(blink_cmds);
60         ao_start_scheduler();
61 }
62
63