-
Notifications
You must be signed in to change notification settings - Fork 259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add BLE Communication #96
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,13 @@ | |
#define CALIBRATION_LOOPS -1//How many loops should be calibrated. Set to -1 to always be calibrated. | ||
|
||
//Comm defines, no touchy | ||
#define COMM_SERIAL 0 | ||
#define COMM_BTSERIAL 1 | ||
#define COMM_SERIAL 0 | ||
#define COMM_BTSERIAL 1 | ||
#define COMM_BLE 2 | ||
|
||
#define BLE_SERVICE_UUID "090b69e6-d509-4c32-8b20-5f8b947b253a" | ||
#define BLE_TX_UUID "0eac19b9-5441-40bc-afca-cffb4380714d" | ||
#define BLE_RX_UUID "54cd1674-a8aa-4b15-ac74-361b0084877f" | ||
Comment on lines
+11
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not too familiar with the BLE spec so feel free to correct me - do these identifiers have to be unique between devices? (so no two gloves can have the same uuid?) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think so - I believe these just describe the "service" - in this case, the "serial" interface. |
||
|
||
//Encoding | ||
#define ENCODING 1 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
//only compiles if BLE is set because it won't compile for a non-compatible board | ||
#if COMMUNICATION == COMM_BLE | ||
#include "ArduinoBLE.h" | ||
class BLECommunication : public ICommunication { | ||
private: | ||
bool m_isOpen; | ||
BLECharacteristic m_tx; | ||
BLECharacteristic m_rx; | ||
BLEService m_serial; | ||
|
||
public: | ||
BLECommunication() : | ||
m_isOpen(false), | ||
m_tx(BLE_TX_UUID, BLERead | BLENotify, 75), | ||
m_rx(BLE_RX_UUID, BLEWrite | BLEWriteWithoutResponse, 75), | ||
m_serial(BLE_SERVICE_UUID) {} | ||
|
||
bool isOpen(){ | ||
return m_isOpen; | ||
} | ||
|
||
void start(){ | ||
Serial.begin(115200); | ||
|
||
BLE.begin(); | ||
|
||
BLE.setLocalName(BLE_DEVICE_NAME); | ||
BLE.setAdvertisedService(m_serial); | ||
m_serial.addCharacteristic(m_tx); | ||
m_serial.addCharacteristic(m_rx); | ||
BLE.addService(m_serial); | ||
|
||
int initialFlexion[10] = { 0 }; | ||
char* encoded = encode(initialFlexion, 0, 0, false, false, false, false, false, false, false, false); | ||
m_tx.writeValue(encoded, strlen(encoded)); | ||
|
||
BLE.advertise(); | ||
|
||
Serial.println("The device started, now you can pair it with bluetooth!"); | ||
m_isOpen = true; | ||
} | ||
|
||
void output(char* data){ | ||
m_tx.writeValue(data); | ||
} | ||
|
||
bool readData(char* input){ | ||
if (input == NULL) { | ||
return false; | ||
} | ||
strcpy(input, reinterpret_cast<const char*>(m_rx.value())); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this an std::string? ideally use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it seems to return a |
||
return strlen(input) > 0; | ||
} | ||
}; | ||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,9 @@ void setup() { | |
comm = new SerialCommunication(); | ||
#elif COMMUNICATION == COMM_BTSERIAL | ||
comm = new BTSerialCommunication(); | ||
#endif | ||
#elif COMMUNICATION == COMM_BLE | ||
comm = new BLECommunication(); | ||
#endif | ||
comm->start(); | ||
|
||
setupInputs(); | ||
|
@@ -25,6 +27,9 @@ void setup() { | |
|
||
void loop() { | ||
if (comm->isOpen()){ | ||
#if COMMUNICATION == COMM_BLE | ||
BLE.poll(); | ||
#endif | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is here because BLE needs polling, otherwise connections will fail. I don't really like the way I did it here, but I'm not sure whether I should add a poll-like method to ICommunication or just keep it this way. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. where is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a global provided by the ArduinoBLE library. |
||
#if USING_CALIB_PIN | ||
bool calibButton = getButton(PIN_CALIB) != INVERT_CALIB; | ||
if (calibButton) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I recommend using standard chars for uart: https://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.sdk5.v13.0.0%2Fble_sdk_app_nus_eval.html
It is widely supported, you can download NRF connect for your phone, I use it quite a lot in my project