From: sjlongland Date: Thu, 24 Sep 2015 08:12:00 +0000 (+0000) Subject: sm1000_leds_switches: Re-locate switch debounce code. X-Git-Url: http://git.whiteaudio.com/gitweb/?a=commitdiff_plain;h=2bff6cc43982649526b3d4afaa9bdd278b39f3a2;p=freetel-svn-tracking.git sm1000_leds_switches: Re-locate switch debounce code. git-svn-id: https://svn.code.sf.net/p/freetel/code@2362 01035d8c-6547-0410-b346-abe4f91aad63 --- diff --git a/codec2-dev/stm32/inc/sm1000_leds_switches.h b/codec2-dev/stm32/inc/sm1000_leds_switches.h index b98f74df..a7fadd4f 100644 --- a/codec2-dev/stm32/inc/sm1000_leds_switches.h +++ b/codec2-dev/stm32/inc/sm1000_leds_switches.h @@ -41,6 +41,35 @@ int switch_select(void); int switch_back(void); int ext_ptt(void); +/*! Switch debounce and logic handling */ +struct switch_t { + /*! Debounce/hold timer */ + uint32_t timer; + /*! Current/debounced observed switch state */ + uint8_t sw; + /*! Raw observed switch state (during debounce) */ + uint8_t raw; + /*! Last steady-state switch state */ + uint8_t last; + /*! Debouncer state */ + uint8_t state; +}; + +/*! Update the state of a switch */ +void switch_update(struct switch_t* const sw, uint8_t state); + +/*! Acknowledge the current state of the switch */ +void switch_ack(struct switch_t* const sw); + +/*! Return how long the switch has been pressed in ticks. */ +uint32_t switch_pressed(const struct switch_t* const sw); + +/*! Return non-zero if the switch has been released. */ +int switch_released(const struct switch_t* const sw); + +/*! Count the tick timers on the switches. */ +void switch_tick(struct switch_t* const sw); + void ColorfulRingOfDeath(int code); #endif diff --git a/codec2-dev/stm32/src/sm1000_leds_switches.c b/codec2-dev/stm32/src/sm1000_leds_switches.c index aa633711..b08d5333 100644 --- a/codec2-dev/stm32/src/sm1000_leds_switches.c +++ b/codec2-dev/stm32/src/sm1000_leds_switches.c @@ -158,3 +158,64 @@ void HardFault_Handler(void) { ColorfulRingOfDeath(1); } void MemManage_Handler(void) { ColorfulRingOfDeath(2); } void BusFault_Handler(void) { ColorfulRingOfDeath(3); } void UsageFault_Handler(void){ ColorfulRingOfDeath(4); } + + +static void switch_tick(struct switch_t* const sw) +{ + if (sw->sw != sw->raw) { + /* State transition, reset timer */ + if (sw->state == SW_STEADY) + sw->last = sw->sw; + sw->state = SW_DEBOUNCE; + sw->timer = DEBOUNCE_DELAY; + sw->sw = sw->raw; + } else if (sw->state == SW_DEBOUNCE) { + if (sw->timer > 0) { + /* Steady so far, keep waiting */ + sw->timer--; + } else { + /* Steady state reached */ + sw->state = SW_STEADY; + } + } else if (sw->sw) { + /* Hold state. Yes this will wrap, but who cares? */ + sw->timer++; + } +} + +void switch_update(struct switch_t* const sw, uint8_t state) +{ + sw->raw = state; + if (sw->raw == sw->sw) + return; + + if (sw->state == SW_STEADY) + sw->last = sw->sw; + sw->timer = DEBOUNCE_DELAY; + sw->sw = sw->raw; + sw->state = SW_DEBOUNCE; +} + +uint32_t switch_pressed(const struct switch_t* const sw) +{ + if ((sw->state == SW_STEADY) && sw->sw) + return sw->timer; + return 0; +} + +int switch_released(const struct switch_t* const sw) +{ + if (sw->state != SW_STEADY) + return 0; + if (!sw->last) + return 0; + if (sw->sw) + return 0; + return 1; +} + +void switch_ack(struct switch_t* const sw) +{ + if (sw->state == SW_STEADY) + sw->last = sw->sw; +} diff --git a/codec2-dev/stm32/src/sm1000_main.c b/codec2-dev/stm32/src/sm1000_main.c index 8b7f983b..22c183e4 100644 --- a/codec2-dev/stm32/src/sm1000_main.c +++ b/codec2-dev/stm32/src/sm1000_main.c @@ -54,43 +54,10 @@ #define SW_STEADY 0 /*!< Switch is in steady-state */ #define SW_DEBOUNCE 1 /*!< Switch is being debounced */ -/*! Switch debounce and logic handling */ -struct switch_t { - /*! Debounce/hold timer */ - uint32_t timer; - /*! Current/debounced observed switch state */ - uint8_t sw; - /*! Raw observed switch state (during debounce) */ - uint8_t raw; - /*! Last steady-state switch state */ - uint8_t last; - /*! Debouncer state */ - uint8_t state; -}; - struct switch_t sw_select; /*!< Switch driver for SELECT button */ struct switch_t sw_back; /*!< Switch driver for BACK button */ struct switch_t sw_ptt; /*!< Switch driver for PTT buttons */ -/*! - * Count the tick timers on the switches. - */ -static void switch_tick(struct switch_t* const sw); - -/*! - * Update the state of a switch - */ -static void switch_update(struct switch_t* const sw, uint8_t state); - -/*! Return how long the switch has been pressed in ticks. */ -static uint32_t switch_pressed(const struct switch_t* const sw); - -/*! Return non-zero if the switch has been released. */ -static int switch_released(const struct switch_t* const sw); - -/*! Acknowledge the current state of the switch */ -static void switch_ack(struct switch_t* const sw); - unsigned int announceTicker = 0; /*! @@ -329,63 +296,3 @@ void SysTick_Handler(void) announceTicker--; } } - -static void switch_tick(struct switch_t* const sw) -{ - if (sw->sw != sw->raw) { - /* State transition, reset timer */ - if (sw->state == SW_STEADY) - sw->last = sw->sw; - sw->state = SW_DEBOUNCE; - sw->timer = DEBOUNCE_DELAY; - sw->sw = sw->raw; - } else if (sw->state == SW_DEBOUNCE) { - if (sw->timer > 0) { - /* Steady so far, keep waiting */ - sw->timer--; - } else { - /* Steady state reached */ - sw->state = SW_STEADY; - } - } else if (sw->sw) { - /* Hold state. Yes this will wrap, but who cares? */ - sw->timer++; - } -} - -static void switch_update(struct switch_t* const sw, uint8_t state) -{ - sw->raw = state; - if (sw->raw == sw->sw) - return; - - if (sw->state == SW_STEADY) - sw->last = sw->sw; - sw->timer = DEBOUNCE_DELAY; - sw->sw = sw->raw; - sw->state = SW_DEBOUNCE; -} - -static uint32_t switch_pressed(const struct switch_t* const sw) -{ - if ((sw->state == SW_STEADY) && sw->sw) - return sw->timer; - return 0; -} - -static int switch_released(const struct switch_t* const sw) -{ - if (sw->state != SW_STEADY) - return 0; - if (!sw->last) - return 0; - if (sw->sw) - return 0; - return 1; -} - -static void switch_ack(struct switch_t* const sw) -{ - if (sw->state == SW_STEADY) - sw->last = sw->sw; -}