Skip to content

Commit 8616844

Browse files
committed
cleanup: GPIO Button handling
1 parent b41f5ba commit 8616844

4 files changed

Lines changed: 18 additions & 16 deletions

File tree

firmware/preloader/src/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ int main(void) {
7272

7373

7474
// 2 second wait, debounced
75-
if (!(GPIOA->IDR & GPIO_IDR_ID13)) {
75+
if (!(GPIOA->IDR & (1 << PIN_13))) {
7676
delayMs(DEBOUNCE_DELAY);
77-
if (!(GPIOA->IDR & GPIO_IDR_ID0)) { // check again
77+
if (!(GPIOA->IDR & (1 << PIN_13))) { // check again
7878
// button pressed -> system bootloader
7979
resetToSystemBootLoader();
8080
}

firmware/userapp/Core/Inc/button.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ extern "C" {
99

1010
extern bool button_pressed;
1111

12-
bool Button_IsPressed(uint32_t *duration_ms);
12+
bool Button_IsPressed();
13+
uint32_t Button_IsPressedDuration();
1314
void Button_Tick();
1415

1516
#ifdef __cplusplus

firmware/userapp/Core/Src/button.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
#include "button.h"
22

33
#define DELAY_TIME_MS 10
4-
5-
static uint32_t btnpress_duration = 0;
64
bool button_pressed = false;
75

8-
bool Button_IsPressed(uint32_t *duration_ms) {
9-
*duration_ms = 0;
10-
if (HAL_GPIO_ReadPin(ENTER_BL_GPIO_Port, ENTER_BL_Pin) == GPIO_PIN_RESET) {
6+
bool Button_IsPressed() {
7+
return (HAL_GPIO_ReadPin(ENTER_BL_GPIO_Port, ENTER_BL_Pin) == GPIO_PIN_RESET);
8+
}
9+
10+
uint32_t Button_IsPressedDuration() {
11+
uint32_t duration_ms = 0;
12+
if (Button_IsPressed()) {
1113
// Measure duration of button press
12-
while (HAL_GPIO_ReadPin(ENTER_BL_GPIO_Port, ENTER_BL_Pin) == GPIO_PIN_RESET) {
14+
while (Button_IsPressed()) {
1315
HAL_Delay(DELAY_TIME_MS);
14-
*duration_ms += DELAY_TIME_MS;
16+
duration_ms += DELAY_TIME_MS;
1517
}
16-
return true;
1718
}
18-
return false;
19+
return duration_ms;
1920
}
2021

2122
void Button_Tick() {
22-
if (Button_IsPressed(&btnpress_duration)) {
23-
// Do something
24-
}
23+
button_pressed = Button_IsPressed();
2524
}

firmware/userapp/Core/Src/display.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "u8g2.h"
33
#include "postcodes.h"
44
#include "bootloader.h"
5+
#include "button.h"
56

67
#define OLED_Addr (0x3C << 1)
78
#define DISPLAY_INIT_FUNC u8g2_Setup_ssd1306_i2c_64x32_1f_f
@@ -133,7 +134,6 @@ void Display_Init(void)
133134

134135
// Cleanup
135136
u8g2_ClearDisplay(&myDisplay);
136-
Display_ShowCode(0x4242, 0x71);
137137
}
138138

139139
void Display_ShowCode(uint16_t code, uint8_t segment) {
@@ -167,6 +167,8 @@ void Display_Tick(void)
167167
POST_ReadCode(),
168168
POST_ReadSegment()
169169
);
170+
} else if (Button_IsPressed()) {
171+
Display_Print("BtnPress");
170172
}
171173
}
172174

0 commit comments

Comments
 (0)