5f4bf9632467f47a1828b28bbd67409521001b4a
[fw/altos] / src / usbrelay-v0.1 / ao_usbrelay.c
1 /*
2  * Copyright © 2014 Bdale Garbee <bdale@gag.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 uint8_t         relay_output;
21
22 // switch relay to selected output, turn correct LED on as a side effect
23 static void
24 ao_relay_control(uint8_t output)
25 {
26         switch (output) {
27         case 1:
28                 ao_led_on(AO_LED_RED);
29                 ao_led_off(AO_LED_GREEN);
30                 break;
31         default:
32                 ao_led_off(AO_LED_RED);
33                 ao_led_on(AO_LED_GREEN);
34         }
35 }
36
37 static void
38 ao_relay_select(void) __reentrant
39 {
40         uint8_t output;
41
42         ao_cmd_decimal();
43         if (ao_cmd_status != ao_cmd_success)
44                 return;
45         output = ao_cmd_lex_i;
46
47         printf ("Relay control not implemented yet, %u selected\n", output);
48         ao_relay_control(output);
49 }
50
51 static __code struct ao_cmds ao_relay_cmds[] = {
52         { ao_relay_select, "R <output>\0Select relay output" },
53         { 0, NULL }
54 };
55
56 void
57 main(void)
58 {
59         ao_clock_init();
60         ao_task_init();
61         ao_timer_init();
62
63         ao_usb_init();
64
65         ao_serial_init();
66
67         ao_led_init(LEDS_AVAILABLE);
68
69         // initialize to default output
70         relay_output = 0;
71         ao_relay_control(relay_output);
72
73         ao_cmd_init();
74
75         ao_cmd_register(ao_relay_cmds);
76
77         ao_start_scheduler();
78 }