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