d908f4dbeebdee342ff9d17751e8599a5ab47950
[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; 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
21 uint8_t         relay_output;
22
23 void
24 ao_relay_init(void)
25 {
26         lpc_scb.sysahbclkctrl |= (1 << LPC_SCB_SYSAHBCLKCTRL_GPIO);
27         lpc_gpio.dir[RELAY_PORT] |= RELAY_BIT;
28 }
29
30 // switch relay to selected output, turn correct LED on as a side effect
31 static void
32 ao_relay_control(uint8_t output)
33 {
34         switch (output) {
35         case 1:
36                 lpc_gpio.pin[RELAY_PORT] |= RELAY_BIT;
37                 ao_led_on(AO_LED_RED);
38                 ao_led_off(AO_LED_GREEN);
39                 break;
40         default:
41                 lpc_gpio.pin[RELAY_PORT] &= ~RELAY_BIT;
42                 ao_led_off(AO_LED_RED);
43                 ao_led_on(AO_LED_GREEN);
44         }
45 }
46
47 static void
48 ao_relay_select(void) __reentrant
49 {
50         uint8_t output;
51
52         ao_cmd_decimal();
53         if (ao_cmd_status != ao_cmd_success)
54                 return;
55         output = ao_cmd_lex_i;
56         if (output > 1) 
57                 printf ("Invalid relay position %u\n", output);
58         else
59                 ao_relay_control(output);
60 }
61
62 static __code struct ao_cmds ao_relay_cmds[] = {
63         { ao_relay_select, "R <output>\0Select relay output" },
64         { 0, NULL }
65 };
66
67 void
68 main(void)
69 {
70         ao_clock_init();
71         ao_task_init();
72         ao_timer_init();
73
74         ao_usb_init();
75
76         ao_serial_init();
77
78         ao_led_init(LEDS_AVAILABLE);
79
80         ao_relay_init();
81
82         // initialize to default output
83         relay_output = 0;
84         ao_relay_control(relay_output);
85
86         ao_cmd_init();
87
88         ao_cmd_register(ao_relay_cmds);
89
90         ao_start_scheduler();
91 }