#include #include #include "buttons.h" static btn_hndlr btnh[Button_Num]; /* Constants */ static const ioline_t button_Lines[Button_Num] = { LINE_BTN_1, LINE_BTN_2, LINE_BTN_3, LINE_BTN_4 }; /* from board.h */ /* Variables */ static binary_semaphore_t btn_bsem; static virtual_timer_t button_vt; static button_state_t button_States[Button_Num] = {0}; static uint8_t button_Time[Button_Num] = {0}; /* Functions */ static void button_Process(virtual_timer_t *vtp, void *p) { (void)vtp; (void)p; /* Entering I-Locked state and signaling the semaphore.*/ chSysLockFromISR(); chBSemSignalI(&btn_bsem); chSysUnlockFromISR(); } /* * BTN serving thread. */ static THD_WORKING_AREA(waBTNThread, 256); static THD_FUNCTION(BTNThread, arg) { (void)arg; static int pause = 0; while (true) { /* Waiting for an binary semaphore. */ chBSemWait(&btn_bsem); if (pause != 0) { pause --; } else { int i; for (i=0; i= BTN_TIME_HOLDED) { /* process long press */ button_Time[i] -= BTN_TIME_REPEATED; button_States[i] = BTN_st_Holded; btnh[i](BTN_st_Pressed); // autorepeat } } else if (button_Time[i] != 0) { /* button released */ if (button_Time[i] >= BTN_TIME_PRESSED) { /* process short press */ button_States[i] = BTN_st_Pressed; btnh[i](BTN_st_Pressed); } button_Time[i] = 0; button_States[i] = BTN_st_Released; pause = BTN_SCAN_PAUSE; //btnh[i](BTN_st_Released); } } /* end FOR */ } /* end Pause check */ } /* end WHILE */ } void buttons_Init(btn_hndlr ha[]) { btnh[Button1] = ha[Button1]; btnh[Button2] = ha[Button2]; btnh[Button3] = ha[Button3]; btnh[Button4] = ha[Button4]; /* Initializing the Button semaphore in the "taken" state. */ chBSemObjectInit(&btn_bsem, true); /* Starting the Button thread. */ chThdCreateStatic(waBTNThread, sizeof(waBTNThread), NORMALPRIO, BTNThread, NULL); /* Starting a virtual timer ticking the thread. */ chVTSetContinuous(&button_vt, TIME_MS2I(BTN_SCAN_PERIOD), button_Process, NULL); } button_state_t buttons_GetState(const button_num_t btn) { return button_States[btn]; }