diff --git a/firmware/lucidgloves-firmware/AdvancedConfig.h b/firmware/lucidgloves-firmware/AdvancedConfig.h index c4e9887..bc5acef 100644 --- a/firmware/lucidgloves-firmware/AdvancedConfig.h +++ b/firmware/lucidgloves-firmware/AdvancedConfig.h @@ -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" //Encoding #define ENCODING 1 diff --git a/firmware/lucidgloves-firmware/SerialBLECommunication.ino b/firmware/lucidgloves-firmware/SerialBLECommunication.ino new file mode 100644 index 0000000..0ad4658 --- /dev/null +++ b/firmware/lucidgloves-firmware/SerialBLECommunication.ino @@ -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(m_rx.value())); + return strlen(input) > 0; + } +}; +#endif diff --git a/firmware/lucidgloves-firmware/_main.ino b/firmware/lucidgloves-firmware/_main.ino index 2a97184..5ba5dc2 100644 --- a/firmware/lucidgloves-firmware/_main.ino +++ b/firmware/lucidgloves-firmware/_main.ino @@ -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 #if USING_CALIB_PIN bool calibButton = getButton(PIN_CALIB) != INVERT_CALIB; if (calibButton) diff --git a/firmware/lucidgloves-firmware/lucidgloves-firmware.ino b/firmware/lucidgloves-firmware/lucidgloves-firmware.ino index 6f89bf5..957e374 100644 --- a/firmware/lucidgloves-firmware/lucidgloves-firmware.ino +++ b/firmware/lucidgloves-firmware/lucidgloves-firmware.ino @@ -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 @@ -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 @@ -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 @@ -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