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_seven_segment.h>
22 #include <ao_quadrature.h>
23 #include <ao_lco_func.h>
24 #include <ao_radio_cmac.h>
29 static uint8_t ao_lco_debug;
30 #define PRINTD(...) do { if (!ao_lco_debug) break; printf ("\r%5u %s: ", ao_tick_count, __func__); printf(__VA_ARGS__); flush(); } while(0)
35 #define AO_LCO_PAD_DIGIT 0
36 #define AO_LCO_BOX_DIGIT_1 1
37 #define AO_LCO_BOX_DIGIT_10 2
39 static uint8_t ao_lco_min_box, ao_lco_max_box;
40 static uint8_t ao_lco_mutex;
41 static uint8_t ao_lco_pad;
42 static uint8_t ao_lco_box;
43 static uint8_t ao_lco_armed;
44 static uint8_t ao_lco_firing;
45 static uint8_t ao_lco_valid;
50 ao_seven_segment_set(AO_LCO_PAD_DIGIT, ao_lco_pad + 1);
56 ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, ao_lco_box % 10);
57 ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, ao_lco_box / 10);
60 #define MASK_SIZE(n) (((n) + 7) >> 3)
61 #define MASK_ID(n) ((n) >> 3)
62 #define MASK_SHIFT(n) ((n) & 7)
64 static uint8_t ao_lco_box_mask[MASK_SIZE(AO_PAD_MAX_BOXES)];
67 ao_lco_box_present(uint8_t box)
69 if (box >= AO_PAD_MAX_BOXES)
71 return (ao_lco_box_mask[MASK_ID(box)] >> MASK_SHIFT(box)) & 1;
77 static struct ao_event event;
80 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
85 PRINTD("event type %d unit %d value %d\n",
86 event.type, event.unit, event.value);
88 case AO_EVENT_QUADRATURE:
90 case AO_QUADRATURE_PAD:
92 ao_lco_pad = event.value & 3;
93 ao_quadrature_count[AO_QUADRATURE_PAD] = ao_lco_pad;
97 case AO_QUADRATURE_BOX:
99 if (event.value == ao_lco_box)
101 dir = (event.value - ao_lco_box) > 0 ? 1 : -1;
102 ao_lco_box = event.value;
103 while (!ao_lco_box_present(ao_lco_box)) {
105 if (ao_lco_box > ao_lco_max_box)
106 ao_lco_box = ao_lco_min_box;
107 else if (ao_lco_box < ao_lco_min_box)
108 ao_lco_box = ao_lco_min_box;
110 ao_quadrature_count[AO_QUADRATURE_PAD] = ao_lco_box;
116 case AO_EVENT_BUTTON:
117 switch (event.unit) {
119 ao_lco_armed = event.value;
120 PRINTD("Armed %d\n", ao_lco_armed);
121 ao_wakeup(&ao_lco_armed);
125 ao_lco_firing = event.value;
126 PRINTD("Firing %d\n", ao_lco_firing);
127 ao_wakeup(&ao_lco_armed);
136 static AO_LED_TYPE continuity_led[AO_LED_CONTINUITY_NUM] = {
137 #ifdef AO_LED_CONTINUITY_0
140 #ifdef AO_LED_CONTINUITY_1
143 #ifdef AO_LED_CONTINUITY_2
146 #ifdef AO_LED_CONTINUITY_3
149 #ifdef AO_LED_CONTINUITY_4
152 #ifdef AO_LED_CONTINUITY_5
155 #ifdef AO_LED_CONTINUITY_6
158 #ifdef AO_LED_CONTINUITY_7
163 static uint16_t ao_lco_tick_offset;
165 static struct ao_pad_query ao_pad_query;
173 r = ao_lco_query(ao_lco_box, &ao_pad_query, &ao_lco_tick_offset);
174 if (r == AO_RADIO_CMAC_OK)
178 PRINTD("lco_query success arm_status %d i0 %d i1 %d i2 %d i3 %d\n",
180 query.igniter_status[0],
181 query.igniter_status[1],
182 query.igniter_status[2],
183 query.igniter_status[3]);
186 ao_wakeup(&ao_pad_query);
190 ao_lco_box_set_present(uint8_t box)
192 if (box >= AO_PAD_MAX_BOXES)
194 ao_lco_box_mask[MASK_ID(box)] |= 1 << MASK_SHIFT(box);
200 uint16_t tick_offset;
203 ao_lco_min_box = 0xff;
204 ao_lco_max_box = 0x00;
205 for (ao_lco_box = 0; ao_lco_box < AO_PAD_MAX_BOXES; ao_lco_box++) {
207 r = ao_lco_query(ao_lco_box, &ao_pad_query, &ao_lco_tick_offset);
208 if (r == AO_RADIO_CMAC_OK) {
209 if (ao_lco_box < ao_lco_min_box)
210 ao_lco_min_box = ao_lco_box;
211 if (ao_lco_box > ao_lco_max_box)
212 ao_lco_max_box = ao_lco_box;
213 ao_lco_box_set_present(ao_lco_box);
216 ao_lco_box = ao_lco_min_box;
221 ao_lco_igniter_status(void)
228 ao_sleep(&ao_pad_query);
231 ao_led_on(AO_LED_RED);
232 ao_led_off(AO_LED_GREEN);
235 PRINTD("RSSI %d\n", ao_radio_cmac_rssi);
236 if (ao_radio_cmac_rssi < -70)
237 ao_led_on(AO_LED_RED|AO_LED_GREEN);
239 ao_led_on(AO_LED_GREEN);
240 ao_led_off(AO_LED_RED);
242 if (ao_pad_query.arm_status)
243 ao_led_on(AO_LED_REMOTE_ARM);
245 ao_led_off(AO_LED_REMOTE_ARM);
246 for (c = 0; c < AO_LED_CONTINUITY_NUM; c++) {
249 if (ao_pad_query.channels & (1 << c))
250 status = ao_pad_query.igniter_status[c];
252 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN;
253 if (status == AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN)
254 ao_led_on(continuity_led[c]);
256 ao_led_off(continuity_led[c]);
262 ao_lco_arm_warn(void)
265 while (!ao_lco_armed)
266 ao_sleep(&ao_lco_armed);
267 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
268 ao_delay(AO_MS_TO_TICKS(200));
272 static struct ao_task ao_lco_input_task;
273 static struct ao_task ao_lco_monitor_task;
274 static struct ao_task ao_lco_arm_warn_task;
275 static struct ao_task ao_lco_igniter_status_task;
283 ao_add_task(&ao_lco_input_task, ao_lco_input, "lco input");
284 ao_add_task(&ao_lco_arm_warn_task, ao_lco_arm_warn, "lco arm warn");
285 ao_add_task(&ao_lco_igniter_status_task, ao_lco_igniter_status, "lco igniter status");
287 PRINTD("monitor armed %d firing %d offset %d\n",
288 ao_lco_armed, ao_lco_firing, ao_lco_tick_offset);
290 if (ao_lco_armed && ao_lco_firing) {
291 PRINTD("Firing box %d pad %d: valid %d\n",
292 ao_lco_box, ao_lco_pad, ao_lco_valid);
296 ao_lco_ignite(ao_lco_box, 1 << ao_lco_pad, ao_lco_tick_offset);
297 } else if (ao_lco_armed) {
298 PRINTD("Arming box %d pad %d\n",
299 ao_lco_box, ao_lco_pad);
302 ao_lco_arm(ao_lco_box, 1 << ao_lco_pad, ao_lco_tick_offset);
307 if (ao_lco_armed && ao_lco_firing)
308 delay = AO_MS_TO_TICKS(100);
310 delay = AO_SEC_TO_TICKS(1);
312 ao_sleep(&ao_lco_armed);
319 ao_lco_set_debug(void)
322 if (ao_cmd_status == ao_cmd_success)
323 ao_lco_debug = ao_cmd_lex_i != 0;
326 __code struct ao_cmds ao_lco_cmds[] = {
327 { ao_lco_set_debug, "D <0 off, 1 on>\0Debug" },
335 ao_add_task(&ao_lco_monitor_task, ao_lco_monitor, "lco monitor");
337 ao_cmd_register(&ao_lco_cmds[0]);