From 54e98a498db0d7026ce16c3ad53bc60986c68253 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Wed, 22 Jan 2020 12:41:40 -0800 Subject: [PATCH] altos: Use AO_TICK_TYPE/AO_TICK_SIGNED for lco/pad code 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 --- src/drivers/ao_lco.c | 18 +++++++++--------- src/drivers/ao_lco.h | 8 ++++---- src/drivers/ao_lco_bits.c | 26 +++++++++++++------------- src/drivers/ao_lco_cmd.c | 1 - src/drivers/ao_lco_func.c | 6 +++--- src/drivers/ao_pad.c | 24 ++++++++++++------------ src/telelco-v2.0/ao_lco_v2.c | 6 +++--- 7 files changed, 44 insertions(+), 45 deletions(-) diff --git a/src/drivers/ao_lco.c b/src/drivers/ao_lco.c index b314ef7c..0d7e4cba 100644 --- a/src/drivers/ao_lco.c +++ b/src/drivers/ao_lco.c @@ -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); diff --git a/src/drivers/ao_lco.h b/src/drivers/ao_lco.h index cdc19d3d..3b123793 100644 --- a/src/drivers/ao_lco.h +++ b/src/drivers/ao_lco.h @@ -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 diff --git a/src/drivers/ao_lco_bits.c b/src/drivers/ao_lco_bits.c index 6e50e44d..bc54dc22 100644 --- a/src/drivers/ao_lco_bits.c +++ b/src/drivers/ao_lco_bits.c @@ -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; } diff --git a/src/drivers/ao_lco_cmd.c b/src/drivers/ao_lco_cmd.c index 6f195e55..01843635 100644 --- a/src/drivers/ao_lco_cmd.c +++ b/src/drivers/ao_lco_cmd.c @@ -28,7 +28,6 @@ static uint16_t lco_box; static uint8_t lco_channels; -static uint16_t tick_offset; static void lco_args(void) diff --git a/src/drivers/ao_lco_func.c b/src/drivers/ao_lco_func.c index 06350694..1960683f 100644 --- a/src/drivers/ao_lco_func.c +++ b/src/drivers/ao_lco_func.c @@ -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; diff --git a/src/drivers/ao_pad.c b/src/drivers/ao_pad.c index 2592a084..07c49afc 100644 --- a/src/drivers/ao_pad.c +++ b/src/drivers/ao_pad.c @@ -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; diff --git a/src/telelco-v2.0/ao_lco_v2.c b/src/telelco-v2.0/ao_lco_v2.c index 904dea0f..1238d72f 100644 --- a/src/telelco-v2.0/ao_lco_v2.c +++ b/src/telelco-v2.0/ao_lco_v2.c @@ -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); -- 2.30.2