-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemg.cpp
More file actions
197 lines (160 loc) · 5.19 KB
/
emg.cpp
File metadata and controls
197 lines (160 loc) · 5.19 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "emg.h"
//#define _PRINT_INFO_
#ifdef _PRINT_INFO_
#define DEBUGLN(X) Serial.println(X)
#define DEBUG(X) Serial.print(X)
#else
#define DEBUGLN(X)
#define DEBUG(X)
#endif
#include <BLEDevice.h>
#include <BLEScan.h>
#include "emg.h"
static bool isConnected = false;
const int LED_BUILTIN = 2;
const int LED_ON = LOW;
const int LED_OFF = HIGH;
volatile int state = LED_OFF;
QueueHandle_t dataQueue = nullptr;
static BLEUUID serviceUUID("e54eeef0-1980-11ea-836a-2e728ce88125");
static BLEUUID DATA_CHARACTERISTIC_UUID("e54eeef1-1980-11ea-836a-2e728ce88125");
static BLEUUID CMD_CHARACTERISTIC_UUID("e54e0002-1980-11ea-836a-2e728ce88125");
static BLEScan* pBLEScan;
static BLERemoteCharacteristic* pRemoteCharacteristic;
static BLERemoteCharacteristic* pCmdCharacteristic;
static BLEAdvertisedDevice myDevice;
static float batteryLevel = 0;
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks
{
String mac;
public:
MyAdvertisedDeviceCallbacks(String mac){
this->mac = mac;
}
/**
* Called for each advertising UNIor BLE module.
*/
void onResult(BLEAdvertisedDevice advertisedDevice)
{
#ifdef _PRINT_INFO_
Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
#endif
// We have found a device, let us now see if it contains the service we are looking for.
if (advertisedDevice.getAddress().equals(BLEAddress(mac.c_str())))
{
DEBUGLN("Device found...");
BLEDevice::getScan()->stop();
myDevice = advertisedDevice;
// pBLEScan->stop();
} // Found our server
} // onResult
}; // MyAdvertisedDeviceCallbacks
class MyClientCallback : public BLEClientCallbacks
{
void onConnect(BLEClient* pclient) {}
void onDisconnect(BLEClient* pclient)
{
isConnected = false;
DEBUGLN("onDisconnect");
}
};
static void notifyCallback(BLERemoteCharacteristic* pBLERemoteCharacteristic, uint8_t* pData, size_t length, bool isNotify)
{
float *pv = (float*) pData;
pv++; // skip packet time
float bat = *pv; // battery level, %
batteryLevel = bat;
pv++;
int cnt = length / sizeof(float);
for (int i = 2 ; i < cnt; i++)
{
if(uxQueueSpacesAvailable(dataQueue) == 0){
DEBUGLN("Code is slow, some data is deleted!!!!");
float tmp;
xQueueReceive(dataQueue, &tmp, 0);
}
xQueueSend(dataQueue, pv, portMAX_DELAY);
pv++;
}
}
// connect to UNIor BLE module
bool connectToServer()
{
DEBUG("Forming a connection to ");
DEBUGLN(myDevice.getAddress().toString().c_str());
BLEClient* pClient = BLEDevice::createClient();
DEBUG(" - Created client");
pClient->setClientCallbacks(new MyClientCallback());
// Connect to UNIor BLE module.
pClient->connect(&myDevice); // if you pass BLEAdvertisedDevice instead of address, it will be recognized type of peer device address (public or private)
DEBUGLN(" - Connected to module");
// Obtain a reference to the service we are after in the UNIor BLE module.
BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
if (pRemoteService == nullptr)
{
DEBUG("Failed to find our service UUID: ");
DEBUGLN(serviceUUID.toString().c_str());
pClient->disconnect();
return false;
}
DEBUGLN(" - Found our service");
// Obtain a reference to the characteristic in the service of the UNIor BLE module.
pRemoteCharacteristic = pRemoteService->getCharacteristic(DATA_CHARACTERISTIC_UUID);
if (pRemoteCharacteristic == nullptr)
{
DEBUG("Failed to find data characteristic UUID: ");
DEBUGLN(DATA_CHARACTERISTIC_UUID.toString().c_str());
pClient->disconnect();
return false;
}
DEBUGLN(" - Found data characteristic");
// Obtain a reference to the characteristic in the service of the UNIor BLE module.
pCmdCharacteristic = pRemoteService->getCharacteristic(CMD_CHARACTERISTIC_UUID);
if (pCmdCharacteristic == nullptr)
{
DEBUG("Failed to find cmd characteristic UUID: ");
DEBUGLN(CMD_CHARACTERISTIC_UUID.toString().c_str());
pClient->disconnect();
return false;
}
DEBUGLN(" - Found cmd characteristic");
if (pRemoteCharacteristic->canNotify())
{
pRemoteCharacteristic->registerForNotify(notifyCallback);
DEBUG(" - Notify");
}
// send start command to UNIor BLE module
if (pCmdCharacteristic->canWrite())
{
uint8_t cmd[6] = {'S', 'T', 'A', 'R', 'T', 0};
pCmdCharacteristic->writeValue(cmd, 6);
DEBUGLN(" - start");
}
isConnected = true;
}
void EMG::init(String mac){
if(isConnected){
return;
}
if(dataQueue == nullptr){
dataQueue = xQueueCreate(100, sizeof(float));
}
BLEDevice::init("");
pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks(mac));
pBLEScan->setActiveScan(true);
pBLEScan->setInterval(100);
pBLEScan->setWindow(99);
pBLEScan->start(0, false);
// pBLEScan->stop
Serial.println(myDevice.getName().c_str());
connectToServer();
}
float EMG::getBatteryLevel(){
return batteryLevel;
}
float EMG::readValue(){
float ret;
xQueueReceive(dataQueue, &ret, portMAX_DELAY);
return ret;
}