Skip to content

Commit bfa6abd

Browse files
committed
ble hid controller working with linux
1 parent 6c9e60c commit bfa6abd

File tree

9 files changed

+53
-102
lines changed

9 files changed

+53
-102
lines changed

components/ble/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
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
6-
PRIV_REQUIRES nimble_peripheral_utils ssd1306)
5+
REQUIRES bt nimble_peripheral_utils ssd1306 crsf
6+
PRIV_REQUIRES )

components/ble/gap.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#include "gap.h"
22

3+
uint16_t conn_handle = 0;
4+
bool notify_state = false;
5+
36
/*
47
* Enables advertising with parameters:
58
* o General discoverable mode
@@ -141,10 +144,8 @@ int bleGAPEevent(struct ble_gap_event *event, void *arg) {
141144

142145
if (event->subscribe.attr_handle == report_data_handle) {
143146
notify_state = event->subscribe.cur_notify;
144-
//blehr_tx_hrate_reset();
145147
} else if (event->subscribe.attr_handle != report_data_handle) {
146148
notify_state = event->subscribe.cur_notify;
147-
//blehr_tx_hrate_stop();
148149
}
149150
ESP_LOGI(tag_GAP, "conn_handle from subscribe=%d", conn_handle);
150151
break;

components/ble/gatt.c

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,34 @@ static const uint8_t reportReferenceChar[2] = {
1919
};
2020

2121
static const uint8_t hidReportMap[] = {
22-
0x05, 0x0C, // Usage Page (Consumer)
23-
0x09, 0x01, // Usage (Consumer Control)
22+
0x05, 0x01, // Usage Page (Generic Desktop Ctrls)
23+
0x09, 0x05, // Usage (Game Pad)
2424
0xA1, 0x01, // Collection (Application)
2525
0x85, 0x01, // Report Id (1)
26-
0x05, 0x0C, // Usage Page (Consumer)
27-
0x09, 0x86, // Usage (Channel)
28-
0x15, 0xFF, // Logical Minimum (-1)
29-
0x25, 0x01, // Logical Maximum (1)
30-
0x75, 0x02, // Report Size (2)
31-
0x95, 0x01, // Report Count (1)
32-
0x81, 0x46, // Input (Data,Var,Rel,No Wrap,Linear,Preferred State,Null State)
33-
0x09, 0xE9, // Usage (Volume Increment)
34-
0x09, 0xEA, // Usage (Volume Decrement)
35-
0x15, 0x00, // Logical Minimum (0)
36-
0x75, 0x01, // Report Size (1)
37-
0x95, 0x02, // Report Count (2)
38-
0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
39-
0x95, 0x01, // Report Count (1)
40-
0x75, 0x04, // Report Size (4)
41-
0x81, 0x03, // Input (Const,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
26+
0xA1, 0x00, // Collection (Physical)
27+
0x05, 0x01, // Usage Page (Generic Desktop Ctrls)
28+
0x09, 0x30, // Usage (X)
29+
0x09, 0x31, // Usage (Y)
30+
0x09, 0x32, // Usage (Z)
31+
0x09, 0x33, // Usage (Rx)
32+
0x09, 0x35, // Usage (Rz)
33+
0x09, 0x34, // Usage (Ry)
34+
0x09, 0x36, // Usage (Slider)
35+
0x09, 0x36, // Usage (Slider)
36+
0x15, 0x00, // Logical Minimum (0)
37+
0x26, 0xFF, 0x07, // Logical Maximum (2047)
38+
0x75, 0x10, // Report Size (16)
39+
0x95, 0x08, // Report Count (8)
40+
0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
41+
0x05, 0x09, // Usage Page (Button)
42+
0x19, 0x01, // Usage Minimum (0x01)
43+
0x29, 0x08, // Usage Maximum (0x08)
44+
0x15, 0x00, // Logical Minimum (0)
45+
0x25, 0x01, // Logical Maximum (1)
46+
0x95, 0x08, // Report Count (8)
47+
0x75, 0x01, // Report Size (1)
48+
0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
49+
0xC0, // End Collection
4250
0xC0, // End Collection
4351
};
4452

@@ -262,7 +270,7 @@ int gatt_svr_chr_hid(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt
262270

263271
//Daten des reports übermitteln
264272
if (uuid == GATT_HID_REPORT_UUID) {
265-
rc = os_mbuf_append(ctxt->om, reportData, sizeof(reportData)/sizeof(reportData[0]));
273+
rc = os_mbuf_append(ctxt->om, &channelData, sizeof(channelData));
266274
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
267275
}
268276

components/ble/include/gap.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ static const char *tag_GAP = "SimLinkModule_GAP";
1616
static const ble_uuid16_t hid_service_uuid = BLE_UUID16_INIT(0x1812);
1717

1818
//eindeutiges handle was bei einem verbindungsaufbau einer verbindung zugeordnet wird
19-
static uint16_t conn_handle;
20-
static bool notify_state;
19+
extern uint16_t conn_handle;
20+
extern bool notify_state;
2121

2222
void bleAdvertise(void);
2323
int bleGAPEevent(struct ble_gap_event *event, void *arg);

components/ble/include/gatt.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "services/gap/ble_svc_gap.h"
77
#include "services/gatt/ble_svc_gatt.h"
88
#include "host/ble_hs.h"
9+
#include "crsf.h"
910

1011
/* device info configuration */
1112
#define GATT_DEVICE_INFO_UUID 0x180A

