diff options
author | Alexander <trotsenkoa@gmail.com> | 2022-06-29 11:03:02 +0300 |
---|---|---|
committer | Alexander <trotsenkoa@gmail.com> | 2022-06-29 11:03:02 +0300 |
commit | 1702ce6ce430a66bb7af51644b91b7c196e719d9 (patch) | |
tree | 6039acbdf047873d32aaf55969286c5f0d55958f /Libraries/Button |
Создаю новый репозиторий. Программа для версии NixieClock_v2. Работает, часы тикают. Есть проблема, что скачет вторая сеносрная кнопка (она в другом канале). Поэтому на нее собираюсь сделать антидребезг.
Diffstat (limited to 'Libraries/Button')
-rw-r--r-- | Libraries/Button/button_handler.c | 253 | ||||
-rw-r--r-- | Libraries/Button/button_handler.h | 21 | ||||
-rw-r--r-- | Libraries/Button/button_task.c | 207 | ||||
-rw-r--r-- | Libraries/Button/button_task.h | 8 |
4 files changed, 489 insertions, 0 deletions
diff --git a/Libraries/Button/button_handler.c b/Libraries/Button/button_handler.c new file mode 100644 index 0000000..e25c2c8 --- /dev/null +++ b/Libraries/Button/button_handler.c @@ -0,0 +1,253 @@ +#include "button_handler.h" +#include "ltimers.h" + +#include "nixie_driver_process.h" + +// FreeRTOS includes +#include "FreeRTOS.h" +#include "task.h" +#include "queue.h" + +// +// , +// + + +#define TIME_BUTTON_LONG_PRESS 1000 // 3 + + +// +typedef enum { + + BUTTON_STATE_START = 0, + BUTTON_STATE_ONE_BUT_IS_PRESSING_NOW, + BUTTON_STATE_TWO_BUTS_AND_TIMER, + BUTTON_STATE_TWO_BUTS_AFTER_TIMER + +} Button_FSMStates_t; + +// ( , .) +enum { + + BUT_COMB_NONE = 0, + BUT_COMB_BUT1 = 1, + BUT_COMB_BUT2 = 2, + BUT_COMB_BOTH = 3 + +} ; + + +//extern const uint8_t tube_digits [MAX_DIGITS]; +//static uint8_t test_button_bufer[MAX_TUBES] = {TUBE_DIGIT_0}; +//static uint8_t cnt [6] = {0}; + +QueueHandle_t queue_but_comb; + +// - , +// , + + +// ---------------------------------------------------------------------------- +// +// ---------------------------------------------------------------------------- +void ButtonInit ( void ) +{ + queue_but_comb = xQueueCreate ( 1, sizeof (ButtonCombName_t) ); + configASSERT( queue_but_comb ); +} + + +// ---------------------------------------------------------------------------- +// - +// Note: +// , .. +// +// ---------------------------------------------------------------------------- +void Button_ProcessFSM ( void ) +{ + static Button_FSMStates_t state_fsm_button = BUTTON_STATE_START; + static uint8_t still_pushed = 0; + + static uint8_t prev_button_comb; + static uint8_t curr_button_comb; + static ButtonCombName_t msg_but_comb_name; + + switch ( state_fsm_button ) + { + // ------------------------------------------------------------------------- + case BUTTON_STATE_START: + + curr_button_comb = Button_GetCurrButtons(); + + if ( curr_button_comb != BUT_COMB_NONE ) + { + prev_button_comb = curr_button_comb; + state_fsm_button = BUTTON_STATE_ONE_BUT_IS_PRESSING_NOW; + still_pushed = 0; + } + + break; + // ------------------------------------------------------------------------- + case BUTTON_STATE_ONE_BUT_IS_PRESSING_NOW: + // - 1 + // - 2 + // - 3 , () + + curr_button_comb = Button_GetCurrButtons(); + + // , + if ( (curr_button_comb == BUT_COMB_NONE) && (still_pushed == 0) ) + { + // - .: " " + + switch ( prev_button_comb ) + { + case BUT_COMB_BUT1: + +// test_button_bufer[0] == TUBE_DIGIT_9 ? cnt[0] = 0 : cnt[0]++; +// test_button_bufer[0] = tube_digits [cnt[0]]; +// NixieDriver_SendValue ( &test_button_bufer[0] ); + xQueueReceive ( queue_but_comb, &msg_but_comb_name, 0 ); + msg_but_comb_name = BUTTON_SINGLE_FORWARD; + xQueueSend ( queue_but_comb, &msg_but_comb_name, 0 ); + + break; + case BUT_COMB_BUT2: +// test_button_bufer[1] == TUBE_DIGIT_9 ? cnt[1] = 0 : cnt[1]++; +// test_button_bufer[1] = tube_digits [cnt[1]]; +// NixieDriver_SendValue ( &test_button_bufer[0] ); + xQueueReceive ( queue_but_comb, &msg_but_comb_name, 0 ); + msg_but_comb_name = BUTTON_SINGLE_BACKWARD; + xQueueSend ( queue_but_comb, &msg_but_comb_name, 0 ); + + break; + + default: + break; + } + + state_fsm_button = BUTTON_STATE_START; + + return; + } + + if ( (curr_button_comb == BUT_COMB_NONE) && (still_pushed == 1) ) + { + state_fsm_button = BUTTON_STATE_START; + return; + } + + switch ( prev_button_comb ) + { + case BUT_COMB_BUT1: + + if ( curr_button_comb == BUT_COMB_BOTH ) + { + // - .: " " +// test_button_bufer[2] == TUBE_DIGIT_9 ? cnt[2] = 0 : cnt[2]++; +// test_button_bufer[2] = tube_digits [cnt[2]]; +// +// NixieDriver_SendValue ( &test_button_bufer[0] ); + xQueueReceive ( queue_but_comb, &msg_but_comb_name, 0 ); + //msg_but_comb_name = BUTTON_HOLD_FORWARD; + msg_but_comb_name = BUTTON_HOLD_BACKWARD; + xQueueSend ( queue_but_comb, &msg_but_comb_name, 0 ); + + StartLTimer ( LTIMER_BUTTON_LONG_PRESS ); + state_fsm_button = BUTTON_STATE_TWO_BUTS_AND_TIMER; + still_pushed = 1; + } + + break; + + case BUT_COMB_BUT2: + + if ( curr_button_comb == BUT_COMB_BOTH ) + { + // - .: " " +// test_button_bufer[3] == TUBE_DIGIT_9 ? cnt[3] = 0 : cnt[3]++; +// test_button_bufer[3] = tube_digits [cnt[3]]; +// +// NixieDriver_SendValue ( &test_button_bufer[0] ); + xQueueReceive ( queue_but_comb, &msg_but_comb_name, 0 ); + //msg_but_comb_name = BUTTON_HOLD_BACKWARD; + msg_but_comb_name = BUTTON_HOLD_FORWARD; + xQueueSend ( queue_but_comb, &msg_but_comb_name, 0 ); + + StartLTimer ( LTIMER_BUTTON_LONG_PRESS ); + state_fsm_button = BUTTON_STATE_TWO_BUTS_AND_TIMER; + still_pushed = 1; + } + + break; + + default: + state_fsm_button = BUTTON_STATE_START; + break; + } + + break; + + // ------------------------------------------------------------------------- + case BUTTON_STATE_TWO_BUTS_AND_TIMER: + + curr_button_comb = Button_GetCurrButtons(); + + if ( curr_button_comb == BUT_COMB_BOTH ) + { + if ( GetLTimer (LTIMER_BUTTON_LONG_PRESS) == TIME_BUTTON_LONG_PRESS ) + { + // - .: " " +// test_button_bufer[4] == TUBE_DIGIT_9 ? cnt[4] = 0 : cnt[4]++; +// test_button_bufer[4] = tube_digits [cnt[4]]; +// +// NixieDriver_SendValue ( &test_button_bufer[0] ); + xQueueReceive ( queue_but_comb, &msg_but_comb_name, 0 ); + msg_but_comb_name = BUTTON_LONG; + xQueueSend ( queue_but_comb, &msg_but_comb_name, 0 ); + + state_fsm_button = BUTTON_STATE_TWO_BUTS_AFTER_TIMER; + } + } + else + { + prev_button_comb = curr_button_comb; + state_fsm_button = BUTTON_STATE_ONE_BUT_IS_PRESSING_NOW; + } + + break; + + // ------------------------------------------------------------------------- + case BUTTON_STATE_TWO_BUTS_AFTER_TIMER: + + curr_button_comb = Button_GetCurrButtons(); + + if ( curr_button_comb == BUT_COMB_NONE ) + { + state_fsm_button = BUTTON_STATE_START; + } + else if ( curr_button_comb != BUT_COMB_BOTH ) + { + prev_button_comb = curr_button_comb; + state_fsm_button = BUTTON_STATE_ONE_BUT_IS_PRESSING_NOW; + } + + break; + + default: + break; + } +} + + + + + + + + + + + + + diff --git a/Libraries/Button/button_handler.h b/Libraries/Button/button_handler.h new file mode 100644 index 0000000..76b21d1 --- /dev/null +++ b/Libraries/Button/button_handler.h @@ -0,0 +1,21 @@ +#ifndef BUTTON_HANDLER_INCLUDED +#define BUTTON_HANDLER_INCLUDED + +#include <stdint.h> + + +typedef enum { + + BUTTON_SINGLE_FORWARD, + BUTTON_SINGLE_BACKWARD, + BUTTON_HOLD_FORWARD, + BUTTON_HOLD_BACKWARD, + BUTTON_LONG + +} ButtonCombName_t; + +void ButtonInit ( void ); +uint8_t Button_GetCurrButtons ( void ); +void Button_ProcessFSM ( void ); + +#endif //BUTTON_HANDLER_INCLUDED
\ No newline at end of file diff --git a/Libraries/Button/button_task.c b/Libraries/Button/button_task.c new file mode 100644 index 0000000..fb5a8c9 --- /dev/null +++ b/Libraries/Button/button_task.c @@ -0,0 +1,207 @@ +#include "head_task.h" +#include "tsl_user.h" +#include "nixie_driver_process.h" + +#include "button_handler.h" + +// FreeRTOS includes +#include "FreeRTOS.h" +#include "task.h" +#include "queue.h" +#include "semphr.h" + + +// +//enum { +// TKEY_NUM_1 = 0, +// TKEY_NUM_2 +//}; + +#define TKEY_NUM_1 0 +#define TKEY_NUM_2 1 + +// - Button_Module + +#define TEST_TKEY(NB) ((MyTKeys[(NB)].p_Data->StateId == TSL_STATEID_DETECT) ||\ + (MyTKeys[(NB)].p_Data->StateId == TSL_STATEID_DEB_RELEASE_DETECT)) + +#define TEST_LINROT(NB) ((MyLinRots[(NB)].p_Data->StateId == TSL_STATEID_DETECT) ||\ + (MyLinRots[(NB)].p_Data->StateId == TSL_STATEID_DEB_RELEASE_DETECT)) + +// For debug purpose with STMStudio +uint8_t DS[TSLPRM_TOTAL_TKEYS + TSLPRM_TOTAL_LNRTS]; // To store the States (one per object) +int16_t DD[TSLPRM_TOTAL_TKEYS + (TSLPRM_TOTAL_LNRTS * 3)]; // To store the Deltas (one per channel) + +//static uint8_t touch_tube_bufer [MAX_TUBES] = {0}; + +// , +// +static volatile uint8_t tkey_buttons_bits = 0; + + +static void ProcessSensors ( void ); + + +// ---------------------------------------------------------------------------- +// - +// button_handler.c (- Button_ProcessFSM ();) +// ---------------------------------------------------------------------------- +uint8_t Button_GetCurrButtons ( void ) +{ + uint8_t curr_buts_bits = tkey_buttons_bits; + return curr_buts_bits; +} + +// ---------------------------------------------------------------------------- +// +// ---------------------------------------------------------------------------- +void ProcessFSM_ButtonTask ( void ) +{ + /* Execute STMTouch Driver state machine */ + if (TSL_user_Action () == TSL_STATUS_OK) + { + ProcessSensors (); // Execute sensors related tasks + + // () + //Button_ProcessFSM (); + } + Button_ProcessFSM (); + taskYIELD(); +} + + +/** + * @brief Manage the activity on sensors when touched/released (example) + * @param None + * @retval None + */ +static void ProcessSensors ( void ) +{ + uint32_t idx; + //uint32_t idxch; + uint32_t idx_ds = 0; + uint32_t idx_dd = 0; + //uint32_t touched = 0; +#if USE_LCD > 0 + static uint32_t started = 0; +#endif + +#if TSLPRM_TOTAL_TKEYS > 0 + // Read all TKeys + for (idx = 0; idx < TSLPRM_TOTAL_TKEYS; idx++) + { + // STMStudio debug + DS[idx_ds++] = MyTKeys[idx].p_Data->StateId; + DD[idx_dd++] = MyTKeys[idx].p_ChD->Delta; +// if (TEST_TKEY(idx)) +// { +// touched = 1; +// } + } +#endif + +#if TSLPRM_TOTAL_LNRTS > 0 + uint32_t idxch; + // Read all Linear and Rotary sensors + for (idx = 0; idx < TSLPRM_TOTAL_LNRTS; idx++) + { + // STMStudio debug + DS[idx_ds++] = MyLinRots[idx].p_Data->StateId; + for (idxch = 0; idxch < MyLinRots[idx].NbChannels; idxch++) + { + DD[idx_dd++] = MyLinRots[idx].p_ChD[idxch].Delta; + } +// if (TEST_LINROT(idx)) +// { +// touched = 1; +// } + } +#endif + + + // + + if (TEST_TKEY(TKEY_NUM_2)) + { + tkey_buttons_bits |= 1 << TKEY_NUM_1; + GPIOB->ODR ^= GPIO_Pin_2; + } + else + { + tkey_buttons_bits &= ~(1 << TKEY_NUM_1); + GPIOB->ODR ^= GPIO_Pin_2; + } + + if (TEST_TKEY(TKEY_NUM_1)) + { + tkey_buttons_bits |= 1 << TKEY_NUM_2; + GPIOB->ODR ^= GPIO_Pin_2; + + } + else + { + tkey_buttons_bits &= ~(1 << TKEY_NUM_2); + GPIOB->ODR ^= GPIO_Pin_2; + } + +} + +/** + * @brief Executed when a sensor is in Off state + * @param None + * @retval None + */ +void MyTKeys_OffStateProcess(void) +{ + /* Add here your own processing when a sensor is in Off state */ +} + +void MyLinRots_OffStateProcess(void) +{ + /* Add here your own processing when a sensor is in Off state */ +} + +/** + * @brief Executed at each timer interruption (option must be enabled) + * @param None + * @retval None + */ +void TSL_CallBack_TimerTick(void) +{ +} + +/** + * @brief Executed when a sensor is in Error state + * @param None + * @retval None + */ +void MyTKeys_ErrorStateProcess(void) +{ + /* Add here your own processing when a sensor is in Error state */ + //TSL_linrot_SetStateOff(); + TSL_tkey_SetStateOff (); + while(1) + { + } +} + +void MyLinRots_ErrorStateProcess(void) +{ + /* Add here your own processing when a sensor is in Error state */ + TSL_linrot_SetStateOff(); + //TSL_tkey_SetStateOff (); + while(1) + { + } +} + + +// ---------------------------------------------------------------------------- +// , +// +// ---------------------------------------------------------------------------- +void Button_Task ( void *pvParameters ) +{ + while(1)ProcessFSM_ButtonTask (); + //vTaskDelete(NULL); +}
\ No newline at end of file diff --git a/Libraries/Button/button_task.h b/Libraries/Button/button_task.h new file mode 100644 index 0000000..c55f8c3 --- /dev/null +++ b/Libraries/Button/button_task.h @@ -0,0 +1,8 @@ +#ifndef BUTTON_TASK_INCLUDED +#define BUTTON_TASK_INCLUDED + +#include "stm32f0xx.h" + +void Button_Task ( void *pvParameters ); + +#endif //BUTTON_TASK_INCLUDED
\ No newline at end of file |