-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbluetooth.cpp
More file actions
173 lines (141 loc) · 5.4 KB
/
bluetooth.cpp
File metadata and controls
173 lines (141 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "mbed.h"
#include "ble/BLE.h"
#include "ble/gatt/GattService.h"
#include "ble/gatt/GattCharacteristic.h"
#include "ble/Gap.h"
#include "ble/gap/AdvertisingDataBuilder.h"
#include "events/EventQueue.h"
#include <string.h>
#include <vector>
#include "arm_math.h"
#include "i2c.h"
#include "fft.h"
#include "bluetooth.h"
// extern globals
extern enum symptom {NO_SYMPTOM, TREMOR, DYSKINESIA} flag;
extern float32_t intensity;
// initialize UART on USB
BufferedSerial serial_port(USBTX, USBRX, 115200);
FileHandle *mbed::mbed_override_console(int) { return &serial_port; }
using namespace ble;
using namespace events;
using namespace std::chrono;
ble::BLE &ble_interface = ble::BLE::Instance();
EventQueue event_queue;
DigitalOut led(LED1);
Ticker ble_check_ticker; // trigger to update data
// UUIDs for the service and characteristics
const UUID SERVICE_UUID("A0E1B2C3-D4E5-F6A7-B8C9-D0E1F2A3B4C5");
const UUID STATE_CHAR_UUID("A1E2B3C4-D5E6-F7A8-B9C0-D1E2F3A4B5C6"); // displays state - TREMOR, STABLE, DYSKINESIA
const UUID INTENSITY_CHAR_UUID("B1C2D3E4-F5A6-B7C8-D9E0-F1A2B3C4D5E6"); // displays intensity (32-bit value)
// State Strings
const char* STATE_STRINGS[] = { "STABLE", "TREMOR", "DYSKINESIA" };
#define MAX_STATE_STRING_LEN 11
// Intensity Values
uint8_t StateValue[MAX_STATE_STRING_LEN];
uint8_t IntensityValue = 0;
// initialize state characteristic
ReadOnlyArrayGattCharacteristic<uint8_t, MAX_STATE_STRING_LEN> StateCharacteristic(
STATE_CHAR_UUID, StateValue, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY
);
// initialize intensity characteristic
GattCharacteristic IntensityCharacteristic(
INTENSITY_CHAR_UUID,
(uint8_t*)&IntensityValue,
sizeof(IntensityValue),
sizeof(IntensityValue),
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY
);
// initialize charactersitic disiplay
GattCharacteristic *charTable[] = { &StateCharacteristic, &IntensityCharacteristic };
GattService DisorderService(SERVICE_UUID, charTable, sizeof(charTable) / sizeof(charTable[0]));
bool device_connected = false;
// Display Function
void display(int flag, float32_t intensity) {
if (!device_connected) {
printf("No device connected. Skipping BLE update.\n");
return;
}
// Clamp flag to valid range [0, 2]
if (flag < 0) flag = 0;
if (flag > 2) flag = 2;
// Update State Characteristic
strcpy((char*)StateValue, STATE_STRINGS[flag]);
ble_interface.gattServer().write(
StateCharacteristic.getValueHandle(),
StateValue,
strlen((char*)StateValue) + 1
);
// Update Intensity Characteristic
IntensityValue = min((uint8_t)(intensity/11), (uint8_t)255);
ble_interface.gattServer().write(
IntensityCharacteristic.getValueHandle(),
(uint8_t*)&IntensityValue,
sizeof(IntensityValue)
);
printf("BLE Update -> State: %s, Intensity: %f\n", (char*)StateValue, intensity);
led = !led; // Blink LED to show transmission
}
// function to be called after connection
void periodic_check() {
if (device_connected) {
// fill the data_array by reading from the IMU
read_acceleration();
// perform FFT operation on data_array
run_fft();
// check for tremor/dyskines symptoms over multiple sampling periods
// modify flag and intensity
detect_tremor_and_dyskinesia();
// display using bluetooth
display(flag, intensity);
} else {
printf("Waiting for BLE connection...\n");
}
}
class ConnectionEventHandler : public ble::Gap::EventHandler {
public:
virtual void onConnectionComplete(const ble::ConnectionCompleteEvent &event) {
if (event.getStatus() == BLE_ERROR_NONE) {
printf("Device connected!\n");
device_connected = true;
ble_check_ticker.attach([]() {
event_queue.call(periodic_check); // Schedule display safely on EventQueue
}, 6s);
}
}
virtual void onDisconnectionComplete(const ble::DisconnectionCompleteEvent &event) {
printf("Device disconnected!\n");
device_connected = false;
ble_interface.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE);
printf("Restarted advertising.\n");
}
};
ConnectionEventHandler connection_handler;
void on_ble_init_complete(BLE::InitializationCompleteCallbackContext *params) {
if (params->error != BLE_ERROR_NONE) {
printf("BLE initialization failed.\n");
return;
}
// Initialize Characteristics
strcpy((char*)StateValue, "STABLE");
IntensityValue = 0;
ble_interface.gattServer().addService(DisorderService);
uint8_t adv_buffer[LEGACY_ADVERTISING_MAX_SIZE];
AdvertisingDataBuilder adv_data(adv_buffer);
adv_data.setFlags();
adv_data.setName("Parkinson's-Monitor");
ble_interface.gap().setAdvertisingParameters(
LEGACY_ADVERTISING_HANDLE,
AdvertisingParameters(advertising_type_t::CONNECTABLE_UNDIRECTED, adv_interval_t(160))
);
ble_interface.gap().setAdvertisingPayload(
LEGACY_ADVERTISING_HANDLE,
adv_data.getAdvertisingData()
);
ble_interface.gap().setEventHandler(&connection_handler);
ble_interface.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE);
printf("BLE advertising started. Waiting for connection...\n");
}
void schedule_ble_events(BLE::OnEventsToProcessCallbackContext *context) {
event_queue.call(callback(&ble_interface, &BLE::processEvents));
}