components/crsf/CMakeLists.txt

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

33
idf_component_register(SRCS "${srcs}"
44
INCLUDE_DIRS "include"
5-
REQUIRES bt)
5+
REQUIRES bt ble)

components/crsf/crsf.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "crsf.h"
22

3+
ChannelDataStruct channelData = {0};
4+
35
uint8_t crcSingleChar(uint8_t crc, uint8_t a)
46
{
57
crc ^= a;
@@ -220,9 +222,18 @@ void crsf_get_ChannelData_task(void *arg)
220222
//Kanaldaten ausgeben
221223
//wenn esp_logi ausgegeben wird dann kann es sein das watchdog timer für den task nicht zurückgesetzt wird ist aber nicht so schlimm solang der output einfach weggelassen wird in stpäteren code
222224
if(changed){
223-
ESP_LOGI("","NOTIFY");
225+
int rc;
226+
struct os_mbuf *om;
227+
228+
if(notify_state){
229+
ESP_LOGI("","BLE NOTIFY");
230+
om = ble_hs_mbuf_from_flat(&channelData, sizeof(channelData));
231+
rc = ble_gattc_notify_custom(conn_handle, report_data_handle, om);
232+
233+
assert(rc == 0);
234+
}
224235
}
225-
ESP_LOGI("Channel-Data","%4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d", channelData.throttle, channelData.yaw, channelData.pitch, channelData.roll, channelData.aux1, channelData.aux2, channelData.aux3, channelData.aux4, (channelData.buttons & (0x01<<0)), (channelData.buttons & (0x01<<1)), (channelData.buttons & (0x01<<2)), (channelData.buttons & (0x01<<3)), (channelData.buttons & (0x01<<4)), (channelData.buttons & (0x01<<5)), (channelData.buttons & (0x01<<6)), (channelData.buttons & (0x01<<7)));
236+
//ESP_LOGI("Channel-Data","%4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d", channelData.throttle, channelData.yaw, channelData.pitch, channelData.roll, channelData.aux1, channelData.aux2, channelData.aux3, channelData.aux4, (channelData.buttons & (0x01<<0)), (channelData.buttons & (0x01<<1)), (channelData.buttons & (0x01<<2)), (channelData.buttons & (0x01<<3)), (channelData.buttons & (0x01<<4)), (channelData.buttons & (0x01<<5)), (channelData.buttons & (0x01<<6)), (channelData.buttons & (0x01<<7)));
226237
break;
227238
}
228239
}

