altos: Use AO_TICK_TYPE/AO_TICK_SIGNED for lco/pad code
authorKeith Packard <keithp@keithp.com>
Wed, 22 Jan 2020 20:41:40 +0000 (12:41 -0800)
committerKeith Packard <keithp@keithp.com>
Wed, 22 Jan 2020 20:41:40 +0000 (12:41 -0800)
Was using 16-bit types, which 'mostly' worked, except that the pad
code compared ao_time() with a 16-bit value when determining the radio
status. After the box was on for 10 minutes (timer wrapped), the RF
timeout check would always fail, so the red LED would be lit.

Signed-off-by: Keith Packard <keithp@keithp.com>
src/drivers/ao_lco.c
src/drivers/ao_lco.h
src/drivers/ao_lco_bits.c
src/drivers/ao_lco_cmd.c
src/drivers/ao_lco_func.c
src/drivers/ao_pad.c
src/telelco-v2.0/ao_lco_v2.c

index b314ef7c0273acf55f8ab277ed927bc05917c363..0d7e4cba2debfca344168ccad24065cb4a94cbb1 100644 (file)
@@ -37,7 +37,7 @@
 #define AO_LCO_BOX_DRAG                0x1000
 
 /* UI values */
-static uint16_t        ao_lco_fire_tick;
+static AO_TICK_TYPE    ao_lco_fire_tick;
 static uint8_t ao_lco_fire_down;
 
 static uint8_t ao_lco_display_mutex;
@@ -124,10 +124,10 @@ ao_lco_box_present(uint16_t box)
 static struct ao_task  ao_lco_drag_task;
 static uint8_t         ao_lco_drag_active;
 
