From 23a68622791e7af2286743de4c5d1a75a067438e Mon Sep 17 00:00:00 2001 From: Lucas LucidVR <35583218+lucas-vrtech@users.noreply.github.com> Date: Fri, 28 Jul 2023 17:17:23 -0400 Subject: [PATCH 01/19] Make gesture into a cpp --- firmware/lucidgloves-firmware/AdvancedConfig.h | 1 + firmware/lucidgloves-firmware/_main.ino | 2 ++ firmware/lucidgloves-firmware/{gesture.ino => gesture.cpp} | 3 +++ firmware/lucidgloves-firmware/gesture.h | 7 +++++++ 4 files changed, 13 insertions(+) rename firmware/lucidgloves-firmware/{gesture.ino => gesture.cpp} (87%) create mode 100644 firmware/lucidgloves-firmware/gesture.h diff --git a/firmware/lucidgloves-firmware/AdvancedConfig.h b/firmware/lucidgloves-firmware/AdvancedConfig.h index 42316b0..bc3106b 100644 --- a/firmware/lucidgloves-firmware/AdvancedConfig.h +++ b/firmware/lucidgloves-firmware/AdvancedConfig.h @@ -1,3 +1,4 @@ +#pragma once //Advanced settings, only for the pros XD #define LOOP_TIME 1 //How much time between data sends (ms), set to 0 for a good time :) diff --git a/firmware/lucidgloves-firmware/_main.ino b/firmware/lucidgloves-firmware/_main.ino index 4626069..0357315 100644 --- a/firmware/lucidgloves-firmware/_main.ino +++ b/firmware/lucidgloves-firmware/_main.ino @@ -1,4 +1,6 @@ #include +#include "gesture.h" + #define ALWAYS_CALIBRATING CALIBRATION_LOOPS == -1 #define CALIB_OVERRIDE false diff --git a/firmware/lucidgloves-firmware/gesture.ino b/firmware/lucidgloves-firmware/gesture.cpp similarity index 87% rename from firmware/lucidgloves-firmware/gesture.ino rename to firmware/lucidgloves-firmware/gesture.cpp index 072f815..a2ad704 100644 --- a/firmware/lucidgloves-firmware/gesture.ino +++ b/firmware/lucidgloves-firmware/gesture.cpp @@ -1,3 +1,6 @@ +#include "gesture.h" +#include "AdvancedConfig.h" + bool grabGesture(int *flexion){ return (flexion[PINKY_IND] + flexion[RING_IND] + flexion[MIDDLE_IND] + flexion[INDEX_IND]) / 4 <= ANALOG_MAX/2 ? 0:1; } diff --git a/firmware/lucidgloves-firmware/gesture.h b/firmware/lucidgloves-firmware/gesture.h new file mode 100644 index 0000000..c4a6932 --- /dev/null +++ b/firmware/lucidgloves-firmware/gesture.h @@ -0,0 +1,7 @@ +#pragma once + +bool grabGesture(int *flexion); + +bool pinchGesture(int *flexion); + +bool triggerGesture(int *flexion); From b1d0a110e5efebc04f2125bfb6150884775e489d Mon Sep 17 00:00:00 2001 From: Lucas LucidVR <35583218+lucas-vrtech@users.noreply.github.com> Date: Fri, 28 Jul 2023 17:39:25 -0400 Subject: [PATCH 02/19] Make ICommunication a header --- .../{ICommunication.ino => ICommunication.h} | 3 ++- firmware/lucidgloves-firmware/SerialBTCommunication.ino | 2 ++ firmware/lucidgloves-firmware/SerialCommunication.ino | 2 ++ 3 files changed, 6 insertions(+), 1 deletion(-) rename firmware/lucidgloves-firmware/{ICommunication.ino => ICommunication.h} (94%) diff --git a/firmware/lucidgloves-firmware/ICommunication.ino b/firmware/lucidgloves-firmware/ICommunication.h similarity index 94% rename from firmware/lucidgloves-firmware/ICommunication.ino rename to firmware/lucidgloves-firmware/ICommunication.h index 34ca1f6..45e2dfd 100644 --- a/firmware/lucidgloves-firmware/ICommunication.ino +++ b/firmware/lucidgloves-firmware/ICommunication.h @@ -1,5 +1,6 @@ -//Interface for communication +#pragma once +//Interface for communication class ICommunication { public: diff --git a/firmware/lucidgloves-firmware/SerialBTCommunication.ino b/firmware/lucidgloves-firmware/SerialBTCommunication.ino index 3b84540..c9d590b 100644 --- a/firmware/lucidgloves-firmware/SerialBTCommunication.ino +++ b/firmware/lucidgloves-firmware/SerialBTCommunication.ino @@ -1,3 +1,5 @@ +#include "ICommunication.h" + //only compiles if BTSerial is set because it won't compile for a non-compatible board #if COMMUNICATION == COMM_BTSERIAL #include "BluetoothSerial.h" diff --git a/firmware/lucidgloves-firmware/SerialCommunication.ino b/firmware/lucidgloves-firmware/SerialCommunication.ino index 32d27c4..5e8805f 100644 --- a/firmware/lucidgloves-firmware/SerialCommunication.ino +++ b/firmware/lucidgloves-firmware/SerialCommunication.ino @@ -1,3 +1,5 @@ +#include "ICommunication.h" + class SerialCommunication : public ICommunication { private: bool m_isOpen; From c3ad4d305d4802aaf8684f5e600f47f045f8be5f Mon Sep 17 00:00:00 2001 From: Lucas LucidVR <35583218+lucas-vrtech@users.noreply.github.com> Date: Sat, 29 Jul 2023 14:01:35 -0400 Subject: [PATCH 03/19] Move configuration to Config.h --- .../lucidgloves-firmware/AdvancedConfig.h | 2 +- firmware/lucidgloves-firmware/Config.h | 158 ++++++++++++++++++ .../lucidgloves-firmware.ino | 156 +---------------- 3 files changed, 160 insertions(+), 156 deletions(-) create mode 100644 firmware/lucidgloves-firmware/Config.h diff --git a/firmware/lucidgloves-firmware/AdvancedConfig.h b/firmware/lucidgloves-firmware/AdvancedConfig.h index bc3106b..be51a88 100644 --- a/firmware/lucidgloves-firmware/AdvancedConfig.h +++ b/firmware/lucidgloves-firmware/AdvancedConfig.h @@ -1,5 +1,5 @@ #pragma once -//Advanced settings, only for the pros XD +//THIS FILE HAS SOME EXTRA SETTINGS, MAIN SETTINGS ARE IN Config.h #define LOOP_TIME 1 //How much time between data sends (ms), set to 0 for a good time :) #define CALIBRATION_LOOPS -1//How many loops should be calibrated. Set to -1 to always be calibrated. diff --git a/firmware/lucidgloves-firmware/Config.h b/firmware/lucidgloves-firmware/Config.h new file mode 100644 index 0000000..decf612 --- /dev/null +++ b/firmware/lucidgloves-firmware/Config.h @@ -0,0 +1,158 @@ +#pragma once +#include "ConfigUtils.h" +#include "AdvancedConfig.h" + +//LUCIDGLOVES CONFIGURATION SETTINGS :) + +//This is the configuration file, main structure in _main.ino +//CONFIGURATION SETTINGS: +#define COMMUNICATION COMM_SERIAL //Which communication to use. Options are: COMM_SERIAL (usb), COMM_BTSERIAL (bluetooth) +//serial over USB + #define SERIAL_BAUD_RATE 115200 + +//serial over Bluetooth + #define BTSERIAL_DEVICE_NAME "lucidgloves-left" + +//ANALOG INPUT CONFIG +#define USING_SPLAY false //whether or not your glove tracks splay. - tracks the side to side "wag" of fingers. Requires 5 more inputs. +#define USING_MULTIPLEXER true //Whether or not you are using a multiplexer for inputs +#define FLIP_FLEXION false //Flip values from potentiometers (for fingers!) if they are backwards +#define FLIP_SPLAY true //Flip values for splay + + +//Gesture enables, make false to use button override +#define TRIGGER_GESTURE true +#define GRAB_GESTURE true +#define PINCH_GESTURE true + + +//BUTTON INVERT +//If a button registers as pressed when not and vice versa (eg. using normally-closed switches), +//you can invert their behaviour here by setting their line to true. +//If unsure, set to false +#define INVERT_A false +#define INVERT_B false +#define INVERT_JOY false +#define INVERT_MENU false +#define INVERT_CALIB false +//These only apply with gesture button override: +#define INVERT_TRIGGER false +#define INVERT_GRAB false +#define INVERT_PINCH false + + +//joystick configuration +#define JOYSTICK_BLANK true //make true if not using the joystick +#define JOY_FLIP_X false +#define JOY_FLIP_Y false +#define JOYSTICK_DEADZONE 10 //deadzone in the joystick to prevent drift (in percent) + +#define NO_THUMB false //If for some reason you don't want to track the thumb + +#define USING_CALIB_PIN true //When PIN_CALIB is shorted (or it's button pushed) it will reset calibration if this is on. + +#define USING_FORCE_FEEDBACK false //Force feedback haptics allow you to feel the solid objects you hold +#define FLIP_FORCE_FEEDBACK true +#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) + //To use a pin on the multiplexer, use MUX(pin). So for example pin 15 on a mux would be MUX(15). + #define PIN_PINKY MUX(12) //These 5 are for flexion + #define PIN_RING MUX(9) + #define PIN_MIDDLE MUX(6) + #define PIN_INDEX MUX(3) + #define PIN_THUMB MUX(0) + #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 32 //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 19 //used for force feedback + #define PIN_RING_MOTOR 18 //^ + #define PIN_MIDDLE_MOTOR 5 //^ + #define PIN_INDEX_MOTOR 17 //^ + #define PIN_THUMB_MOTOR 16 //^ + #define PIN_MENU_BTN 34 + + //Splay pins. Only used for splay tracking gloves. Use MUX(pin) if you are using a multiplexer for it. + #define PIN_PINKY_SPLAY MUX(14) + #define PIN_RING_SPLAY MUX(11) + #define PIN_MIDDLE_SPLAY MUX(8) + #define PIN_INDEX_SPLAY MUX(5) + #define PIN_THUMB_SPLAY MUX(2) + + + //Select pins for multiplexers, set as needed if using a mux. You can add or remove pins as needed depending on how many select pins your mux needs. + #define PINS_MUX_SELECT 27, /*S0 pin*/ \ + 14, /*S1 pin*/ \ + 12, /*S2 pin*/ \ + 13 /*S3 pin (if your mux is 3-bit like 74HC4051 then you can remove this line and the backslash before it.)*/ + + #define MUX_INPUT 35 //the input or SIG pin of the multiplexer. This can't be a mux pin. + + //Signal mixing for finger values. Options are: MIXING_NONE, MIXING_SINCOS + //For double rotary hall effect sensors use MIXING_SINCOS. For potentiometers use MIXING_NONE. + #define FLEXION_MIXING MIXING_SINCOS + //Secondary analog pins for mixing flexion values. Only used by MIXING_SINCOS. Use MUX(pin) if you are using a multiplexer for it. + #define PIN_PINKY_SECOND MUX(13) + #define PIN_RING_SECOND MUX(10) + #define PIN_MIDDLE_SECOND MUX(7) + #define PIN_INDEX_SECOND MUX(4) + #define PIN_THUMB_SECOND MUX(1) + +//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 + + //Splay pins. Only used for splay tracking gloves. Use MUX(pin) if you are using a multiplexer for it. + #define PIN_PINKY_SPLAY MUX(10) + #define PIN_RING_SPLAY MUX(11) + #define PIN_MIDDLE_SPLAY MUX(12) + #define PIN_INDEX_SPLAY MUX(13) + #define PIN_THUMB_SPLAY MUX(14) + + //Select pins for multiplexers, set as needed if using a mux. You can add or remove pins as needed depending on how many select pins your mux needs. + #define PINS_MUX_SELECT 10, /*S0 pin*/ \ + 11, /*S1 pin*/ \ + 12, /*S2 pin*/ \ + 13 /*S3 pin (if your mux is 3-bit like 74HC4051 then you can remove this line and the backslash before it.)*/ + + #define MUX_INPUT A0 //the input or SIG pin of the multiplexer. This can't be a mux pin. + + //Signal mixing for finger values. Options are: MIXING_NONE, MIXING_SINCOS + //For double rotary hall effect sensors use MIXING_SINCOS. For potentiometers use MIXING_NONE. + #define FLEXION_MIXING MIXING_NONE + //Secondary analog pins for mixing. Only used by MIXING_SINCOS. Use MUX(pin) if you are using a multiplexer for it. + #define PIN_PINKY_SECOND MUX(1) + #define PIN_RING_SECOND MUX(3) + #define PIN_MIDDLE_SECOND MUX(5) + #define PIN_INDEX_SECOND MUX(7) + #define PIN_THUMB_SECOND MUX(9) +#endif diff --git a/firmware/lucidgloves-firmware/lucidgloves-firmware.ino b/firmware/lucidgloves-firmware/lucidgloves-firmware.ino index 8a1fa69..dfa00a6 100644 --- a/firmware/lucidgloves-firmware/lucidgloves-firmware.ino +++ b/firmware/lucidgloves-firmware/lucidgloves-firmware.ino @@ -4,159 +4,5 @@ * lucidvrtech.com */ -#include "ConfigUtils.h" +#include "Config.h" #include "AdvancedConfig.h" - - -//This is the configuration file, main structure in _main.ino -//CONFIGURATION SETTINGS: -#define COMMUNICATION COMM_SERIAL //Which communication protocol to use. Options are: COMM_SERIAL (usb), COMM_BTSERIAL (bluetooth) -//serial over USB - #define SERIAL_BAUD_RATE 115200 - -//serial over Bluetooth - #define BTSERIAL_DEVICE_NAME "lucidgloves-left" - -//ANALOG INPUT CONFIG -#define USING_SPLAY false //whether or not your glove tracks splay. - tracks the side to side "wag" of fingers. Requires 5 more inputs. -#define USING_MULTIPLEXER true //Whether or not you are using a multiplexer for inputs -#define FLIP_FLEXION false //Flip values from potentiometers (for fingers!) if they are backwards -#define FLIP_SPLAY true //Flip values for splay - - -//Gesture enables, make false to use button override -#define TRIGGER_GESTURE true -#define GRAB_GESTURE true -#define PINCH_GESTURE true - - -//BUTTON INVERT -//If a button registers as pressed when not and vice versa (eg. using normally-closed switches), -//you can invert their behaviour here by setting their line to true. -//If unsure, set to false -#define INVERT_A false -#define INVERT_B false -#define INVERT_JOY false -#define INVERT_MENU false -#define INVERT_CALIB false -//These only apply with gesture button override: -#define INVERT_TRIGGER false -#define INVERT_GRAB false -#define INVERT_PINCH false - - -//joystick configuration -#define JOYSTICK_BLANK true //make true if not using the joystick -#define JOY_FLIP_X false -#define JOY_FLIP_Y false -#define JOYSTICK_DEADZONE 10 //deadzone in the joystick to prevent drift (in percent) - -#define NO_THUMB false //If for some reason you don't want to track the thumb - -#define USING_CALIB_PIN true //When PIN_CALIB is shorted (or it's button pushed) it will reset calibration if this is on. - -#define USING_FORCE_FEEDBACK false //Force feedback haptics allow you to feel the solid objects you hold -#define FLIP_FORCE_FEEDBACK true -#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) - //To use a pin on the multiplexer, use MUX(pin). So for example pin 15 on a mux would be MUX(15). - #define PIN_PINKY MUX(12) //These 5 are for flexion - #define PIN_RING MUX(9) - #define PIN_MIDDLE MUX(6) - #define PIN_INDEX MUX(3) - #define PIN_THUMB MUX(0) - #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 32 //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 19 //used for force feedback - #define PIN_RING_MOTOR 18 //^ - #define PIN_MIDDLE_MOTOR 5 //^ - #define PIN_INDEX_MOTOR 17 //^ - #define PIN_THUMB_MOTOR 16 //^ - #define PIN_MENU_BTN 34 - - //Splay pins. Only used for splay tracking gloves. Use MUX(pin) if you are using a multiplexer for it. - #define PIN_PINKY_SPLAY MUX(14) - #define PIN_RING_SPLAY MUX(11) - #define PIN_MIDDLE_SPLAY MUX(8) - #define PIN_INDEX_SPLAY MUX(5) - #define PIN_THUMB_SPLAY MUX(2) - - - //Select pins for multiplexers, set as needed if using a mux. You can add or remove pins as needed depending on how many select pins your mux needs. - #define PINS_MUX_SELECT 27, /*S0 pin*/ \ - 14, /*S1 pin*/ \ - 12, /*S2 pin*/ \ - 13 /*S3 pin (if your mux is 3-bit like 74HC4051 then you can remove this line and the backslash before it.)*/ - - #define MUX_INPUT 35 //the input or SIG pin of the multiplexer. This can't be a mux pin. - - //Signal mixing for finger values. Options are: MIXING_NONE, MIXING_SINCOS - //For double rotary hall effect sensors use MIXING_SINCOS. For potentiometers use MIXING_NONE. - #define FLEXION_MIXING MIXING_SINCOS - //Secondary analog pins for mixing flexion values. Only used by MIXING_SINCOS. Use MUX(pin) if you are using a multiplexer for it. - #define PIN_PINKY_SECOND MUX(13) - #define PIN_RING_SECOND MUX(10) - #define PIN_MIDDLE_SECOND MUX(7) - #define PIN_INDEX_SECOND MUX(4) - #define PIN_THUMB_SECOND MUX(1) - -//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 - - //Splay pins. Only used for splay tracking gloves. Use MUX(pin) if you are using a multiplexer for it. - #define PIN_PINKY_SPLAY MUX(10) - #define PIN_RING_SPLAY MUX(11) - #define PIN_MIDDLE_SPLAY MUX(12) - #define PIN_INDEX_SPLAY MUX(13) - #define PIN_THUMB_SPLAY MUX(14) - - //Select pins for multiplexers, set as needed if using a mux. You can add or remove pins as needed depending on how many select pins your mux needs. - #define PINS_MUX_SELECT 10, /*S0 pin*/ \ - 11, /*S1 pin*/ \ - 12, /*S2 pin*/ \ - 13 /*S3 pin (if your mux is 3-bit like 74HC4051 then you can remove this line and the backslash before it.)*/ - - #define MUX_INPUT A0 //the input or SIG pin of the multiplexer. This can't be a mux pin. - - //Signal mixing for finger values. Options are: MIXING_NONE, MIXING_SINCOS - //For double rotary hall effect sensors use MIXING_SINCOS. For potentiometers use MIXING_NONE. - #define FLEXION_MIXING MIXING_NONE - //Secondary analog pins for mixing. Only used by MIXING_SINCOS. Use MUX(pin) if you are using a multiplexer for it. - #define PIN_PINKY_SECOND MUX(1) - #define PIN_RING_SECOND MUX(3) - #define PIN_MIDDLE_SECOND MUX(5) - #define PIN_INDEX_SECOND MUX(7) - #define PIN_THUMB_SECOND MUX(9) -#endif From afe004f0a9808cdc44dac2af4347eae7a445c554 Mon Sep 17 00:00:00 2001 From: Lucas LucidVR <35583218+lucas-vrtech@users.noreply.github.com> Date: Sat, 29 Jul 2023 14:38:06 -0400 Subject: [PATCH 04/19] BTSerialCommunication to cpp --- .../BTSerialCommunication.cpp | 37 +++++++++++++++ .../BTSerialCommunication.h | 24 ++++++++++ firmware/lucidgloves-firmware/Config.h | 2 +- firmware/lucidgloves-firmware/ConfigUtils.h | 12 ++--- .../SerialBTCommunication.ino | 45 ------------------- firmware/lucidgloves-firmware/_main.ino | 1 + .../lucidgloves-firmware.ino | 10 +++++ 7 files changed, 76 insertions(+), 55 deletions(-) create mode 100644 firmware/lucidgloves-firmware/BTSerialCommunication.cpp create mode 100644 firmware/lucidgloves-firmware/BTSerialCommunication.h delete mode 100644 firmware/lucidgloves-firmware/SerialBTCommunication.ino diff --git a/firmware/lucidgloves-firmware/BTSerialCommunication.cpp b/firmware/lucidgloves-firmware/BTSerialCommunication.cpp new file mode 100644 index 0000000..2df700b --- /dev/null +++ b/firmware/lucidgloves-firmware/BTSerialCommunication.cpp @@ -0,0 +1,37 @@ +#include "BTSerialCommunication.h" + +#if COMMUNICATION == COMM_BTSERIAL + +BTSerialCommunication::BTSerialCommunication() { + m_isOpen = false; +} + +bool BTSerialCommunication::isOpen() { + return m_isOpen; +} + +void BTSerialCommunication::start() { + m_SerialBT.begin(BTSERIAL_DEVICE_NAME); + #if BT_ECHO + Serial.begin(SERIAL_BAUD_RATE); + Serial.println("The device started, now you can pair it with bluetooth!"); + #endif + m_isOpen = true; +} + +void BTSerialCommunication::output(char* data) { + m_SerialBT.print(data); + #if BT_ECHO + Serial.print(data); + Serial.flush(); + #endif +} + +bool BTSerialCommunication::readData(char* input) { + /*byte size = m_SerialBT.readBytesUntil('\n', input, 100); + input[size] = NULL;*/ + String message = m_SerialBT.readStringUntil('\n'); + strcpy(input, message.c_str()); + return input != NULL && strlen(input) > 0; +} +#endif diff --git a/firmware/lucidgloves-firmware/BTSerialCommunication.h b/firmware/lucidgloves-firmware/BTSerialCommunication.h new file mode 100644 index 0000000..a55dba1 --- /dev/null +++ b/firmware/lucidgloves-firmware/BTSerialCommunication.h @@ -0,0 +1,24 @@ +#pragma once +#include "ICommunication.h" +#include "config.h" + +#if COMMUNICATION == COMM_BTSERIAL +#include "BluetoothSerial.h" + +class BTSerialCommunication : public ICommunication { +private: + bool m_isOpen; + BluetoothSerial m_SerialBT; + +public: + BTSerialCommunication(); + + bool isOpen() override; + + void start() override; + + void output(char* data) override; + + bool readData(char* input) override; +}; +#endif diff --git a/firmware/lucidgloves-firmware/Config.h b/firmware/lucidgloves-firmware/Config.h index decf612..a48547d 100644 --- a/firmware/lucidgloves-firmware/Config.h +++ b/firmware/lucidgloves-firmware/Config.h @@ -6,7 +6,7 @@ //This is the configuration file, main structure in _main.ino //CONFIGURATION SETTINGS: -#define COMMUNICATION COMM_SERIAL //Which communication to use. Options are: COMM_SERIAL (usb), COMM_BTSERIAL (bluetooth) +#define COMMUNICATION COMM_BTSERIAL //Which communication to use. Options are: COMM_SERIAL (usb), COMM_BTSERIAL (bluetooth) //serial over USB #define SERIAL_BAUD_RATE 115200 diff --git a/firmware/lucidgloves-firmware/ConfigUtils.h b/firmware/lucidgloves-firmware/ConfigUtils.h index c1305e6..d70b0e6 100644 --- a/firmware/lucidgloves-firmware/ConfigUtils.h +++ b/firmware/lucidgloves-firmware/ConfigUtils.h @@ -1,14 +1,12 @@ -//Contains the definitions that need to be evaluated before the main config file (lucidgloves-firmware.ino). +//Contains the definitions that need to be evaluated before the main config file (Config.h). //These shouldn't need to be changed. +#pragma once #include #include #include +#include -int lockTime = 0; -int lockLoops = 1; -int lockTimeTotal = 0; -int lockTimeLast = 0; class ordered_lock { std::queue cvar; @@ -17,10 +15,6 @@ class ordered_lock { public: ordered_lock() : locked(false) {}; void lock() { - lockTime = micros() - lockTimeLast; - lockTimeLast = micros(); - lockTimeTotal += lockTime; - lockLoops++; std::unique_lock acquire(cvar_lock); if (locked) { std::condition_variable signal; diff --git a/firmware/lucidgloves-firmware/SerialBTCommunication.ino b/firmware/lucidgloves-firmware/SerialBTCommunication.ino deleted file mode 100644 index c9d590b..0000000 --- a/firmware/lucidgloves-firmware/SerialBTCommunication.ino +++ /dev/null @@ -1,45 +0,0 @@ -#include "ICommunication.h" - -//only compiles if BTSerial is set because it won't compile for a non-compatible board -#if COMMUNICATION == COMM_BTSERIAL -#include "BluetoothSerial.h" -class BTSerialCommunication : public ICommunication { - private: - bool m_isOpen; - BluetoothSerial m_SerialBT; - - public: - BTSerialCommunication() { - m_isOpen = false; - } - - bool isOpen(){ - return m_isOpen; - } - - void start(){ - m_SerialBT.begin(BTSERIAL_DEVICE_NAME); - #if BT_ECHO - Serial.begin(SERIAL_BAUD_RATE); - Serial.println("The device started, now you can pair it with bluetooth!"); - #endif - m_isOpen = true; - } - - void output(char* data){ - m_SerialBT.print(data); - #if BT_ECHO - Serial.print(data); - Serial.flush(); - #endif - } - - bool readData(char* input){ - /*byte size = m_SerialBT.readBytesUntil('\n', input, 100); - input[size] = NULL;*/ - String message = m_SerialBT.readStringUntil('\n'); - strcpy(input, message.c_str()); - return input != NULL && strlen(input) > 0; - } -}; -#endif diff --git a/firmware/lucidgloves-firmware/_main.ino b/firmware/lucidgloves-firmware/_main.ino index 0357315..19da2b7 100644 --- a/firmware/lucidgloves-firmware/_main.ino +++ b/firmware/lucidgloves-firmware/_main.ino @@ -1,5 +1,6 @@ #include #include "gesture.h" +#include "BTSerialCommunication.h" #define ALWAYS_CALIBRATING CALIBRATION_LOOPS == -1 diff --git a/firmware/lucidgloves-firmware/lucidgloves-firmware.ino b/firmware/lucidgloves-firmware/lucidgloves-firmware.ino index dfa00a6..a936030 100644 --- a/firmware/lucidgloves-firmware/lucidgloves-firmware.ino +++ b/firmware/lucidgloves-firmware/lucidgloves-firmware.ino @@ -6,3 +6,13 @@ #include "Config.h" #include "AdvancedConfig.h" + +/* + * + * THE CONFIG SETTINGS ARE NO LONGER STORED HERE. THEY HAVE BEEN MOVED TO Config.h + * + * You can now change the settings over at config.h before uploading to your gloves. + * + */ + + From aa619d27e96d01133b20e40cb2644f79164f7451 Mon Sep 17 00:00:00 2001 From: Lucas LucidVR <35583218+lucas-vrtech@users.noreply.github.com> Date: Sat, 29 Jul 2023 14:50:07 -0400 Subject: [PATCH 05/19] Convert Serial to cpp --- firmware/lucidgloves-firmware/Config.h | 2 +- .../SerialCommunication.cpp | 26 +++++++++++++++ .../SerialCommunication.h | 20 ++++++++++++ .../SerialCommunication.ino | 32 ------------------- firmware/lucidgloves-firmware/_main.ino | 1 + 5 files changed, 48 insertions(+), 33 deletions(-) create mode 100644 firmware/lucidgloves-firmware/SerialCommunication.cpp create mode 100644 firmware/lucidgloves-firmware/SerialCommunication.h delete mode 100644 firmware/lucidgloves-firmware/SerialCommunication.ino diff --git a/firmware/lucidgloves-firmware/Config.h b/firmware/lucidgloves-firmware/Config.h index a48547d..decf612 100644 --- a/firmware/lucidgloves-firmware/Config.h +++ b/firmware/lucidgloves-firmware/Config.h @@ -6,7 +6,7 @@ //This is the configuration file, main structure in _main.ino //CONFIGURATION SETTINGS: -#define COMMUNICATION COMM_BTSERIAL //Which communication to use. Options are: COMM_SERIAL (usb), COMM_BTSERIAL (bluetooth) +#define COMMUNICATION COMM_SERIAL //Which communication to use. Options are: COMM_SERIAL (usb), COMM_BTSERIAL (bluetooth) //serial over USB #define SERIAL_BAUD_RATE 115200 diff --git a/firmware/lucidgloves-firmware/SerialCommunication.cpp b/firmware/lucidgloves-firmware/SerialCommunication.cpp new file mode 100644 index 0000000..521dcde --- /dev/null +++ b/firmware/lucidgloves-firmware/SerialCommunication.cpp @@ -0,0 +1,26 @@ +#include "SerialCommunication.h" + +SerialCommunication::SerialCommunication() { + m_isOpen = false; +} + +bool SerialCommunication::isOpen() { + return m_isOpen; +} + +void SerialCommunication::start() { + //Serial.setTimeout(1000000); + Serial.begin(SERIAL_BAUD_RATE); + m_isOpen = true; +} + +void SerialCommunication::output(char* data) { + Serial.print(data); + Serial.flush(); +} + +bool SerialCommunication::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/SerialCommunication.h b/firmware/lucidgloves-firmware/SerialCommunication.h new file mode 100644 index 0000000..5b1aa66 --- /dev/null +++ b/firmware/lucidgloves-firmware/SerialCommunication.h @@ -0,0 +1,20 @@ +#pragma once +#include "ICommunication.h" +#include "config.h" +#include + +class SerialCommunication : public ICommunication { +private: + bool m_isOpen; + +public: + SerialCommunication(); + + bool isOpen() override; + + void start() override; + + void output(char* data) override; + + bool readData(char* input) override; +}; diff --git a/firmware/lucidgloves-firmware/SerialCommunication.ino b/firmware/lucidgloves-firmware/SerialCommunication.ino deleted file mode 100644 index 5e8805f..0000000 --- a/firmware/lucidgloves-firmware/SerialCommunication.ino +++ /dev/null @@ -1,32 +0,0 @@ -#include "ICommunication.h" - -class SerialCommunication : public ICommunication { - private: - bool m_isOpen; - - public: - SerialCommunication() { - m_isOpen = false; - } - - bool isOpen(){ - return m_isOpen; - } - - void start(){ - //Serial.setTimeout(1000000); - Serial.begin(SERIAL_BAUD_RATE); - m_isOpen = true; - } - - void output(char* data){ - Serial.print(data); - Serial.flush(); - } - - 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 19da2b7..f57ed6d 100644 --- a/firmware/lucidgloves-firmware/_main.ino +++ b/firmware/lucidgloves-firmware/_main.ino @@ -1,5 +1,6 @@ #include #include "gesture.h" +#include "SerialCommunication.h" #include "BTSerialCommunication.h" #define ALWAYS_CALIBRATING CALIBRATION_LOOPS == -1 From 02338b404e0260deeff6af16e429991a1e03398b Mon Sep 17 00:00:00 2001 From: Lucas LucidVR <35583218+lucas-vrtech@users.noreply.github.com> Date: Sat, 29 Jul 2023 15:07:51 -0400 Subject: [PATCH 06/19] haptics.ino to cpp --- firmware/lucidgloves-firmware/Config.h | 2 +- firmware/lucidgloves-firmware/Haptics.cpp | 39 +++++++++++++++ firmware/lucidgloves-firmware/Haptics.h | 26 ++++++++++ firmware/lucidgloves-firmware/_main.ino | 6 ++- firmware/lucidgloves-firmware/haptics.ino | 59 ----------------------- 5 files changed, 70 insertions(+), 62 deletions(-) create mode 100644 firmware/lucidgloves-firmware/Haptics.cpp create mode 100644 firmware/lucidgloves-firmware/Haptics.h delete mode 100644 firmware/lucidgloves-firmware/haptics.ino diff --git a/firmware/lucidgloves-firmware/Config.h b/firmware/lucidgloves-firmware/Config.h index decf612..ff08133 100644 --- a/firmware/lucidgloves-firmware/Config.h +++ b/firmware/lucidgloves-firmware/Config.h @@ -51,7 +51,7 @@ #define USING_CALIB_PIN true //When PIN_CALIB is shorted (or it's button pushed) it will reset calibration if this is on. -#define USING_FORCE_FEEDBACK false //Force feedback haptics allow you to feel the solid objects you hold +#define USING_FORCE_FEEDBACK true //Force feedback haptics allow you to feel the solid objects you hold #define FLIP_FORCE_FEEDBACK true #define SERVO_SCALING false //dynamic scaling of servo motors diff --git a/firmware/lucidgloves-firmware/Haptics.cpp b/firmware/lucidgloves-firmware/Haptics.cpp new file mode 100644 index 0000000..7fdeea8 --- /dev/null +++ b/firmware/lucidgloves-firmware/Haptics.cpp @@ -0,0 +1,39 @@ +#include "Haptics.h" + +#if USING_FORCE_FEEDBACK + +void Haptics::setupServoHaptics() { + pinkyServo.attach(PIN_PINKY_MOTOR); + ringServo.attach(PIN_RING_MOTOR); + middleServo.attach(PIN_MIDDLE_MOTOR); + indexServo.attach(PIN_INDEX_MOTOR); + thumbServo.attach(PIN_THUMB_MOTOR); +} + +void Haptics::scaleLimits(int* hapticLimits, float* scaledLimits) { + for (int i = 0; i < 5; i++) { +#if FLIP_FORCE_FEEDBACK + scaledLimits[i] = hapticLimits[i] / 1000.0f * 180.0f; +#else + scaledLimits[i] = 180.0f - hapticLimits[i] / 1000.0f * 180.0f; +#endif + } +} + +void Haptics::dynScaleLimits(int* hapticLimits, float* scaledLimits) { + for (int i = 0; i < sizeof(hapticLimits); i++) { + scaledLimits[i] = hapticLimits[i] / 1000.0f * 180.0f; + } +} + +void Haptics::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]); +} + +#endif diff --git a/firmware/lucidgloves-firmware/Haptics.h b/firmware/lucidgloves-firmware/Haptics.h new file mode 100644 index 0000000..7fe0473 --- /dev/null +++ b/firmware/lucidgloves-firmware/Haptics.h @@ -0,0 +1,26 @@ +#pragma once +#include "config.h" + +#if USING_FORCE_FEEDBACK +#if defined(ESP32) + #include "ESP32Servo.h" +#else + #include "Servo.h" +#endif + +class Haptics { +private: + Servo pinkyServo; + Servo ringServo; + Servo middleServo; + Servo indexServo; + Servo thumbServo; + +public: + void setupServoHaptics(); + void scaleLimits(int* hapticLimits, float* scaledLimits); + void dynScaleLimits(int* hapticLimits, float* scaledLimits); + void writeServoHaptics(int* hapticLimits); +}; + +#endif diff --git a/firmware/lucidgloves-firmware/_main.ino b/firmware/lucidgloves-firmware/_main.ino index f57ed6d..e09faf0 100644 --- a/firmware/lucidgloves-firmware/_main.ino +++ b/firmware/lucidgloves-firmware/_main.ino @@ -1,5 +1,6 @@ #include #include "gesture.h" +#include "Haptics.h" #include "SerialCommunication.h" #include "BTSerialCommunication.h" @@ -15,6 +16,7 @@ bool calibButton = false; int* fingerPos = (int[]){0,0,0,0,0,0,0,0,0,0}; ICommunication* comm; +Haptics haptics; #if ESP32_DUAL_CORE_SET //std::mutex fingerPosMutex; @@ -61,7 +63,7 @@ void setup() { setupInputs(); #if USING_FORCE_FEEDBACK - setupServoHaptics(); + haptics.setupServoHaptics(); #endif #if ESP32_DUAL_CORE_SET @@ -176,7 +178,7 @@ void loop() { //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() >= 5) { decodeData(received, hapticLimits); - writeServoHaptics(hapticLimits); + haptics.writeServoHaptics(hapticLimits); } } #endif diff --git a/firmware/lucidgloves-firmware/haptics.ino b/firmware/lucidgloves-firmware/haptics.ino deleted file mode 100644 index 5162272..0000000 --- a/firmware/lucidgloves-firmware/haptics.ino +++ /dev/null @@ -1,59 +0,0 @@ -#if USING_FORCE_FEEDBACK - -#if defined(ESP32) - #include "ESP32Servo.h" -#else - #include "Servo.h" -#endif - -Servo pinkyServo; -Servo ringServo; -Servo middleServo; -Servo indexServo; -Servo thumbServo; - -void setupServoHaptics(){ - pinkyServo.attach(PIN_PINKY_MOTOR); - ringServo.attach(PIN_RING_MOTOR); - middleServo.attach(PIN_MIDDLE_MOTOR); - indexServo.attach(PIN_INDEX_MOTOR); - thumbServo.attach(PIN_THUMB_MOTOR); -} - -//static scaling, maps to entire range of servo -void scaleLimits(int* hapticLimits, float* scaledLimits){ - for (int i = 0; i < 5; i++){ - #if FLIP_FORCE_FEEDBACK - scaledLimits[i] = hapticLimits[i] / 1000.0f * 180.0f; - #else - scaledLimits[i] = 180.0f - hapticLimits[i] / 1000.0f * 180.0f; - #endif - } - -} - -//dynamic scaling, maps to the limits calibrated from your finger -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++){ - scaledLimits[i] = hapticLimits[i] / 1000.0f * 180.0f; - } -} - -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]); -} - -#endif From e76510a50081ee4ed88bbb1bf66c72e5ad014fd0 Mon Sep 17 00:00:00 2001 From: Lucas LucidVR <35583218+lucas-vrtech@users.noreply.github.com> Date: Sat, 29 Jul 2023 15:53:39 -0400 Subject: [PATCH 07/19] Make gesture a class --- .../lucidgloves-firmware/{gesture.cpp => Gesture.cpp} | 9 ++++----- firmware/lucidgloves-firmware/Gesture.h | 9 +++++++++ firmware/lucidgloves-firmware/_main.ino | 9 +++++---- firmware/lucidgloves-firmware/gesture.h | 7 ------- 4 files changed, 18 insertions(+), 16 deletions(-) rename firmware/lucidgloves-firmware/{gesture.cpp => Gesture.cpp} (63%) create mode 100644 firmware/lucidgloves-firmware/Gesture.h delete mode 100644 firmware/lucidgloves-firmware/gesture.h diff --git a/firmware/lucidgloves-firmware/gesture.cpp b/firmware/lucidgloves-firmware/Gesture.cpp similarity index 63% rename from firmware/lucidgloves-firmware/gesture.cpp rename to firmware/lucidgloves-firmware/Gesture.cpp index a2ad704..49bab54 100644 --- a/firmware/lucidgloves-firmware/gesture.cpp +++ b/firmware/lucidgloves-firmware/Gesture.cpp @@ -1,14 +1,13 @@ -#include "gesture.h" -#include "AdvancedConfig.h" +#include "Gesture.h" -bool grabGesture(int *flexion){ +bool Gesture::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){ +bool Gesture::pinchGesture(int *flexion){ return (flexion[INDEX_IND] + flexion[THUMB_IND]) / 2 <= ANALOG_MAX/2 ? 0:1; } -bool triggerGesture(int *flexion){ +bool Gesture::triggerGesture(int *flexion){ return flexion[INDEX_IND]<=(ANALOG_MAX/2)?0:1; } diff --git a/firmware/lucidgloves-firmware/Gesture.h b/firmware/lucidgloves-firmware/Gesture.h new file mode 100644 index 0000000..6f82f24 --- /dev/null +++ b/firmware/lucidgloves-firmware/Gesture.h @@ -0,0 +1,9 @@ +#pragma once +#include "AdvancedConfig.h" + +class Gesture { +public: + bool grabGesture(int *flexion); + bool pinchGesture(int *flexion); + bool triggerGesture(int *flexion); +}; diff --git a/firmware/lucidgloves-firmware/_main.ino b/firmware/lucidgloves-firmware/_main.ino index e09faf0..7612eed 100644 --- a/firmware/lucidgloves-firmware/_main.ino +++ b/firmware/lucidgloves-firmware/_main.ino @@ -1,5 +1,5 @@ #include -#include "gesture.h" +#include "Gesture.h" #include "Haptics.h" #include "SerialCommunication.h" #include "BTSerialCommunication.h" @@ -17,6 +17,7 @@ int* fingerPos = (int[]){0,0,0,0,0,0,0,0,0,0}; ICommunication* comm; Haptics haptics; +Gesture gesture; #if ESP32_DUAL_CORE_SET //std::mutex fingerPosMutex; @@ -130,7 +131,7 @@ void loop() { bool joyButton = getButton(PIN_JOY_BTN) != INVERT_JOY; #if TRIGGER_GESTURE - bool triggerButton = triggerGesture(fingerPos); + bool triggerButton = gesture.triggerGesture(fingerPos); #else bool triggerButton = getButton(PIN_TRIG_BTN) != INVERT_TRIGGER; #endif @@ -139,13 +140,13 @@ void loop() { bool bButton = getButton(PIN_B_BTN) != INVERT_B; #if GRAB_GESTURE - bool grabButton = grabGesture(fingerPos); + bool grabButton = gesture.grabGesture(fingerPos); #else bool grabButton = getButton(PIN_GRAB_BTN) != INVERT_GRAB; #endif #if PINCH_GESTURE - bool pinchButton = pinchGesture(fingerPos); + bool pinchButton = gesture.pinchGesture(fingerPos); #else bool pinchButton = getButton(PIN_PNCH_BTN) != INVERT_PINCH; #endif diff --git a/firmware/lucidgloves-firmware/gesture.h b/firmware/lucidgloves-firmware/gesture.h deleted file mode 100644 index c4a6932..0000000 --- a/firmware/lucidgloves-firmware/gesture.h +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once - -bool grabGesture(int *flexion); - -bool pinchGesture(int *flexion); - -bool triggerGesture(int *flexion); From 4f11d225474e23037d6ec30d23caa75a968b17bc Mon Sep 17 00:00:00 2001 From: Lucas LucidVR <35583218+lucas-vrtech@users.noreply.github.com> Date: Sun, 30 Jul 2023 16:20:52 -0400 Subject: [PATCH 08/19] Add new structs for decoded data --- firmware/lucidgloves-firmware/DataStructs.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 firmware/lucidgloves-firmware/DataStructs.h diff --git a/firmware/lucidgloves-firmware/DataStructs.h b/firmware/lucidgloves-firmware/DataStructs.h new file mode 100644 index 0000000..6890498 --- /dev/null +++ b/firmware/lucidgloves-firmware/DataStructs.h @@ -0,0 +1,20 @@ +#pragma once +#include "AdvancedConfig.h" + +struct ReceivedFields { + bool servoValuesReceived[NUM_FINGERS]; + bool specialCommandReceived; +}; + +struct DecodedData { + ReceivedFields fields; + int servoValues[NUM_FINGERS]; + SpecialCommands command; +}; + +enum SpecialCommands : uint8_t { + NO_COMMAND = 0, + CLEAR_FLAGS, + SAVE_INTERMEDIATE, + SAVE_TRAVEL, +}; From bd989186f62377b18e15c7ad5f4e62e04e5e31a1 Mon Sep 17 00:00:00 2001 From: Lucas LucidVR <35583218+lucas-vrtech@users.noreply.github.com> Date: Sun, 30 Jul 2023 20:16:59 -0400 Subject: [PATCH 09/19] Refactor encoding --- .../lucidgloves-firmware/AlphaEncoding.cpp | 54 +++++++++ firmware/lucidgloves-firmware/AlphaEncoding.h | 12 ++ firmware/lucidgloves-firmware/DataStructs.h | 19 ++-- firmware/lucidgloves-firmware/Encoding.ino | 105 ------------------ firmware/lucidgloves-firmware/IEncoding.h | 12 ++ .../lucidgloves-firmware/LegacyEncoding.cpp | 28 +++++ .../lucidgloves-firmware/LegacyEncoding.h | 10 ++ firmware/lucidgloves-firmware/_main.ino | 31 +++++- 8 files changed, 153 insertions(+), 118 deletions(-) create mode 100644 firmware/lucidgloves-firmware/AlphaEncoding.cpp create mode 100644 firmware/lucidgloves-firmware/AlphaEncoding.h delete mode 100644 firmware/lucidgloves-firmware/Encoding.ino create mode 100644 firmware/lucidgloves-firmware/IEncoding.h create mode 100644 firmware/lucidgloves-firmware/LegacyEncoding.cpp create mode 100644 firmware/lucidgloves-firmware/LegacyEncoding.h diff --git a/firmware/lucidgloves-firmware/AlphaEncoding.cpp b/firmware/lucidgloves-firmware/AlphaEncoding.cpp new file mode 100644 index 0000000..d2b1cd5 --- /dev/null +++ b/firmware/lucidgloves-firmware/AlphaEncoding.cpp @@ -0,0 +1,54 @@ +#include "AlphaEncoding.h" + +char* AlphaEncoding::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; + #if USING_SPLAY + sprintf(stringToEncode, "A%dB%dC%dD%dE%dF%dG%dP%d%s%s%s%s%s%s%s%s(AB)%d(BB)%d(CB)%d(DB)%d(EB)%d\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":"", + flexion[5], flexion[6], flexion[7], flexion[8], flexion[9] + ); + #else + 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":"" + ); + #endif + return stringToEncode; +} + +DecodedData AlphaEncoding::decodeData(char* stringToDecode) { + DecodedData decodedData = {}; + + if (strchr(stringToDecode, 'Z') != NULL) { + for (int i = 0; i < NUM_SPECIAL_COMMANDS; i++) { + if (strstr(stringToDecode, SPECIAL_COMMANDS[i]) != NULL) { + decodedData.command = SPECIAL_COMMANDS[i]; + decodedData.fields.specialCommandReceived = true; + return decodedData; + } + } + } + + for (int i = 0; i < NUM_FINGERS; i++) { + int value = getArgument(stringToDecode, 'A' + i); + if (value != -1) { + decodedData.servoValues[i] = value; + decodedData.fields.servoValuesReceived[i] = true; + } + } + + return decodedData; +} + + +int AlphaEncoding::getArgument(char* stringToDecode, char command){ + char* start = strchr(stringToDecode, command); + if (start == NULL) + return -1; + else + return atoi(start + 1); +} diff --git a/firmware/lucidgloves-firmware/AlphaEncoding.h b/firmware/lucidgloves-firmware/AlphaEncoding.h new file mode 100644 index 0000000..4f85a36 --- /dev/null +++ b/firmware/lucidgloves-firmware/AlphaEncoding.h @@ -0,0 +1,12 @@ +#pragma once + +#include "IEncoding.h" +#include "Config.h" + +class AlphaEncoding : public IEncoding { +public: + char* encode(int* flexion, int joyX, int joyY, bool joyClick, bool triggerButton, bool aButton, bool bButton, bool grab, bool pinch, bool calib, bool menu) override; + DecodedData decodeData(char* stringToDecode) override; +private: + int getArgument(char* stringToDecode, char command); +}; diff --git a/firmware/lucidgloves-firmware/DataStructs.h b/firmware/lucidgloves-firmware/DataStructs.h index 6890498..24041a2 100644 --- a/firmware/lucidgloves-firmware/DataStructs.h +++ b/firmware/lucidgloves-firmware/DataStructs.h @@ -1,6 +1,16 @@ #pragma once #include "AdvancedConfig.h" +const char* const SPECIAL_COMMANDS[] = { + "SaveInter", + "SaveTravel", + "ClearData" + // Add more commands as needed +}; + +//NUM_SPECIAL_COMMANDS can be defined this way because all char* are pointers of the same size +const int NUM_SPECIAL_COMMANDS = sizeof(SPECIAL_COMMANDS) / sizeof(SPECIAL_COMMANDS[0]); + struct ReceivedFields { bool servoValuesReceived[NUM_FINGERS]; bool specialCommandReceived; @@ -9,12 +19,5 @@ struct ReceivedFields { struct DecodedData { ReceivedFields fields; int servoValues[NUM_FINGERS]; - SpecialCommands command; -}; - -enum SpecialCommands : uint8_t { - NO_COMMAND = 0, - CLEAR_FLAGS, - SAVE_INTERMEDIATE, - SAVE_TRAVEL, + const char* command; }; diff --git a/firmware/lucidgloves-firmware/Encoding.ino b/firmware/lucidgloves-firmware/Encoding.ino deleted file mode 100644 index 5b2ba12..0000000 --- a/firmware/lucidgloves-firmware/Encoding.ino +++ /dev/null @@ -1,105 +0,0 @@ -/*struct inputData { - int* flexion; - int joyX; - int joyY; - bool joyClick; - bool triggerButton; - bool aButton; - bool bButton; - bool grab; - bool pinch; -}; - -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){ - 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 - ); - 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 - } -} -#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){ - static char stringToEncode[75]; - int trigger = (flexion[1] > ANALOG_MAX/2) ? (flexion[1] - ANALOG_MAX/2) * 2:0; - #if USING_SPLAY - sprintf(stringToEncode, "A%dB%dC%dD%dE%dF%dG%dP%d%s%s%s%s%s%s%s%s(AB)%d(BB)%d(CB)%d(DB)%d(EB)%d\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":"", - flexion[5], flexion[6], flexion[7], flexion[8], flexion[9] - ); - #else - 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":"" - ); - #endif - return stringToEncode; -} - -//alpha decoding -void decodeData(char* stringToDecode, int* hapticLimits){ - - //Check if a Z command was received - //Serial.println("Message recieved"); - if (strchr(stringToDecode, 'Z') != NULL) { - //Serial.println("Found Z!"); - bool toReturn = false; - if (strstr(stringToDecode, "ClearData") != NULL) { - clearFlags(); - toReturn = true; - } - if (strstr(stringToDecode, "SaveInter") != NULL) { - saveIntermediate(); - toReturn = true; - } - if (strstr(stringToDecode, "SaveTravel") != NULL) { - saveTravel(); - toReturn = true; - } - - if (toReturn) - return; - } - - hapticLimits[0] = getArgument(stringToDecode, 'A'); //thumb - hapticLimits[1] = getArgument(stringToDecode, 'B'); //index - hapticLimits[2] = getArgument(stringToDecode, 'C'); //middle - hapticLimits[3] = getArgument(stringToDecode, 'D'); //ring - hapticLimits[4] = getArgument(stringToDecode, 'E'); //pinky -} - -int getArgument(char* stringToDecode, char command){ - char* start = strchr(stringToDecode, command); - if (start == NULL) - return -1; - else - return atoi(start + 1); -} - -#endif diff --git a/firmware/lucidgloves-firmware/IEncoding.h b/firmware/lucidgloves-firmware/IEncoding.h new file mode 100644 index 0000000..0f31386 --- /dev/null +++ b/firmware/lucidgloves-firmware/IEncoding.h @@ -0,0 +1,12 @@ +#pragma once +#include "DataStructs.h" + +// Interface for encoding +class IEncoding { +public: + virtual char* encode(int* flexion, int joyX, int joyY, bool joyClick, + bool triggerButton, bool aButton, bool bButton, + bool grab, bool pinch, bool calib, bool menu) = 0; + + virtual DecodedData decodeData(char* stringToDecode) = 0; +}; diff --git a/firmware/lucidgloves-firmware/LegacyEncoding.cpp b/firmware/lucidgloves-firmware/LegacyEncoding.cpp new file mode 100644 index 0000000..2c41d89 --- /dev/null +++ b/firmware/lucidgloves-firmware/LegacyEncoding.cpp @@ -0,0 +1,28 @@ +#include "LegacyEncoding.h" + +char* LegacyEncoding::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 + ); + return stringToEncode; +} + +DecodedData LegacyEncoding::decodeData(char* stringToDecode) { + DecodedData decodedData = {}; + + byte index = 0; + char* ptr = strtok(stringToDecode, "&"); + while(ptr != NULL && index < NUM_FINGERS) + { + decodedData.servoValues[index] = atoi(ptr); + decodedData.fields.servoValuesReceived[index] = true; + index++; + ptr = strtok(NULL, "&"); + } + + return decodedData; +} diff --git a/firmware/lucidgloves-firmware/LegacyEncoding.h b/firmware/lucidgloves-firmware/LegacyEncoding.h new file mode 100644 index 0000000..ae18756 --- /dev/null +++ b/firmware/lucidgloves-firmware/LegacyEncoding.h @@ -0,0 +1,10 @@ +#pragma once + +#include "IEncoding.h" +#include "Config.h" + +class LegacyEncoding : public IEncoding { +public: + char* encode(int* flexion, int joyX, int joyY, bool joyClick, bool triggerButton, bool aButton, bool bButton, bool grab, bool pinch, bool calib, bool menu) override; + DecodedData decodeData(char* stringToDecode) override; +}; diff --git a/firmware/lucidgloves-firmware/_main.ino b/firmware/lucidgloves-firmware/_main.ino index 7612eed..0d1bc61 100644 --- a/firmware/lucidgloves-firmware/_main.ino +++ b/firmware/lucidgloves-firmware/_main.ino @@ -1,6 +1,8 @@ #include #include "Gesture.h" #include "Haptics.h" +#include "AlphaEncoding.h" +#include "LegacyEncoding.h" #include "SerialCommunication.h" #include "BTSerialCommunication.h" @@ -16,6 +18,7 @@ bool calibButton = false; int* fingerPos = (int[]){0,0,0,0,0,0,0,0,0,0}; ICommunication* comm; +IEncoding* encoding; Haptics haptics; Gesture gesture; @@ -53,12 +56,21 @@ int loops = 0; void setup() { pinMode(32, INPUT_PULLUP); pinMode(DEBUG_LED, OUTPUT); - digitalWrite(DEBUG_LED, HIGH); + digitalWrite(DEBUG_LED, HIGH); + #if COMMUNICATION == COMM_SERIAL comm = new SerialCommunication(); #elif COMMUNICATION == COMM_BTSERIAL comm = new BTSerialCommunication(); - #endif + #endif + + #if ENCODING == ENCODING_ALPHA + encoding = new AlphaEncoding(); + #elif ENCODING == ENCODING_LEGACY + encoding = new LegacyEncoding(); + #endif + + comm->start(); setupInputs(); @@ -171,15 +183,24 @@ void loop() { } - comm->output(encode(fingerPosCopy, getJoyX(), getJoyY(), joyButton, triggerButton, aButton, bButton, grabButton, pinchButton, calibButton, menuButton)); + comm->output(encoding->encode(fingerPosCopy, 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() >= 5) { - decodeData(received, hapticLimits); - haptics.writeServoHaptics(hapticLimits); + DecodedData recievedData = encoding->decodeData(received); + haptics.writeServoHaptics(recievedData.servoValues); + + if (recievedData.fields.specialCommandReceived){ + if (recievedData.command == "ClearData") + clearFlags(); + else if (recievedData.command == "SaveInter") + saveIntermediate(); + else if (recievedData.command == "SaveTravel") + saveTravel(); + } } } #endif From 3156243b5c826855ffd20a47c02888f117a024c1 Mon Sep 17 00:00:00 2001 From: Lucas LucidVR <35583218+lucas-vrtech@users.noreply.github.com> Date: Tue, 1 Aug 2023 16:40:35 -0400 Subject: [PATCH 10/19] Fix compile w no haptics --- firmware/lucidgloves-firmware/Haptics.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/firmware/lucidgloves-firmware/Haptics.h b/firmware/lucidgloves-firmware/Haptics.h index 7fe0473..0077bbb 100644 --- a/firmware/lucidgloves-firmware/Haptics.h +++ b/firmware/lucidgloves-firmware/Haptics.h @@ -1,7 +1,6 @@ #pragma once #include "config.h" -#if USING_FORCE_FEEDBACK #if defined(ESP32) #include "ESP32Servo.h" #else @@ -22,5 +21,3 @@ class Haptics { void dynScaleLimits(int* hapticLimits, float* scaledLimits); void writeServoHaptics(int* hapticLimits); }; - -#endif From 112b6afbc3391882f8c93ebb6a4440906844dd79 Mon Sep 17 00:00:00 2001 From: Lucas LucidVR <35583218+lucas-vrtech@users.noreply.github.com> Date: Tue, 1 Aug 2023 16:44:42 -0400 Subject: [PATCH 11/19] Fix encode macro typo --- firmware/lucidgloves-firmware/AdvancedConfig.h | 1 + firmware/lucidgloves-firmware/_main.ino | 9 +++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/firmware/lucidgloves-firmware/AdvancedConfig.h b/firmware/lucidgloves-firmware/AdvancedConfig.h index be51a88..8aae9eb 100644 --- a/firmware/lucidgloves-firmware/AdvancedConfig.h +++ b/firmware/lucidgloves-firmware/AdvancedConfig.h @@ -1,4 +1,5 @@ #pragma once +#include "ConfigUtils.h" //THIS FILE HAS SOME EXTRA SETTINGS, MAIN SETTINGS ARE IN Config.h #define LOOP_TIME 1 //How much time between data sends (ms), set to 0 for a good time :) diff --git a/firmware/lucidgloves-firmware/_main.ino b/firmware/lucidgloves-firmware/_main.ino index 0d1bc61..fa60133 100644 --- a/firmware/lucidgloves-firmware/_main.ino +++ b/firmware/lucidgloves-firmware/_main.ino @@ -5,6 +5,7 @@ #include "LegacyEncoding.h" #include "SerialCommunication.h" #include "BTSerialCommunication.h" +#include "AdvancedConfig.h" #define ALWAYS_CALIBRATING CALIBRATION_LOOPS == -1 @@ -62,12 +63,16 @@ void setup() { comm = new SerialCommunication(); #elif COMMUNICATION == COMM_BTSERIAL comm = new BTSerialCommunication(); + #else + #error "Communication not set." #endif - #if ENCODING == ENCODING_ALPHA + #if ENCODING == ENCODE_ALPHA encoding = new AlphaEncoding(); - #elif ENCODING == ENCODING_LEGACY + #elif ENCODING == ENCODE_LEGACY encoding = new LegacyEncoding(); + #else + #error "Encoding not set." #endif From 44120d7b3ca088576f416887c64b5e9896d48ac1 Mon Sep 17 00:00:00 2001 From: Lucas LucidVR <35583218+lucas-vrtech@users.noreply.github.com> Date: Tue, 1 Aug 2023 23:19:00 -0400 Subject: [PATCH 12/19] Convert input into class --- .../{input.ino => InputManager.cpp} | 165 ++++-------------- firmware/lucidgloves-firmware/InputManager.h | 93 ++++++++++ firmware/lucidgloves-firmware/_main.ino | 32 ++-- 3 files changed, 140 insertions(+), 150 deletions(-) rename firmware/lucidgloves-firmware/{input.ino => InputManager.cpp} (67%) create mode 100644 firmware/lucidgloves-firmware/InputManager.h diff --git a/firmware/lucidgloves-firmware/input.ino b/firmware/lucidgloves-firmware/InputManager.cpp similarity index 67% rename from firmware/lucidgloves-firmware/input.ino rename to firmware/lucidgloves-firmware/InputManager.cpp index f1df62f..33c1288 100644 --- a/firmware/lucidgloves-firmware/input.ino +++ b/firmware/lucidgloves-firmware/InputManager.cpp @@ -1,81 +1,16 @@ -#include -#include - -// Requires RunningMedian library by Rob Tillaart -#if (ENABLE_MEDIAN_FILTER || ((INTERFILTER_MODE != INTERFILTER_NONE) && (FLEXION_MIXING != MIXING_NONE))) - #include -#endif - -#if ENABLE_MEDIAN_FILTER - RunningMedian rmSamples[2* NUM_FINGERS] = { - RunningMedian(MEDIAN_SAMPLES), - RunningMedian(MEDIAN_SAMPLES), - RunningMedian(MEDIAN_SAMPLES), - RunningMedian(MEDIAN_SAMPLES), - RunningMedian(MEDIAN_SAMPLES), - RunningMedian(MEDIAN_SAMPLES), - RunningMedian(MEDIAN_SAMPLES), - RunningMedian(MEDIAN_SAMPLES), - RunningMedian(MEDIAN_SAMPLES), - RunningMedian(MEDIAN_SAMPLES) - }; -#endif - -#if ((INTERFILTER_MODE != INTERFILTER_NONE) && (FLEXION_MIXING == MIXING_SINCOS)) - RunningMedian sinSamples[NUM_FINGERS] = { - RunningMedian(INTERFILTER_SAMPLES), - RunningMedian(INTERFILTER_SAMPLES), - RunningMedian(INTERFILTER_SAMPLES), - RunningMedian(INTERFILTER_SAMPLES), - RunningMedian(INTERFILTER_SAMPLES) - }; - - RunningMedian cosSamples[NUM_FINGERS] = { - RunningMedian(INTERFILTER_SAMPLES), - RunningMedian(INTERFILTER_SAMPLES), - RunningMedian(INTERFILTER_SAMPLES), - RunningMedian(INTERFILTER_SAMPLES), - RunningMedian(INTERFILTER_SAMPLES) - }; -#endif - -#define DEFAULT_VREF 1100 -esp_adc_cal_characteristics_t *adc_chars; - -byte selectPins[] = {PINS_MUX_SELECT}; - -int maxFingers[2* NUM_FINGERS] = {0,0,0,0,0,0,0,0,0,0}; -int minFingers[2* NUM_FINGERS] = {ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX}; -int maxTravel[2*NUM_FINGERS] = {ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX}; -#if FLEXION_MIXING == MIXING_SINCOS - int sinMin[NUM_FINGERS] = {ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX}; - int sinMax[NUM_FINGERS] = {0,0,0,0,0}; - - int cosMin[NUM_FINGERS] = {ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX}; - int cosMax[NUM_FINGERS] = {0,0,0,0,0}; - - bool atanPositive[NUM_FINGERS] = {true, true, true, true, true}; - - - int totalOffset1[NUM_FINGERS] = {0,0,0,0,0}; -#endif - - -bool savedInter = false; -bool savedTravel = false; - -void setupInputs(){ +#include "InputManager.h" +InputManager::InputManager() { + // Constructor logic here... +} +void InputManager::setupInputs() { + EEPROM.begin(0x78 + 1); - Serial.begin(115200); //DON'T FORGET TO REMOVE THISSSSS - Serial.println("Setting up input!"); if (isSavedLimits()){ - Serial.println("Is saved limits!"); savedTravel = true; loadTravel(); } if (isSavedIntermediate()){ - Serial.println("IsSavedIntermediate!"); savedInter = true; loadIntermediate(); } @@ -103,15 +38,17 @@ void setupInputs(){ #endif #if USING_MULTIPLEXER - byte selectPins[] = {PINS_MUX_SELECT}; //pinMode(MUX_INPUT, INPUT); - for (int i = 0; i < sizeof(selectPins); i++){ - pinMode(selectPins[i], OUTPUT); + { + byte selectPins[] = {PINS_MUX_SELECT}; + for (int i = 0; i < sizeof(selectPins); i++){ + pinMode(selectPins[i], OUTPUT); + } } #endif } -int analogPinRead(int pin){ +int InputManager::analogPinRead(int pin) { #if USING_MULTIPLEXER if (ISMUX(pin)){ return readMux(UNMUX(pin)); @@ -124,8 +61,11 @@ int analogPinRead(int pin){ #endif } +// Other methods follow the same pattern... + #if USING_MULTIPLEXER -int readMux(byte pin){ +int InputManager::readMux(byte pin) { + byte selectPins[] = {PINS_MUX_SELECT}; int numSelectPins = sizeof(selectPins) / sizeof(selectPins[0]); for (int i = numSelectPins - 1; i > -1; i--){ @@ -137,8 +77,7 @@ int readMux(byte pin){ } #endif -int targetSinMin, targetSinMax, targetSinCurrent, targetCosMin, targetCosMax, targetCosCurrent, targetFlexionMin, targetFlexionMax, targetFlexionCurrent, targetMaxTravel, targetProcessed; -void getFingerPositions(bool calibrating, bool reset){ +void InputManager::getFingerPositions(bool calibrating, bool reset, int* fingerPos) { #if FLEXION_MIXING == MIXING_NONE //no mixing, just linear int rawFingersFlexion[NUM_FINGERS] = {NO_THUMB?0:analogPinRead(PIN_THUMB), analogPinRead(PIN_INDEX), analogPinRead(PIN_MIDDLE), analogPinRead(PIN_RING), analogPinRead(PIN_PINKY)}; @@ -226,16 +165,8 @@ void getFingerPositions(bool calibrating, bool reset){ } for (int i = 0; i +#include "Config.h" +#include "AdvancedConfig.h" + + +#if (ENABLE_MEDIAN_FILTER || ((INTERFILTER_MODE != INTERFILTER_NONE) && (FLEXION_MIXING != MIXING_NONE))) + #include +#endif + +class InputManager +{ +public: + InputManager(); + void setupInputs(); + int analogPinRead(int pin); + #if USING_MULTIPLEXER + int readMux(byte pin); + #endif + void getFingerPositions(bool calibrating, bool reset, int* fingerPos); + int analogReadDeadzone(int pin); + int getJoyX(); + int getJoyY(); + bool getButton(byte pin); + #if FLEXION_MIXING == MIXING_SINCOS + int sinCosMix(int sinPin, int cosPin, int i); + #endif + void saveTravel(); + void saveIntermediate(); + void clearFlags(); + bool isSavedLimits(); + bool isSavedIntermediate(); + void loadTravel(); + void loadIntermediate(); +private: + // Add your private variables here + bool savedInter = false; + bool savedTravel = false; + + int maxFingers[2* NUM_FINGERS] = {0,0,0,0,0,0,0,0,0,0}; + int minFingers[2* NUM_FINGERS] = {ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX}; + int maxTravel[2*NUM_FINGERS] = {ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX}; + + #if FLEXION_MIXING == MIXING_SINCOS + int sinMin[NUM_FINGERS] = {ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX}; + int sinMax[NUM_FINGERS] = {0,0,0,0,0}; + + int cosMin[NUM_FINGERS] = {ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX}; + int cosMax[NUM_FINGERS] = {0,0,0,0,0}; + + bool atanPositive[NUM_FINGERS] = {true, true, true, true, true}; + + + int totalOffset1[NUM_FINGERS] = {0,0,0,0,0}; + #endif + + #if ENABLE_MEDIAN_FILTER + RunningMedian rmSamples[2* NUM_FINGERS] = { + RunningMedian(MEDIAN_SAMPLES), + RunningMedian(MEDIAN_SAMPLES), + RunningMedian(MEDIAN_SAMPLES), + RunningMedian(MEDIAN_SAMPLES), + RunningMedian(MEDIAN_SAMPLES), + RunningMedian(MEDIAN_SAMPLES), + RunningMedian(MEDIAN_SAMPLES), + RunningMedian(MEDIAN_SAMPLES), + RunningMedian(MEDIAN_SAMPLES), + RunningMedian(MEDIAN_SAMPLES) + }; + #endif + + #if ((INTERFILTER_MODE != INTERFILTER_NONE) && (FLEXION_MIXING == MIXING_SINCOS)) + RunningMedian sinSamples[NUM_FINGERS] = { + RunningMedian(INTERFILTER_SAMPLES), + RunningMedian(INTERFILTER_SAMPLES), + RunningMedian(INTERFILTER_SAMPLES), + RunningMedian(INTERFILTER_SAMPLES), + RunningMedian(INTERFILTER_SAMPLES) + }; + + RunningMedian cosSamples[NUM_FINGERS] = { + RunningMedian(INTERFILTER_SAMPLES), + RunningMedian(INTERFILTER_SAMPLES), + RunningMedian(INTERFILTER_SAMPLES), + RunningMedian(INTERFILTER_SAMPLES), + RunningMedian(INTERFILTER_SAMPLES) + }; + #endif + + + +}; diff --git a/firmware/lucidgloves-firmware/_main.ino b/firmware/lucidgloves-firmware/_main.ino index fa60133..64debec 100644 --- a/firmware/lucidgloves-firmware/_main.ino +++ b/firmware/lucidgloves-firmware/_main.ino @@ -6,6 +6,7 @@ #include "SerialCommunication.h" #include "BTSerialCommunication.h" #include "AdvancedConfig.h" +#include "InputManager.h" #define ALWAYS_CALIBRATING CALIBRATION_LOOPS == -1 @@ -22,6 +23,7 @@ ICommunication* comm; IEncoding* encoding; Haptics haptics; Gesture gesture; +InputManager input; #if ESP32_DUAL_CORE_SET //std::mutex fingerPosMutex; @@ -40,7 +42,7 @@ void getInputs(void* parameter){ { fingerPosLock->lock(); totalLocks++; - getFingerPositions(calibrate, calibButton); //Save finger positions in thread + input.getFingerPositions(calibrate, calibButton, fingerPos); //Save finger positions in thread fingerPosLock->unlock(); } @@ -78,7 +80,7 @@ void setup() { comm->start(); - setupInputs(); + input.setupInputs(); #if USING_FORCE_FEEDBACK haptics.setupServoHaptics(); @@ -124,7 +126,7 @@ void loop() { if (comm->isOpen()){ #if USING_CALIB_PIN - calibButton = getButton(PIN_CALIB) != INVERT_CALIB; + calibButton = input.getButton(PIN_CALIB) != INVERT_CALIB; //Serial.println(getButton(PIN_CALIB)); if (calibButton) loops = 0; @@ -143,34 +145,34 @@ void loop() { } #if !ESP32_DUAL_CORE_SET - getFingerPositions(calibrate, calibButton); + input.getFingerPositions(calibrate, calibButton); #endif - bool joyButton = getButton(PIN_JOY_BTN) != INVERT_JOY; + bool joyButton = input.getButton(PIN_JOY_BTN) != INVERT_JOY; #if TRIGGER_GESTURE bool triggerButton = gesture.triggerGesture(fingerPos); #else - bool triggerButton = getButton(PIN_TRIG_BTN) != INVERT_TRIGGER; + bool triggerButton = input.getButton(PIN_TRIG_BTN) != INVERT_TRIGGER; #endif - bool aButton = getButton(PIN_A_BTN) != INVERT_A; - bool bButton = getButton(PIN_B_BTN) != INVERT_B; + bool aButton = input.getButton(PIN_A_BTN) != INVERT_A; + bool bButton = input.getButton(PIN_B_BTN) != INVERT_B; #if GRAB_GESTURE bool grabButton = gesture.grabGesture(fingerPos); #else - bool grabButton = getButton(PIN_GRAB_BTN) != INVERT_GRAB; + bool grabButton = input.getButton(PIN_GRAB_BTN) != INVERT_GRAB; #endif #if PINCH_GESTURE bool pinchButton = gesture.pinchGesture(fingerPos); #else - bool pinchButton = getButton(PIN_PNCH_BTN) != INVERT_PINCH; + bool pinchButton = input.getButton(PIN_PNCH_BTN) != INVERT_PINCH; #endif int fingerPosCopy[10]; int mutexTimeDone; - bool menuButton = getButton(PIN_MENU_BTN) != INVERT_MENU; + bool menuButton = input.getButton(PIN_MENU_BTN) != INVERT_MENU; { #if ESP32_DUAL_CORE_SET int mutexTime = micros(); @@ -188,7 +190,7 @@ void loop() { } - comm->output(encoding->encode(fingerPosCopy, getJoyX(), getJoyY(), joyButton, triggerButton, aButton, bButton, grabButton, pinchButton, calibButton, menuButton)); + comm->output(encoding->encode(fingerPosCopy, input.getJoyX(), input.getJoyY(), joyButton, triggerButton, aButton, bButton, grabButton, pinchButton, calibButton, menuButton)); #if USING_FORCE_FEEDBACK char received[100]; if (comm->readData(received)){ @@ -200,11 +202,11 @@ void loop() { if (recievedData.fields.specialCommandReceived){ if (recievedData.command == "ClearData") - clearFlags(); + input.clearFlags(); else if (recievedData.command == "SaveInter") - saveIntermediate(); + input.saveIntermediate(); else if (recievedData.command == "SaveTravel") - saveTravel(); + input.saveTravel(); } } } From 2ac60a929fa416e4f9b6e2b2e8a5ae593610d966 Mon Sep 17 00:00:00 2001 From: Lucas LucidVR <35583218+lucas-vrtech@users.noreply.github.com> Date: Tue, 1 Aug 2023 23:53:27 -0400 Subject: [PATCH 13/19] Convert main to cpp --- .../{_main.ino => Main.cpp} | 111 ++++++------------ firmware/lucidgloves-firmware/Main.h | 45 +++++++ .../lucidgloves-firmware.ino | 15 ++- 3 files changed, 90 insertions(+), 81 deletions(-) rename firmware/lucidgloves-firmware/{_main.ino => Main.cpp} (75%) create mode 100644 firmware/lucidgloves-firmware/Main.h diff --git a/firmware/lucidgloves-firmware/_main.ino b/firmware/lucidgloves-firmware/Main.cpp similarity index 75% rename from firmware/lucidgloves-firmware/_main.ino rename to firmware/lucidgloves-firmware/Main.cpp index 64debec..ad41bc9 100644 --- a/firmware/lucidgloves-firmware/_main.ino +++ b/firmware/lucidgloves-firmware/Main.cpp @@ -1,62 +1,16 @@ -#include -#include "Gesture.h" -#include "Haptics.h" -#include "AlphaEncoding.h" -#include "LegacyEncoding.h" -#include "SerialCommunication.h" -#include "BTSerialCommunication.h" -#include "AdvancedConfig.h" -#include "InputManager.h" +#include "Main.h" #define ALWAYS_CALIBRATING CALIBRATION_LOOPS == -1 - #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." #endif -bool calibrate = false; -bool calibButton = false; -int* fingerPos = (int[]){0,0,0,0,0,0,0,0,0,0}; - -ICommunication* comm; -IEncoding* encoding; -Haptics haptics; -Gesture gesture; -InputManager input; - -#if ESP32_DUAL_CORE_SET -//std::mutex fingerPosMutex; -ordered_lock* fingerPosLock = new ordered_lock(); -TaskHandle_t Task1; -int threadLoops = 1; -int totalLocks = 0; -int lastMicros = 0; -int fullLoopTime = 0; -int fullLoopTotal = 0; -void getInputs(void* parameter){ - for(;;){ - fullLoopTime = micros() - lastMicros; - fullLoopTotal += fullLoopTime; - lastMicros = micros(); - { - fingerPosLock->lock(); - totalLocks++; - input.getFingerPositions(calibrate, calibButton, fingerPos); //Save finger positions in thread - - fingerPosLock->unlock(); - } - threadLoops++; - if (threadLoops%100 == 0){ - vTaskDelay(1); //keep watchdog fed - } - delayMicroseconds(1); - } +Main::Main() { + // Constructor code here } -#endif -int loops = 0; -void setup() { +void Main::setup() { pinMode(32, INPUT_PULLUP); pinMode(DEBUG_LED, OUTPUT); digitalWrite(DEBUG_LED, HIGH); @@ -88,41 +42,17 @@ void setup() { #if ESP32_DUAL_CORE_SET xTaskCreatePinnedToCore( - getInputs, /* Function to implement the task */ + Main::getInputsWrapper, /* Function to implement the task */ "Get_Inputs", /* Name of the task */ 10000, /* Stack size in words */ - NULL, /* Task input parameter */ + this, /* Task input parameter */ tskIDLE_PRIORITY, /* Priority of the task */ &Task1, /* Task handle. */ 0); /* Core where the task should run */ #endif } - -int lastMainMicros = micros(); -int mainMicros = 0; -int mainMicrosTotal = 0; -int mainloops = 1; - -int target = 0; -bool latch = false; - -void loop() { - mainloops++; - mainMicros = micros() - lastMainMicros; - mainMicrosTotal += mainMicros; - lastMainMicros = micros(); - - if (!digitalRead(27)){ - if (!latch){ - target++; - target %= 5; - - latch = true; - } - } - else - latch = false; +void Main::loop() { if (comm->isOpen()){ #if USING_CALIB_PIN @@ -145,7 +75,7 @@ void loop() { } #if !ESP32_DUAL_CORE_SET - input.getFingerPositions(calibrate, calibButton); + input.getFingerPositions(calibrate, calibButton, fingerPos); #endif bool joyButton = input.getButton(PIN_JOY_BTN) != INVERT_JOY; @@ -214,3 +144,28 @@ void loop() { delay(LOOP_TIME); } } + + +#if ESP32_DUAL_CORE_SET +void Main::getInputs(){ + for(;;){ + { + fingerPosLock->lock(); + input.getFingerPositions(calibrate, calibButton, fingerPos); //Save finger positions in thread + + fingerPosLock->unlock(); + } + threadLoops++; + if (threadLoops%100 == 0){ + vTaskDelay(1); //keep watchdog fed + } + delayMicroseconds(1); + } +} + +void Main::getInputsWrapper(void* _this) { + // Cast the void* parameter back to a Main* and call getInputs on it + static_cast(_this)->getInputs(); +} + +#endif diff --git a/firmware/lucidgloves-firmware/Main.h b/firmware/lucidgloves-firmware/Main.h new file mode 100644 index 0000000..555044a --- /dev/null +++ b/firmware/lucidgloves-firmware/Main.h @@ -0,0 +1,45 @@ +#pragma once + +#include +#include "Gesture.h" +#include "Haptics.h" +#include "AlphaEncoding.h" +#include "LegacyEncoding.h" +#include "SerialCommunication.h" +#include "BTSerialCommunication.h" +#include "AdvancedConfig.h" +#include "ConfigUtils.h" +#include "InputManager.h" + + +class Main { +public: + Main(); + void setup(); + void loop(); + + #if ESP32_DUAL_CORE_SET + static void getInputsWrapper(void* _this); + #endif + +private: + + ICommunication* comm; + IEncoding* encoding; + Haptics haptics; + Gesture gesture; + InputManager input; + + bool calibrate = false; + bool calibButton = false; + int* fingerPos = (int[]){0,0,0,0,0,0,0,0,0,0}; + int loops = 0; + + #if ESP32_DUAL_CORE_SET + void getInputs(); + ordered_lock* fingerPosLock = new ordered_lock(); + TaskHandle_t Task1; + int threadLoops = 1; + #endif + +}; diff --git a/firmware/lucidgloves-firmware/lucidgloves-firmware.ino b/firmware/lucidgloves-firmware/lucidgloves-firmware.ino index a936030..b3d03dd 100644 --- a/firmware/lucidgloves-firmware/lucidgloves-firmware.ino +++ b/firmware/lucidgloves-firmware/lucidgloves-firmware.ino @@ -4,8 +4,8 @@ * lucidvrtech.com */ -#include "Config.h" -#include "AdvancedConfig.h" + +#include "Main.h" /* * @@ -15,4 +15,13 @@ * */ - + +Main mainClass; + +void setup(){ + mainClass.setup(); +} + +void loop(){ + mainClass.loop(); +} From 71102d7a1ebbee7d56afe7599e30d895f79bae7b Mon Sep 17 00:00:00 2001 From: Lucas LucidVR <35583218+lucas-vrtech@users.noreply.github.com> Date: Wed, 2 Aug 2023 22:32:21 -0400 Subject: [PATCH 14/19] Project folder structure --- .../lucidgloves-firmware/AdvancedConfig.h | 2 +- firmware/lucidgloves-firmware/Config.h | 2 +- firmware/lucidgloves-firmware/LICENSE | 21 ------------------- .../lucidgloves-firmware.ino | 2 +- .../Communication}/BTSerialCommunication.cpp | 0 .../Communication}/BTSerialCommunication.h | 2 +- .../{ => src/Communication}/ICommunication.h | 0 .../Communication}/SerialCommunication.cpp | 0 .../Communication}/SerialCommunication.h | 2 +- .../{ => src/Controller}/Gesture.cpp | 0 .../{ => src/Controller}/Gesture.h | 2 +- .../{ => src/Controller}/Haptics.cpp | 0 .../{ => src/Controller}/Haptics.h | 2 +- .../{ => src/Controller}/InputManager.cpp | 0 .../{ => src/Controller}/InputManager.h | 4 ++-- .../{ => src/Encoding}/AlphaEncoding.cpp | 0 .../{ => src/Encoding}/AlphaEncoding.h | 0 .../{ => src/Encoding}/IEncoding.h | 2 +- .../{ => src/Encoding}/LegacyEncoding.cpp | 0 .../{ => src/Encoding}/LegacyEncoding.h | 0 .../lucidgloves-firmware/{ => src}/Main.cpp | 0 .../lucidgloves-firmware/{ => src}/Main.h | 18 ++++++++-------- .../{ => src/Util}/ConfigUtils.h | 0 .../{ => src/Util}/DataStructs.h | 2 +- 24 files changed, 20 insertions(+), 41 deletions(-) delete mode 100644 firmware/lucidgloves-firmware/LICENSE rename firmware/lucidgloves-firmware/{ => src/Communication}/BTSerialCommunication.cpp (100%) rename firmware/lucidgloves-firmware/{ => src/Communication}/BTSerialCommunication.h (94%) rename firmware/lucidgloves-firmware/{ => src/Communication}/ICommunication.h (100%) rename firmware/lucidgloves-firmware/{ => src/Communication}/SerialCommunication.cpp (100%) rename firmware/lucidgloves-firmware/{ => src/Communication}/SerialCommunication.h (92%) rename firmware/lucidgloves-firmware/{ => src/Controller}/Gesture.cpp (100%) rename firmware/lucidgloves-firmware/{ => src/Controller}/Gesture.h (81%) rename firmware/lucidgloves-firmware/{ => src/Controller}/Haptics.cpp (100%) rename firmware/lucidgloves-firmware/{ => src/Controller}/Haptics.h (94%) rename firmware/lucidgloves-firmware/{ => src/Controller}/InputManager.cpp (100%) rename firmware/lucidgloves-firmware/{ => src/Controller}/InputManager.h (97%) rename firmware/lucidgloves-firmware/{ => src/Encoding}/AlphaEncoding.cpp (100%) rename firmware/lucidgloves-firmware/{ => src/Encoding}/AlphaEncoding.h (100%) rename firmware/lucidgloves-firmware/{ => src/Encoding}/IEncoding.h (91%) rename firmware/lucidgloves-firmware/{ => src/Encoding}/LegacyEncoding.cpp (100%) rename firmware/lucidgloves-firmware/{ => src/Encoding}/LegacyEncoding.h (100%) rename firmware/lucidgloves-firmware/{ => src}/Main.cpp (100%) rename firmware/lucidgloves-firmware/{ => src}/Main.h (63%) rename firmware/lucidgloves-firmware/{ => src/Util}/ConfigUtils.h (100%) rename firmware/lucidgloves-firmware/{ => src/Util}/DataStructs.h (94%) diff --git a/firmware/lucidgloves-firmware/AdvancedConfig.h b/firmware/lucidgloves-firmware/AdvancedConfig.h index 8aae9eb..d088ca1 100644 --- a/firmware/lucidgloves-firmware/AdvancedConfig.h +++ b/firmware/lucidgloves-firmware/AdvancedConfig.h @@ -1,5 +1,5 @@ #pragma once -#include "ConfigUtils.h" +#include "src/Util/ConfigUtils.h" //THIS FILE HAS SOME EXTRA SETTINGS, MAIN SETTINGS ARE IN Config.h #define LOOP_TIME 1 //How much time between data sends (ms), set to 0 for a good time :) diff --git a/firmware/lucidgloves-firmware/Config.h b/firmware/lucidgloves-firmware/Config.h index ff08133..ebfd0f7 100644 --- a/firmware/lucidgloves-firmware/Config.h +++ b/firmware/lucidgloves-firmware/Config.h @@ -1,5 +1,5 @@ #pragma once -#include "ConfigUtils.h" +#include "src/Util/ConfigUtils.h" #include "AdvancedConfig.h" //LUCIDGLOVES CONFIGURATION SETTINGS :) diff --git a/firmware/lucidgloves-firmware/LICENSE b/firmware/lucidgloves-firmware/LICENSE deleted file mode 100644 index aa29a64..0000000 --- a/firmware/lucidgloves-firmware/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) 2021 LucidVR - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/firmware/lucidgloves-firmware/lucidgloves-firmware.ino b/firmware/lucidgloves-firmware/lucidgloves-firmware.ino index b3d03dd..912cf25 100644 --- a/firmware/lucidgloves-firmware/lucidgloves-firmware.ino +++ b/firmware/lucidgloves-firmware/lucidgloves-firmware.ino @@ -5,7 +5,7 @@ */ -#include "Main.h" +#include "src/Main.h" /* * diff --git a/firmware/lucidgloves-firmware/BTSerialCommunication.cpp b/firmware/lucidgloves-firmware/src/Communication/BTSerialCommunication.cpp similarity index 100% rename from firmware/lucidgloves-firmware/BTSerialCommunication.cpp rename to firmware/lucidgloves-firmware/src/Communication/BTSerialCommunication.cpp diff --git a/firmware/lucidgloves-firmware/BTSerialCommunication.h b/firmware/lucidgloves-firmware/src/Communication/BTSerialCommunication.h similarity index 94% rename from firmware/lucidgloves-firmware/BTSerialCommunication.h rename to firmware/lucidgloves-firmware/src/Communication/BTSerialCommunication.h index a55dba1..c463631 100644 --- a/firmware/lucidgloves-firmware/BTSerialCommunication.h +++ b/firmware/lucidgloves-firmware/src/Communication/BTSerialCommunication.h @@ -1,6 +1,6 @@ #pragma once #include "ICommunication.h" -#include "config.h" +#include "../../Config.h" #if COMMUNICATION == COMM_BTSERIAL #include "BluetoothSerial.h" diff --git a/firmware/lucidgloves-firmware/ICommunication.h b/firmware/lucidgloves-firmware/src/Communication/ICommunication.h similarity index 100% rename from firmware/lucidgloves-firmware/ICommunication.h rename to firmware/lucidgloves-firmware/src/Communication/ICommunication.h diff --git a/firmware/lucidgloves-firmware/SerialCommunication.cpp b/firmware/lucidgloves-firmware/src/Communication/SerialCommunication.cpp similarity index 100% rename from firmware/lucidgloves-firmware/SerialCommunication.cpp rename to firmware/lucidgloves-firmware/src/Communication/SerialCommunication.cpp diff --git a/firmware/lucidgloves-firmware/SerialCommunication.h b/firmware/lucidgloves-firmware/src/Communication/SerialCommunication.h similarity index 92% rename from firmware/lucidgloves-firmware/SerialCommunication.h rename to firmware/lucidgloves-firmware/src/Communication/SerialCommunication.h index 5b1aa66..71ceb9f 100644 --- a/firmware/lucidgloves-firmware/SerialCommunication.h +++ b/firmware/lucidgloves-firmware/src/Communication/SerialCommunication.h @@ -1,6 +1,6 @@ #pragma once #include "ICommunication.h" -#include "config.h" +#include "../../Config.h" #include class SerialCommunication : public ICommunication { diff --git a/firmware/lucidgloves-firmware/Gesture.cpp b/firmware/lucidgloves-firmware/src/Controller/Gesture.cpp similarity index 100% rename from firmware/lucidgloves-firmware/Gesture.cpp rename to firmware/lucidgloves-firmware/src/Controller/Gesture.cpp diff --git a/firmware/lucidgloves-firmware/Gesture.h b/firmware/lucidgloves-firmware/src/Controller/Gesture.h similarity index 81% rename from firmware/lucidgloves-firmware/Gesture.h rename to firmware/lucidgloves-firmware/src/Controller/Gesture.h index 6f82f24..bdcc496 100644 --- a/firmware/lucidgloves-firmware/Gesture.h +++ b/firmware/lucidgloves-firmware/src/Controller/Gesture.h @@ -1,5 +1,5 @@ #pragma once -#include "AdvancedConfig.h" +#include "../../AdvancedConfig.h" class Gesture { public: diff --git a/firmware/lucidgloves-firmware/Haptics.cpp b/firmware/lucidgloves-firmware/src/Controller/Haptics.cpp similarity index 100% rename from firmware/lucidgloves-firmware/Haptics.cpp rename to firmware/lucidgloves-firmware/src/Controller/Haptics.cpp diff --git a/firmware/lucidgloves-firmware/Haptics.h b/firmware/lucidgloves-firmware/src/Controller/Haptics.h similarity index 94% rename from firmware/lucidgloves-firmware/Haptics.h rename to firmware/lucidgloves-firmware/src/Controller/Haptics.h index 0077bbb..7de6b06 100644 --- a/firmware/lucidgloves-firmware/Haptics.h +++ b/firmware/lucidgloves-firmware/src/Controller/Haptics.h @@ -1,5 +1,5 @@ #pragma once -#include "config.h" +#include "../../Config.h" #if defined(ESP32) #include "ESP32Servo.h" diff --git a/firmware/lucidgloves-firmware/InputManager.cpp b/firmware/lucidgloves-firmware/src/Controller/InputManager.cpp similarity index 100% rename from firmware/lucidgloves-firmware/InputManager.cpp rename to firmware/lucidgloves-firmware/src/Controller/InputManager.cpp diff --git a/firmware/lucidgloves-firmware/InputManager.h b/firmware/lucidgloves-firmware/src/Controller/InputManager.h similarity index 97% rename from firmware/lucidgloves-firmware/InputManager.h rename to firmware/lucidgloves-firmware/src/Controller/InputManager.h index b7fc807..7ad453c 100644 --- a/firmware/lucidgloves-firmware/InputManager.h +++ b/firmware/lucidgloves-firmware/src/Controller/InputManager.h @@ -1,8 +1,8 @@ #pragma once #include -#include "Config.h" -#include "AdvancedConfig.h" +#include "../../Config.h" +#include "../../AdvancedConfig.h" #if (ENABLE_MEDIAN_FILTER || ((INTERFILTER_MODE != INTERFILTER_NONE) && (FLEXION_MIXING != MIXING_NONE))) diff --git a/firmware/lucidgloves-firmware/AlphaEncoding.cpp b/firmware/lucidgloves-firmware/src/Encoding/AlphaEncoding.cpp similarity index 100% rename from firmware/lucidgloves-firmware/AlphaEncoding.cpp rename to firmware/lucidgloves-firmware/src/Encoding/AlphaEncoding.cpp diff --git a/firmware/lucidgloves-firmware/AlphaEncoding.h b/firmware/lucidgloves-firmware/src/Encoding/AlphaEncoding.h similarity index 100% rename from firmware/lucidgloves-firmware/AlphaEncoding.h rename to firmware/lucidgloves-firmware/src/Encoding/AlphaEncoding.h diff --git a/firmware/lucidgloves-firmware/IEncoding.h b/firmware/lucidgloves-firmware/src/Encoding/IEncoding.h similarity index 91% rename from firmware/lucidgloves-firmware/IEncoding.h rename to firmware/lucidgloves-firmware/src/Encoding/IEncoding.h index 0f31386..da92055 100644 --- a/firmware/lucidgloves-firmware/IEncoding.h +++ b/firmware/lucidgloves-firmware/src/Encoding/IEncoding.h @@ -1,5 +1,5 @@ #pragma once -#include "DataStructs.h" +#include "../Util/DataStructs.h" // Interface for encoding class IEncoding { diff --git a/firmware/lucidgloves-firmware/LegacyEncoding.cpp b/firmware/lucidgloves-firmware/src/Encoding/LegacyEncoding.cpp similarity index 100% rename from firmware/lucidgloves-firmware/LegacyEncoding.cpp rename to firmware/lucidgloves-firmware/src/Encoding/LegacyEncoding.cpp diff --git a/firmware/lucidgloves-firmware/LegacyEncoding.h b/firmware/lucidgloves-firmware/src/Encoding/LegacyEncoding.h similarity index 100% rename from firmware/lucidgloves-firmware/LegacyEncoding.h rename to firmware/lucidgloves-firmware/src/Encoding/LegacyEncoding.h diff --git a/firmware/lucidgloves-firmware/Main.cpp b/firmware/lucidgloves-firmware/src/Main.cpp similarity index 100% rename from firmware/lucidgloves-firmware/Main.cpp rename to firmware/lucidgloves-firmware/src/Main.cpp diff --git a/firmware/lucidgloves-firmware/Main.h b/firmware/lucidgloves-firmware/src/Main.h similarity index 63% rename from firmware/lucidgloves-firmware/Main.h rename to firmware/lucidgloves-firmware/src/Main.h index 555044a..654bf91 100644 --- a/firmware/lucidgloves-firmware/Main.h +++ b/firmware/lucidgloves-firmware/src/Main.h @@ -1,15 +1,15 @@ #pragma once #include -#include "Gesture.h" -#include "Haptics.h" -#include "AlphaEncoding.h" -#include "LegacyEncoding.h" -#include "SerialCommunication.h" -#include "BTSerialCommunication.h" -#include "AdvancedConfig.h" -#include "ConfigUtils.h" -#include "InputManager.h" +#include "Controller/Gesture.h" +#include "Controller/Haptics.h" +#include "Encoding/AlphaEncoding.h" +#include "Encoding/LegacyEncoding.h" +#include "Communication/SerialCommunication.h" +#include "Communication/BTSerialCommunication.h" +#include "../AdvancedConfig.h" +#include "Util/ConfigUtils.h" +#include "Controller/InputManager.h" class Main { diff --git a/firmware/lucidgloves-firmware/ConfigUtils.h b/firmware/lucidgloves-firmware/src/Util/ConfigUtils.h similarity index 100% rename from firmware/lucidgloves-firmware/ConfigUtils.h rename to firmware/lucidgloves-firmware/src/Util/ConfigUtils.h diff --git a/firmware/lucidgloves-firmware/DataStructs.h b/firmware/lucidgloves-firmware/src/Util/DataStructs.h similarity index 94% rename from firmware/lucidgloves-firmware/DataStructs.h rename to firmware/lucidgloves-firmware/src/Util/DataStructs.h index 24041a2..8ca06b5 100644 --- a/firmware/lucidgloves-firmware/DataStructs.h +++ b/firmware/lucidgloves-firmware/src/Util/DataStructs.h @@ -1,5 +1,5 @@ #pragma once -#include "AdvancedConfig.h" +#include "../../AdvancedConfig.h" const char* const SPECIAL_COMMANDS[] = { "SaveInter", From 5985df7415d5c75010a209093107d5f8e998e10a Mon Sep 17 00:00:00 2001 From: Lucas LucidVR <35583218+lucas-vrtech@users.noreply.github.com> Date: Thu, 3 Aug 2023 16:12:40 -0400 Subject: [PATCH 15/19] Clean up includes --- firmware/lucidgloves-firmware/src/Encoding/AlphaEncoding.cpp | 2 +- firmware/lucidgloves-firmware/src/Encoding/LegacyEncoding.cpp | 2 +- firmware/lucidgloves-firmware/src/Main.h | 2 +- firmware/lucidgloves-firmware/src/Util/ConfigUtils.h | 1 - 4 files changed, 3 insertions(+), 4 deletions(-) diff --git a/firmware/lucidgloves-firmware/src/Encoding/AlphaEncoding.cpp b/firmware/lucidgloves-firmware/src/Encoding/AlphaEncoding.cpp index d2b1cd5..2e7a793 100644 --- a/firmware/lucidgloves-firmware/src/Encoding/AlphaEncoding.cpp +++ b/firmware/lucidgloves-firmware/src/Encoding/AlphaEncoding.cpp @@ -1,5 +1,5 @@ #include "AlphaEncoding.h" - +#include char* AlphaEncoding::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; diff --git a/firmware/lucidgloves-firmware/src/Encoding/LegacyEncoding.cpp b/firmware/lucidgloves-firmware/src/Encoding/LegacyEncoding.cpp index 2c41d89..a1b9eb9 100644 --- a/firmware/lucidgloves-firmware/src/Encoding/LegacyEncoding.cpp +++ b/firmware/lucidgloves-firmware/src/Encoding/LegacyEncoding.cpp @@ -1,5 +1,5 @@ #include "LegacyEncoding.h" - +#include char* LegacyEncoding::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]; diff --git a/firmware/lucidgloves-firmware/src/Main.h b/firmware/lucidgloves-firmware/src/Main.h index 654bf91..52361ad 100644 --- a/firmware/lucidgloves-firmware/src/Main.h +++ b/firmware/lucidgloves-firmware/src/Main.h @@ -32,7 +32,7 @@ class Main { bool calibrate = false; bool calibButton = false; - int* fingerPos = (int[]){0,0,0,0,0,0,0,0,0,0}; + int fingerPos[10] = {0,0,0,0,0,0,0,0,0,0}; int loops = 0; #if ESP32_DUAL_CORE_SET diff --git a/firmware/lucidgloves-firmware/src/Util/ConfigUtils.h b/firmware/lucidgloves-firmware/src/Util/ConfigUtils.h index d70b0e6..68e3342 100644 --- a/firmware/lucidgloves-firmware/src/Util/ConfigUtils.h +++ b/firmware/lucidgloves-firmware/src/Util/ConfigUtils.h @@ -5,7 +5,6 @@ #include #include #include -#include class ordered_lock { From 230fc216e2d34b06c1c711ae64ca04f65bad9e2c Mon Sep 17 00:00:00 2001 From: Lucas LucidVR <35583218+lucas-vrtech@users.noreply.github.com> Date: Thu, 3 Aug 2023 16:18:41 -0400 Subject: [PATCH 16/19] Main includes --- firmware/lucidgloves-firmware/src/Main.cpp | 5 +++++ firmware/lucidgloves-firmware/src/Main.h | 6 ++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/firmware/lucidgloves-firmware/src/Main.cpp b/firmware/lucidgloves-firmware/src/Main.cpp index ad41bc9..8c3d5f0 100644 --- a/firmware/lucidgloves-firmware/src/Main.cpp +++ b/firmware/lucidgloves-firmware/src/Main.cpp @@ -1,4 +1,9 @@ #include "Main.h" +#include "Communication/SerialCommunication.h" +#include "Communication/BTSerialCommunication.h" +#include "Encoding/AlphaEncoding.h" +#include "Encoding/LegacyEncoding.h" + #define ALWAYS_CALIBRATING CALIBRATION_LOOPS == -1 #define CALIB_OVERRIDE false diff --git a/firmware/lucidgloves-firmware/src/Main.h b/firmware/lucidgloves-firmware/src/Main.h index 52361ad..d4da4bd 100644 --- a/firmware/lucidgloves-firmware/src/Main.h +++ b/firmware/lucidgloves-firmware/src/Main.h @@ -3,11 +3,9 @@ #include #include "Controller/Gesture.h" #include "Controller/Haptics.h" -#include "Encoding/AlphaEncoding.h" -#include "Encoding/LegacyEncoding.h" -#include "Communication/SerialCommunication.h" -#include "Communication/BTSerialCommunication.h" #include "../AdvancedConfig.h" +#include "Communication/ICommunication.h" +#include "Encoding/IEncoding.h" #include "Util/ConfigUtils.h" #include "Controller/InputManager.h" From d8f00398a3b9bb41e26530e65edb735e2585cf15 Mon Sep 17 00:00:00 2001 From: Lucas LucidVR <35583218+lucas-vrtech@users.noreply.github.com> Date: Thu, 3 Aug 2023 18:49:11 -0400 Subject: [PATCH 17/19] OutboundData struct --- .../src/Encoding/AlphaEncoding.cpp | 25 +++++------ .../src/Encoding/AlphaEncoding.h | 2 +- .../src/Encoding/IEncoding.h | 7 +-- .../src/Encoding/LegacyEncoding.cpp | 8 ++-- .../src/Encoding/LegacyEncoding.h | 2 +- firmware/lucidgloves-firmware/src/Main.cpp | 43 ++++++++++--------- firmware/lucidgloves-firmware/src/Main.h | 3 +- .../src/Util/DataStructs.h | 18 ++++++++ 8 files changed, 63 insertions(+), 45 deletions(-) diff --git a/firmware/lucidgloves-firmware/src/Encoding/AlphaEncoding.cpp b/firmware/lucidgloves-firmware/src/Encoding/AlphaEncoding.cpp index 2e7a793..323b00b 100644 --- a/firmware/lucidgloves-firmware/src/Encoding/AlphaEncoding.cpp +++ b/firmware/lucidgloves-firmware/src/Encoding/AlphaEncoding.cpp @@ -1,25 +1,22 @@ #include "AlphaEncoding.h" #include -char* AlphaEncoding::encode(int* flexion, int joyX, int joyY, bool joyClick, bool triggerButton, bool aButton, bool bButton, bool grab, bool pinch, bool calib, bool menu){ +char* AlphaEncoding::encode(OutboundData data){ static char stringToEncode[75]; - int trigger = (flexion[1] > ANALOG_MAX/2) ? (flexion[1] - ANALOG_MAX/2) * 2:0; + int trigger = (data.fingers[1] > ANALOG_MAX/2) ? (data.fingers[1] - ANALOG_MAX/2) * 2:0; + char splayString[30] = ""; #if USING_SPLAY - sprintf(stringToEncode, "A%dB%dC%dD%dE%dF%dG%dP%d%s%s%s%s%s%s%s%s(AB)%d(BB)%d(CB)%d(DB)%d(EB)%d\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":"", - flexion[5], flexion[6], flexion[7], flexion[8], flexion[9] - ); - #else - 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":"" + sprintf(splayString, "(AB)%d(BB)%d(CB)%d(DB)%d(EB)%d", + data.splay[0], data.splay[1], data.splay[2], data.splay[3], data.splay[4] ); #endif + sprintf(stringToEncode, "A%dB%dC%dD%dE%dF%dG%dP%d%s%s%s%s%s%s%s%s%s%s\n", + data.fingers[0], data.fingers[1], data.fingers[2], data.fingers[3], data.fingers[4], + data.joyX, data.joyY, trigger, data.joyClick?"H":"", + data.triggerButton?"I":"", data.aButton?"J":"", data.bButton?"K":"", data.grab?"L":"", data.pinch?"M":"", data.menu?"N":"", data.calib?"O":"", + splayString + ); return stringToEncode; } - DecodedData AlphaEncoding::decodeData(char* stringToDecode) { DecodedData decodedData = {}; diff --git a/firmware/lucidgloves-firmware/src/Encoding/AlphaEncoding.h b/firmware/lucidgloves-firmware/src/Encoding/AlphaEncoding.h index 4f85a36..c55978d 100644 --- a/firmware/lucidgloves-firmware/src/Encoding/AlphaEncoding.h +++ b/firmware/lucidgloves-firmware/src/Encoding/AlphaEncoding.h @@ -5,7 +5,7 @@ class AlphaEncoding : public IEncoding { public: - char* encode(int* flexion, int joyX, int joyY, bool joyClick, bool triggerButton, bool aButton, bool bButton, bool grab, bool pinch, bool calib, bool menu) override; + char* encode(OutboundData data) override; DecodedData decodeData(char* stringToDecode) override; private: int getArgument(char* stringToDecode, char command); diff --git a/firmware/lucidgloves-firmware/src/Encoding/IEncoding.h b/firmware/lucidgloves-firmware/src/Encoding/IEncoding.h index da92055..31d31fe 100644 --- a/firmware/lucidgloves-firmware/src/Encoding/IEncoding.h +++ b/firmware/lucidgloves-firmware/src/Encoding/IEncoding.h @@ -1,12 +1,13 @@ #pragma once #include "../Util/DataStructs.h" + + + // Interface for encoding class IEncoding { public: - virtual char* encode(int* flexion, int joyX, int joyY, bool joyClick, - bool triggerButton, bool aButton, bool bButton, - bool grab, bool pinch, bool calib, bool menu) = 0; + virtual char* encode(OutboundData data) = 0; virtual DecodedData decodeData(char* stringToDecode) = 0; }; diff --git a/firmware/lucidgloves-firmware/src/Encoding/LegacyEncoding.cpp b/firmware/lucidgloves-firmware/src/Encoding/LegacyEncoding.cpp index a1b9eb9..3b67529 100644 --- a/firmware/lucidgloves-firmware/src/Encoding/LegacyEncoding.cpp +++ b/firmware/lucidgloves-firmware/src/Encoding/LegacyEncoding.cpp @@ -1,12 +1,12 @@ #include "LegacyEncoding.h" #include -char* LegacyEncoding::encode(int* flexion, int joyX, int joyY, bool joyClick, bool triggerButton, bool aButton, bool bButton, bool grab, bool pinch, bool calib, bool menu){ +char* LegacyEncoding::encode(OutboundData data){ 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 + data.fingers[0], data.fingers[1], data.fingers[2], data.fingers[3], data.fingers[4], + data.joyX, data.joyY, data.joyClick, + data.triggerButton, data.aButton, data.bButton, data.grab, data.pinch ); return stringToEncode; } diff --git a/firmware/lucidgloves-firmware/src/Encoding/LegacyEncoding.h b/firmware/lucidgloves-firmware/src/Encoding/LegacyEncoding.h index ae18756..0bc6f61 100644 --- a/firmware/lucidgloves-firmware/src/Encoding/LegacyEncoding.h +++ b/firmware/lucidgloves-firmware/src/Encoding/LegacyEncoding.h @@ -5,6 +5,6 @@ class LegacyEncoding : public IEncoding { public: - char* encode(int* flexion, int joyX, int joyY, bool joyClick, bool triggerButton, bool aButton, bool bButton, bool grab, bool pinch, bool calib, bool menu) override; + char* encode(OutboundData data) override; DecodedData decodeData(char* stringToDecode) override; }; diff --git a/firmware/lucidgloves-firmware/src/Main.cpp b/firmware/lucidgloves-firmware/src/Main.cpp index 8c3d5f0..1c15f0f 100644 --- a/firmware/lucidgloves-firmware/src/Main.cpp +++ b/firmware/lucidgloves-firmware/src/Main.cpp @@ -3,7 +3,7 @@ #include "Communication/BTSerialCommunication.h" #include "Encoding/AlphaEncoding.h" #include "Encoding/LegacyEncoding.h" - +#include "Util/DataStructs.h" #define ALWAYS_CALIBRATING CALIBRATION_LOOPS == -1 #define CALIB_OVERRIDE false @@ -60,17 +60,14 @@ void Main::setup() { void Main::loop() { if (comm->isOpen()){ + #if USING_CALIB_PIN - calibButton = input.getButton(PIN_CALIB) != INVERT_CALIB; - //Serial.println(getButton(PIN_CALIB)); - if (calibButton) + data.calib = input.getButton(PIN_CALIB) != INVERT_CALIB; + if (data.calib) loops = 0; #else - calibButton = false; + data.calib = false; #endif - - - //bool calibrate = false; if (loops < CALIBRATION_LOOPS || ALWAYS_CALIBRATING){ calibrate = true; loops++; @@ -80,34 +77,34 @@ void Main::loop() { } #if !ESP32_DUAL_CORE_SET - input.getFingerPositions(calibrate, calibButton, fingerPos); + input.getFingerPositions(calibrate, data.calib, fingerPos); #endif - bool joyButton = input.getButton(PIN_JOY_BTN) != INVERT_JOY; + data.joyClick = input.getButton(PIN_JOY_BTN) != INVERT_JOY; #if TRIGGER_GESTURE - bool triggerButton = gesture.triggerGesture(fingerPos); + data.triggerButton = gesture.triggerGesture(fingerPos); #else - bool triggerButton = input.getButton(PIN_TRIG_BTN) != INVERT_TRIGGER; + data.triggerButton = input.getButton(PIN_TRIG_BTN) != INVERT_TRIGGER; #endif - bool aButton = input.getButton(PIN_A_BTN) != INVERT_A; - bool bButton = input.getButton(PIN_B_BTN) != INVERT_B; + data.aButton = input.getButton(PIN_A_BTN) != INVERT_A; + data.bButton = input.getButton(PIN_B_BTN) != INVERT_B; #if GRAB_GESTURE - bool grabButton = gesture.grabGesture(fingerPos); + data.grab = gesture.grabGesture(fingerPos); #else - bool grabButton = input.getButton(PIN_GRAB_BTN) != INVERT_GRAB; + data.grab = input.getButton(PIN_GRAB_BTN) != INVERT_GRAB; #endif #if PINCH_GESTURE - bool pinchButton = gesture.pinchGesture(fingerPos); + data.pinch = gesture.pinchGesture(fingerPos); #else - bool pinchButton = input.getButton(PIN_PNCH_BTN) != INVERT_PINCH; + data.pinch = input.getButton(PIN_PNCH_BTN) != INVERT_PINCH; #endif int fingerPosCopy[10]; int mutexTimeDone; - bool menuButton = input.getButton(PIN_MENU_BTN) != INVERT_MENU; + data.menu = input.getButton(PIN_MENU_BTN) != INVERT_MENU; { #if ESP32_DUAL_CORE_SET int mutexTime = micros(); @@ -125,7 +122,11 @@ void Main::loop() { } - comm->output(encoding->encode(fingerPosCopy, input.getJoyX(), input.getJoyY(), joyButton, triggerButton, aButton, bButton, grabButton, pinchButton, calibButton, menuButton)); + memcpy(data.fingers, fingerPosCopy, 5 * sizeof(int)); + data.joyX = input.getJoyX(); + data.joyY = input.getJoyY(); + + comm->output(encoding->encode(data)); #if USING_FORCE_FEEDBACK char received[100]; if (comm->readData(received)){ @@ -156,7 +157,7 @@ void Main::getInputs(){ for(;;){ { fingerPosLock->lock(); - input.getFingerPositions(calibrate, calibButton, fingerPos); //Save finger positions in thread + input.getFingerPositions(calibrate, data.calib, fingerPos); //Save finger positions in thread fingerPosLock->unlock(); } diff --git a/firmware/lucidgloves-firmware/src/Main.h b/firmware/lucidgloves-firmware/src/Main.h index d4da4bd..0fc2796 100644 --- a/firmware/lucidgloves-firmware/src/Main.h +++ b/firmware/lucidgloves-firmware/src/Main.h @@ -28,8 +28,9 @@ class Main { Gesture gesture; InputManager input; + OutboundData data = {}; + bool calibrate = false; - bool calibButton = false; int fingerPos[10] = {0,0,0,0,0,0,0,0,0,0}; int loops = 0; diff --git a/firmware/lucidgloves-firmware/src/Util/DataStructs.h b/firmware/lucidgloves-firmware/src/Util/DataStructs.h index 8ca06b5..473bec2 100644 --- a/firmware/lucidgloves-firmware/src/Util/DataStructs.h +++ b/firmware/lucidgloves-firmware/src/Util/DataStructs.h @@ -21,3 +21,21 @@ struct DecodedData { int servoValues[NUM_FINGERS]; const char* command; }; + +struct OutboundData { + int fingers[NUM_FINGERS]; + int joyX; + int joyY; + bool joyClick; + bool triggerButton; + bool aButton; + bool bButton; + bool grab; + bool pinch; + bool calib; + bool menu; + + #if USING_SPLAY + int splay[NUM_FINGERS]; + #endif +}; \ No newline at end of file From dd41264bfd9e60b734699ab2f88f960f2bce722c Mon Sep 17 00:00:00 2001 From: Lucas LucidVR <35583218+lucas-vrtech@users.noreply.github.com> Date: Thu, 3 Aug 2023 21:51:26 -0400 Subject: [PATCH 18/19] Encode take string arg --- firmware/lucidgloves-firmware/src/Encoding/AlphaEncoding.cpp | 4 +--- firmware/lucidgloves-firmware/src/Encoding/AlphaEncoding.h | 2 +- firmware/lucidgloves-firmware/src/Encoding/IEncoding.h | 2 +- .../lucidgloves-firmware/src/Encoding/LegacyEncoding.cpp | 5 +---- firmware/lucidgloves-firmware/src/Encoding/LegacyEncoding.h | 2 +- firmware/lucidgloves-firmware/src/Main.cpp | 4 +++- 6 files changed, 8 insertions(+), 11 deletions(-) diff --git a/firmware/lucidgloves-firmware/src/Encoding/AlphaEncoding.cpp b/firmware/lucidgloves-firmware/src/Encoding/AlphaEncoding.cpp index 323b00b..0dcc664 100644 --- a/firmware/lucidgloves-firmware/src/Encoding/AlphaEncoding.cpp +++ b/firmware/lucidgloves-firmware/src/Encoding/AlphaEncoding.cpp @@ -1,7 +1,6 @@ #include "AlphaEncoding.h" #include -char* AlphaEncoding::encode(OutboundData data){ - static char stringToEncode[75]; +void AlphaEncoding::encode(OutboundData data, char* stringToEncode){ int trigger = (data.fingers[1] > ANALOG_MAX/2) ? (data.fingers[1] - ANALOG_MAX/2) * 2:0; char splayString[30] = ""; #if USING_SPLAY @@ -15,7 +14,6 @@ char* AlphaEncoding::encode(OutboundData data){ data.triggerButton?"I":"", data.aButton?"J":"", data.bButton?"K":"", data.grab?"L":"", data.pinch?"M":"", data.menu?"N":"", data.calib?"O":"", splayString ); - return stringToEncode; } DecodedData AlphaEncoding::decodeData(char* stringToDecode) { DecodedData decodedData = {}; diff --git a/firmware/lucidgloves-firmware/src/Encoding/AlphaEncoding.h b/firmware/lucidgloves-firmware/src/Encoding/AlphaEncoding.h index c55978d..57ef3e9 100644 --- a/firmware/lucidgloves-firmware/src/Encoding/AlphaEncoding.h +++ b/firmware/lucidgloves-firmware/src/Encoding/AlphaEncoding.h @@ -5,7 +5,7 @@ class AlphaEncoding : public IEncoding { public: - char* encode(OutboundData data) override; + void encode(OutboundData data, char* stringToEncode) override; DecodedData decodeData(char* stringToDecode) override; private: int getArgument(char* stringToDecode, char command); diff --git a/firmware/lucidgloves-firmware/src/Encoding/IEncoding.h b/firmware/lucidgloves-firmware/src/Encoding/IEncoding.h index 31d31fe..059d8f2 100644 --- a/firmware/lucidgloves-firmware/src/Encoding/IEncoding.h +++ b/firmware/lucidgloves-firmware/src/Encoding/IEncoding.h @@ -7,7 +7,7 @@ // Interface for encoding class IEncoding { public: - virtual char* encode(OutboundData data) = 0; + virtual void encode(OutboundData data, char* stringToEncode) = 0; virtual DecodedData decodeData(char* stringToDecode) = 0; }; diff --git a/firmware/lucidgloves-firmware/src/Encoding/LegacyEncoding.cpp b/firmware/lucidgloves-firmware/src/Encoding/LegacyEncoding.cpp index 3b67529..56e5852 100644 --- a/firmware/lucidgloves-firmware/src/Encoding/LegacyEncoding.cpp +++ b/firmware/lucidgloves-firmware/src/Encoding/LegacyEncoding.cpp @@ -1,14 +1,11 @@ #include "LegacyEncoding.h" #include -char* LegacyEncoding::encode(OutboundData data){ - static char stringToEncode[75]; - +void LegacyEncoding::encode(OutboundData data, char* stringToEncode){ sprintf(stringToEncode, "%d&%d&%d&%d&%d&%d&%d&%d&%d&%d&%d&%d&%d\n", data.fingers[0], data.fingers[1], data.fingers[2], data.fingers[3], data.fingers[4], data.joyX, data.joyY, data.joyClick, data.triggerButton, data.aButton, data.bButton, data.grab, data.pinch ); - return stringToEncode; } DecodedData LegacyEncoding::decodeData(char* stringToDecode) { diff --git a/firmware/lucidgloves-firmware/src/Encoding/LegacyEncoding.h b/firmware/lucidgloves-firmware/src/Encoding/LegacyEncoding.h index 0bc6f61..590a5d4 100644 --- a/firmware/lucidgloves-firmware/src/Encoding/LegacyEncoding.h +++ b/firmware/lucidgloves-firmware/src/Encoding/LegacyEncoding.h @@ -5,6 +5,6 @@ class LegacyEncoding : public IEncoding { public: - char* encode(OutboundData data) override; + void encode(OutboundData data, char* stringToEncode) override; DecodedData decodeData(char* stringToDecode) override; }; diff --git a/firmware/lucidgloves-firmware/src/Main.cpp b/firmware/lucidgloves-firmware/src/Main.cpp index 1c15f0f..7e4aacb 100644 --- a/firmware/lucidgloves-firmware/src/Main.cpp +++ b/firmware/lucidgloves-firmware/src/Main.cpp @@ -126,7 +126,9 @@ void Main::loop() { data.joyX = input.getJoyX(); data.joyY = input.getJoyY(); - comm->output(encoding->encode(data)); + static char encodedString[75]; + encoding->encode(data, encodedString); + comm->output(encodedString); #if USING_FORCE_FEEDBACK char received[100]; if (comm->readData(received)){ From 7f752435ae05c904d731aacf8ef5b56b60e41006 Mon Sep 17 00:00:00 2001 From: Lucas LucidVR <35583218+lucas-vrtech@users.noreply.github.com> Date: Sun, 6 Aug 2023 12:37:13 -0400 Subject: [PATCH 19/19] Fix memory error --- .../src/Encoding/AlphaEncoding.cpp | 5 ++--- firmware/lucidgloves-firmware/src/Main.cpp | 12 ++++++++---- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/firmware/lucidgloves-firmware/src/Encoding/AlphaEncoding.cpp b/firmware/lucidgloves-firmware/src/Encoding/AlphaEncoding.cpp index 0dcc664..571e587 100644 --- a/firmware/lucidgloves-firmware/src/Encoding/AlphaEncoding.cpp +++ b/firmware/lucidgloves-firmware/src/Encoding/AlphaEncoding.cpp @@ -8,12 +8,11 @@ void AlphaEncoding::encode(OutboundData data, char* stringToEncode){ data.splay[0], data.splay[1], data.splay[2], data.splay[3], data.splay[4] ); #endif - sprintf(stringToEncode, "A%dB%dC%dD%dE%dF%dG%dP%d%s%s%s%s%s%s%s%s%s%s\n", + snprintf(stringToEncode, 100, "A%dB%dC%dD%dE%dF%dG%dP%d%s%s%s%s%s%s%s%s%s\n", data.fingers[0], data.fingers[1], data.fingers[2], data.fingers[3], data.fingers[4], data.joyX, data.joyY, trigger, data.joyClick?"H":"", data.triggerButton?"I":"", data.aButton?"J":"", data.bButton?"K":"", data.grab?"L":"", data.pinch?"M":"", data.menu?"N":"", data.calib?"O":"", - splayString - ); + splayString); } DecodedData AlphaEncoding::decodeData(char* stringToDecode) { DecodedData decodedData = {}; diff --git a/firmware/lucidgloves-firmware/src/Main.cpp b/firmware/lucidgloves-firmware/src/Main.cpp index 7e4aacb..4e67b1a 100644 --- a/firmware/lucidgloves-firmware/src/Main.cpp +++ b/firmware/lucidgloves-firmware/src/Main.cpp @@ -126,19 +126,19 @@ void Main::loop() { data.joyX = input.getJoyX(); data.joyY = input.getJoyY(); - static char encodedString[75]; + static char encodedString[100] = {0}; encoding->encode(data, encodedString); comm->output(encodedString); #if USING_FORCE_FEEDBACK - char received[100]; + static 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() >= 5) { DecodedData recievedData = encoding->decodeData(received); haptics.writeServoHaptics(recievedData.servoValues); - if (recievedData.fields.specialCommandReceived){ + Serial.println("Special command recieved!!!"); if (recievedData.command == "ClearData") input.clearFlags(); else if (recievedData.command == "SaveInter") @@ -149,7 +149,11 @@ void Main::loop() { } } #endif - delay(LOOP_TIME); + #if defined(ESP32) + vTaskDelay(LOOP_TIME); + #else + delay(LOOP_TIME); + #endif } }