altos/telelco-v3.0: Fix up search UI
[fw/altos] / src / drivers / ao_lco_bits.c
index bc54dc22b8ab9992d4b8b2a0419fc9774a62e228..eca39d5bfe2e7aed9699293f3c15d02b6bc3135d 100644 (file)
 uint8_t                ao_lco_debug;
 
 uint8_t                ao_lco_pad;
-int16_t                ao_lco_box;
+uint16_t       ao_lco_box;
 
 uint8_t                ao_lco_armed;                                   /* arm active */
 uint8_t                ao_lco_firing;                                  /* fire active */
 
-uint8_t                ao_lco_min_box, ao_lco_max_box;
+uint16_t       ao_lco_min_box, ao_lco_max_box;
+
+uint8_t                ao_lco_pretending;
 
 #if AO_LCO_DRAG
 uint8_t                ao_lco_drag_race;
@@ -82,6 +84,10 @@ ao_lco_igniter_status(void)
                else
 #endif
                        ao_sleep(&ao_pad_query);
+               if (ao_lco_box == AO_LCO_LCO_VOLTAGE) {
+                       ao_led_off(AO_LED_GREEN|AO_LED_AMBER|AO_LED_RED);
+                       continue;
+               }
                PRINTD("RSSI %d VALID %d\n", ao_radio_cmac_rssi, ao_lco_valid[ao_lco_box]);
                if (!(ao_lco_valid[ao_lco_box] & AO_LCO_VALID_LAST)) {
                        ao_led_on(AO_LED_RED);
@@ -136,7 +142,7 @@ ao_lco_igniter_status(void)
 }
 
 uint8_t
-ao_lco_pad_present(uint8_t box, uint8_t pad)
+ao_lco_pad_present(uint16_t box, uint8_t pad)
 {
        /* voltage measurement is always valid */
        if (pad == AO_LCO_PAD_VOLTAGE)
@@ -149,7 +155,7 @@ ao_lco_pad_present(uint8_t box, uint8_t pad)
 }
 
 uint8_t
-ao_lco_pad_first(uint8_t box)
+ao_lco_pad_first(uint16_t box)
 {
        uint8_t pad;
 
@@ -160,7 +166,7 @@ ao_lco_pad_first(uint8_t box)
 }
 
 static uint8_t
-ao_lco_get_channels(uint8_t box, struct ao_pad_query *query)
+ao_lco_get_channels(uint16_t box, struct ao_pad_query *query)
 {
        int8_t                  r;
 
@@ -169,7 +175,7 @@ ao_lco_get_channels(uint8_t box, struct ao_pad_query *query)
                ao_lco_channels[box] = query->channels;
                ao_lco_valid[box] = AO_LCO_VALID_LAST | AO_LCO_VALID_EVER;
        } else
-               ao_lco_valid[box] &= ~AO_LCO_VALID_LAST;
+               ao_lco_valid[box] &= (uint8_t) ~AO_LCO_VALID_LAST;
        PRINTD("ao_lco_get_channels(%d) rssi %d valid %d ret %d offset %d\n", box, ao_radio_cmac_rssi, ao_lco_valid[box], r, ao_lco_tick_offset[box]);
        ao_wakeup(&ao_pad_query);
        return ao_lco_valid[box];
