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;
46 static uint16_t ao_lco_tick_offset;
48 static struct ao_pad_query ao_pad_query;
53 ao_seven_segment_set(AO_LCO_PAD_DIGIT, ao_lco_pad + 1);
59 ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, ao_lco_box % 10);
60 ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, ao_lco_box / 10);
63 #define MASK_SIZE(n) (((n) + 7) >> 3)
64 #define MASK_ID(n) ((n) >> 3)
65 #define MASK_SHIFT(n) ((n) & 7)
67 static uint8_t ao_lco_box_mask[MASK_SIZE(AO_PAD_MAX_BOXES)];
70 ao_lco_box_present(uint8_t box)
72 if (box >= AO_PAD_MAX_BOXES)
74 return (ao_lco_box_mask[MASK_ID(box)] >> MASK_SHIFT(box)) & 1;
78 ao_lco_pad_present(uint8_t pad)
80 if (!ao_lco_valid || !ao_pad_query.channels)
82 if (pad >= AO_PAD_MAX_CHANNELS)
84 return (ao_pad_query.channels >> pad) & 1;
88 ao_lco_pad_first(void)
92 for (pad = 0; pad < AO_PAD_MAX_CHANNELS; pad++)
93 if (ao_lco_pad_present(pad))
101 static struct ao_event event;
102 int8_t dir, new_box, new_pad;
104 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
108 ao_event_get(&event);
109 PRINTD("event type %d unit %d value %d\n",
110 event.type, event.unit, event.value);
111 switch (event.type) {
112 case AO_EVENT_QUADRATURE:
113 switch (event.unit) {
114 case AO_QUADRATURE_PAD:
116 if (event.value == ao_lco_pad)
118 dir = ((int8_t) event.value - (int8_t) ao_lco_pad) > 0 ? 1 : -1;
119 new_pad = event.value;
120 while (!ao_lco_pad_present(new_pad)) {
122 if (new_pad > AO_PAD_MAX_CHANNELS)
124 else if (new_pad < 0)
125 new_pad = AO_PAD_MAX_CHANNELS - 1;
126 if (new_pad == ao_lco_pad)
129 if (new_pad != ao_lco_pad) {
130 ao_quadrature_count[AO_QUADRATURE_PAD] = ao_lco_pad;
135 case AO_QUADRATURE_BOX:
137 if (event.value == ao_lco_box)
139 dir = ((int8_t) event.value - (int8_t) ao_lco_box) > 0 ? 1 : -1;
140 new_box = event.value;
141 while (!ao_lco_box_present(new_box)) {
143 if (new_box > ao_lco_max_box)
144 new_box = ao_lco_min_box;
145 else if (new_box < ao_lco_min_box)
146 new_box = ao_lco_min_box;
147 if (new_box == ao_lco_box)
150 ao_quadrature_count[AO_QUADRATURE_PAD] = new_box;
151 if (ao_lco_box != new_box) {
152 ao_lco_box = new_box;
160 case AO_EVENT_BUTTON:
161 switch (event.unit) {
163 ao_lco_armed = event.value;
164 PRINTD("Armed %d\n", ao_lco_armed);
165 ao_wakeup(&ao_lco_armed);
169 ao_lco_firing = event.value;
170 PRINTD("Firing %d\n", ao_lco_firing);
171 ao_wakeup(&ao_lco_armed);
180 static AO_LED_TYPE continuity_led[AO_LED_CONTINUITY_NUM] = {
181 #ifdef AO_LED_CONTINUITY_0
184 #ifdef AO_LED_CONTINUITY_1
187 #ifdef AO_LED_CONTINUITY_2
190 #ifdef AO_LED_CONTINUITY_3
193 #ifdef AO_LED_CONTINUITY_4
196 #ifdef AO_LED_CONTINUITY_5
199 #ifdef AO_LED_CONTINUITY_6
202 #ifdef AO_LED_CONTINUITY_7
213 r = ao_lco_query(ao_lco_box, &ao_pad_query, &ao_lco_tick_offset);
214 if (r == AO_RADIO_CMAC_OK) {
218 ao_lco_pad = ao_lco_pad_first();
224 PRINTD("lco_query success arm_status %d i0 %d i1 %d i2 %d i3 %d\n",
226 query.igniter_status[0],
227 query.igniter_status[1],
228 query.igniter_status[2],
229 query.igniter_status[3]);
232 ao_wakeup(&ao_pad_query);
236 ao_lco_box_set_present(uint8_t box)
238 if (box >= AO_PAD_MAX_BOXES)
240 ao_lco_box_mask[MASK_ID(box)] |= 1 << MASK_SHIFT(box);
246 uint16_t tick_offset;
249 ao_lco_min_box = 0xff;
250 ao_lco_max_box = 0x00;
251 for (ao_lco_box = 0; ao_lco_box < AO_PAD_MAX_BOXES; ao_lco_box++) {
253 r = ao_lco_query(ao_lco_box, &ao_pad_query, &ao_lco_tick_offset);
254 if (r == AO_RADIO_CMAC_OK) {
255 if (ao_lco_box < ao_lco_min_box)
256 ao_lco_min_box = ao_lco_box;
257 if (ao_lco_box > ao_lco_max_box)
258 ao_lco_max_box = ao_lco_box;
259 ao_lco_box_set_present(ao_lco_box);
262 if (ao_lco_min_box <= ao_lco_max_box)
263 ao_lco_box = ao_lco_min_box;
265 ao_lco_min_box = ao_lco_max_box = ao_lco_box = 0;
271 ao_lco_igniter_status(void)
278 ao_sleep(&ao_pad_query);
281 ao_led_on(AO_LED_RED);
282 ao_led_off(AO_LED_GREEN);
285 PRINTD("RSSI %d\n", ao_radio_cmac_rssi);
286 if (ao_radio_cmac_rssi < -90)
287 ao_led_on(AO_LED_RED|AO_LED_GREEN);
289 ao_led_on(AO_LED_GREEN);
290 ao_led_off(AO_LED_RED);
292 if (ao_pad_query.arm_status)
293 ao_led_on(AO_LED_REMOTE_ARM);
295 ao_led_off(AO_LED_REMOTE_ARM);
296 for (c = 0; c < AO_LED_CONTINUITY_NUM; c++) {
299 if (ao_pad_query.channels & (1 << c))
300 status = ao_pad_query.igniter_status[c];
302 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN;
303 if (status == AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN)
304 ao_led_on(continuity_led[c]);
306 ao_led_off(continuity_led[c]);
312 ao_lco_arm_warn(void)
315 while (!ao_lco_armed)
316 ao_sleep(&ao_lco_armed);
317 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
318 ao_delay(AO_MS_TO_TICKS(200));
322 static struct ao_task ao_lco_input_task;
323 static struct ao_task ao_lco_monitor_task;
324 static struct ao_task ao_lco_arm_warn_task;
325 static struct ao_task ao_lco_igniter_status_task;
333 ao_add_task(&ao_lco_input_task, ao_lco_input, "lco input");
334 ao_add_task(&ao_lco_arm_warn_task, ao_lco_arm_warn, "lco arm warn");
335 ao_add_task(&ao_lco_igniter_status_task, ao_lco_igniter_status, "lco igniter status");
337 PRINTD("monitor armed %d firing %d offset %d\n",
338 ao_lco_armed, ao_lco_firing, ao_lco_tick_offset);
340 if (ao_lco_armed && ao_lco_firing) {
341 PRINTD("Firing box %d pad %d: valid %d\n",
342 ao_lco_box, ao_lco_pad, ao_lco_valid);
346 ao_lco_ignite(ao_lco_box, 1 << ao_lco_pad, ao_lco_tick_offset);
347 } else if (ao_lco_armed) {
348 PRINTD("Arming box %d pad %d\n",
349 ao_lco_box, ao_lco_pad);
352 ao_lco_arm(ao_lco_box, 1 << ao_lco_pad, ao_lco_tick_offset);
357 if (ao_lco_armed && ao_lco_firing)
358 delay = AO_MS_TO_TICKS(100);
360 delay = AO_SEC_TO_TICKS(1);
362 ao_sleep(&ao_lco_armed);
369 ao_lco_set_debug(void)
372 if (ao_cmd_status == ao_cmd_success)
373 ao_lco_debug = ao_cmd_lex_i != 0;
376 __code struct ao_cmds ao_lco_cmds[] = {
377 { ao_lco_set_debug, "D <0 off, 1 on>\0Debug" },
385 ao_add_task(&ao_lco_monitor_task, ao_lco_monitor, "lco monitor");
387 ao_cmd_register(&ao_lco_cmds[0]);