Skip to content

Commit 21fb227

Browse files
committed
button support and binding procedure updated
1 parent 9db0561 commit 21fb227

File tree

10 files changed

+109
-13
lines changed

10 files changed

+109
-13
lines changed

.vscode/settings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
"console.h": "c",
1313
"esp_nimble_hci.h": "c",
1414
"stdio.h": "c",
15-
"freertosconfig.h": "c"
15+
"freertosconfig.h": "c",
16+
"inttypes.h": "c",
17+
"glue.h": "c"
1618
}
1719
}

components/ble/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ set(srcs "gap.c" "gatt.c" "ble.c")
22

33
idf_component_register(SRCS "${srcs}"
44
INCLUDE_DIRS "include"
5-
REQUIRES bt ssd1306 crsf nvs_flash battery
5+
REQUIRES bt ssd1306 crsf nvs_flash battery button
66
PRIV_REQUIRES )

components/ble/gap.c

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,18 +194,24 @@ int bleGAPEevent(struct ble_gap_event *event, void *arg) {
194194
ESP_LOGI(tag_GAP, "PASSKEY_ACTION_EVENT started \n");
195195

196196
struct ble_sm_io pkey = {0};
197-
int key = 0;
197+
BUTTON selectedButton = RIGHTBUTTON;
198198

199199
if (event->passkey.params.action == BLE_SM_IOACT_DISP) {
200200
//BLE_SM_IO_CAP_DISP_ONLY option
201201
pkey.action = event->passkey.params.action;
202-
pkey.passkey = 123456;
203202

203+
pkey.passkey = esp_random();
204+
/* Max value is 999999 */
205+
pkey.passkey %= 1000000;
206+
207+
char pinStr[12];
208+
snprintf(pinStr, 12,"%06d", (int)pkey.passkey);
209+
204210
ssd1306_clear();
205211
ssd1306_setString("PIN",48,0);
206-
ssd1306_setString("123456",31,18);
212+
ssd1306_setString(pinStr,31,18);
207213
ssd1306_display();
208-
//display passkey for the other side 123456
214+
//display passkey for the other side
209215

210216
rc = ble_sm_inject_io(event->passkey.conn_handle, &pkey);
211217
ESP_LOGI(tag_GAP, "ble_sm_inject_io result: %d\n", rc);
@@ -215,22 +221,35 @@ int bleGAPEevent(struct ble_gap_event *event, void *arg) {
215221
//gegenseite kann auch mit yes oder no bestätigen den pin
216222
//verify with buttons Yes or No --> add timeout after x seconds
217223
char pinStr[12];
218-
snprintf(pinStr, 12,"%d", (int)event->passkey.params.numcmp);
224+
snprintf(pinStr, 12,"%06d", (int)event->passkey.params.numcmp);
219225
ssd1306_clear();
220226
ssd1306_setString(pinStr,0,0);
221227
ssd1306_setString("ja",0,18);
222228
ssd1306_setString("nein",128-43,18);
223229
ssd1306_display();
224230

225231
pkey.action = event->passkey.params.action;
226-
if (true) {
227-
pkey.numcmp_accept = true;
232+
233+
clearStoredButtons();
234+
if (getButton(&selectedButton)) {
235+
if(selectedButton == LEFTBUTTON){
236+
ESP_LOGI(tag_GAP,"LEFT BUTTON");
237+
pkey.numcmp_accept = true;
238+
} else {
239+
pkey.numcmp_accept = false;
240+
}
228241
} else {
229-
pkey.numcmp_accept = 0;
242+
pkey.numcmp_accept = false;
230243
ESP_LOGE(tag_GAP, "Timeout! Rejecting the key");
231244
}
232245

246+
ESP_LOGI(tag_GAP, "NUMCP_ACCEPT: %d", pkey.numcmp_accept);
233247
rc = ble_sm_inject_io(event->passkey.conn_handle, &pkey);
248+
if(rc != 0){
249+
//https://github.com/espressif/esp-idf/issues/8555#issuecomment-1066591071
250+
// 0x05 = Authentication Failure error code
251+
ble_gap_terminate(conn_handle, 0x05);
252+
}
234253
ESP_LOGI(tag_GAP, "ble_sm_inject_io result: %d\n", rc);
235254
}
236255
return 0;

components/ble/include/gap.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
#include "host/ble_hs.h"
77

88
#include "esp_log.h"
9+
#include "esp_random.h"
910

1011
#include "gatt.h"
1112
#include "ble.h"
1213
#include "ssd1306.h"
14+
#include "button.h"
1315

1416
static const char *tag_GAP = "SimLinkModule_GAP";
1517
static const ble_uuid16_t hid_service_uuid = BLE_UUID16_INIT(0x1812);

components/button/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
set(srcs "button.c")
2+
3+
idf_component_register(SRCS "${srcs}"
4+
INCLUDE_DIRS "include"
5+
REQUIRES driver esp_rom)

components/button/button.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include "button.h"
2+
3+
void initButtons(){
4+
//setup queue
5+
buttonQueue = xQueueCreate(1, sizeof(BUTTON));
6+
7+
gpio_install_isr_service(0);
8+
9+
//left button setup
10+
gpio_set_direction(GPIO_NUM_26,GPIO_MODE_INPUT);
11+
gpio_set_intr_type(GPIO_NUM_26,GPIO_INTR_NEGEDGE);
12+
gpio_isr_handler_add(GPIO_NUM_26, buttonISRHandler, (void *) LEFTBUTTON);
13+
gpio_set_pull_mode(GPIO_NUM_26,GPIO_PULLUP_ONLY);
14+
15+
//right button setup
16+
gpio_set_direction(GPIO_NUM_25,GPIO_MODE_INPUT);
17+
gpio_set_intr_type(GPIO_NUM_25,GPIO_INTR_NEGEDGE);
18+
gpio_isr_handler_add(GPIO_NUM_25, buttonISRHandler, (void *) RIGHTBUTTON);
19+
gpio_set_pull_mode(GPIO_NUM_25,GPIO_PULLUP_ONLY);
20+
}
21+
22+
static void IRAM_ATTR buttonISRHandler(void* arg)
23+
{
24+
BUTTON pressedButton = (BUTTON) arg;
25+
//ets_printf("BUTTON %d", pressedButton);
26+
xQueueSendFromISR(buttonQueue, &pressedButton, NULL);
27+
}
28+
29+
int getButton(BUTTON *selectedButton)
30+
{
31+
//timeout after 30 seconds
32+
return xQueueReceive(buttonQueue, selectedButton, 30000 / portTICK_PERIOD_MS);
33+
}
34+
35+
void clearStoredButtons(){
36+
xQueueReset(buttonQueue);
37+
}

components/button/include/button.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef BUTTON_H
2+
#define BUTTON_H
3+
4+
#include "freertos/FreeRTOS.h"
5+
#include "freertos/queue.h"
6+
#include "driver/gpio.h"
7+
8+
#include "esp_log.h"
9+
//#include "rom/ets_sys.h"
10+
11+
typedef enum BUTTON {
12+
LEFTBUTTON,
13+
RIGHTBUTTON,
14+
} BUTTON;
15+
16+
static QueueHandle_t buttonQueue = NULL;
17+
18+
void initButtons();
19+
static void IRAM_ATTR buttonISRHandler(void* arg);
20+
int getButton(BUTTON *selectedButton);
21+
void clearStoredButtons();
22+
23+
#endif

docs/Pinout.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@
66
| 16 | UART-Eingang |
77
| 21 | SDA |
88
| 22 | SCK |
9+
| 25 | rechter Button |
10+
| 26 | linker Button |
911

10-
**Anmerkung:** Wenn der ADC für die Ermittlung der Spannung verwendet werden soll. (Wenn ein Pin für die Batteriespannung vorhanden ist) Dann muss ein Spannungsteiler verwendet werden, welche die Maximalspannung der Batterie auf 3.3V herunterregelt. Des Weiteren muss noch die Formel in der Batteriekomponente angepasst werden. Im Testaufbau war R1 650 Ohm und R2 2000 Ohm.
12+
**Anmerkung:**
13+
14+
- Wenn der ADC für die Ermittlung der Spannung verwendet werden soll. (Wenn ein Pin für die Batteriespannung vorhanden ist) Dann muss ein Spannungsteiler verwendet werden, welche die Maximalspannung der Batterie auf 3.3V herunterregelt. Des Weiteren muss noch die Formel in der Batteriekomponente angepasst werden. Im Testaufbau war R1 650 Ohm und R2 2000 Ohm.
15+
- Die Buttons sind jeweils mit GND verbunden. Ein interner Pullup-Widerstand im ESP zieht den Pin im Ruhezustand auf 3.3V.
1116

1217
# OLED-Display
1318
| Pin | Anmerkung |

docs/todo

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
- buttons hinzufügen
21
- LEDs hinzufügen
3-
- BLE Verbindung mit ausgabe von einem PIN und ja nein antworten hinzufügen --> gap.c 212
42

3+
- includes, variablen und funktionen durchschauen, wo die liegen müssen und ob sie static, extern oder so sein müssen
54
- kommentieren und in .md-Datein dokumentieren, wo was zu finden ist
65

76
- Bei einem erneuten verbindungsaufbau wird das gestoppte updaten der daten nicht wieder gestartet

main/main.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "ssd1306.h"
66
#include "ble.h"
77
#include "battery.h"
8+
#include "button.h"
89

910
//#include "freertos/projdefs.h"
1011
#include "freertos/FreeRTOSConfig.h"
@@ -40,6 +41,9 @@ void app_main(void)
4041
//task to read crsf uart data
4142
xTaskCreate(crsf_get_ChannelData_task, "crsf_task", 4096, NULL, 10, NULL);
4243

44+
//setup buttons
45+
initButtons();
46+
4347
/*##########################################################
4448
############################################################
4549
# nicht verwendet, da keine Batteriespannung vorhanden ist #

0 commit comments

Comments
 (0)