2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
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.
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.
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.
21 #include <ao_lco_func.h>
22 #include <ao_radio_cmac.h>
27 static uint8_t ao_lco_debug;
29 #define DEBUG_STATUS 2
30 #define PRINTD(l, ...) do { if (!(ao_lco_debug & l)) break; printf ("\r%5u %s: ", ao_tick_count, __func__); printf(__VA_ARGS__); flush(); } while(0)
35 #define AO_LCO_VALID_LAST 1
36 #define AO_LCO_VALID_EVER 2
38 static uint8_t ao_lco_suspended;
39 static uint8_t ao_lco_selected;
40 static uint8_t ao_lco_valid;
41 static uint8_t ao_lco_channels;
42 static uint16_t ao_lco_tick_offset;
45 static uint8_t ao_lco_armed;
46 static uint8_t ao_lco_firing;
48 #define ao_lco_box (ao_config.pad_box)
50 static struct ao_pad_query ao_pad_query;
52 #define MASK_SIZE(n) (((n) + 7) >> 3)
53 #define MASK_ID(n) ((n) >> 3)
54 #define MASK_SHIFT(n) ((n) & 7)
57 ao_lco_set_armed(int pad, int armed)
59 uint8_t bit = (1 << pad);
62 ao_lco_selected |= bit;
65 ao_lco_selected &= ~bit;
68 PRINTD(DEBUG_EVENT, "pad %d bit 0x%x armed %d ao_lco_selected 0x%x ao_lco_armed 0x%x\n",
69 pad, bit, armed, ao_lco_selected, ao_lco_armed);
70 ao_wakeup(&ao_lco_armed);
76 if (!ao_lco_suspended) {
77 PRINTD(DEBUG_EVENT, "suspend\n");
81 ao_wakeup(&ao_pad_query);
88 if (ao_lco_suspended) {
90 ao_wakeup(&ao_lco_suspended);
97 static struct ao_event event;
102 if (ao_config.pad_idle && !ao_lco_suspended) {
103 timeout = ao_event_get_for(&event, AO_SEC_TO_TICKS(ao_config.pad_idle));
109 ao_event_get(&event);
112 PRINTD(DEBUG_EVENT, "event type %d unit %d value %d\n",
113 event.type, event.unit, event.value);
114 switch (event.type) {
115 case AO_EVENT_BUTTON:
116 switch (event.unit) {
117 case AO_BUTTON_ARM_0:
118 ao_lco_set_armed(0, event.value);
120 #if AO_BUTTON_ARM_NUM > 1
121 case AO_BUTTON_ARM_1:
122 ao_lco_set_armed(1, event.value);
127 ao_lco_firing = event.value;
128 PRINTD(DEBUG_EVENT, "Firing %d\n", ao_lco_firing);
129 ao_wakeup(&ao_lco_armed);
138 static AO_LED_TYPE continuity_led[AO_LED_CONTINUITY_NUM] = {
139 #ifdef AO_LED_CONTINUITY_0
142 #ifdef AO_LED_CONTINUITY_1
145 #ifdef AO_LED_CONTINUITY_2
148 #ifdef AO_LED_CONTINUITY_3
151 #ifdef AO_LED_CONTINUITY_4
154 #ifdef AO_LED_CONTINUITY_5
157 #ifdef AO_LED_CONTINUITY_6
160 #ifdef AO_LED_CONTINUITY_7
166 ao_lco_get_channels(void)
170 r = ao_lco_query(ao_lco_box, &ao_pad_query, &ao_lco_tick_offset);
171 if (r == AO_RADIO_CMAC_OK) {
172 ao_lco_channels = ao_pad_query.channels;
173 ao_lco_valid = AO_LCO_VALID_LAST | AO_LCO_VALID_EVER;
175 ao_lco_valid &= ~AO_LCO_VALID_LAST;
176 PRINTD(DEBUG_STATUS, "ao_lco_get_channels() rssi %d valid %d ret %d offset %d\n", ao_radio_cmac_rssi, ao_lco_valid, r, ao_lco_tick_offset);
177 ao_wakeup(&ao_pad_query);
182 ao_lco_igniter_status(void)
188 ao_sleep(&ao_pad_query);
189 while (ao_lco_suspended) {
190 ao_led_off(AO_LED_GREEN|AO_LED_AMBER|AO_LED_RED|AO_LED_REMOTE_ARM);
191 for (c = 0; c < AO_LED_CONTINUITY_NUM; c++)
192 ao_led_off(continuity_led[c]);
193 ao_sleep(&ao_lco_suspended);
195 PRINTD(DEBUG_STATUS, "RSSI %d VALID %d\n", ao_radio_cmac_rssi, ao_lco_valid);
196 if (!(ao_lco_valid & AO_LCO_VALID_LAST)) {
197 ao_led_on(AO_LED_RED);
198 ao_led_off(AO_LED_GREEN|AO_LED_AMBER);
201 if (ao_radio_cmac_rssi < -90) {
202 ao_led_on(AO_LED_AMBER);
203 ao_led_off(AO_LED_RED|AO_LED_GREEN);
205 ao_led_on(AO_LED_GREEN);
206 ao_led_off(AO_LED_RED|AO_LED_AMBER);
208 if (ao_pad_query.arm_status)
209 ao_led_on(AO_LED_REMOTE_ARM);
211 ao_led_off(AO_LED_REMOTE_ARM);
213 for (c = 0; c < AO_LED_CONTINUITY_NUM; c++) {
216 if (ao_pad_query.channels & (1 << c))
217 status = ao_pad_query.igniter_status[c];
219 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN;
220 PRINTD(DEBUG_STATUS, "\tchannel %d status %d\n", c, status);
221 if (status == AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN)
222 ao_led_on(continuity_led[c]);
224 ao_led_off(continuity_led[c]);
231 ao_lco_arm_warn(void)
235 while (ao_lco_suspended)
236 ao_sleep(&ao_lco_suspended);
237 while (!ao_lco_armed)
238 ao_sleep(&ao_lco_armed);
239 for (i = 0; i < ao_lco_armed; i++) {
240 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(100));
241 ao_delay(AO_MS_TO_TICKS(100));
243 ao_delay(AO_MS_TO_TICKS(300));
247 static struct ao_task ao_lco_input_task;
248 static struct ao_task ao_lco_monitor_task;
249 static struct ao_task ao_lco_arm_warn_task;
250 static struct ao_task ao_lco_igniter_status_task;
257 ao_add_task(&ao_lco_input_task, ao_lco_input, "lco input");
258 ao_add_task(&ao_lco_arm_warn_task, ao_lco_arm_warn, "lco arm warn");
259 ao_add_task(&ao_lco_igniter_status_task, ao_lco_igniter_status, "lco igniter status");
260 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
262 while (ao_lco_suspended)
263 ao_sleep(&ao_lco_suspended);
265 PRINTD(DEBUG_STATUS, "monitor armed %d firing %d\n",
266 ao_lco_armed, ao_lco_firing);
268 if (ao_lco_armed && ao_lco_firing) {
271 ao_lco_get_channels();
273 if (ao_lco_selected) {
274 PRINTD(DEBUG_STATUS, "Arming pads %x\n",
276 if (ao_lco_valid & AO_LCO_VALID_EVER) {
277 ao_lco_arm(ao_lco_box, ao_lco_selected, ao_lco_tick_offset);
278 ao_delay(AO_MS_TO_TICKS(10));
283 if (ao_lco_armed && ao_lco_firing)
284 delay = AO_MS_TO_TICKS(100);
286 delay = AO_SEC_TO_TICKS(1);
288 ao_sleep_for(&ao_lco_armed, delay);
294 ao_lco_set_debug(void)
297 if (ao_cmd_status == ao_cmd_success)
298 ao_lco_debug = ao_cmd_lex_i;
301 __code struct ao_cmds ao_lco_cmds[] = {
302 { ao_lco_set_debug, "D <0 off, 1 on>\0Debug" },
310 ao_add_task(&ao_lco_monitor_task, ao_lco_monitor, "lco monitor");
312 ao_cmd_register(&ao_lco_cmds[0]);