Skip to content

Commit

Permalink
fix: rotary switch debouncing
Browse files Browse the repository at this point in the history
  • Loading branch information
federico-carbone committed Aug 31, 2024
1 parent 14577ce commit 44b2f1a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Core/Inc/conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
/* ---------- Exported constants --------------------------------------------*/
#define VERSION_MAJOR 1
#define VERSION_MINOR 0
#define VERSION_PATCH 3
#define VERSION_PATCH 4

#define DEBUG_SERIAL
#define DEBUG_AIN
Expand Down
47 changes: 30 additions & 17 deletions Core/Src/bsp.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,43 +207,56 @@ static float ROT_SW_ain_V_to_state_map[ROT_SW_State_NUM][2U] = {
(4.89) + ROT_SW_STATE_ERR_MARGIN_V},
};
/*---------- Private Functions -----------------------------------------------*/
enum ROT_SW_State __ROT_SW_analogV_to_state(float analogV) {
if (analogV < 0) {
// Error condition
return ROT_SW_State_NUM;
} else if (analogV >= ROT_SW_ain_V_to_state_map[ROT_SW_State0][0] &&
enum ROT_SW_State __ROT_SW_analogV_to_state(float analogV, enum ROT_SW_Device device) {
static enum ROT_SW_State last_state1 = ROT_SW_State_NUM+1;
static enum ROT_SW_State last_state2 = ROT_SW_State_NUM+1;

enum ROT_SW_State *last_state = device == ROT_SW_Device1 ? &last_state1 : &last_state2;
enum ROT_SW_State state = ROT_SW_State_NUM+1;

if (analogV >= ROT_SW_ain_V_to_state_map[ROT_SW_State0][0] &&
analogV < ROT_SW_ain_V_to_state_map[ROT_SW_State0][1]) {
return ROT_SW_State0;
state = ROT_SW_State0;
} else if (analogV >= ROT_SW_ain_V_to_state_map[ROT_SW_State1][0] &&
analogV < ROT_SW_ain_V_to_state_map[ROT_SW_State1][1]) {
return ROT_SW_State1;
state = ROT_SW_State1;
} else if (analogV >= ROT_SW_ain_V_to_state_map[ROT_SW_State2][0] &&
analogV < ROT_SW_ain_V_to_state_map[ROT_SW_State2][1]) {
return ROT_SW_State2;
state = ROT_SW_State2;
} else if (analogV >= ROT_SW_ain_V_to_state_map[ROT_SW_State3][0] &&
analogV < ROT_SW_ain_V_to_state_map[ROT_SW_State3][1]) {
return ROT_SW_State3;
state = ROT_SW_State3;
} else if (analogV >= ROT_SW_ain_V_to_state_map[ROT_SW_State4][0] &&
analogV < ROT_SW_ain_V_to_state_map[ROT_SW_State4][1]) {
return ROT_SW_State4;
state = ROT_SW_State4;
} else if (analogV >= ROT_SW_ain_V_to_state_map[ROT_SW_State5][0] &&
analogV < ROT_SW_ain_V_to_state_map[ROT_SW_State5][1]) {
return ROT_SW_State5;
state = ROT_SW_State5;
} else if (analogV >= ROT_SW_ain_V_to_state_map[ROT_SW_State6][0] &&
analogV < ROT_SW_ain_V_to_state_map[ROT_SW_State6][1]) {
return ROT_SW_State6;
state = ROT_SW_State6;
} else if (analogV >= ROT_SW_ain_V_to_state_map[ROT_SW_State7][0] &&
analogV < ROT_SW_ain_V_to_state_map[ROT_SW_State7][1]) {
return ROT_SW_State7;
state = ROT_SW_State7;
} else if (analogV >= ROT_SW_ain_V_to_state_map[ROT_SW_State8][0] &&
analogV < ROT_SW_ain_V_to_state_map[ROT_SW_State8][1]) {
return ROT_SW_State8;
state = ROT_SW_State8;
} else if (analogV >= ROT_SW_ain_V_to_state_map[ROT_SW_State9][0] &&
analogV < ROT_SW_ain_V_to_state_map[ROT_SW_State9][1]) {
return ROT_SW_State9;
state = ROT_SW_State9;
}

if(*last_state == ROT_SW_State_NUM+1) {
*last_state = state;
}

if(state == *last_state || state == (*last_state)+1 || state == (*last_state)-1) {
*last_state = state;
} else {
return ROT_SW_State_NUM;
state = *last_state;
}

return state;
}

/*---------- Exported Variables ----------------------------------------------*/
Expand All @@ -260,7 +273,7 @@ float ROT_SW_getAanalog_V(enum ROT_SW_Device device) {
}
enum ROT_SW_State ROT_SW_getState(enum ROT_SW_Device device) {
assert_param(device != ROT_SW_State_NUM);
return __ROT_SW_analogV_to_state(ROT_SW_getAanalog_V(device));
return __ROT_SW_analogV_to_state(ROT_SW_getAanalog_V(device), device);
}

/* LED MONO (Monochrome Leds) ################################################*/
Expand Down

0 comments on commit 44b2f1a

Please sign in to comment.