-static uint16_t
-ao_lco_drag_button_check(uint16_t now, uint16_t delay)
+static AO_TICK_TYPE
+ao_lco_drag_button_check(AO_TICK_TYPE now, AO_TICK_TYPE delay)
 {
-       uint16_t        button_delay = ~0;
+       AO_TICK_TYPE    button_delay = ~0;
 
        /*
         * Check to see if the button has been held down long enough
@@ -135,14 +135,14 @@ ao_lco_drag_button_check(uint16_t now, uint16_t delay)
         */
        if (ao_lco_fire_down) {
                if (ao_lco_drag_race) {
-                       if ((int16_t) (now - ao_lco_fire_tick) >= AO_LCO_DRAG_RACE_STOP_TIME) {
+                       if ((AO_TICK_SIGNED) (now - ao_lco_fire_tick) >= AO_LCO_DRAG_RACE_STOP_TIME) {
                                ao_lco_drag_disable();
                                ao_lco_fire_down = 0;
                        }
                        else
                                button_delay = ao_lco_fire_tick + AO_LCO_DRAG_RACE_STOP_TIME - now;
                } else {
-                       if ((int16_t) (now - ao_lco_fire_tick) >= AO_LCO_DRAG_RACE_START_TIME) {
+                       if ((AO_TICK_SIGNED) (now - ao_lco_fire_tick) >= AO_LCO_DRAG_RACE_START_TIME) {
                                ao_lco_drag_enable();
                                ao_lco_fire_down = 0;
                        }
@@ -158,14 +158,14 @@ ao_lco_drag_button_check(uint16_t now, uint16_t delay)
 static void
 ao_lco_drag_monitor(void)
 {
-       uint16_t        delay = ~0;
-       uint16_t        now;
+       AO_TICK_TYPE    delay = ~0;
+       AO_TICK_TYPE    now;
 
        ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
        for (;;) {
                PRINTD("Drag monitor count %d active %d delay %d\n",
                       ao_lco_drag_beep_count, ao_lco_drag_active, delay);
-               if (delay == (uint16_t) ~0)
+               if (delay == (AO_TICK_TYPE) ~0)
                        ao_sleep(&ao_lco_drag_beep_count);
                else
                        ao_sleep_for(&ao_lco_drag_beep_count, delay);
index cdc19d3d76837a7d7627d5dd250eb431005733cf..3b123793d7ff4d8327e4d837e2ee8dddc7df1867 100644 (file)
@@ -107,12 +107,12 @@ void
 ao_lco_drag_disable(void);
 
 /* Handle drag beeps, return new delay */
-uint16_t
-ao_lco_drag_beep_check(uint16_t now, uint16_t delay);
+AO_TICK_TYPE
+ao_lco_drag_beep_check(AO_TICK_TYPE now, AO_TICK_TYPE delay);
 
 /* Check if it's time to beep during drag race. Return new delay */
-uint16_t
-ao_lco_drag_warn_check(uint16_t now, uint16_t delay);
+AO_TICK_TYPE
+ao_lco_drag_warn_check(AO_TICK_TYPE now, AO_TICK_TYPE delay);
 
 /* Request 'beeps' additional drag race beeps */
 void
index 6e50e44db4f8bec93edf71a42ab72a8f940f64c2..bc54dc22b8ab9992d4b8b2a0419fc9774a62e228 100644 (file)
@@ -33,7 +33,7 @@ uint8_t               ao_lco_drag_race;
 struct ao_pad_query    ao_pad_query;                           /* latest query response */
 
 static uint8_t         ao_lco_channels[AO_PAD_MAX_BOXES];      /* pad channels available on each box */
-static uint16_t                ao_lco_tick_offset[AO_PAD_MAX_BOXES];   /* offset from local to remote tick count */
+static uint16_t                ao_lco_tick_offset[AO_PAD_MAX_BOXES];   /* 16 bit offset from local to remote tick count */
 static uint8_t         ao_lco_selected[AO_PAD_MAX_BOXES];      /* pads selected to fire */
 
 #define AO_LCO_VALID_LAST      1
@@ -322,7 +322,7 @@ ao_lco_search(void)
 void
 ao_lco_monitor(void)
 {
-       uint16_t                delay;
+       AO_TICK_TYPE            delay;
        uint8_t                 box;
 
        for (;;) {
@@ -358,8 +358,8 @@ ao_lco_monitor(void)
 
 uint8_t                        ao_lco_drag_beep_count;
 static uint8_t         ao_lco_drag_beep_on;
-static uint16_t                ao_lco_drag_beep_time;
-static uint16_t                ao_lco_drag_warn_time;
+static AO_TICK_TYPE    ao_lco_drag_beep_time;
+static AO_TICK_TYPE    ao_lco_drag_warn_time;
 
 #define AO_LCO_DRAG_BEEP_TIME  AO_MS_TO_TICKS(50)
 #define AO_LCO_DRAG_WARN_TIME  AO_SEC_TO_TICKS(5)
@@ -391,14 +391,14 @@ ao_lco_toggle_drag(void)
  * turn it on or off as necessary and bump the remaining beep counts
  */
 
-uint16_t
-ao_lco_drag_beep_check(uint16_t now, uint16_t delay)
+AO_TICK_TYPE
+ao_lco_drag_beep_check(AO_TICK_TYPE now, AO_TICK_TYPE delay)
 {
        PRINTD("beep check count %d delta %d\n",
               ao_lco_drag_beep_count,
-              (int16_t) (now - ao_lco_drag_beep_time));
+              (AO_TICK_SIGNED) (now - ao_lco_drag_beep_time));
        if (ao_lco_drag_beep_count) {
-               if ((int16_t) (now - ao_lco_drag_beep_time) >= 0) {
+               if ((AO_TICK_SIGNED) (now - ao_lco_drag_beep_time) >= 0) {
                        if (ao_lco_drag_beep_on) {
                                ao_beep(0);
                                PRINTD("beep stop\n");
@@ -418,7 +418,7 @@ ao_lco_drag_beep_check(uint16_t now, uint16_t delay)
        }
 
        if (ao_lco_drag_beep_count) {
-               uint16_t beep_delay = 0;
+               AO_TICK_TYPE beep_delay = 0;
 
                if (ao_lco_drag_beep_time > now)
                        beep_delay = ao_lco_drag_beep_time - now;
@@ -463,13 +463,13 @@ ao_lco_drag_disable(void)
  * active
  */
 
-uint16_t
-ao_lco_drag_warn_check(uint16_t now, uint16_t delay)
+AO_TICK_TYPE
+ao_lco_drag_warn_check(AO_TICK_TYPE now, AO_TICK_TYPE delay)
 {
        if (ao_lco_drag_race) {
-               uint16_t        warn_delay;
+               AO_TICK_TYPE    warn_delay;
 
-               if ((int16_t) (now - ao_lco_drag_warn_time) >= 0) {
+               if ((AO_TICK_SIGNED) (now - ao_lco_drag_warn_time) >= 0) {
                        ao_lco_drag_add_beeps(1);
                        ao_lco_drag_warn_time = now + AO_LCO_DRAG_WARN_TIME;
                }
index 6f195e550cfc909063984a0115a9163807038da5..0184363533390736f2e831f829285e8a83e6e676 100644 (file)
@@ -28,7 +28,6 @@
 
 static uint16_t        lco_box;
 static uint8_t lco_channels;
-static uint16_t        tick_offset;
 
 static void
 lco_args(void) 
index 06350694145d4bda978ac894eb2a011160543ada..1960683fa6b214a56cd7fd87800cd1a7c6e7562d 100644 (file)
@@ -28,8 +28,8 @@ int8_t
 ao_lco_query(uint16_t box, struct ao_pad_query *query, uint16_t *tick_offset)
 {
        int8_t          r;
-       uint16_t        sent_time;
-       uint16_t        timeout = AO_MS_TO_TICKS(10);
+       AO_TICK_TYPE    sent_time;
+       AO_TICK_TYPE    timeout = AO_MS_TO_TICKS(10);
 
 #if HAS_RADIO_RATE
        switch (ao_config.radio_rate) {
@@ -62,7 +62,7 @@ void
 ao_lco_arm(uint16_t box, uint8_t channels, uint16_t tick_offset)
 {
        ao_mutex_get(&ao_lco_mutex);
-       command.tick = ao_time() - tick_offset;
+       command.tick = (uint16_t) ao_time() - tick_offset;
        command.box = box;
        command.cmd = AO_PAD_ARM;
        command.channels = channels;
index 2592a084261bec25640f0820dfc018c579f44486..07c49afc6b890a4094a842360223eddc52eef715 100644 (file)
@@ -26,10 +26,10 @@ static uint8_t ao_pad_ignite;
 static struct ao_pad_command   command;
 static struct ao_pad_query     query;
 static uint8_t ao_pad_armed;
-static uint16_t        ao_pad_arm_time;
+static AO_TICK_TYPE    ao_pad_arm_time;
 static uint8_t ao_pad_box;
 static uint8_t ao_pad_disabled;
-static uint16_t        ao_pad_packet_time;
+static AO_TICK_TYPE    ao_pad_packet_time;
 
 #ifndef AO_PAD_RSSI_MINIMUM
 #define AO_PAD_RSSI_MINIMUM    -90
@@ -288,7 +288,7 @@ ao_pad_monitor(void)
                        prev = cur;
                }
 
-               if (ao_pad_armed && (int16_t) (ao_time() - ao_pad_arm_time) > AO_PAD_ARM_TIME)
+               if (ao_pad_armed && (AO_TICK_SIGNED) (ao_time() - ao_pad_arm_time) > AO_PAD_ARM_TIME)
                        ao_pad_armed = 0;
 
                if (ao_pad_armed) {
@@ -369,7 +369,7 @@ static int ao_pad_read_box(void) {
 static void
 ao_pad(void)
 {
-       int16_t time_difference;
+       int16_t tick_difference;
        int8_t  ret;
 
        ao_pad_box = 0;
@@ -398,12 +398,12 @@ ao_pad(void)
                        if (command.channels & ~(AO_PAD_ALL_CHANNELS))
                                break;
 
-                       time_difference = command.tick - ao_time();
-                       PRINTD ("arm tick %d local tick %d\n", command.tick, ao_time());
-                       if (time_difference < 0)
-                               time_difference = -time_difference;
-                       if (time_difference > 10) {
-                               PRINTD ("time difference too large %d\n", time_difference);
+                       tick_difference = command.tick - (uint16_t) ao_time();
+                       PRINTD ("arm tick %d local tick %d\n", command.tick, (uint16_t) ao_time());
+                       if (tick_difference < 0)
+                               tick_difference = -tick_difference;
+                       if (tick_difference > 10) {
+                               PRINTD ("tick difference too large %d\n", tick_difference);
                                break;
                        }
                        if (query.arm_status != AO_PAD_ARM_STATUS_ARMED) {
@@ -439,7 +439,7 @@ ao_pad(void)
                                PRINTD ("not armed\n");
                                break;
                        }
-                       if ((uint16_t) (ao_time() - ao_pad_arm_time) > AO_SEC_TO_TICKS(20)) {
+                       if ((AO_TICK_SIGNED) (ao_time() - ao_pad_arm_time) > AO_SEC_TO_TICKS(20)) {
                                PRINTD ("late pad arm_time %d time %d\n",
                                        ao_pad_arm_time, ao_time());
                                break;
@@ -457,7 +457,7 @@ ao_pad(void)
 #if HAS_LOG
                        if (!ao_log_running) ao_log_start();
 #endif
-                       if ((uint16_t) (ao_time() - ao_pad_arm_time) > AO_SEC_TO_TICKS(20)) {
+                       if ((AO_TICK_SIGNED) (ao_time() - ao_pad_arm_time) > AO_SEC_TO_TICKS(20)) {
                                PRINTD ("late pad arm_time %d time %d\n",
                                        ao_pad_arm_time, ao_time());
                                break;
index 904dea0fe501f039e685366ae4b1d4664b916576..1238d72f01dae3316ab352d00d47ad558fe12fce 100644 (file)
@@ -151,13 +151,13 @@ static struct ao_task     ao_lco_drag_task;
 static void
 ao_lco_drag_monitor(void)
 {
-       uint16_t        delay = ~0;
-       uint16_t        now;
+       AO_TICK_TYPE    delay = ~0;
+       AO_TICK_TYPE    now;
 
        ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
        for (;;) {
                PRINTD("Drag monitor count %d delay %d\n", ao_lco_drag_beep_count, delay);
-               if (delay == (uint16_t) ~0)
+               if (delay == (AO_TICK_TYPE) ~0)
                        ao_sleep(&ao_lco_drag_beep_count);
                else
                        ao_sleep_for(&ao_lco_drag_beep_count, delay);