Skip to content
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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions firmware/lucidgloves-firmware/AdvancedConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Copy link

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

#define BLE_TX_UUID "0eac19b9-5441-40bc-afca-cffb4380714d"
#define BLE_RX_UUID "54cd1674-a8aa-4b15-ac74-361b0084877f"
Comment on lines +11 to +13
Copy link
Member

Choose a reason for hiding this comment

The 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?)

Copy link
Author

Choose a reason for hiding this comment

The 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
Expand Down
55 changes: 55 additions & 0 deletions firmware/lucidgloves-firmware/SerialBLECommunication.ino
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()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this an std::string? ideally use .c_str() if so, or if it's an arduino string iirc there's a .toCharArray - might that be helpful?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems to return a uint8_t *

return strlen(input) > 0;
}
};
#endif
7 changes: 6 additions & 1 deletion firmware/lucidgloves-firmware/_main.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -25,6 +27,9 @@ void setup() {

void loop() {
if (comm->isOpen()){
#if COMMUNICATION == COMM_BLE
BLE.poll();
#endif
Copy link
Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where is BLE defined?
and yeah, probably best to add a new method to ICommunication, but let's see what Lucas says.

Copy link
Author

Choose a reason for hiding this comment

The 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)
Expand Down
19 changes: 11 additions & 8 deletions firmware/lucidgloves-firmware/lucidgloves-firmware.ino
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
//This is the configuration file, main structure in _main.ino
//CONFIGURATION SETTINGS:
#define COMMUNICATION COMM_SERIAL //Which communication protocol to use
//serial over USB
//serial over USB
#define SERIAL_BAUD_RATE 115200
//serial over Bluetooth

//serial over Bluetooth
#define BTSERIAL_DEVICE_NAME "lucidgloves-left"

//Bluetooth Low Energy
#define BLE_DEVICE_NAME "lucidgloves-left"

//ANALOG INPUT CONFIG
#define FLIP_POTS false //Flip values from potentiometers (for fingers!) if they are backwards

Expand Down Expand Up @@ -62,7 +65,7 @@
#define PIN_JOY_X 33
#define PIN_JOY_Y 25
#define PIN_JOY_BTN 26
#define PIN_A_BTN 27
#define PIN_A_BTN 27
#define PIN_B_BTN 14
#define PIN_TRIG_BTN 12 //unused if gesture set
#define PIN_GRAB_BTN 13 //unused if gesture set
Expand All @@ -75,8 +78,8 @@
#define PIN_INDEX_MOTOR 21 //^
#define PIN_THUMB_MOTOR 17 //^
#define PIN_MENU_BTN 27
//PINS CONFIGURATION

//PINS CONFIGURATION
#elif defined(__AVR__)
//(This configuration is for Arduino Nano so make sure to change if you're on another board)
#define PIN_PINKY A0
Expand All @@ -86,8 +89,8 @@
#define PIN_THUMB A4
#define PIN_JOY_X A6
#define PIN_JOY_Y A7
#define PIN_JOY_BTN 7
#define PIN_A_BTN 8
#define PIN_JOY_BTN 7
#define PIN_A_BTN 8
#define PIN_B_BTN 9
#define PIN_TRIG_BTN 10 //unused if gesture set
#define PIN_GRAB_BTN 11 //unused if gesture set
Expand Down