altos: Add lambdakey
[fw/altos] / src / lambdakey-v1.0 / ao_lambdakey.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 #include <ao_lisp.h>
17
18 static uint16_t blink_delay, blink_running;
19
20 static void blink(void) {
21         blink_running = 1;
22         while (blink_delay) {
23                 ao_led_on(AO_LED_RED);
24                 ao_delay(blink_delay);
25                 ao_led_off(AO_LED_RED);
26                 ao_delay(blink_delay);
27         }
28         blink_running = 0;
29         ao_wakeup(&blink_running);
30         ao_exit();
31 }
32
33 struct ao_task blink_task;
34
35 static void blink_cmd() {
36         ao_cmd_decimal();
37         blink_delay = ao_cmd_lex_i;
38         if (blink_delay && !blink_running)
39                 ao_add_task(&blink_task, blink, "blink");
40         if (!blink_delay)
41                 while (blink_running)
42                         ao_sleep(&blink_running);
43 }
44
45 static void lisp_cmd() {
46         ao_lisp_read_eval_print();
47 }
48
49 static const struct ao_cmds blink_cmds[] = {
50         { blink_cmd,    "b <delay, 0 off>\0Blink the green LED" },
51         { lisp_cmd,     "l\0Run lisp interpreter" },
52         { 0, 0 }
53 };
54
55
56 void main(void)
57 {
58         ao_led_init(LEDS_AVAILABLE);
59         ao_clock_init();
60         ao_task_init();
61         ao_timer_init();
62         ao_dma_init();
63         ao_usb_init();
64         ao_serial_init();
65         ao_cmd_init();
66         ao_cmd_register(blink_cmds);
67         ao_start_scheduler();
68 }
69
70