@@ -178,6 +184,11 @@ ao_lco_get_channels(uint8_t box, struct ao_pad_query *query)
 void
 ao_lco_update(void)
 {
+       if (ao_lco_box == AO_LCO_LCO_VOLTAGE) {
+               ao_lco_show();
+               return;
+       }
+
        uint8_t previous_valid = ao_lco_valid[ao_lco_box];
 
        if (ao_lco_get_channels(ao_lco_box, &ao_pad_query) & AO_LCO_VALID_LAST) {
@@ -197,11 +208,12 @@ ao_lco_box_reset_present(void)
 {
        ao_lco_min_box = 0xff;
        ao_lco_max_box = 0x00;
+       ao_lco_pretending = 0;
        memset(ao_lco_box_mask, 0, sizeof (ao_lco_box_mask));
 }
 
 static void
-ao_lco_box_set_present(uint8_t box)
+ao_lco_box_set_present(uint16_t box)
 {
        if (box < ao_lco_min_box)
                ao_lco_min_box = box;
@@ -209,7 +221,7 @@ ao_lco_box_set_present(uint8_t box)
                ao_lco_max_box = box;
        if (box >= AO_PAD_MAX_BOXES)
                return;
-       ao_lco_box_mask[AO_LCO_MASK_ID(box)] |= 1 << AO_LCO_MASK_SHIFT(box);
+       ao_lco_box_mask[AO_LCO_MASK_ID(box)] |= (uint8_t) (1 << AO_LCO_MASK_SHIFT(box));
 }
 
 void
@@ -223,8 +235,15 @@ void
 ao_lco_set_box(uint16_t new_box)
 {
        ao_lco_box = new_box;
-       if (ao_lco_box < AO_PAD_MAX_BOXES)
-               ao_lco_channels[ao_lco_box] = 0;
+       if (ao_lco_box != AO_LCO_LCO_VOLTAGE)
+       {
+               if (ao_lco_box < AO_PAD_MAX_BOXES) {
+                       if (ao_lco_pretending)
+                               ao_lco_channels[ao_lco_box] = 0xff;
+                       else
+                               ao_lco_channels[ao_lco_box] = 0;
+               }
+       }
        ao_lco_pad = 1;
        ao_lco_show();
 }
@@ -232,9 +251,9 @@ ao_lco_set_box(uint16_t new_box)
 void
 ao_lco_step_pad(int8_t dir)
 {
-       int8_t  new_pad;
+       int16_t new_pad;
 
-       new_pad = ao_lco_pad;
+       new_pad = (int16_t) ao_lco_pad;
        do {
                new_pad += dir;
                if (new_pad > AO_PAD_MAX_CHANNELS)
@@ -243,19 +262,49 @@ ao_lco_step_pad(int8_t dir)
                        new_pad = AO_PAD_MAX_CHANNELS;
                if (new_pad == ao_lco_pad)
                        break;
-       } while (!ao_lco_pad_present(ao_lco_box, new_pad));
-       ao_lco_set_pad(new_pad);
+       } while (!ao_lco_pad_present(ao_lco_box, (uint8_t) new_pad));
+       ao_lco_set_pad((uint8_t) new_pad);
+}
+
+uint8_t
+ao_lco_box_present(uint16_t box)
+{
+       if (box == AO_LCO_LCO_VOLTAGE)
+               return 1;
+       if (box >= AO_PAD_MAX_BOXES)
+               return 0;
+       return (ao_lco_box_mask[AO_LCO_MASK_ID(box)] >> AO_LCO_MASK_SHIFT(box)) & 1;
+}
+
+void
+ao_lco_step_box(int8_t dir)
+{
+       int32_t new_box = (int32_t) ao_lco_box;
+
+       do {
+               new_box += dir;
+               if (new_box > ao_lco_max_box)
+                       new_box = AO_LCO_LCO_VOLTAGE;
+               else if (new_box < 0)
+                       new_box = ao_lco_max_box;
+               if (new_box == ao_lco_box)
+                       break;
+       } while (!ao_lco_box_present((uint16_t) new_box));
+       ao_lco_set_box((uint16_t) new_box);
 }
 
 void
 ao_lco_set_armed(uint8_t armed)
 {
+       if (ao_lco_box == AO_LCO_LCO_VOLTAGE)
+               return;
+
        ao_lco_armed = armed;
        PRINTD("Armed %d\n", ao_lco_armed);
        if (ao_lco_armed) {
 #if AO_LCO_DRAG
                if (ao_lco_drag_race) {
-                       uint8_t box;
+                       uint16_t        box;
 
                        for (box = ao_lco_min_box; box <= ao_lco_max_box; box++)
                                if (ao_lco_selected[box])
@@ -288,15 +337,23 @@ ao_lco_search(void)
 {
        int8_t          r;
        int8_t          try;
-       uint8_t         box;
-       uint8_t         boxes = 0;
+       uint16_t        box;
+       uint16_t        boxes = 0;
 
        ao_lco_box_reset_present();
+#ifdef AO_LCO_SEARCH_API
+       ao_lco_search_start();
+#else
        ao_lco_show_box(0);
        ao_lco_show_pad(0);
+#endif
        for (box = 0; box < AO_PAD_MAX_BOXES; box++) {
+#ifdef AO_LCO_SEARCH_API
+               ao_lco_search_box_check(box);
+#else
                if ((box % 10) == 0)
                        ao_lco_show_box(box);
+#endif
                for (try = 0; try < 3; try++) {
                        ao_lco_tick_offset[box] = 0;
                        r = ao_lco_query(box, &ao_pad_query, &ao_lco_tick_offset[box]);
@@ -304,7 +361,11 @@ ao_lco_search(void)
                        if (r == AO_RADIO_CMAC_OK) {
                                ++boxes;
                                ao_lco_box_set_present(box);
-                               ao_lco_show_pad(boxes % 10);
+#ifdef AO_LCO_SEARCH_API
+                               ao_lco_search_box_present(box);
+#else
+                               ao_lco_show_pad((uint8_t) (boxes % 10));
+#endif
                                ao_delay(AO_MS_TO_TICKS(30));
                                break;
                        }
@@ -316,6 +377,25 @@ ao_lco_search(void)
                ao_lco_min_box = ao_lco_max_box = ao_lco_box = 0;
        memset(ao_lco_valid, 0, sizeof (ao_lco_valid));
        memset(ao_lco_channels, 0, sizeof (ao_lco_channels));
+#ifdef AO_LCO_SEARCH_API
+       ao_lco_search_done();
+#endif
+       ao_lco_set_box(ao_lco_min_box);
+}
+
+void
+ao_lco_pretend(void)
+{
+       uint16_t box;
+
+       ao_lco_pretending = 1;
+       ao_lco_min_box = 1;
+       ao_lco_max_box = AO_PAD_MAX_BOXES - 1;
+       for (box = ao_lco_min_box; box < ao_lco_max_box; box++)
+               ao_lco_box_set_present(box);
+       ao_lco_box = ao_lco_min_box;
+       memset(ao_lco_valid, 0, sizeof (ao_lco_valid));
+       memset(ao_lco_channels, 0, sizeof (ao_lco_channels));
        ao_lco_set_box(ao_lco_min_box);
 }
 
@@ -323,7 +403,7 @@ void
 ao_lco_monitor(void)
 {
        AO_TICK_TYPE            delay;
-       uint8_t                 box;
+       uint16_t                box;
 
        for (;;) {
                PRINTD("monitor armed %d firing %d\n",
@@ -379,8 +459,8 @@ ao_lco_drag_add_beeps(uint8_t beeps)
 void
 ao_lco_toggle_drag(void)
 {
-       if (ao_lco_drag_race && ao_lco_pad != AO_LCO_PAD_VOLTAGE) {
-               ao_lco_selected[ao_lco_box] ^= (1 << (ao_lco_pad - 1));
+       if (ao_lco_drag_race && ao_lco_pad != AO_LCO_PAD_VOLTAGE && ao_lco_box != AO_LCO_LCO_VOLTAGE) {
+               ao_lco_selected[ao_lco_box] ^= (uint8_t) (1 << (ao_lco_pad - 1));
                PRINTD("Toggle box %d pad %d (pads now %x) to drag race\n",
                       ao_lco_pad, ao_lco_box, ao_lco_selected[ao_lco_box]);
                ao_lco_drag_add_beeps(ao_lco_pad);
@@ -394,9 +474,9 @@ ao_lco_toggle_drag(void)
 AO_TICK_TYPE
 ao_lco_drag_beep_check(AO_TICK_TYPE now, AO_TICK_TYPE delay)
 {
-       PRINTD("beep check count %d delta %d\n",
+       PRINTD("beep check count %d delta %ld\n",
               ao_lco_drag_beep_count,
-              (AO_TICK_SIGNED) (now - ao_lco_drag_beep_time));
+              (long) (AO_TICK_SIGNED) (now - ao_lco_drag_beep_time));
        if (ao_lco_drag_beep_count) {
                if ((AO_TICK_SIGNED) (now - ao_lco_drag_beep_time) >= 0) {
                        if (ao_lco_drag_beep_on) {