components/crsf/include/crsf.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include "esp_log.h"
88
#include <stdbool.h>
99

10+
#include "gap.h"
11+
1012
//struct for channel data
1113
typedef struct ChannelDataStruct{
1214
uint16_t roll; //roll = x
@@ -21,7 +23,7 @@ typedef struct ChannelDataStruct{
2123
} ChannelDataStruct;
2224

2325
//store for channeldata
24-
static ChannelDataStruct channelData = {0};
26+
extern ChannelDataStruct channelData;
2527

2628
uint8_t crcSingleChar(uint8_t crc, uint8_t a);
2729
uint8_t crcMessage(uint8_t message[], uint8_t length);

main/main.c

Lines changed: 1 addition & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -7,75 +7,6 @@
77

88
#include "freertos/FreeRTOSConfig.h"
99

10-
static xTimerHandle blehr_tx_timer;
11-
12-
static bool volumeUp = true;
13-
14-
/**
15-
* Utility function to log an array of bytes.
16-
*/
17-
void
18-
print_bytes(const uint8_t *bytes, int len)
19-
{
20-
int i;
21-
for (i = 0; i < len; i++) {
22-
ESP_LOGI("VOLUME-SERVICE", "%s0x%02x", i != 0 ? ":" : "", bytes[i]);
23-
}
24-
}
25-
26-
static void
27-
blehr_tx_hrate_stop(void)
28-
{
29-
volumeUp = true;
30-
xTimerStop( blehr_tx_timer, 1000 / portTICK_PERIOD_MS );
31-
}
32-
33-
/* Reset heart rate measurement */
34-
static void
35-
blehr_tx_hrate_reset(void)
36-
{
37-
38-
int rc;
39-
40-
if (xTimerReset(blehr_tx_timer, 1000 / portTICK_PERIOD_MS ) == pdPASS) {
41-
rc = 0;
42-
} else {
43-
rc = 1;
44-
}
45-
46-
assert(rc == 0);
47-
48-
}
49-
50-
/* This function simulates heart beat and notifies it to the client */
51-
static void
52-
blehr_tx_hrate(xTimerHandle ev)
53-
{
54-
int rc;
55-
struct os_mbuf *om;
56-
57-
if (!notify_state) {
58-
blehr_tx_hrate_stop();
59-
return;
60-
}
61-
62-
/* Simulation of volume */
63-
ESP_LOGW("VOLUME-SERVICE", "%d",volumeUp);
64-
if(volumeUp){
65-
reportData[0] = 0b00000100;
66-
} else {
67-
reportData[0] = 0b00001000;
68-
}
69-
volumeUp = !volumeUp;
70-
71-
om = ble_hs_mbuf_from_flat(reportData, sizeof(reportData));
72-
rc = ble_gattc_notify_custom(conn_handle, report_data_handle, om);
73-
74-
assert(rc == 0);
75-
76-
blehr_tx_hrate_reset();
77-
}
78-
7910
void app_main(void)
8011
{
8112
//init and configure nimble
@@ -97,12 +28,9 @@ void app_main(void)
9728
ssd1306_clear();
9829
ssd1306_display();
9930

100-
/* name, period/time, auto reload, timer ID, callback */
101-
blehr_tx_timer = xTimerCreate("blehr_tx_timer", pdMS_TO_TICKS(2000), pdTRUE, (void *)0, blehr_tx_hrate);
102-
10331
/* Start ble task */
10432
nimble_port_freertos_init(bleHostTask);
10533

10634
//task to read crsf uart data
107-
xTaskCreate(crsf_get_ChannelData_task, "crsf_task", 2048, NULL, 10, NULL);
35+
xTaskCreate(crsf_get_ChannelData_task, "crsf_task", 4096, NULL, 10, NULL);
10836
}

0 commit comments

Comments
 (0)