From b60a639ae14d980aa9f3b2952162d07a1a6cc6ee Mon Sep 17 00:00:00 2001 From: Kozova1 Date: Fri, 14 Apr 2023 13:19:26 +0300 Subject: [PATCH 1/3] Fix formatting with Arduino's Ctrl+T Signed-off-by: Kozova1 --- .../lucidgloves-firmware/AdvancedConfig.h | 13 +- firmware/lucidgloves-firmware/Encoding.ino | 56 +++---- .../SerialBLECommunication.ino | 55 +++++++ .../SerialBTCommunication.ino | 12 +- .../SerialCommunication.ino | 8 +- firmware/lucidgloves-firmware/_main.ino | 79 +++++----- firmware/lucidgloves-firmware/gesture.ino | 12 +- firmware/lucidgloves-firmware/haptics.ino | 36 ++--- firmware/lucidgloves-firmware/input.ino | 138 +++++++++--------- .../lucidgloves-firmware.ino | 109 +++++++------- 10 files changed, 293 insertions(+), 225 deletions(-) create mode 100644 firmware/lucidgloves-firmware/SerialBLECommunication.ino diff --git a/firmware/lucidgloves-firmware/AdvancedConfig.h b/firmware/lucidgloves-firmware/AdvancedConfig.h index c4e9887..7cdec50 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 @@ -23,13 +28,13 @@ #if defined(__AVR__) #define ANALOG_MAX 1023 #elif defined(ESP32) -#define ANALOG_MAX 4095 +#define ANALOG_MAX 4095 #endif //ANALOG_MAX OVERRIDE: //uncomment and set as needed (only touch if you know what you are doing) -//#define ANALOG_MAX 4095 +//#define ANALOG_MAX 4095 #ifndef ANALOG_MAX #error "This board doesn't have an auto ANALOG_MAX assignment, please set it manually by uncommenting ANALOG_MAX OVERRIDE!" diff --git a/firmware/lucidgloves-firmware/Encoding.ino b/firmware/lucidgloves-firmware/Encoding.ino index 713d34c..42abb1c 100644 --- a/firmware/lucidgloves-firmware/Encoding.ino +++ b/firmware/lucidgloves-firmware/Encoding.ino @@ -8,53 +8,53 @@ bool bButton; bool grab; bool pinch; -}; + }; -struct outputData{ + struct outputData{ int* hapticLimits; -}; + }; */ #if ENCODING == ENCODING_LEGACY //legacy encoding -char* encode(int* flexion, int joyX, int joyY, bool joyClick, bool triggerButton, bool aButton, bool bButton, bool grab, bool pinch, bool calib, bool menu){ +char* encode(int* flexion, int joyX, int joyY, bool joyClick, bool triggerButton, bool aButton, bool bButton, bool grab, bool pinch, bool calib, bool menu) { static char stringToEncode[75]; - - sprintf(stringToEncode, "%d&%d&%d&%d&%d&%d&%d&%d&%d&%d&%d&%d&%d\n", - flexion[0], flexion[1], flexion[2], flexion[3], flexion[4], - joyX, joyY, joyClick, - triggerButton, aButton, bButton, grab, pinch - ); + + sprintf(stringToEncode, "%d&%d&%d&%d&%d&%d&%d&%d&%d&%d&%d&%d&%d\n", + flexion[0], flexion[1], flexion[2], flexion[3], flexion[4], + joyX, joyY, joyClick, + triggerButton, aButton, bButton, grab, pinch + ); return stringToEncode; } //legacy decoding -void decodeData(char* stringToDecode, int* hapticLimits){ - byte index = 0; - char* ptr = strtok(stringToDecode, "&"); // takes a list of delimiters - while(ptr != NULL) - { - hapticLimits[index] = atoi(ptr); - index++; - ptr = strtok(NULL, "&"); // takes a list of delimiters - } +void decodeData(char* stringToDecode, int* hapticLimits) { + byte index = 0; + char* ptr = strtok(stringToDecode, "&"); // takes a list of delimiters + while (ptr != NULL) + { + hapticLimits[index] = atoi(ptr); + index++; + ptr = strtok(NULL, "&"); // takes a list of delimiters + } } #endif #if ENCODING == ENCODE_ALPHA //alphabetic encoding -char* encode(int* flexion, int joyX, int joyY, bool joyClick, bool triggerButton, bool aButton, bool bButton, bool grab, bool pinch, bool calib, bool menu){ +char* encode(int* flexion, int joyX, int joyY, bool joyClick, bool triggerButton, bool aButton, bool bButton, bool grab, bool pinch, bool calib, bool menu) { static char stringToEncode[75]; - int trigger = (flexion[1] > ANALOG_MAX/2) ? (flexion[1] - ANALOG_MAX/2) * 2:0; - sprintf(stringToEncode, "A%dB%dC%dD%dE%dF%dG%dP%d%s%s%s%s%s%s%s%s\n", - flexion[0], flexion[1], flexion[2], flexion[3], flexion[4], - joyX, joyY, trigger, joyClick?"H":"", - triggerButton?"I":"", aButton?"J":"", bButton?"K":"", grab?"L":"", pinch?"M":"", menu?"N":"", calib?"O":"" - ); + int trigger = (flexion[1] > ANALOG_MAX / 2) ? (flexion[1] - ANALOG_MAX / 2) * 2 : 0; + sprintf(stringToEncode, "A%dB%dC%dD%dE%dF%dG%dP%d%s%s%s%s%s%s%s%s\n", + flexion[0], flexion[1], flexion[2], flexion[3], flexion[4], + joyX, joyY, trigger, joyClick ? "H" : "", + triggerButton ? "I" : "", aButton ? "J" : "", bButton ? "K" : "", grab ? "L" : "", pinch ? "M" : "", menu ? "N" : "", calib ? "O" : "" + ); return stringToEncode; } //legacy decoding -void decodeData(char* stringToDecode, int* hapticLimits){ +void decodeData(char* stringToDecode, int* hapticLimits) { hapticLimits[0] = getArgument(stringToDecode, 'A'); //thumb hapticLimits[1] = getArgument(stringToDecode, 'B'); //index hapticLimits[2] = getArgument(stringToDecode, 'C'); //middle @@ -63,7 +63,7 @@ void decodeData(char* stringToDecode, int* hapticLimits){ //Serial.println("Haptic: "+ (String)hapticLimits[0] + " " + (String)hapticLimits[1] + " " + (String)hapticLimits[2] + " " + (String)hapticLimits[3] + " " + (String)hapticLimits[4] + " "); } -int getArgument(char* stringToDecode, char command){ +int getArgument(char* stringToDecode, char command) { char* start = strchr(stringToDecode, command); if (start == NULL) return -1; diff --git a/firmware/lucidgloves-firmware/SerialBLECommunication.ino b/firmware/lucidgloves-firmware/SerialBLECommunication.ino new file mode 100644 index 0000000..f17db11 --- /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/SerialBTCommunication.ino b/firmware/lucidgloves-firmware/SerialBTCommunication.ino index 282dfbc..d185474 100644 --- a/firmware/lucidgloves-firmware/SerialBTCommunication.ino +++ b/firmware/lucidgloves-firmware/SerialBTCommunication.ino @@ -5,30 +5,30 @@ class BTSerialCommunication : public ICommunication { private: bool m_isOpen; BluetoothSerial m_SerialBT; - + public: BTSerialCommunication() { m_isOpen = false; } - bool isOpen(){ + bool isOpen() { return m_isOpen; } - void start(){ + void start() { Serial.begin(115200); m_SerialBT.begin(BTSERIAL_DEVICE_NAME); Serial.println("The device started, now you can pair it with bluetooth!"); m_isOpen = true; } - void output(char* data){ + void output(char* data) { m_SerialBT.print(data); } - bool readData(char* input){ + bool readData(char* input) { /*byte size = m_SerialBT.readBytesUntil('\n', input, 100); - input[size] = NULL;*/ + input[size] = NULL;*/ String message = m_SerialBT.readStringUntil('\n'); strcpy(input, message.c_str()); return input != NULL && strlen(input) > 0; diff --git a/firmware/lucidgloves-firmware/SerialCommunication.ino b/firmware/lucidgloves-firmware/SerialCommunication.ino index 32d27c4..f6affea 100644 --- a/firmware/lucidgloves-firmware/SerialCommunication.ino +++ b/firmware/lucidgloves-firmware/SerialCommunication.ino @@ -7,22 +7,22 @@ class SerialCommunication : public ICommunication { m_isOpen = false; } - bool isOpen(){ + bool isOpen() { return m_isOpen; } - void start(){ + void start() { //Serial.setTimeout(1000000); Serial.begin(SERIAL_BAUD_RATE); m_isOpen = true; } - void output(char* data){ + void output(char* data) { Serial.print(data); Serial.flush(); } - bool readData(char* input){ + bool readData(char* input) { byte size = Serial.readBytesUntil('\n', input, 100); input[size] = NULL; return input != NULL && strlen(input) > 0; diff --git a/firmware/lucidgloves-firmware/_main.ino b/firmware/lucidgloves-firmware/_main.ino index 2a97184..ff5b22b 100644 --- a/firmware/lucidgloves-firmware/_main.ino +++ b/firmware/lucidgloves-firmware/_main.ino @@ -2,82 +2,87 @@ #define CALIB_OVERRIDE false #if USING_CALIB_PIN && COMMUNICATION == COMM_SERIAL && PIN_CALIB == 0 && !CALIB_OVERRIDE - #error "You can't set your calibration pin to 0 over usb. You can calibrate with the BOOT button when using bluetooth only. Set CalibOverride to true to override this." +#error "You can't set your calibration pin to 0 over usb. You can calibrate with the BOOT button when using bluetooth only. Set CalibOverride to true to override this." #endif ICommunication* comm; int loops = 0; void setup() { - #if COMMUNICATION == COMM_SERIAL - comm = new SerialCommunication(); - #elif COMMUNICATION == COMM_BTSERIAL - comm = new BTSerialCommunication(); - #endif +#if COMMUNICATION == COMM_SERIAL + comm = new SerialCommunication(); +#elif COMMUNICATION == COMM_BTSERIAL + comm = new BTSerialCommunication(); +#elif COMMUNICATION == COMM_BLE + comm = new BLECommunication(); +#endif comm->start(); setupInputs(); - #if USING_FORCE_FEEDBACK - setupServoHaptics(); - #endif - +#if USING_FORCE_FEEDBACK + setupServoHaptics(); +#endif + } void loop() { - if (comm->isOpen()){ - #if USING_CALIB_PIN + if (comm->isOpen()) { +#if COMMUNICATION == COMM_BLE + BLE.poll(); +#endif +#if USING_CALIB_PIN bool calibButton = getButton(PIN_CALIB) != INVERT_CALIB; if (calibButton) loops = 0; - #else +#else bool calibButton = false; - #endif - +#endif + bool calibrate = false; - if (loops < CALIBRATION_LOOPS || ALWAYS_CALIBRATING){ + if (loops < CALIBRATION_LOOPS || ALWAYS_CALIBRATING) { calibrate = true; loops++; } - + int* fingerPos = getFingerPositions(calibrate, calibButton); bool joyButton = getButton(PIN_JOY_BTN) != INVERT_JOY; - #if TRIGGER_GESTURE +#if TRIGGER_GESTURE bool triggerButton = triggerGesture(fingerPos); - #else +#else bool triggerButton = getButton(PIN_TRIG_BTN) != INVERT_TRIGGER; - #endif +#endif bool aButton = getButton(PIN_A_BTN) != INVERT_A; bool bButton = getButton(PIN_B_BTN) != INVERT_B; - #if GRAB_GESTURE +#if GRAB_GESTURE bool grabButton = grabGesture(fingerPos); - #else +#else bool grabButton = getButton(PIN_GRAB_BTN) != INVERT_GRAB; - #endif +#endif - #if PINCH_GESTURE +#if PINCH_GESTURE bool pinchButton = pinchGesture(fingerPos); - #else +#else bool pinchButton = getButton(PIN_PNCH_BTN) != INVERT_PINCH; - #endif +#endif bool menuButton = getButton(PIN_MENU_BTN) != INVERT_MENU; - + comm->output(encode(fingerPos, getJoyX(), getJoyY(), joyButton, triggerButton, aButton, bButton, grabButton, pinchButton, calibButton, menuButton)); - #if USING_FORCE_FEEDBACK - char received[100]; - if (comm->readData(received)){ - int hapticLimits[5]; - //This check is a temporary hack to fix an issue with haptics on v0.5 of the driver, will make it more snobby code later - if(String(received).length() >= 10) { - decodeData(received, hapticLimits); - writeServoHaptics(hapticLimits); - } +#if USING_FORCE_FEEDBACK + char received[100]; + if (comm->readData(received)) { + int hapticLimits[5]; + //This check is a temporary hack to fix an issue with haptics on v0.5 of the driver, will make it more snobby code later + if (String(received).length() >= 10) { + decodeData(received, hapticLimits); + writeServoHaptics(hapticLimits); } - #endif + } +#endif delay(LOOP_TIME); } } diff --git a/firmware/lucidgloves-firmware/gesture.ino b/firmware/lucidgloves-firmware/gesture.ino index 072f815..c072a22 100644 --- a/firmware/lucidgloves-firmware/gesture.ino +++ b/firmware/lucidgloves-firmware/gesture.ino @@ -1,11 +1,11 @@ -bool grabGesture(int *flexion){ - return (flexion[PINKY_IND] + flexion[RING_IND] + flexion[MIDDLE_IND] + flexion[INDEX_IND]) / 4 <= ANALOG_MAX/2 ? 0:1; +bool grabGesture(int *flexion) { + return (flexion[PINKY_IND] + flexion[RING_IND] + flexion[MIDDLE_IND] + flexion[INDEX_IND]) / 4 <= ANALOG_MAX / 2 ? 0 : 1; } -bool pinchGesture(int *flexion){ - return (flexion[INDEX_IND] + flexion[THUMB_IND]) / 2 <= ANALOG_MAX/2 ? 0:1; +bool pinchGesture(int *flexion) { + return (flexion[INDEX_IND] + flexion[THUMB_IND]) / 2 <= ANALOG_MAX / 2 ? 0 : 1; } -bool triggerGesture(int *flexion){ - return flexion[INDEX_IND]<=(ANALOG_MAX/2)?0:1; +bool triggerGesture(int *flexion) { + return flexion[INDEX_IND] <= (ANALOG_MAX / 2) ? 0 : 1; } diff --git a/firmware/lucidgloves-firmware/haptics.ino b/firmware/lucidgloves-firmware/haptics.ino index a915557..4c49bd1 100644 --- a/firmware/lucidgloves-firmware/haptics.ino +++ b/firmware/lucidgloves-firmware/haptics.ino @@ -1,9 +1,9 @@ #if USING_FORCE_FEEDBACK #if defined(ESP32) - #include "ESP32Servo.h" +#include "ESP32Servo.h" #else - #include "Servo.h" +#include "Servo.h" #endif Servo pinkyServo; @@ -12,7 +12,7 @@ Servo middleServo; Servo indexServo; Servo thumbServo; -void setupServoHaptics(){ +void setupServoHaptics() { pinkyServo.attach(PIN_PINKY_MOTOR); ringServo.attach(PIN_RING_MOTOR); middleServo.attach(PIN_MIDDLE_MOTOR); @@ -21,34 +21,34 @@ void setupServoHaptics(){ } //static scaling, maps to entire range of servo -void scaleLimits(int* hapticLimits, float* scaledLimits){ - for (int i = 0; i < 5; i++){ +void scaleLimits(int* hapticLimits, float* scaledLimits) { + for (int i = 0; i < 5; i++) { scaledLimits[i] = 180.0f - hapticLimits[i] / 1000.0f * 180.0f; } } //dynamic scaling, maps to the limits calibrated from your finger -void dynScaleLimits(int* hapticLimits, float* scaledLimits){ +void dynScaleLimits(int* hapticLimits, float* scaledLimits) { //will be refactored to take min and max as an argument - /* this implementation of dynamic scaling relies on the assumption - * that the servo reaches 2/3 of the potentiometer's range, - * and that 0 degrees is geared to the start of the potentiometer. - * Different hardware types may need to handle dynamic scaling differently. - */ - for (int i = 0; i < sizeof(hapticLimits); i++){ + /* this implementation of dynamic scaling relies on the assumption + that the servo reaches 2/3 of the potentiometer's range, + and that 0 degrees is geared to the start of the potentiometer. + Different hardware types may need to handle dynamic scaling differently. + */ + for (int i = 0; i < sizeof(hapticLimits); i++) { scaledLimits[i] = hapticLimits[i] / 1000.0f * 180.0f; } } -void writeServoHaptics(int* hapticLimits){ +void writeServoHaptics(int* hapticLimits) { float scaledLimits[5]; scaleLimits(hapticLimits, scaledLimits); - if(hapticLimits[0] >= 0) thumbServo.write(scaledLimits[0]); - if(hapticLimits[1] >= 0) indexServo.write(scaledLimits[1]); - if(hapticLimits[2] >= 0) middleServo.write(scaledLimits[2]); - if(hapticLimits[3] >= 0) ringServo.write(scaledLimits[3]); - if(hapticLimits[4] >= 0) pinkyServo.write(scaledLimits[4]); + if (hapticLimits[0] >= 0) thumbServo.write(scaledLimits[0]); + if (hapticLimits[1] >= 0) indexServo.write(scaledLimits[1]); + if (hapticLimits[2] >= 0) middleServo.write(scaledLimits[2]); + if (hapticLimits[3] >= 0) ringServo.write(scaledLimits[3]); + if (hapticLimits[4] >= 0) pinkyServo.write(scaledLimits[4]); } #endif diff --git a/firmware/lucidgloves-firmware/input.ino b/firmware/lucidgloves-firmware/input.ino index cc70952..58025ca 100644 --- a/firmware/lucidgloves-firmware/input.ino +++ b/firmware/lucidgloves-firmware/input.ino @@ -1,133 +1,133 @@ // Requires RunningMedian library by Rob Tillaart #if ENABLE_MEDIAN_FILTER - #include - RunningMedian rmSamples[5] = { - RunningMedian(MEDIAN_SAMPLES), - RunningMedian(MEDIAN_SAMPLES), - RunningMedian(MEDIAN_SAMPLES), - RunningMedian(MEDIAN_SAMPLES), - RunningMedian(MEDIAN_SAMPLES) - }; +#include +RunningMedian rmSamples[5] = { + RunningMedian(MEDIAN_SAMPLES), + RunningMedian(MEDIAN_SAMPLES), + RunningMedian(MEDIAN_SAMPLES), + RunningMedian(MEDIAN_SAMPLES), + RunningMedian(MEDIAN_SAMPLES) +}; #endif -int maxFingers[5] = {0,0,0,0,0}; +int maxFingers[5] = {0, 0, 0, 0, 0}; int minFingers[5] = {ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX}; -void setupInputs(){ +void setupInputs() { pinMode(PIN_JOY_BTN, INPUT_PULLUP); pinMode(PIN_A_BTN, INPUT_PULLUP); pinMode(PIN_B_BTN, INPUT_PULLUP); pinMode(PIN_MENU_BTN, INPUT_PULLUP); - - #if !TRIGGER_GESTURE + +#if !TRIGGER_GESTURE pinMode(PIN_TRIG_BTN, INPUT_PULLUP); - #endif +#endif - #if !GRAB_GESTURE +#if !GRAB_GESTURE pinMode(PIN_GRAB_BTN, INPUT_PULLUP); - #endif +#endif - #if !PINCH_GESTURE +#if !PINCH_GESTURE pinMode(PIN_PNCH_BTN, INPUT_PULLUP); - #endif +#endif - #if USING_CALIB_PIN +#if USING_CALIB_PIN pinMode(PIN_CALIB, INPUT_PULLUP); - #endif +#endif } -int* getFingerPositions(bool calibrating, bool reset){ - int rawFingers[5] = {NO_THUMB?0:analogRead(PIN_THUMB), analogRead(PIN_INDEX), analogRead(PIN_MIDDLE), analogRead(PIN_RING), analogRead(PIN_PINKY)}; +int* getFingerPositions(bool calibrating, bool reset) { + int rawFingers[5] = {NO_THUMB ? 0 : analogRead(PIN_THUMB), analogRead(PIN_INDEX), analogRead(PIN_MIDDLE), analogRead(PIN_RING), analogRead(PIN_PINKY)}; //flip pot values if needed - #if FLIP_POTS - for (int i = 0; i <5; i++){ +#if FLIP_POTS + for (int i = 0; i < 5; i++) { rawFingers[i] = ANALOG_MAX - rawFingers[i]; } - #endif +#endif - #if ENABLE_MEDIAN_FILTER - for (int i = 0; i < 5; i++){ +#if ENABLE_MEDIAN_FILTER + for (int i = 0; i < 5; i++) { rmSamples[i].add( rawFingers[i] ); rawFingers[i] = rmSamples[i].getMedian(); } - #endif +#endif //reset max and mins as needed - if (reset){ - for (int i = 0; i <5; i++){ + if (reset) { + for (int i = 0; i < 5; i++) { maxFingers[i] = 0; minFingers[i] = ANALOG_MAX; } } - + //if during the calibration sequence, make sure to update max and mins - if (calibrating){ - for (int i = 0; i <5; i++){ + if (calibrating) { + for (int i = 0; i < 5; i++) { if (rawFingers[i] > maxFingers[i]) - #if CLAMP_FLEXION - maxFingers[i] = ( rawFingers[i] <= CLAMP_MAX )? rawFingers[i] : CLAMP_MAX; - #else - maxFingers[i] = rawFingers[i]; - #endif +#if CLAMP_FLEXION + maxFingers[i] = ( rawFingers[i] <= CLAMP_MAX ) ? rawFingers[i] : CLAMP_MAX; +#else + maxFingers[i] = rawFingers[i]; +#endif if (rawFingers[i] < minFingers[i]) - #if CLAMP_FLEXION - minFingers[i] = ( rawFingers[i] >= CLAMP_MIN )? rawFingers[i] : CLAMP_MIN; - #else - minFingers[i] = rawFingers[i]; - #endif +#if CLAMP_FLEXION + minFingers[i] = ( rawFingers[i] >= CLAMP_MIN ) ? rawFingers[i] : CLAMP_MIN; +#else + minFingers[i] = rawFingers[i]; +#endif } } - static int calibrated[5] = {511,511,511,511,511}; - - for (int i = 0; i<5; i++){ - if (minFingers[i] != maxFingers[i]){ + static int calibrated[5] = {511, 511, 511, 511, 511}; + + for (int i = 0; i < 5; i++) { + if (minFingers[i] != maxFingers[i]) { calibrated[i] = map( rawFingers[i], minFingers[i], maxFingers[i], 0, ANALOG_MAX ); - #if CLAMP_ANALOG_MAP - if (calibrated[i] < 0) - calibrated[i] = 0; - if (calibrated[i] > ANALOG_MAX) - calibrated[i] = ANALOG_MAX; - #endif +#if CLAMP_ANALOG_MAP + if (calibrated[i] < 0) + calibrated[i] = 0; + if (calibrated[i] > ANALOG_MAX) + calibrated[i] = ANALOG_MAX; +#endif } else { calibrated[i] = ANALOG_MAX / 2; } } return calibrated; - + } -int analogReadDeadzone(byte pin){ +int analogReadDeadzone(byte pin) { int raw = analogRead(pin); - if (abs(ANALOG_MAX/2 - raw) < JOYSTICK_DEADZONE * ANALOG_MAX / 100) - return ANALOG_MAX/2; + if (abs(ANALOG_MAX / 2 - raw) < JOYSTICK_DEADZONE * ANALOG_MAX / 100) + return ANALOG_MAX / 2; else return raw; } -int getJoyX(){ - #if JOYSTICK_BLANK - return ANALOG_MAX/2; - #elif JOY_FLIP_X +int getJoyX() { +#if JOYSTICK_BLANK + return ANALOG_MAX / 2; +#elif JOY_FLIP_X return ANALOG_MAX - analogReadDeadzone(PIN_JOY_X); - #else +#else return analogReadDeadzone(PIN_JOY_X); - #endif +#endif } -int getJoyY(){ - #if JOYSTICK_BLANK - return ANALOG_MAX/2; - #elif JOY_FLIP_Y +int getJoyY() { +#if JOYSTICK_BLANK + return ANALOG_MAX / 2; +#elif JOY_FLIP_Y return ANALOG_MAX - analogReadDeadzone(PIN_JOY_Y); - #else +#else return analogReadDeadzone(PIN_JOY_Y); - #endif +#endif } -bool getButton(byte pin){ +bool getButton(byte pin) { return digitalRead(pin) != HIGH; } diff --git a/firmware/lucidgloves-firmware/lucidgloves-firmware.ino b/firmware/lucidgloves-firmware/lucidgloves-firmware.ino index 6f89bf5..cf8016c 100644 --- a/firmware/lucidgloves-firmware/lucidgloves-firmware.ino +++ b/firmware/lucidgloves-firmware/lucidgloves-firmware.ino @@ -1,8 +1,8 @@ /* - * LucidGloves Firmware Version 4 - * Author: Lucas_VRTech - LucidVR - * lucidvrtech.com - */ + LucidGloves Firmware Version 4 + Author: Lucas_VRTech - LucidVR + lucidvrtech.com +*/ #include "AdvancedConfig.h" @@ -10,10 +10,13 @@ //CONFIGURATION SETTINGS: #define COMMUNICATION COMM_SERIAL //Which communication protocol to use //serial over USB - #define SERIAL_BAUD_RATE 115200 - +#define SERIAL_BAUD_RATE 115200 + //serial over Bluetooth - #define BTSERIAL_DEVICE_NAME "lucidgloves-left" +#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 @@ -53,51 +56,51 @@ #define SERVO_SCALING false //dynamic scaling of servo motors #if defined(ESP32) - //(This configuration is for ESP32 DOIT V1 so make sure to change if you're on another board) - #define PIN_PINKY 36 - #define PIN_RING 39 - #define PIN_MIDDLE 34 - #define PIN_INDEX 35 - #define PIN_THUMB 32 - #define PIN_JOY_X 33 - #define PIN_JOY_Y 25 - #define PIN_JOY_BTN 26 - #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 - #define PIN_PNCH_BTN 23 //unused if gesture set - #define PIN_CALIB 12 //button for recalibration (You can set this to GPIO0 to use the BOOT button, but only when using Bluetooth.) - #define DEBUG_LED 2 - #define PIN_PINKY_MOTOR 5 //used for force feedback - #define PIN_RING_MOTOR 18 //^ - #define PIN_MIDDLE_MOTOR 19 //^ - #define PIN_INDEX_MOTOR 21 //^ - #define PIN_THUMB_MOTOR 17 //^ - #define PIN_MENU_BTN 27 - -//PINS CONFIGURATION +//(This configuration is for ESP32 DOIT V1 so make sure to change if you're on another board) +#define PIN_PINKY 36 +#define PIN_RING 39 +#define PIN_MIDDLE 34 +#define PIN_INDEX 35 +#define PIN_THUMB 32 +#define PIN_JOY_X 33 +#define PIN_JOY_Y 25 +#define PIN_JOY_BTN 26 +#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 +#define PIN_PNCH_BTN 23 //unused if gesture set +#define PIN_CALIB 12 //button for recalibration (You can set this to GPIO0 to use the BOOT button, but only when using Bluetooth.) +#define DEBUG_LED 2 +#define PIN_PINKY_MOTOR 5 //used for force feedback +#define PIN_RING_MOTOR 18 //^ +#define PIN_MIDDLE_MOTOR 19 //^ +#define PIN_INDEX_MOTOR 21 //^ +#define PIN_THUMB_MOTOR 17 //^ +#define PIN_MENU_BTN 27 + +//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 - #define PIN_RING A1 - #define PIN_MIDDLE A2 - #define PIN_INDEX A3 - #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_B_BTN 9 - #define PIN_TRIG_BTN 10 //unused if gesture set - #define PIN_GRAB_BTN 11 //unused if gesture set - #define PIN_PNCH_BTN 12 //unused if gesture set - #define PIN_CALIB 13 //button for recalibration - #define DEBUG_LED LED_BUILTIN - #define PIN_PINKY_MOTOR 2 //used for force feedback - #define PIN_RING_MOTOR 3 //^ - #define PIN_MIDDLE_MOTOR 4 //^ - #define PIN_INDEX_MOTOR 5 //^ - #define PIN_THUMB_MOTOR 6 //^ - #define PIN_MENU_BTN 8 +//(This configuration is for Arduino Nano so make sure to change if you're on another board) +#define PIN_PINKY A0 +#define PIN_RING A1 +#define PIN_MIDDLE A2 +#define PIN_INDEX A3 +#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_B_BTN 9 +#define PIN_TRIG_BTN 10 //unused if gesture set +#define PIN_GRAB_BTN 11 //unused if gesture set +#define PIN_PNCH_BTN 12 //unused if gesture set +#define PIN_CALIB 13 //button for recalibration +#define DEBUG_LED LED_BUILTIN +#define PIN_PINKY_MOTOR 2 //used for force feedback +#define PIN_RING_MOTOR 3 //^ +#define PIN_MIDDLE_MOTOR 4 //^ +#define PIN_INDEX_MOTOR 5 //^ +#define PIN_THUMB_MOTOR 6 //^ +#define PIN_MENU_BTN 8 #endif From f493d3ead503c757e0e3506bbbbee323fe30b0bf Mon Sep 17 00:00:00 2001 From: Kozova1 Date: Fri, 14 Apr 2023 13:22:47 +0300 Subject: [PATCH 2/3] Revert "Fix formatting with Arduino's Ctrl+T" This reverts commit b60a639ae14d980aa9f3b2952162d07a1a6cc6ee. --- .../lucidgloves-firmware/AdvancedConfig.h | 13 +- firmware/lucidgloves-firmware/Encoding.ino | 56 +++---- .../SerialBLECommunication.ino | 55 ------- .../SerialBTCommunication.ino | 12 +- .../SerialCommunication.ino | 8 +- firmware/lucidgloves-firmware/_main.ino | 79 +++++----- firmware/lucidgloves-firmware/gesture.ino | 12 +- firmware/lucidgloves-firmware/haptics.ino | 36 ++--- firmware/lucidgloves-firmware/input.ino | 138 +++++++++--------- .../lucidgloves-firmware.ino | 109 +++++++------- 10 files changed, 225 insertions(+), 293 deletions(-) delete mode 100644 firmware/lucidgloves-firmware/SerialBLECommunication.ino diff --git a/firmware/lucidgloves-firmware/AdvancedConfig.h b/firmware/lucidgloves-firmware/AdvancedConfig.h index 7cdec50..c4e9887 100644 --- a/firmware/lucidgloves-firmware/AdvancedConfig.h +++ b/firmware/lucidgloves-firmware/AdvancedConfig.h @@ -4,13 +4,8 @@ #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_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" +#define COMM_SERIAL 0 +#define COMM_BTSERIAL 1 //Encoding #define ENCODING 1 @@ -28,13 +23,13 @@ #if defined(__AVR__) #define ANALOG_MAX 1023 #elif defined(ESP32) -#define ANALOG_MAX 4095 +#define ANALOG_MAX 4095 #endif //ANALOG_MAX OVERRIDE: //uncomment and set as needed (only touch if you know what you are doing) -//#define ANALOG_MAX 4095 +//#define ANALOG_MAX 4095 #ifndef ANALOG_MAX #error "This board doesn't have an auto ANALOG_MAX assignment, please set it manually by uncommenting ANALOG_MAX OVERRIDE!" diff --git a/firmware/lucidgloves-firmware/Encoding.ino b/firmware/lucidgloves-firmware/Encoding.ino index 42abb1c..713d34c 100644 --- a/firmware/lucidgloves-firmware/Encoding.ino +++ b/firmware/lucidgloves-firmware/Encoding.ino @@ -8,53 +8,53 @@ bool bButton; bool grab; bool pinch; - }; +}; - struct outputData{ +struct outputData{ int* hapticLimits; - }; +}; */ #if ENCODING == ENCODING_LEGACY //legacy encoding -char* encode(int* flexion, int joyX, int joyY, bool joyClick, bool triggerButton, bool aButton, bool bButton, bool grab, bool pinch, bool calib, bool menu) { +char* encode(int* flexion, int joyX, int joyY, bool joyClick, bool triggerButton, bool aButton, bool bButton, bool grab, bool pinch, bool calib, bool menu){ static char stringToEncode[75]; - - sprintf(stringToEncode, "%d&%d&%d&%d&%d&%d&%d&%d&%d&%d&%d&%d&%d\n", - flexion[0], flexion[1], flexion[2], flexion[3], flexion[4], - joyX, joyY, joyClick, - triggerButton, aButton, bButton, grab, pinch - ); + + sprintf(stringToEncode, "%d&%d&%d&%d&%d&%d&%d&%d&%d&%d&%d&%d&%d\n", + flexion[0], flexion[1], flexion[2], flexion[3], flexion[4], + joyX, joyY, joyClick, + triggerButton, aButton, bButton, grab, pinch + ); return stringToEncode; } //legacy decoding -void decodeData(char* stringToDecode, int* hapticLimits) { - byte index = 0; - char* ptr = strtok(stringToDecode, "&"); // takes a list of delimiters - while (ptr != NULL) - { - hapticLimits[index] = atoi(ptr); - index++; - ptr = strtok(NULL, "&"); // takes a list of delimiters - } +void decodeData(char* stringToDecode, int* hapticLimits){ + byte index = 0; + char* ptr = strtok(stringToDecode, "&"); // takes a list of delimiters + while(ptr != NULL) + { + hapticLimits[index] = atoi(ptr); + index++; + ptr = strtok(NULL, "&"); // takes a list of delimiters + } } #endif #if ENCODING == ENCODE_ALPHA //alphabetic encoding -char* encode(int* flexion, int joyX, int joyY, bool joyClick, bool triggerButton, bool aButton, bool bButton, bool grab, bool pinch, bool calib, bool menu) { +char* encode(int* flexion, int joyX, int joyY, bool joyClick, bool triggerButton, bool aButton, bool bButton, bool grab, bool pinch, bool calib, bool menu){ static char stringToEncode[75]; - int trigger = (flexion[1] > ANALOG_MAX / 2) ? (flexion[1] - ANALOG_MAX / 2) * 2 : 0; - sprintf(stringToEncode, "A%dB%dC%dD%dE%dF%dG%dP%d%s%s%s%s%s%s%s%s\n", - flexion[0], flexion[1], flexion[2], flexion[3], flexion[4], - joyX, joyY, trigger, joyClick ? "H" : "", - triggerButton ? "I" : "", aButton ? "J" : "", bButton ? "K" : "", grab ? "L" : "", pinch ? "M" : "", menu ? "N" : "", calib ? "O" : "" - ); + int trigger = (flexion[1] > ANALOG_MAX/2) ? (flexion[1] - ANALOG_MAX/2) * 2:0; + sprintf(stringToEncode, "A%dB%dC%dD%dE%dF%dG%dP%d%s%s%s%s%s%s%s%s\n", + flexion[0], flexion[1], flexion[2], flexion[3], flexion[4], + joyX, joyY, trigger, joyClick?"H":"", + triggerButton?"I":"", aButton?"J":"", bButton?"K":"", grab?"L":"", pinch?"M":"", menu?"N":"", calib?"O":"" + ); return stringToEncode; } //legacy decoding -void decodeData(char* stringToDecode, int* hapticLimits) { +void decodeData(char* stringToDecode, int* hapticLimits){ hapticLimits[0] = getArgument(stringToDecode, 'A'); //thumb hapticLimits[1] = getArgument(stringToDecode, 'B'); //index hapticLimits[2] = getArgument(stringToDecode, 'C'); //middle @@ -63,7 +63,7 @@ void decodeData(char* stringToDecode, int* hapticLimits) { //Serial.println("Haptic: "+ (String)hapticLimits[0] + " " + (String)hapticLimits[1] + " " + (String)hapticLimits[2] + " " + (String)hapticLimits[3] + " " + (String)hapticLimits[4] + " "); } -int getArgument(char* stringToDecode, char command) { +int getArgument(char* stringToDecode, char command){ char* start = strchr(stringToDecode, command); if (start == NULL) return -1; diff --git a/firmware/lucidgloves-firmware/SerialBLECommunication.ino b/firmware/lucidgloves-firmware/SerialBLECommunication.ino deleted file mode 100644 index f17db11..0000000 --- a/firmware/lucidgloves-firmware/SerialBLECommunication.ino +++ /dev/null @@ -1,55 +0,0 @@ -//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/SerialBTCommunication.ino b/firmware/lucidgloves-firmware/SerialBTCommunication.ino index d185474..282dfbc 100644 --- a/firmware/lucidgloves-firmware/SerialBTCommunication.ino +++ b/firmware/lucidgloves-firmware/SerialBTCommunication.ino @@ -5,30 +5,30 @@ class BTSerialCommunication : public ICommunication { private: bool m_isOpen; BluetoothSerial m_SerialBT; - + public: BTSerialCommunication() { m_isOpen = false; } - bool isOpen() { + bool isOpen(){ return m_isOpen; } - void start() { + void start(){ Serial.begin(115200); m_SerialBT.begin(BTSERIAL_DEVICE_NAME); Serial.println("The device started, now you can pair it with bluetooth!"); m_isOpen = true; } - void output(char* data) { + void output(char* data){ m_SerialBT.print(data); } - bool readData(char* input) { + bool readData(char* input){ /*byte size = m_SerialBT.readBytesUntil('\n', input, 100); - input[size] = NULL;*/ + input[size] = NULL;*/ String message = m_SerialBT.readStringUntil('\n'); strcpy(input, message.c_str()); return input != NULL && strlen(input) > 0; diff --git a/firmware/lucidgloves-firmware/SerialCommunication.ino b/firmware/lucidgloves-firmware/SerialCommunication.ino index f6affea..32d27c4 100644 --- a/firmware/lucidgloves-firmware/SerialCommunication.ino +++ b/firmware/lucidgloves-firmware/SerialCommunication.ino @@ -7,22 +7,22 @@ class SerialCommunication : public ICommunication { m_isOpen = false; } - bool isOpen() { + bool isOpen(){ return m_isOpen; } - void start() { + void start(){ //Serial.setTimeout(1000000); Serial.begin(SERIAL_BAUD_RATE); m_isOpen = true; } - void output(char* data) { + void output(char* data){ Serial.print(data); Serial.flush(); } - bool readData(char* input) { + bool readData(char* input){ byte size = Serial.readBytesUntil('\n', input, 100); input[size] = NULL; return input != NULL && strlen(input) > 0; diff --git a/firmware/lucidgloves-firmware/_main.ino b/firmware/lucidgloves-firmware/_main.ino index ff5b22b..2a97184 100644 --- a/firmware/lucidgloves-firmware/_main.ino +++ b/firmware/lucidgloves-firmware/_main.ino @@ -2,87 +2,82 @@ #define CALIB_OVERRIDE false #if USING_CALIB_PIN && COMMUNICATION == COMM_SERIAL && PIN_CALIB == 0 && !CALIB_OVERRIDE -#error "You can't set your calibration pin to 0 over usb. You can calibrate with the BOOT button when using bluetooth only. Set CalibOverride to true to override this." + #error "You can't set your calibration pin to 0 over usb. You can calibrate with the BOOT button when using bluetooth only. Set CalibOverride to true to override this." #endif ICommunication* comm; int loops = 0; void setup() { -#if COMMUNICATION == COMM_SERIAL - comm = new SerialCommunication(); -#elif COMMUNICATION == COMM_BTSERIAL - comm = new BTSerialCommunication(); -#elif COMMUNICATION == COMM_BLE - comm = new BLECommunication(); -#endif + #if COMMUNICATION == COMM_SERIAL + comm = new SerialCommunication(); + #elif COMMUNICATION == COMM_BTSERIAL + comm = new BTSerialCommunication(); + #endif comm->start(); setupInputs(); -#if USING_FORCE_FEEDBACK - setupServoHaptics(); -#endif - + #if USING_FORCE_FEEDBACK + setupServoHaptics(); + #endif + } void loop() { - if (comm->isOpen()) { -#if COMMUNICATION == COMM_BLE - BLE.poll(); -#endif -#if USING_CALIB_PIN + if (comm->isOpen()){ + #if USING_CALIB_PIN bool calibButton = getButton(PIN_CALIB) != INVERT_CALIB; if (calibButton) loops = 0; -#else + #else bool calibButton = false; -#endif - + #endif + bool calibrate = false; - if (loops < CALIBRATION_LOOPS || ALWAYS_CALIBRATING) { + if (loops < CALIBRATION_LOOPS || ALWAYS_CALIBRATING){ calibrate = true; loops++; } - + int* fingerPos = getFingerPositions(calibrate, calibButton); bool joyButton = getButton(PIN_JOY_BTN) != INVERT_JOY; -#if TRIGGER_GESTURE + #if TRIGGER_GESTURE bool triggerButton = triggerGesture(fingerPos); -#else + #else bool triggerButton = getButton(PIN_TRIG_BTN) != INVERT_TRIGGER; -#endif + #endif bool aButton = getButton(PIN_A_BTN) != INVERT_A; bool bButton = getButton(PIN_B_BTN) != INVERT_B; -#if GRAB_GESTURE + #if GRAB_GESTURE bool grabButton = grabGesture(fingerPos); -#else + #else bool grabButton = getButton(PIN_GRAB_BTN) != INVERT_GRAB; -#endif + #endif -#if PINCH_GESTURE + #if PINCH_GESTURE bool pinchButton = pinchGesture(fingerPos); -#else + #else bool pinchButton = getButton(PIN_PNCH_BTN) != INVERT_PINCH; -#endif + #endif bool menuButton = getButton(PIN_MENU_BTN) != INVERT_MENU; - + comm->output(encode(fingerPos, getJoyX(), getJoyY(), joyButton, triggerButton, aButton, bButton, grabButton, pinchButton, calibButton, menuButton)); -#if USING_FORCE_FEEDBACK - char received[100]; - if (comm->readData(received)) { - int hapticLimits[5]; - //This check is a temporary hack to fix an issue with haptics on v0.5 of the driver, will make it more snobby code later - if (String(received).length() >= 10) { - decodeData(received, hapticLimits); - writeServoHaptics(hapticLimits); + #if USING_FORCE_FEEDBACK + char received[100]; + if (comm->readData(received)){ + int hapticLimits[5]; + //This check is a temporary hack to fix an issue with haptics on v0.5 of the driver, will make it more snobby code later + if(String(received).length() >= 10) { + decodeData(received, hapticLimits); + writeServoHaptics(hapticLimits); + } } - } -#endif + #endif delay(LOOP_TIME); } } diff --git a/firmware/lucidgloves-firmware/gesture.ino b/firmware/lucidgloves-firmware/gesture.ino index c072a22..072f815 100644 --- a/firmware/lucidgloves-firmware/gesture.ino +++ b/firmware/lucidgloves-firmware/gesture.ino @@ -1,11 +1,11 @@ -bool grabGesture(int *flexion) { - return (flexion[PINKY_IND] + flexion[RING_IND] + flexion[MIDDLE_IND] + flexion[INDEX_IND]) / 4 <= ANALOG_MAX / 2 ? 0 : 1; +bool grabGesture(int *flexion){ + return (flexion[PINKY_IND] + flexion[RING_IND] + flexion[MIDDLE_IND] + flexion[INDEX_IND]) / 4 <= ANALOG_MAX/2 ? 0:1; } -bool pinchGesture(int *flexion) { - return (flexion[INDEX_IND] + flexion[THUMB_IND]) / 2 <= ANALOG_MAX / 2 ? 0 : 1; +bool pinchGesture(int *flexion){ + return (flexion[INDEX_IND] + flexion[THUMB_IND]) / 2 <= ANALOG_MAX/2 ? 0:1; } -bool triggerGesture(int *flexion) { - return flexion[INDEX_IND] <= (ANALOG_MAX / 2) ? 0 : 1; +bool triggerGesture(int *flexion){ + return flexion[INDEX_IND]<=(ANALOG_MAX/2)?0:1; } diff --git a/firmware/lucidgloves-firmware/haptics.ino b/firmware/lucidgloves-firmware/haptics.ino index 4c49bd1..a915557 100644 --- a/firmware/lucidgloves-firmware/haptics.ino +++ b/firmware/lucidgloves-firmware/haptics.ino @@ -1,9 +1,9 @@ #if USING_FORCE_FEEDBACK #if defined(ESP32) -#include "ESP32Servo.h" + #include "ESP32Servo.h" #else -#include "Servo.h" + #include "Servo.h" #endif Servo pinkyServo; @@ -12,7 +12,7 @@ Servo middleServo; Servo indexServo; Servo thumbServo; -void setupServoHaptics() { +void setupServoHaptics(){ pinkyServo.attach(PIN_PINKY_MOTOR); ringServo.attach(PIN_RING_MOTOR); middleServo.attach(PIN_MIDDLE_MOTOR); @@ -21,34 +21,34 @@ void setupServoHaptics() { } //static scaling, maps to entire range of servo -void scaleLimits(int* hapticLimits, float* scaledLimits) { - for (int i = 0; i < 5; i++) { +void scaleLimits(int* hapticLimits, float* scaledLimits){ + for (int i = 0; i < 5; i++){ scaledLimits[i] = 180.0f - hapticLimits[i] / 1000.0f * 180.0f; } } //dynamic scaling, maps to the limits calibrated from your finger -void dynScaleLimits(int* hapticLimits, float* scaledLimits) { +void dynScaleLimits(int* hapticLimits, float* scaledLimits){ //will be refactored to take min and max as an argument - /* this implementation of dynamic scaling relies on the assumption - that the servo reaches 2/3 of the potentiometer's range, - and that 0 degrees is geared to the start of the potentiometer. - Different hardware types may need to handle dynamic scaling differently. - */ - for (int i = 0; i < sizeof(hapticLimits); i++) { + /* this implementation of dynamic scaling relies on the assumption + * that the servo reaches 2/3 of the potentiometer's range, + * and that 0 degrees is geared to the start of the potentiometer. + * Different hardware types may need to handle dynamic scaling differently. + */ + for (int i = 0; i < sizeof(hapticLimits); i++){ scaledLimits[i] = hapticLimits[i] / 1000.0f * 180.0f; } } -void writeServoHaptics(int* hapticLimits) { +void writeServoHaptics(int* hapticLimits){ float scaledLimits[5]; scaleLimits(hapticLimits, scaledLimits); - if (hapticLimits[0] >= 0) thumbServo.write(scaledLimits[0]); - if (hapticLimits[1] >= 0) indexServo.write(scaledLimits[1]); - if (hapticLimits[2] >= 0) middleServo.write(scaledLimits[2]); - if (hapticLimits[3] >= 0) ringServo.write(scaledLimits[3]); - if (hapticLimits[4] >= 0) pinkyServo.write(scaledLimits[4]); + if(hapticLimits[0] >= 0) thumbServo.write(scaledLimits[0]); + if(hapticLimits[1] >= 0) indexServo.write(scaledLimits[1]); + if(hapticLimits[2] >= 0) middleServo.write(scaledLimits[2]); + if(hapticLimits[3] >= 0) ringServo.write(scaledLimits[3]); + if(hapticLimits[4] >= 0) pinkyServo.write(scaledLimits[4]); } #endif diff --git a/firmware/lucidgloves-firmware/input.ino b/firmware/lucidgloves-firmware/input.ino index 58025ca..cc70952 100644 --- a/firmware/lucidgloves-firmware/input.ino +++ b/firmware/lucidgloves-firmware/input.ino @@ -1,133 +1,133 @@ // Requires RunningMedian library by Rob Tillaart #if ENABLE_MEDIAN_FILTER -#include -RunningMedian rmSamples[5] = { - RunningMedian(MEDIAN_SAMPLES), - RunningMedian(MEDIAN_SAMPLES), - RunningMedian(MEDIAN_SAMPLES), - RunningMedian(MEDIAN_SAMPLES), - RunningMedian(MEDIAN_SAMPLES) -}; + #include + RunningMedian rmSamples[5] = { + RunningMedian(MEDIAN_SAMPLES), + RunningMedian(MEDIAN_SAMPLES), + RunningMedian(MEDIAN_SAMPLES), + RunningMedian(MEDIAN_SAMPLES), + RunningMedian(MEDIAN_SAMPLES) + }; #endif -int maxFingers[5] = {0, 0, 0, 0, 0}; +int maxFingers[5] = {0,0,0,0,0}; int minFingers[5] = {ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX}; -void setupInputs() { +void setupInputs(){ pinMode(PIN_JOY_BTN, INPUT_PULLUP); pinMode(PIN_A_BTN, INPUT_PULLUP); pinMode(PIN_B_BTN, INPUT_PULLUP); pinMode(PIN_MENU_BTN, INPUT_PULLUP); - -#if !TRIGGER_GESTURE + + #if !TRIGGER_GESTURE pinMode(PIN_TRIG_BTN, INPUT_PULLUP); -#endif + #endif -#if !GRAB_GESTURE + #if !GRAB_GESTURE pinMode(PIN_GRAB_BTN, INPUT_PULLUP); -#endif + #endif -#if !PINCH_GESTURE + #if !PINCH_GESTURE pinMode(PIN_PNCH_BTN, INPUT_PULLUP); -#endif + #endif -#if USING_CALIB_PIN + #if USING_CALIB_PIN pinMode(PIN_CALIB, INPUT_PULLUP); -#endif + #endif } -int* getFingerPositions(bool calibrating, bool reset) { - int rawFingers[5] = {NO_THUMB ? 0 : analogRead(PIN_THUMB), analogRead(PIN_INDEX), analogRead(PIN_MIDDLE), analogRead(PIN_RING), analogRead(PIN_PINKY)}; +int* getFingerPositions(bool calibrating, bool reset){ + int rawFingers[5] = {NO_THUMB?0:analogRead(PIN_THUMB), analogRead(PIN_INDEX), analogRead(PIN_MIDDLE), analogRead(PIN_RING), analogRead(PIN_PINKY)}; //flip pot values if needed -#if FLIP_POTS - for (int i = 0; i < 5; i++) { + #if FLIP_POTS + for (int i = 0; i <5; i++){ rawFingers[i] = ANALOG_MAX - rawFingers[i]; } -#endif + #endif -#if ENABLE_MEDIAN_FILTER - for (int i = 0; i < 5; i++) { + #if ENABLE_MEDIAN_FILTER + for (int i = 0; i < 5; i++){ rmSamples[i].add( rawFingers[i] ); rawFingers[i] = rmSamples[i].getMedian(); } -#endif + #endif //reset max and mins as needed - if (reset) { - for (int i = 0; i < 5; i++) { + if (reset){ + for (int i = 0; i <5; i++){ maxFingers[i] = 0; minFingers[i] = ANALOG_MAX; } } - + //if during the calibration sequence, make sure to update max and mins - if (calibrating) { - for (int i = 0; i < 5; i++) { + if (calibrating){ + for (int i = 0; i <5; i++){ if (rawFingers[i] > maxFingers[i]) -#if CLAMP_FLEXION - maxFingers[i] = ( rawFingers[i] <= CLAMP_MAX ) ? rawFingers[i] : CLAMP_MAX; -#else - maxFingers[i] = rawFingers[i]; -#endif + #if CLAMP_FLEXION + maxFingers[i] = ( rawFingers[i] <= CLAMP_MAX )? rawFingers[i] : CLAMP_MAX; + #else + maxFingers[i] = rawFingers[i]; + #endif if (rawFingers[i] < minFingers[i]) -#if CLAMP_FLEXION - minFingers[i] = ( rawFingers[i] >= CLAMP_MIN ) ? rawFingers[i] : CLAMP_MIN; -#else - minFingers[i] = rawFingers[i]; -#endif + #if CLAMP_FLEXION + minFingers[i] = ( rawFingers[i] >= CLAMP_MIN )? rawFingers[i] : CLAMP_MIN; + #else + minFingers[i] = rawFingers[i]; + #endif } } - static int calibrated[5] = {511, 511, 511, 511, 511}; - - for (int i = 0; i < 5; i++) { - if (minFingers[i] != maxFingers[i]) { + static int calibrated[5] = {511,511,511,511,511}; + + for (int i = 0; i<5; i++){ + if (minFingers[i] != maxFingers[i]){ calibrated[i] = map( rawFingers[i], minFingers[i], maxFingers[i], 0, ANALOG_MAX ); -#if CLAMP_ANALOG_MAP - if (calibrated[i] < 0) - calibrated[i] = 0; - if (calibrated[i] > ANALOG_MAX) - calibrated[i] = ANALOG_MAX; -#endif + #if CLAMP_ANALOG_MAP + if (calibrated[i] < 0) + calibrated[i] = 0; + if (calibrated[i] > ANALOG_MAX) + calibrated[i] = ANALOG_MAX; + #endif } else { calibrated[i] = ANALOG_MAX / 2; } } return calibrated; - + } -int analogReadDeadzone(byte pin) { +int analogReadDeadzone(byte pin){ int raw = analogRead(pin); - if (abs(ANALOG_MAX / 2 - raw) < JOYSTICK_DEADZONE * ANALOG_MAX / 100) - return ANALOG_MAX / 2; + if (abs(ANALOG_MAX/2 - raw) < JOYSTICK_DEADZONE * ANALOG_MAX / 100) + return ANALOG_MAX/2; else return raw; } -int getJoyX() { -#if JOYSTICK_BLANK - return ANALOG_MAX / 2; -#elif JOY_FLIP_X +int getJoyX(){ + #if JOYSTICK_BLANK + return ANALOG_MAX/2; + #elif JOY_FLIP_X return ANALOG_MAX - analogReadDeadzone(PIN_JOY_X); -#else + #else return analogReadDeadzone(PIN_JOY_X); -#endif + #endif } -int getJoyY() { -#if JOYSTICK_BLANK - return ANALOG_MAX / 2; -#elif JOY_FLIP_Y +int getJoyY(){ + #if JOYSTICK_BLANK + return ANALOG_MAX/2; + #elif JOY_FLIP_Y return ANALOG_MAX - analogReadDeadzone(PIN_JOY_Y); -#else + #else return analogReadDeadzone(PIN_JOY_Y); -#endif + #endif } -bool getButton(byte pin) { +bool getButton(byte pin){ return digitalRead(pin) != HIGH; } diff --git a/firmware/lucidgloves-firmware/lucidgloves-firmware.ino b/firmware/lucidgloves-firmware/lucidgloves-firmware.ino index cf8016c..6f89bf5 100644 --- a/firmware/lucidgloves-firmware/lucidgloves-firmware.ino +++ b/firmware/lucidgloves-firmware/lucidgloves-firmware.ino @@ -1,8 +1,8 @@ /* - LucidGloves Firmware Version 4 - Author: Lucas_VRTech - LucidVR - lucidvrtech.com -*/ + * LucidGloves Firmware Version 4 + * Author: Lucas_VRTech - LucidVR + * lucidvrtech.com + */ #include "AdvancedConfig.h" @@ -10,13 +10,10 @@ //CONFIGURATION SETTINGS: #define COMMUNICATION COMM_SERIAL //Which communication protocol to use //serial over USB -#define SERIAL_BAUD_RATE 115200 - + #define SERIAL_BAUD_RATE 115200 + //serial over Bluetooth -#define BTSERIAL_DEVICE_NAME "lucidgloves-left" - -//Bluetooth Low Energy -#define BLE_DEVICE_NAME "lucidgloves-left" + #define BTSERIAL_DEVICE_NAME "lucidgloves-left" //ANALOG INPUT CONFIG #define FLIP_POTS false //Flip values from potentiometers (for fingers!) if they are backwards @@ -56,51 +53,51 @@ #define SERVO_SCALING false //dynamic scaling of servo motors #if defined(ESP32) -//(This configuration is for ESP32 DOIT V1 so make sure to change if you're on another board) -#define PIN_PINKY 36 -#define PIN_RING 39 -#define PIN_MIDDLE 34 -#define PIN_INDEX 35 -#define PIN_THUMB 32 -#define PIN_JOY_X 33 -#define PIN_JOY_Y 25 -#define PIN_JOY_BTN 26 -#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 -#define PIN_PNCH_BTN 23 //unused if gesture set -#define PIN_CALIB 12 //button for recalibration (You can set this to GPIO0 to use the BOOT button, but only when using Bluetooth.) -#define DEBUG_LED 2 -#define PIN_PINKY_MOTOR 5 //used for force feedback -#define PIN_RING_MOTOR 18 //^ -#define PIN_MIDDLE_MOTOR 19 //^ -#define PIN_INDEX_MOTOR 21 //^ -#define PIN_THUMB_MOTOR 17 //^ -#define PIN_MENU_BTN 27 - -//PINS CONFIGURATION + //(This configuration is for ESP32 DOIT V1 so make sure to change if you're on another board) + #define PIN_PINKY 36 + #define PIN_RING 39 + #define PIN_MIDDLE 34 + #define PIN_INDEX 35 + #define PIN_THUMB 32 + #define PIN_JOY_X 33 + #define PIN_JOY_Y 25 + #define PIN_JOY_BTN 26 + #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 + #define PIN_PNCH_BTN 23 //unused if gesture set + #define PIN_CALIB 12 //button for recalibration (You can set this to GPIO0 to use the BOOT button, but only when using Bluetooth.) + #define DEBUG_LED 2 + #define PIN_PINKY_MOTOR 5 //used for force feedback + #define PIN_RING_MOTOR 18 //^ + #define PIN_MIDDLE_MOTOR 19 //^ + #define PIN_INDEX_MOTOR 21 //^ + #define PIN_THUMB_MOTOR 17 //^ + #define PIN_MENU_BTN 27 + +//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 -#define PIN_RING A1 -#define PIN_MIDDLE A2 -#define PIN_INDEX A3 -#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_B_BTN 9 -#define PIN_TRIG_BTN 10 //unused if gesture set -#define PIN_GRAB_BTN 11 //unused if gesture set -#define PIN_PNCH_BTN 12 //unused if gesture set -#define PIN_CALIB 13 //button for recalibration -#define DEBUG_LED LED_BUILTIN -#define PIN_PINKY_MOTOR 2 //used for force feedback -#define PIN_RING_MOTOR 3 //^ -#define PIN_MIDDLE_MOTOR 4 //^ -#define PIN_INDEX_MOTOR 5 //^ -#define PIN_THUMB_MOTOR 6 //^ -#define PIN_MENU_BTN 8 + //(This configuration is for Arduino Nano so make sure to change if you're on another board) + #define PIN_PINKY A0 + #define PIN_RING A1 + #define PIN_MIDDLE A2 + #define PIN_INDEX A3 + #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_B_BTN 9 + #define PIN_TRIG_BTN 10 //unused if gesture set + #define PIN_GRAB_BTN 11 //unused if gesture set + #define PIN_PNCH_BTN 12 //unused if gesture set + #define PIN_CALIB 13 //button for recalibration + #define DEBUG_LED LED_BUILTIN + #define PIN_PINKY_MOTOR 2 //used for force feedback + #define PIN_RING_MOTOR 3 //^ + #define PIN_MIDDLE_MOTOR 4 //^ + #define PIN_INDEX_MOTOR 5 //^ + #define PIN_THUMB_MOTOR 6 //^ + #define PIN_MENU_BTN 8 #endif From e4e92d6ecd8d6f10d4a5fa1f436a706ec6b96e1a Mon Sep 17 00:00:00 2001 From: Kozova1 Date: Fri, 14 Apr 2023 13:24:00 +0300 Subject: [PATCH 3/3] Actually do the formatting right this time :) Signed-off-by: Kozova1 --- .../lucidgloves-firmware/AdvancedConfig.h | 8 +- firmware/lucidgloves-firmware/Encoding.ino | 56 +++---- .../SerialBTCommunication.ino | 12 +- .../SerialCommunication.ino | 8 +- firmware/lucidgloves-firmware/_main.ino | 74 +++++----- firmware/lucidgloves-firmware/gesture.ino | 12 +- firmware/lucidgloves-firmware/haptics.ino | 36 ++--- firmware/lucidgloves-firmware/input.ino | 138 +++++++++--------- .../lucidgloves-firmware.ino | 106 +++++++------- 9 files changed, 225 insertions(+), 225 deletions(-) diff --git a/firmware/lucidgloves-firmware/AdvancedConfig.h b/firmware/lucidgloves-firmware/AdvancedConfig.h index c4e9887..20b2cb0 100644 --- a/firmware/lucidgloves-firmware/AdvancedConfig.h +++ b/firmware/lucidgloves-firmware/AdvancedConfig.h @@ -4,8 +4,8 @@ #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 //Encoding #define ENCODING 1 @@ -23,13 +23,13 @@ #if defined(__AVR__) #define ANALOG_MAX 1023 #elif defined(ESP32) -#define ANALOG_MAX 4095 +#define ANALOG_MAX 4095 #endif //ANALOG_MAX OVERRIDE: //uncomment and set as needed (only touch if you know what you are doing) -//#define ANALOG_MAX 4095 +//#define ANALOG_MAX 4095 #ifndef ANALOG_MAX #error "This board doesn't have an auto ANALOG_MAX assignment, please set it manually by uncommenting ANALOG_MAX OVERRIDE!" diff --git a/firmware/lucidgloves-firmware/Encoding.ino b/firmware/lucidgloves-firmware/Encoding.ino index 713d34c..42abb1c 100644 --- a/firmware/lucidgloves-firmware/Encoding.ino +++ b/firmware/lucidgloves-firmware/Encoding.ino @@ -8,53 +8,53 @@ bool bButton; bool grab; bool pinch; -}; + }; -struct outputData{ + struct outputData{ int* hapticLimits; -}; + }; */ #if ENCODING == ENCODING_LEGACY //legacy encoding -char* encode(int* flexion, int joyX, int joyY, bool joyClick, bool triggerButton, bool aButton, bool bButton, bool grab, bool pinch, bool calib, bool menu){ +char* encode(int* flexion, int joyX, int joyY, bool joyClick, bool triggerButton, bool aButton, bool bButton, bool grab, bool pinch, bool calib, bool menu) { static char stringToEncode[75]; - - sprintf(stringToEncode, "%d&%d&%d&%d&%d&%d&%d&%d&%d&%d&%d&%d&%d\n", - flexion[0], flexion[1], flexion[2], flexion[3], flexion[4], - joyX, joyY, joyClick, - triggerButton, aButton, bButton, grab, pinch - ); + + sprintf(stringToEncode, "%d&%d&%d&%d&%d&%d&%d&%d&%d&%d&%d&%d&%d\n", + flexion[0], flexion[1], flexion[2], flexion[3], flexion[4], + joyX, joyY, joyClick, + triggerButton, aButton, bButton, grab, pinch + ); return stringToEncode; } //legacy decoding -void decodeData(char* stringToDecode, int* hapticLimits){ - byte index = 0; - char* ptr = strtok(stringToDecode, "&"); // takes a list of delimiters - while(ptr != NULL) - { - hapticLimits[index] = atoi(ptr); - index++; - ptr = strtok(NULL, "&"); // takes a list of delimiters - } +void decodeData(char* stringToDecode, int* hapticLimits) { + byte index = 0; + char* ptr = strtok(stringToDecode, "&"); // takes a list of delimiters + while (ptr != NULL) + { + hapticLimits[index] = atoi(ptr); + index++; + ptr = strtok(NULL, "&"); // takes a list of delimiters + } } #endif #if ENCODING == ENCODE_ALPHA //alphabetic encoding -char* encode(int* flexion, int joyX, int joyY, bool joyClick, bool triggerButton, bool aButton, bool bButton, bool grab, bool pinch, bool calib, bool menu){ +char* encode(int* flexion, int joyX, int joyY, bool joyClick, bool triggerButton, bool aButton, bool bButton, bool grab, bool pinch, bool calib, bool menu) { static char stringToEncode[75]; - int trigger = (flexion[1] > ANALOG_MAX/2) ? (flexion[1] - ANALOG_MAX/2) * 2:0; - sprintf(stringToEncode, "A%dB%dC%dD%dE%dF%dG%dP%d%s%s%s%s%s%s%s%s\n", - flexion[0], flexion[1], flexion[2], flexion[3], flexion[4], - joyX, joyY, trigger, joyClick?"H":"", - triggerButton?"I":"", aButton?"J":"", bButton?"K":"", grab?"L":"", pinch?"M":"", menu?"N":"", calib?"O":"" - ); + int trigger = (flexion[1] > ANALOG_MAX / 2) ? (flexion[1] - ANALOG_MAX / 2) * 2 : 0; + sprintf(stringToEncode, "A%dB%dC%dD%dE%dF%dG%dP%d%s%s%s%s%s%s%s%s\n", + flexion[0], flexion[1], flexion[2], flexion[3], flexion[4], + joyX, joyY, trigger, joyClick ? "H" : "", + triggerButton ? "I" : "", aButton ? "J" : "", bButton ? "K" : "", grab ? "L" : "", pinch ? "M" : "", menu ? "N" : "", calib ? "O" : "" + ); return stringToEncode; } //legacy decoding -void decodeData(char* stringToDecode, int* hapticLimits){ +void decodeData(char* stringToDecode, int* hapticLimits) { hapticLimits[0] = getArgument(stringToDecode, 'A'); //thumb hapticLimits[1] = getArgument(stringToDecode, 'B'); //index hapticLimits[2] = getArgument(stringToDecode, 'C'); //middle @@ -63,7 +63,7 @@ void decodeData(char* stringToDecode, int* hapticLimits){ //Serial.println("Haptic: "+ (String)hapticLimits[0] + " " + (String)hapticLimits[1] + " " + (String)hapticLimits[2] + " " + (String)hapticLimits[3] + " " + (String)hapticLimits[4] + " "); } -int getArgument(char* stringToDecode, char command){ +int getArgument(char* stringToDecode, char command) { char* start = strchr(stringToDecode, command); if (start == NULL) return -1; diff --git a/firmware/lucidgloves-firmware/SerialBTCommunication.ino b/firmware/lucidgloves-firmware/SerialBTCommunication.ino index 282dfbc..d185474 100644 --- a/firmware/lucidgloves-firmware/SerialBTCommunication.ino +++ b/firmware/lucidgloves-firmware/SerialBTCommunication.ino @@ -5,30 +5,30 @@ class BTSerialCommunication : public ICommunication { private: bool m_isOpen; BluetoothSerial m_SerialBT; - + public: BTSerialCommunication() { m_isOpen = false; } - bool isOpen(){ + bool isOpen() { return m_isOpen; } - void start(){ + void start() { Serial.begin(115200); m_SerialBT.begin(BTSERIAL_DEVICE_NAME); Serial.println("The device started, now you can pair it with bluetooth!"); m_isOpen = true; } - void output(char* data){ + void output(char* data) { m_SerialBT.print(data); } - bool readData(char* input){ + bool readData(char* input) { /*byte size = m_SerialBT.readBytesUntil('\n', input, 100); - input[size] = NULL;*/ + input[size] = NULL;*/ String message = m_SerialBT.readStringUntil('\n'); strcpy(input, message.c_str()); return input != NULL && strlen(input) > 0; diff --git a/firmware/lucidgloves-firmware/SerialCommunication.ino b/firmware/lucidgloves-firmware/SerialCommunication.ino index 32d27c4..f6affea 100644 --- a/firmware/lucidgloves-firmware/SerialCommunication.ino +++ b/firmware/lucidgloves-firmware/SerialCommunication.ino @@ -7,22 +7,22 @@ class SerialCommunication : public ICommunication { m_isOpen = false; } - bool isOpen(){ + bool isOpen() { return m_isOpen; } - void start(){ + void start() { //Serial.setTimeout(1000000); Serial.begin(SERIAL_BAUD_RATE); m_isOpen = true; } - void output(char* data){ + void output(char* data) { Serial.print(data); Serial.flush(); } - bool readData(char* input){ + bool readData(char* input) { byte size = Serial.readBytesUntil('\n', input, 100); input[size] = NULL; return input != NULL && strlen(input) > 0; diff --git a/firmware/lucidgloves-firmware/_main.ino b/firmware/lucidgloves-firmware/_main.ino index 2a97184..d29b428 100644 --- a/firmware/lucidgloves-firmware/_main.ino +++ b/firmware/lucidgloves-firmware/_main.ino @@ -2,82 +2,82 @@ #define CALIB_OVERRIDE false #if USING_CALIB_PIN && COMMUNICATION == COMM_SERIAL && PIN_CALIB == 0 && !CALIB_OVERRIDE - #error "You can't set your calibration pin to 0 over usb. You can calibrate with the BOOT button when using bluetooth only. Set CalibOverride to true to override this." +#error "You can't set your calibration pin to 0 over usb. You can calibrate with the BOOT button when using bluetooth only. Set CalibOverride to true to override this." #endif ICommunication* comm; int loops = 0; void setup() { - #if COMMUNICATION == COMM_SERIAL - comm = new SerialCommunication(); - #elif COMMUNICATION == COMM_BTSERIAL - comm = new BTSerialCommunication(); - #endif +#if COMMUNICATION == COMM_SERIAL + comm = new SerialCommunication(); +#elif COMMUNICATION == COMM_BTSERIAL + comm = new BTSerialCommunication(); +#endif comm->start(); setupInputs(); - #if USING_FORCE_FEEDBACK - setupServoHaptics(); - #endif - +#if USING_FORCE_FEEDBACK + setupServoHaptics(); +#endif + } void loop() { - if (comm->isOpen()){ - #if USING_CALIB_PIN + if (comm->isOpen()) { +#if USING_CALIB_PIN bool calibButton = getButton(PIN_CALIB) != INVERT_CALIB; if (calibButton) loops = 0; - #else +#else bool calibButton = false; - #endif - +#endif + bool calibrate = false; - if (loops < CALIBRATION_LOOPS || ALWAYS_CALIBRATING){ + if (loops < CALIBRATION_LOOPS || ALWAYS_CALIBRATING) { calibrate = true; loops++; } - + int* fingerPos = getFingerPositions(calibrate, calibButton); bool joyButton = getButton(PIN_JOY_BTN) != INVERT_JOY; - #if TRIGGER_GESTURE +#if TRIGGER_GESTURE bool triggerButton = triggerGesture(fingerPos); - #else +#else bool triggerButton = getButton(PIN_TRIG_BTN) != INVERT_TRIGGER; - #endif +#endif bool aButton = getButton(PIN_A_BTN) != INVERT_A; bool bButton = getButton(PIN_B_BTN) != INVERT_B; - #if GRAB_GESTURE +#if GRAB_GESTURE bool grabButton = grabGesture(fingerPos); - #else +#else bool grabButton = getButton(PIN_GRAB_BTN) != INVERT_GRAB; - #endif +#endif - #if PINCH_GESTURE +#if PINCH_GESTURE bool pinchButton = pinchGesture(fingerPos); - #else +#else bool pinchButton = getButton(PIN_PNCH_BTN) != INVERT_PINCH; - #endif +#endif bool menuButton = getButton(PIN_MENU_BTN) != INVERT_MENU; - + comm->output(encode(fingerPos, getJoyX(), getJoyY(), joyButton, triggerButton, aButton, bButton, grabButton, pinchButton, calibButton, menuButton)); - #if USING_FORCE_FEEDBACK - char received[100]; - if (comm->readData(received)){ - int hapticLimits[5]; - //This check is a temporary hack to fix an issue with haptics on v0.5 of the driver, will make it more snobby code later - if(String(received).length() >= 10) { - decodeData(received, hapticLimits); - writeServoHaptics(hapticLimits); - } +#if USING_FORCE_FEEDBACK + char received[100]; + if (comm->readData(received)) { + int hapticLimits[5]; + //This check is a temporary hack to fix an issue with haptics on v0.5 of the driver, will make it more snobby code later + if (String(received).length() >= 10) { + decodeData(received, hapticLimits); + writeServoHaptics(hapticLimits); } - #endif + } +#endif delay(LOOP_TIME); } } diff --git a/firmware/lucidgloves-firmware/gesture.ino b/firmware/lucidgloves-firmware/gesture.ino index 072f815..c072a22 100644 --- a/firmware/lucidgloves-firmware/gesture.ino +++ b/firmware/lucidgloves-firmware/gesture.ino @@ -1,11 +1,11 @@ -bool grabGesture(int *flexion){ - return (flexion[PINKY_IND] + flexion[RING_IND] + flexion[MIDDLE_IND] + flexion[INDEX_IND]) / 4 <= ANALOG_MAX/2 ? 0:1; +bool grabGesture(int *flexion) { + return (flexion[PINKY_IND] + flexion[RING_IND] + flexion[MIDDLE_IND] + flexion[INDEX_IND]) / 4 <= ANALOG_MAX / 2 ? 0 : 1; } -bool pinchGesture(int *flexion){ - return (flexion[INDEX_IND] + flexion[THUMB_IND]) / 2 <= ANALOG_MAX/2 ? 0:1; +bool pinchGesture(int *flexion) { + return (flexion[INDEX_IND] + flexion[THUMB_IND]) / 2 <= ANALOG_MAX / 2 ? 0 : 1; } -bool triggerGesture(int *flexion){ - return flexion[INDEX_IND]<=(ANALOG_MAX/2)?0:1; +bool triggerGesture(int *flexion) { + return flexion[INDEX_IND] <= (ANALOG_MAX / 2) ? 0 : 1; } diff --git a/firmware/lucidgloves-firmware/haptics.ino b/firmware/lucidgloves-firmware/haptics.ino index a915557..4c49bd1 100644 --- a/firmware/lucidgloves-firmware/haptics.ino +++ b/firmware/lucidgloves-firmware/haptics.ino @@ -1,9 +1,9 @@ #if USING_FORCE_FEEDBACK #if defined(ESP32) - #include "ESP32Servo.h" +#include "ESP32Servo.h" #else - #include "Servo.h" +#include "Servo.h" #endif Servo pinkyServo; @@ -12,7 +12,7 @@ Servo middleServo; Servo indexServo; Servo thumbServo; -void setupServoHaptics(){ +void setupServoHaptics() { pinkyServo.attach(PIN_PINKY_MOTOR); ringServo.attach(PIN_RING_MOTOR); middleServo.attach(PIN_MIDDLE_MOTOR); @@ -21,34 +21,34 @@ void setupServoHaptics(){ } //static scaling, maps to entire range of servo -void scaleLimits(int* hapticLimits, float* scaledLimits){ - for (int i = 0; i < 5; i++){ +void scaleLimits(int* hapticLimits, float* scaledLimits) { + for (int i = 0; i < 5; i++) { scaledLimits[i] = 180.0f - hapticLimits[i] / 1000.0f * 180.0f; } } //dynamic scaling, maps to the limits calibrated from your finger -void dynScaleLimits(int* hapticLimits, float* scaledLimits){ +void dynScaleLimits(int* hapticLimits, float* scaledLimits) { //will be refactored to take min and max as an argument - /* this implementation of dynamic scaling relies on the assumption - * that the servo reaches 2/3 of the potentiometer's range, - * and that 0 degrees is geared to the start of the potentiometer. - * Different hardware types may need to handle dynamic scaling differently. - */ - for (int i = 0; i < sizeof(hapticLimits); i++){ + /* this implementation of dynamic scaling relies on the assumption + that the servo reaches 2/3 of the potentiometer's range, + and that 0 degrees is geared to the start of the potentiometer. + Different hardware types may need to handle dynamic scaling differently. + */ + for (int i = 0; i < sizeof(hapticLimits); i++) { scaledLimits[i] = hapticLimits[i] / 1000.0f * 180.0f; } } -void writeServoHaptics(int* hapticLimits){ +void writeServoHaptics(int* hapticLimits) { float scaledLimits[5]; scaleLimits(hapticLimits, scaledLimits); - if(hapticLimits[0] >= 0) thumbServo.write(scaledLimits[0]); - if(hapticLimits[1] >= 0) indexServo.write(scaledLimits[1]); - if(hapticLimits[2] >= 0) middleServo.write(scaledLimits[2]); - if(hapticLimits[3] >= 0) ringServo.write(scaledLimits[3]); - if(hapticLimits[4] >= 0) pinkyServo.write(scaledLimits[4]); + if (hapticLimits[0] >= 0) thumbServo.write(scaledLimits[0]); + if (hapticLimits[1] >= 0) indexServo.write(scaledLimits[1]); + if (hapticLimits[2] >= 0) middleServo.write(scaledLimits[2]); + if (hapticLimits[3] >= 0) ringServo.write(scaledLimits[3]); + if (hapticLimits[4] >= 0) pinkyServo.write(scaledLimits[4]); } #endif diff --git a/firmware/lucidgloves-firmware/input.ino b/firmware/lucidgloves-firmware/input.ino index cc70952..58025ca 100644 --- a/firmware/lucidgloves-firmware/input.ino +++ b/firmware/lucidgloves-firmware/input.ino @@ -1,133 +1,133 @@ // Requires RunningMedian library by Rob Tillaart #if ENABLE_MEDIAN_FILTER - #include - RunningMedian rmSamples[5] = { - RunningMedian(MEDIAN_SAMPLES), - RunningMedian(MEDIAN_SAMPLES), - RunningMedian(MEDIAN_SAMPLES), - RunningMedian(MEDIAN_SAMPLES), - RunningMedian(MEDIAN_SAMPLES) - }; +#include +RunningMedian rmSamples[5] = { + RunningMedian(MEDIAN_SAMPLES), + RunningMedian(MEDIAN_SAMPLES), + RunningMedian(MEDIAN_SAMPLES), + RunningMedian(MEDIAN_SAMPLES), + RunningMedian(MEDIAN_SAMPLES) +}; #endif -int maxFingers[5] = {0,0,0,0,0}; +int maxFingers[5] = {0, 0, 0, 0, 0}; int minFingers[5] = {ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX}; -void setupInputs(){ +void setupInputs() { pinMode(PIN_JOY_BTN, INPUT_PULLUP); pinMode(PIN_A_BTN, INPUT_PULLUP); pinMode(PIN_B_BTN, INPUT_PULLUP); pinMode(PIN_MENU_BTN, INPUT_PULLUP); - - #if !TRIGGER_GESTURE + +#if !TRIGGER_GESTURE pinMode(PIN_TRIG_BTN, INPUT_PULLUP); - #endif +#endif - #if !GRAB_GESTURE +#if !GRAB_GESTURE pinMode(PIN_GRAB_BTN, INPUT_PULLUP); - #endif +#endif - #if !PINCH_GESTURE +#if !PINCH_GESTURE pinMode(PIN_PNCH_BTN, INPUT_PULLUP); - #endif +#endif - #if USING_CALIB_PIN +#if USING_CALIB_PIN pinMode(PIN_CALIB, INPUT_PULLUP); - #endif +#endif } -int* getFingerPositions(bool calibrating, bool reset){ - int rawFingers[5] = {NO_THUMB?0:analogRead(PIN_THUMB), analogRead(PIN_INDEX), analogRead(PIN_MIDDLE), analogRead(PIN_RING), analogRead(PIN_PINKY)}; +int* getFingerPositions(bool calibrating, bool reset) { + int rawFingers[5] = {NO_THUMB ? 0 : analogRead(PIN_THUMB), analogRead(PIN_INDEX), analogRead(PIN_MIDDLE), analogRead(PIN_RING), analogRead(PIN_PINKY)}; //flip pot values if needed - #if FLIP_POTS - for (int i = 0; i <5; i++){ +#if FLIP_POTS + for (int i = 0; i < 5; i++) { rawFingers[i] = ANALOG_MAX - rawFingers[i]; } - #endif +#endif - #if ENABLE_MEDIAN_FILTER - for (int i = 0; i < 5; i++){ +#if ENABLE_MEDIAN_FILTER + for (int i = 0; i < 5; i++) { rmSamples[i].add( rawFingers[i] ); rawFingers[i] = rmSamples[i].getMedian(); } - #endif +#endif //reset max and mins as needed - if (reset){ - for (int i = 0; i <5; i++){ + if (reset) { + for (int i = 0; i < 5; i++) { maxFingers[i] = 0; minFingers[i] = ANALOG_MAX; } } - + //if during the calibration sequence, make sure to update max and mins - if (calibrating){ - for (int i = 0; i <5; i++){ + if (calibrating) { + for (int i = 0; i < 5; i++) { if (rawFingers[i] > maxFingers[i]) - #if CLAMP_FLEXION - maxFingers[i] = ( rawFingers[i] <= CLAMP_MAX )? rawFingers[i] : CLAMP_MAX; - #else - maxFingers[i] = rawFingers[i]; - #endif +#if CLAMP_FLEXION + maxFingers[i] = ( rawFingers[i] <= CLAMP_MAX ) ? rawFingers[i] : CLAMP_MAX; +#else + maxFingers[i] = rawFingers[i]; +#endif if (rawFingers[i] < minFingers[i]) - #if CLAMP_FLEXION - minFingers[i] = ( rawFingers[i] >= CLAMP_MIN )? rawFingers[i] : CLAMP_MIN; - #else - minFingers[i] = rawFingers[i]; - #endif +#if CLAMP_FLEXION + minFingers[i] = ( rawFingers[i] >= CLAMP_MIN ) ? rawFingers[i] : CLAMP_MIN; +#else + minFingers[i] = rawFingers[i]; +#endif } } - static int calibrated[5] = {511,511,511,511,511}; - - for (int i = 0; i<5; i++){ - if (minFingers[i] != maxFingers[i]){ + static int calibrated[5] = {511, 511, 511, 511, 511}; + + for (int i = 0; i < 5; i++) { + if (minFingers[i] != maxFingers[i]) { calibrated[i] = map( rawFingers[i], minFingers[i], maxFingers[i], 0, ANALOG_MAX ); - #if CLAMP_ANALOG_MAP - if (calibrated[i] < 0) - calibrated[i] = 0; - if (calibrated[i] > ANALOG_MAX) - calibrated[i] = ANALOG_MAX; - #endif +#if CLAMP_ANALOG_MAP + if (calibrated[i] < 0) + calibrated[i] = 0; + if (calibrated[i] > ANALOG_MAX) + calibrated[i] = ANALOG_MAX; +#endif } else { calibrated[i] = ANALOG_MAX / 2; } } return calibrated; - + } -int analogReadDeadzone(byte pin){ +int analogReadDeadzone(byte pin) { int raw = analogRead(pin); - if (abs(ANALOG_MAX/2 - raw) < JOYSTICK_DEADZONE * ANALOG_MAX / 100) - return ANALOG_MAX/2; + if (abs(ANALOG_MAX / 2 - raw) < JOYSTICK_DEADZONE * ANALOG_MAX / 100) + return ANALOG_MAX / 2; else return raw; } -int getJoyX(){ - #if JOYSTICK_BLANK - return ANALOG_MAX/2; - #elif JOY_FLIP_X +int getJoyX() { +#if JOYSTICK_BLANK + return ANALOG_MAX / 2; +#elif JOY_FLIP_X return ANALOG_MAX - analogReadDeadzone(PIN_JOY_X); - #else +#else return analogReadDeadzone(PIN_JOY_X); - #endif +#endif } -int getJoyY(){ - #if JOYSTICK_BLANK - return ANALOG_MAX/2; - #elif JOY_FLIP_Y +int getJoyY() { +#if JOYSTICK_BLANK + return ANALOG_MAX / 2; +#elif JOY_FLIP_Y return ANALOG_MAX - analogReadDeadzone(PIN_JOY_Y); - #else +#else return analogReadDeadzone(PIN_JOY_Y); - #endif +#endif } -bool getButton(byte pin){ +bool getButton(byte pin) { return digitalRead(pin) != HIGH; } diff --git a/firmware/lucidgloves-firmware/lucidgloves-firmware.ino b/firmware/lucidgloves-firmware/lucidgloves-firmware.ino index 6f89bf5..2dafc13 100644 --- a/firmware/lucidgloves-firmware/lucidgloves-firmware.ino +++ b/firmware/lucidgloves-firmware/lucidgloves-firmware.ino @@ -1,8 +1,8 @@ /* - * LucidGloves Firmware Version 4 - * Author: Lucas_VRTech - LucidVR - * lucidvrtech.com - */ + LucidGloves Firmware Version 4 + Author: Lucas_VRTech - LucidVR + lucidvrtech.com +*/ #include "AdvancedConfig.h" @@ -10,10 +10,10 @@ //CONFIGURATION SETTINGS: #define COMMUNICATION COMM_SERIAL //Which communication protocol to use //serial over USB - #define SERIAL_BAUD_RATE 115200 - +#define SERIAL_BAUD_RATE 115200 + //serial over Bluetooth - #define BTSERIAL_DEVICE_NAME "lucidgloves-left" +#define BTSERIAL_DEVICE_NAME "lucidgloves-left" //ANALOG INPUT CONFIG #define FLIP_POTS false //Flip values from potentiometers (for fingers!) if they are backwards @@ -53,51 +53,51 @@ #define SERVO_SCALING false //dynamic scaling of servo motors #if defined(ESP32) - //(This configuration is for ESP32 DOIT V1 so make sure to change if you're on another board) - #define PIN_PINKY 36 - #define PIN_RING 39 - #define PIN_MIDDLE 34 - #define PIN_INDEX 35 - #define PIN_THUMB 32 - #define PIN_JOY_X 33 - #define PIN_JOY_Y 25 - #define PIN_JOY_BTN 26 - #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 - #define PIN_PNCH_BTN 23 //unused if gesture set - #define PIN_CALIB 12 //button for recalibration (You can set this to GPIO0 to use the BOOT button, but only when using Bluetooth.) - #define DEBUG_LED 2 - #define PIN_PINKY_MOTOR 5 //used for force feedback - #define PIN_RING_MOTOR 18 //^ - #define PIN_MIDDLE_MOTOR 19 //^ - #define PIN_INDEX_MOTOR 21 //^ - #define PIN_THUMB_MOTOR 17 //^ - #define PIN_MENU_BTN 27 - -//PINS CONFIGURATION +//(This configuration is for ESP32 DOIT V1 so make sure to change if you're on another board) +#define PIN_PINKY 36 +#define PIN_RING 39 +#define PIN_MIDDLE 34 +#define PIN_INDEX 35 +#define PIN_THUMB 32 +#define PIN_JOY_X 33 +#define PIN_JOY_Y 25 +#define PIN_JOY_BTN 26 +#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 +#define PIN_PNCH_BTN 23 //unused if gesture set +#define PIN_CALIB 12 //button for recalibration (You can set this to GPIO0 to use the BOOT button, but only when using Bluetooth.) +#define DEBUG_LED 2 +#define PIN_PINKY_MOTOR 5 //used for force feedback +#define PIN_RING_MOTOR 18 //^ +#define PIN_MIDDLE_MOTOR 19 //^ +#define PIN_INDEX_MOTOR 21 //^ +#define PIN_THUMB_MOTOR 17 //^ +#define PIN_MENU_BTN 27 + +//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 - #define PIN_RING A1 - #define PIN_MIDDLE A2 - #define PIN_INDEX A3 - #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_B_BTN 9 - #define PIN_TRIG_BTN 10 //unused if gesture set - #define PIN_GRAB_BTN 11 //unused if gesture set - #define PIN_PNCH_BTN 12 //unused if gesture set - #define PIN_CALIB 13 //button for recalibration - #define DEBUG_LED LED_BUILTIN - #define PIN_PINKY_MOTOR 2 //used for force feedback - #define PIN_RING_MOTOR 3 //^ - #define PIN_MIDDLE_MOTOR 4 //^ - #define PIN_INDEX_MOTOR 5 //^ - #define PIN_THUMB_MOTOR 6 //^ - #define PIN_MENU_BTN 8 +//(This configuration is for Arduino Nano so make sure to change if you're on another board) +#define PIN_PINKY A0 +#define PIN_RING A1 +#define PIN_MIDDLE A2 +#define PIN_INDEX A3 +#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_B_BTN 9 +#define PIN_TRIG_BTN 10 //unused if gesture set +#define PIN_GRAB_BTN 11 //unused if gesture set +#define PIN_PNCH_BTN 12 //unused if gesture set +#define PIN_CALIB 13 //button for recalibration +#define DEBUG_LED LED_BUILTIN +#define PIN_PINKY_MOTOR 2 //used for force feedback +#define PIN_RING_MOTOR 3 //^ +#define PIN_MIDDLE_MOTOR 4 //^ +#define PIN_INDEX_MOTOR 5 //^ +#define PIN_THUMB_MOTOR 6 //^ +#define PIN_MENU_BTN 8 #endif