|
@@ -10,6 +10,7 @@ static const ioline_t button_Lines[Button_Num] = {
|
|
};
|
|
};
|
|
|
|
|
|
/* Variables */
|
|
/* Variables */
|
|
|
|
+static binary_semaphore_t btn_bsem;
|
|
static virtual_timer_t button_vt;
|
|
static virtual_timer_t button_vt;
|
|
static button_state_t button_States[Button_Num] = {0};
|
|
static button_state_t button_States[Button_Num] = {0};
|
|
static uint8_t button_Time[Button_Num] = {0};
|
|
static uint8_t button_Time[Button_Num] = {0};
|
|
@@ -18,40 +19,57 @@ static uint8_t button_Time[Button_Num] = {0};
|
|
static void button_Process(virtual_timer_t *vtp, void *p) {
|
|
static void button_Process(virtual_timer_t *vtp, void *p) {
|
|
(void)vtp;
|
|
(void)vtp;
|
|
(void)p;
|
|
(void)p;
|
|
|
|
+
|
|
|
|
+ /* Entering I-Locked state and signaling the semaphore.*/
|
|
|
|
+ chSysLockFromISR();
|
|
|
|
+ chBSemSignalI(&btn_bsem);
|
|
|
|
+ chSysUnlockFromISR();
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/*
|
|
|
|
+ * BTN serving thread.
|
|
|
|
+ */
|
|
|
|
+static THD_WORKING_AREA(waBTNThread, 128);
|
|
|
|
+static THD_FUNCTION(BTNThread, arg) {
|
|
|
|
+ (void)arg;
|
|
static int pause = 0;
|
|
static int pause = 0;
|
|
|
|
|
|
- if (pause != 0) {
|
|
|
|
- pause --;
|
|
|
|
- } else {
|
|
|
|
-
|
|
|
|
- int i;
|
|
|
|
- for (i=0; i<Button_Num; i++) {
|
|
|
|
-
|
|
|
|
- if (palReadLine(button_Lines[(button_num_t)i]) == BUTTON_PRESSED) {
|
|
|
|
- /* button pressed */
|
|
|
|
- button_Time[i] ++;
|
|
|
|
- if (button_Time[i] >= (BTN_TIME_HOLDED/BTN_SCAN_PERIOD)) {
|
|
|
|
- /* process long press */
|
|
|
|
- button_Time[i] -= (BTN_TIME_REPEATED/BTN_SCAN_PERIOD);
|
|
|
|
- button_States[i] = BTN_st_Holded;
|
|
|
|
- btnh[i](BTN_st_Pressed); // autorepeat
|
|
|
|
- }
|
|
|
|
|
|
+ while (true) {
|
|
|
|
+ /* Waiting for an binary semaphore. */
|
|
|
|
+ chBSemWait(&btn_bsem);
|
|
|
|
+
|
|
|
|
+ if (pause != 0) {
|
|
|
|
+ pause --;
|
|
|
|
+ } else {
|
|
|
|
|
|
- } else if (button_Time[i] != 0) {
|
|
|
|
- /* button released */
|
|
|
|
- if (button_Time[i] >= (BTN_TIME_PRESSED/BTN_SCAN_PERIOD)) {
|
|
|
|
- /* process short press */
|
|
|
|
- button_States[i] = BTN_st_Pressed;
|
|
|
|
|
|
+ int i;
|
|
|
|
+ for (i=0; i<Button_Num; i++) {
|
|
|
|
+
|
|
|
|
+ if (palReadLine(button_Lines[(button_num_t)i]) == BUTTON_PRESSED) {
|
|
|
|
+ /* button pressed */
|
|
|
|
+ button_Time[i] ++;
|
|
|
|
+ if (button_Time[i] >= (BTN_TIME_HOLDED/BTN_SCAN_PERIOD)) {
|
|
|
|
+ /* process long press */
|
|
|
|
+ button_Time[i] -= (BTN_TIME_REPEATED/BTN_SCAN_PERIOD);
|
|
|
|
+ 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/BTN_SCAN_PERIOD)) {
|
|
|
|
+ /* 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/BTN_SCAN_PERIOD);
|
|
btnh[i](BTN_st_Pressed);
|
|
btnh[i](BTN_st_Pressed);
|
|
}
|
|
}
|
|
- button_Time[i] = 0;
|
|
|
|
- button_States[i] = BTN_st_Released;
|
|
|
|
- pause = (BTN_SCAN_PAUSE/BTN_SCAN_PERIOD);
|
|
|
|
- btnh[i](BTN_st_Pressed);
|
|
|
|
- }
|
|
|
|
- } /* end FOR */
|
|
|
|
-
|
|
|
|
- } /* end Pause check */
|
|
|
|
|
|
+ } /* end FOR */
|
|
|
|
+ } /* end Pause check */
|
|
|
|
+ } /* end WHILE */
|
|
}
|
|
}
|
|
|
|
|
|
void buttons_Init(btn_hndlr ha[]) {
|
|
void buttons_Init(btn_hndlr ha[]) {
|
|
@@ -60,6 +78,13 @@ void buttons_Init(btn_hndlr ha[]) {
|
|
btnh[Button3] = ha[Button3];
|
|
btnh[Button3] = ha[Button3];
|
|
btnh[Button4] = ha[Button4];
|
|
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);
|
|
chVTSetContinuous(&button_vt, TIME_MS2I(BTN_SCAN_PERIOD), button_Process, NULL);
|
|
}
|
|
}
|
|
|
|
|