From 6d8a12c455389eaaba79817d3d1997920d7c8a38 Mon Sep 17 00:00:00 2001 From: veroxzik Date: Fri, 19 Apr 2019 00:29:37 -0400 Subject: [PATCH] Fixes (again) It didn't build but now it does Fixed the USB def for switching between 32U4/328P Fixes indents on all files --- Firmware/AirSensor.h | 52 ++++++++--------- Firmware/CapacitiveSensor.cpp | 56 +++++++++--------- Firmware/CapacitiveSensor.h | 104 +++++++++++++++++----------------- Firmware/OpeNITHM.ino | 11 ++-- Firmware/Output.h | 6 +- Firmware/PinConfig.h | 18 +++--- Firmware/SerialOutput.cpp | 6 +- Firmware/SerialOutput.h | 14 ++--- Firmware/Touchboard.h | 44 +++++++------- Firmware/USBOutput.cpp | 25 ++++---- Firmware/USBOutput.h | 16 +++--- 11 files changed, 177 insertions(+), 175 deletions(-) diff --git a/Firmware/AirSensor.h b/Firmware/AirSensor.h index 91b5783..555f93b 100644 --- a/Firmware/AirSensor.h +++ b/Firmware/AirSensor.h @@ -4,9 +4,9 @@ #define _AIRSENSOR_h #if defined(ARDUINO) && ARDUINO >= 100 - #include "arduino.h" +#include "arduino.h" #else - #include "WProgram.h" +#include "WProgram.h" #endif #include "PinConfig.h" @@ -16,34 +16,34 @@ class AirSensor { -private: - void changeLight(int light); - void turnOffLight(); + private: + void changeLight(int light); + void turnOffLight(); - int thresholds[6]; - int calibrationSamples[6]; - int skippedSamples[6]; - float sensorValues[6]; - int samplesToAcquire; - int samplesToSkip; + int thresholds[6]; + int calibrationSamples[6]; + int skippedSamples[6]; + float sensorValues[6]; + int samplesToAcquire; + int samplesToSkip; - bool calibrated[6]; - bool allCalibrated; + bool calibrated[6]; + bool allCalibrated; - uint16_t deadzone; - float alpha; -public: - AirSensor(int requiredSamples, int skippedSamples); - bool isCalibrated(); - bool getSensorState(int sensor); - int getValue(int sensor, bool light); - float getHandPosition(); + uint16_t deadzone; + float alpha; + public: + AirSensor(int requiredSamples, int skippedSamples); + bool isCalibrated(); + bool getSensorState(int sensor); + int getValue(int sensor, bool light); + float getHandPosition(); - void setDeadzone(int deadzone); - void setAlpha(float alpha); - int getDeadzone(); - float getAlpha(); - void recalibrate(); + void setDeadzone(int deadzone); + void setAlpha(float alpha); + int getDeadzone(); + float getAlpha(); + void recalibrate(); }; diff --git a/Firmware/CapacitiveSensor.cpp b/Firmware/CapacitiveSensor.cpp index 8f6c35d..050b340 100644 --- a/Firmware/CapacitiveSensor.cpp +++ b/Firmware/CapacitiveSensor.cpp @@ -1,30 +1,30 @@ /* - CapacitiveSense.h - Capacitive Sensing Library for 'duino / Wiring - https://github.com/PaulStoffregen/CapacitiveSensor - http://www.pjrc.com/teensy/td_libs_CapacitiveSensor.html - http://playground.arduino.cc/Main/CapacitiveSensor - Copyright (c) 2009 Paul Bagder - Updates for other hardare by Paul Stoffregen, 2010-2016 - Several modifications to better suit OpeNITHM by Jonathan Montineri, 2019 - vim: set ts=4: + CapacitiveSense.h - Capacitive Sensing Library for 'duino / Wiring + https://github.com/PaulStoffregen/CapacitiveSensor + http://www.pjrc.com/teensy/td_libs_CapacitiveSensor.html + http://playground.arduino.cc/Main/CapacitiveSensor + Copyright (c) 2009 Paul Bagder + Updates for other hardare by Paul Stoffregen, 2010-2016 + Several modifications to better suit OpeNITHM by Jonathan Montineri, 2019 + vim: set ts=4: - 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: + 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 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. + 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. */ #if ARDUINO >= 100 @@ -63,7 +63,7 @@ CapacitiveSensor::CapacitiveSensor(uint8_t sendPin, uint8_t receivePin1, uint8_t // Get pin bitmask and registers - sBit = PIN_TO_BITMASK(sendPin); + sBit = PIN_TO_BITMASK(sendPin); sReg = PIN_TO_BASEREG(sendPin); r1Bit = PIN_TO_BITMASK(receivePin1); @@ -124,7 +124,7 @@ int CapacitiveSensor::SenseOneCycle(void) total2 += !pin2State; // Break once both pins are high - if(pin1State && pin2State) break; + if (pin1State && pin2State) break; } if (total1 > CS_Timeout_Millis) { @@ -142,18 +142,18 @@ int CapacitiveSensor::SenseOneCycle(void) DIRECT_WRITE_LOW(sReg, sBit); // sendPin LOW // Same loop as above, but measuring capacitor discharge time instead - while ( (total1 < CS_Timeout_Millis) ) { + while ( (total1 < CS_Timeout_Millis) ) { pin1State = DIRECT_READ(r1Reg, r1Bit); pin2State = DIRECT_READ(r2Reg, r2Bit); total1 += pin1State; total2 += pin2State; - if(!(pin1State || pin2State)) break; + if (!(pin1State || pin2State)) break; } if (total1 >= CS_Timeout_Millis) { - return -2; + return -2; } else { return 1; } diff --git a/Firmware/CapacitiveSensor.h b/Firmware/CapacitiveSensor.h index e34ecae..03455ae 100644 --- a/Firmware/CapacitiveSensor.h +++ b/Firmware/CapacitiveSensor.h @@ -132,55 +132,55 @@ static inline __attribute__((always_inline)) IO_REG_TYPE directRead(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) { - IO_REG_TYPE ret; - if (SS_GPIO == GPIO_TYPE(pin)) { - ret = READ_ARC_REG(((IO_REG_TYPE)base + EXT_PORT_OFFSET_SS)); - } else { - ret = MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, EXT_PORT_OFFSET_SOC); - } - return ((ret >> GPIO_ID(pin)) & 0x01); + IO_REG_TYPE ret; + if (SS_GPIO == GPIO_TYPE(pin)) { + ret = READ_ARC_REG(((IO_REG_TYPE)base + EXT_PORT_OFFSET_SS)); + } else { + ret = MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, EXT_PORT_OFFSET_SOC); + } + return ((ret >> GPIO_ID(pin)) & 0x01); } static inline __attribute__((always_inline)) void directModeInput(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) { - if (SS_GPIO == GPIO_TYPE(pin)) { - WRITE_ARC_REG(READ_ARC_REG((((IO_REG_TYPE)base) + DIR_OFFSET_SS)) & ~(0x01 << GPIO_ID(pin)), - ((IO_REG_TYPE)(base) + DIR_OFFSET_SS)); - } else { - MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, DIR_OFFSET_SOC) &= ~(0x01 << GPIO_ID(pin)); - } + if (SS_GPIO == GPIO_TYPE(pin)) { + WRITE_ARC_REG(READ_ARC_REG((((IO_REG_TYPE)base) + DIR_OFFSET_SS)) & ~(0x01 << GPIO_ID(pin)), + ((IO_REG_TYPE)(base) + DIR_OFFSET_SS)); + } else { + MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, DIR_OFFSET_SOC) &= ~(0x01 << GPIO_ID(pin)); + } } static inline __attribute__((always_inline)) void directModeOutput(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) { - if (SS_GPIO == GPIO_TYPE(pin)) { - WRITE_ARC_REG(READ_ARC_REG(((IO_REG_TYPE)(base) + DIR_OFFSET_SS)) | (0x01 << GPIO_ID(pin)), - ((IO_REG_TYPE)(base) + DIR_OFFSET_SS)); - } else { - MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, DIR_OFFSET_SOC) |= (0x01 << GPIO_ID(pin)); - } + if (SS_GPIO == GPIO_TYPE(pin)) { + WRITE_ARC_REG(READ_ARC_REG(((IO_REG_TYPE)(base) + DIR_OFFSET_SS)) | (0x01 << GPIO_ID(pin)), + ((IO_REG_TYPE)(base) + DIR_OFFSET_SS)); + } else { + MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, DIR_OFFSET_SOC) |= (0x01 << GPIO_ID(pin)); + } } static inline __attribute__((always_inline)) void directWriteLow(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) { - if (SS_GPIO == GPIO_TYPE(pin)) { - WRITE_ARC_REG(READ_ARC_REG(base) & ~(0x01 << GPIO_ID(pin)), base); - } else { - MMIO_REG_VAL(base) &= ~(0x01 << GPIO_ID(pin)); - } + if (SS_GPIO == GPIO_TYPE(pin)) { + WRITE_ARC_REG(READ_ARC_REG(base) & ~(0x01 << GPIO_ID(pin)), base); + } else { + MMIO_REG_VAL(base) &= ~(0x01 << GPIO_ID(pin)); + } } static inline __attribute__((always_inline)) void directWriteHigh(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) { - if (SS_GPIO == GPIO_TYPE(pin)) { - WRITE_ARC_REG(READ_ARC_REG(base) | (0x01 << GPIO_ID(pin)), base); - } else { - MMIO_REG_VAL(base) |= (0x01 << GPIO_ID(pin)); - } + if (SS_GPIO == GPIO_TYPE(pin)) { + WRITE_ARC_REG(READ_ARC_REG(base) | (0x01 << GPIO_ID(pin)), base); + } else { + MMIO_REG_VAL(base) |= (0x01 << GPIO_ID(pin)); + } } #define DIRECT_READ(base, pin) directRead(base, pin) @@ -201,32 +201,32 @@ void directWriteHigh(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) // library interface description class CapacitiveSensor { - // user-accessible "public" interface + // user-accessible "public" interface public: - // methods - CapacitiveSensor(uint8_t sendPin, uint8_t receivePin1, uint8_t receivePin2); - unsigned int* sense(uint8_t samples); - // library-accessible "private" interface + // methods + CapacitiveSensor(uint8_t sendPin, uint8_t receivePin1, uint8_t receivePin2); + unsigned int* sense(uint8_t samples); + // library-accessible "private" interface private: - // variables - int error; - unsigned long leastTotal; - unsigned int loopTimingFactor; - unsigned long CS_Timeout_Millis; - unsigned int total1; - unsigned int total2; - unsigned int *retVal; - bool pin1State; - bool pin2State; + // variables + int error; + unsigned long leastTotal; + unsigned int loopTimingFactor; + unsigned long CS_Timeout_Millis; + unsigned int total1; + unsigned int total2; + unsigned int *retVal; + bool pin1State; + bool pin2State; - IO_REG_TYPE sBit; // send pin's ports and bitmask - volatile IO_REG_TYPE *sReg; - IO_REG_TYPE r1Bit; // receive 1 pin's ports and bitmask - volatile IO_REG_TYPE *r1Reg; - IO_REG_TYPE r2Bit; // receive 2 pin's ports and bitmask - volatile IO_REG_TYPE *r2Reg; - // methods - int SenseOneCycle(void); + IO_REG_TYPE sBit; // send pin's ports and bitmask + volatile IO_REG_TYPE *sReg; + IO_REG_TYPE r1Bit; // receive 1 pin's ports and bitmask + volatile IO_REG_TYPE *r1Reg; + IO_REG_TYPE r2Bit; // receive 2 pin's ports and bitmask + volatile IO_REG_TYPE *r2Reg; + // methods + int SenseOneCycle(void); }; #endif diff --git a/Firmware/OpeNITHM.ino b/Firmware/OpeNITHM.ino index 5cb57fc..50a23ca 100644 --- a/Firmware/OpeNITHM.ino +++ b/Firmware/OpeNITHM.ino @@ -1,6 +1,4 @@ -#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega32u4__) -#define USB -#endif +#include "PinConfig.h" #ifdef USB #include "USBOutput.h" @@ -11,7 +9,6 @@ #include "AirSensor.h" #include "Touchboard.h" #include -#include "PinConfig.h" CRGB leds[16]; @@ -47,7 +44,7 @@ void parseCommand() { case 't': // touchboard while (!Serial.available()); - char input2 = Serial.read(); + input2 = Serial.read(); switch (input2) { case 't': // threshold @@ -65,7 +62,7 @@ void parseCommand() break; case 'i': // ir sensors while (!Serial.available()); - char input2 = Serial.read(); + input2 = Serial.read(); switch (input2) { case 'd': // dead zone @@ -115,7 +112,7 @@ void parseCommand() break; case 'l': // change led color while (!Serial.available()); - char input2 = Serial.read(); + input2 = Serial.read(); switch (input2) { case 'o': // on diff --git a/Firmware/Output.h b/Firmware/Output.h index 1d05145..a96e145 100644 --- a/Firmware/Output.h +++ b/Firmware/Output.h @@ -5,9 +5,9 @@ class Output { - public: - virtual void sendKeyEvent(int key, bool pressed, bool doublePressed); - virtual void sendSensorEvent(float position); + public: + virtual void sendKeyEvent(int key, bool pressed, bool doublePressed); + virtual void sendSensorEvent(float position); }; #endif diff --git a/Firmware/PinConfig.h b/Firmware/PinConfig.h index 6e73000..1dc5737 100644 --- a/Firmware/PinConfig.h +++ b/Firmware/PinConfig.h @@ -1,6 +1,10 @@ #ifndef _PINCONFIG_h #define _PINCONFIG_h +#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega32u4__) +#define USB +#endif + //#define IR_SENSOR_MULTIPLEXED //#define IR_SENSOR_ANALOG // Uncomment if IR sensors are hooked up to analog pins #define CALIBRATION_SAMPLES 500 @@ -16,14 +20,14 @@ #define LED_2 3 #ifdef IR_SENSOR_MULTIPLEXED - #define SENSOR_IN A0 +#define SENSOR_IN A0 #else - #define AIR_SENSOR_0_PIN 4 - #define AIR_SENSOR_1_PIN 5 - #define AIR_SENSOR_2_PIN 6 - #define AIR_SENSOR_3_PIN 7 - #define AIR_SENSOR_4_PIN 8 - #define AIR_SENSOR_5_PIN 9 +#define AIR_SENSOR_0_PIN 4 +#define AIR_SENSOR_1_PIN 5 +#define AIR_SENSOR_2_PIN 6 +#define AIR_SENSOR_3_PIN 7 +#define AIR_SENSOR_4_PIN 8 +#define AIR_SENSOR_5_PIN 9 #endif // Capsense pin settings diff --git a/Firmware/SerialOutput.cpp b/Firmware/SerialOutput.cpp index 0752f2f..3bbbffc 100644 --- a/Firmware/SerialOutput.cpp +++ b/Firmware/SerialOutput.cpp @@ -1,6 +1,6 @@ -// -// -// +// +// +// #include "SerialOutput.h" diff --git a/Firmware/SerialOutput.h b/Firmware/SerialOutput.h index 84193bb..b152aba 100644 --- a/Firmware/SerialOutput.h +++ b/Firmware/SerialOutput.h @@ -4,9 +4,9 @@ #define _SERIALOUTPUT_h #if defined(ARDUINO) && ARDUINO >= 100 - #include "arduino.h" +#include "arduino.h" #else - #include "WProgram.h" +#include "WProgram.h" #endif #include "Output.h" @@ -31,11 +31,11 @@ union Packet class SerialOutput : public Output { -private: - Packet builtPacket = Packet(); -public: - void sendKeyEvent(int key, bool pressed, bool doublePressed) override; - void sendSensorEvent(float position) override; + private: + Packet builtPacket = Packet(); + public: + void sendKeyEvent(int key, bool pressed, bool doublePressed) override; + void sendSensorEvent(float position) override; }; #endif diff --git a/Firmware/Touchboard.h b/Firmware/Touchboard.h index 83143e7..e1e8c67 100644 --- a/Firmware/Touchboard.h +++ b/Firmware/Touchboard.h @@ -10,32 +10,32 @@ class Touchboard { -private: - CapacitiveSensor sensor; + private: + CapacitiveSensor sensor; - uint16_t threshold; - uint16_t deadzone; - float alpha; + uint16_t threshold; + uint16_t deadzone; + float alpha; - float em_averages[16]; - unsigned int keys[16]; - int neutral_values[16]; - void (*onKeyPress)(int, bool); + float em_averages[16]; + unsigned int keys[16]; + int neutral_values[16]; + void (*onKeyPress)(int, bool); -public: - boolean key_states[16]; + public: + boolean key_states[16]; - Touchboard(void(*keyPressCallback)(int, bool)); - void scan(); - bool update(int key); - void setThreshold(uint16_t threshold); - void setDeadzone(uint16_t deadzone); - void setAlpha(float alpha); - int getThreshold(); - int getDeadzone(); - float getAlpha(); - float getEmAverages(int key); - void calibrateKeys(); + Touchboard(void(*keyPressCallback)(int, bool)); + void scan(); + bool update(int key); + void setThreshold(uint16_t threshold); + void setDeadzone(uint16_t deadzone); + void setAlpha(float alpha); + int getThreshold(); + int getDeadzone(); + float getAlpha(); + float getEmAverages(int key); + void calibrateKeys(); }; #endif diff --git a/Firmware/USBOutput.cpp b/Firmware/USBOutput.cpp index 1469f90..cccb52f 100644 --- a/Firmware/USBOutput.cpp +++ b/Firmware/USBOutput.cpp @@ -1,5 +1,6 @@ -#ifdef USB +#include "PinConfig.h" +#ifdef USB #include "USBOutput.h" char bottomRow[] = {'a', 'z', 's', 'x', 'd', 'c', 'f', 'v', 'g', 'b', 'h', 'n', 'j', 'm', 'k', ','}; @@ -7,15 +8,15 @@ char topRow[] = {'1', 'q', '2', 'w', '3', 'e', '4', 'r', '5', 't', '6', 'y', '7' void USBOutput::sendKeyEvent(int key, bool pressed, bool doublePressed) { - if(pressed){ - if(doublePressed){ + if (pressed) { + if (doublePressed) { NKROKeyboard.write(topRow[key]); } - else{ + else { NKROKeyboard.press(bottomRow[key]); } } - else{ + else { NKROKeyboard.release(bottomRow[key]); } } @@ -23,29 +24,29 @@ void USBOutput::sendKeyEvent(int key, bool pressed, bool doublePressed) void USBOutput::sendSensorEvent(float position) { // Send hand up / hand down key - if(position > lastPosition){ + if (position > lastPosition) { NKROKeyboard.write(KEY_PAGE_UP); } - if(position < lastPosition){ + if (position < lastPosition) { NKROKeyboard.write(KEY_PAGE_DOWN); } // Send hand seen / unseen key - if(position > 0.05f){ + if (position > 0.05f) { NKROKeyboard.press(KEY_HOME); } - else{ + else { NKROKeyboard.release(KEY_HOME); } // Send hand moved key NKROKeyboard.write(KEY_END); - + } -USBOutput::USBOutput(){ +USBOutput::USBOutput() { NKROKeyboard.begin(); lastPosition = 0; } -#endif \ No newline at end of file +#endif diff --git a/Firmware/USBOutput.h b/Firmware/USBOutput.h index 3094419..582f7ef 100644 --- a/Firmware/USBOutput.h +++ b/Firmware/USBOutput.h @@ -6,21 +6,21 @@ #include "Output.h" #if defined(ARDUINO) && ARDUINO >= 100 - #include "arduino.h" +#include "arduino.h" #else - #include "WProgram.h" +#include "WProgram.h" #endif #include class USBOutput : public Output { -private: - float lastPosition; -public: - void sendKeyEvent(int key, bool pressed, bool doublePressed) override; - void sendSensorEvent(float position) override; - USBOutput(); + private: + float lastPosition; + public: + void sendKeyEvent(int key, bool pressed, bool doublePressed) override; + void sendSensorEvent(float position) override; + USBOutput(); }; #endif