diff --git a/.clang-format b/.clang-format index 241ca76..2b97310 100644 --- a/.clang-format +++ b/.clang-format @@ -16,4 +16,11 @@ AlwaysBreakAfterReturnType: None PenaltyReturnTypeOnItsOwnLine: 1000000 BreakConstructorInitializers: BeforeComma -ConstructorInitializerIndentWidth: 0 \ No newline at end of file +ConstructorInitializerIndentWidth: 0 +BraceWrapping: + AfterEnum: true + AfterStruct: false + SplitEmptyFunction: false + AfterCaseLabel: false + AfterControlStatement: true + AfterFunction: true \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index 58bfbfb..e4400d5 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,5 +4,4 @@ "*.tcc": "cpp" }, "editor.formatOnSave": true, - "clang-format.executable": "/usr/bin/clang-format-9" } \ No newline at end of file diff --git a/include/myled.h b/include/myled.h index f6397ec..3fb4285 100644 --- a/include/myled.h +++ b/include/myled.h @@ -1,7 +1,6 @@ #include -class MyLed -{ +class MyLed { public: MyLed(int ledPin, int ledCount); @@ -20,8 +19,7 @@ public: void adjustBrightness(float diff); private: - - //! Called by public setBrightness. This function + //! Called by public setBrightness. This function //! will not store the brightness void _setBrightness(float brightness); @@ -34,5 +32,4 @@ private: const std::array _defColor; const std::array _color; - }; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 3f4c52e..80b7217 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,139 +1,140 @@ -#include #include "myled.h" +#include // Sharp Sensor -const int sensorPin = A0; +const int sensorPin = A0; // Neopixel const int ledPin = D8; -const int ledCount = 4; +const int ledCount = 4; MyLed myLed(ledPin, ledCount); typedef struct { - float distance; // Distance in mm - float distanceNormalized; - bool validReading; // In within sensor range + float distance; // Distance in mm + float distanceNormalized; + bool validReading; // In within sensor range } SensorData; -void setup() { - Serial.begin(9600); +void setup() +{ + Serial.begin(9600); - myLed.initialize(); + myLed.initialize(); } float filter(float input) { - static float pool = 0; - const float factor = 0.5; - //! @todo: Make use of time in filter - //unsigned long t = millis(); + static float pool = 0; + const float factor = 0.5; + //! @todo: Make use of time in filter + // unsigned long t = millis(); - pool = pool * factor + input * (1.0 - factor); - return pool; + pool = pool * factor + input * (1.0 - factor); + return pool; } - SensorData getDist() { - // Based on the sharp 2Y0A21 - // https://global.sharp/products/device/lineup/data/pdf/datasheet/gp2y0a21yk_e.pdf - const int maxVal = 780; // 60 mm - const int minVal = 173; // 800 mm - const int maxValidDist = 610; - const int minValidDist = 70; + // Based on the sharp 2Y0A21 + // https://global.sharp/products/device/lineup/data/pdf/datasheet/gp2y0a21yk_e.pdf + const int maxVal = 780; // 60 mm + const int minVal = 173; // 800 mm + const int maxValidDist = 610; + const int minValidDist = 70; - long dist = map(analogRead(sensorPin), maxVal, minVal, 60, 800); - dist = filter(dist); - bool validReading = (dist > minValidDist && dist < maxValidDist); + long dist = map(analogRead(sensorPin), maxVal, minVal, 60, 800); + dist = filter(dist); + bool validReading = (dist > minValidDist && dist < maxValidDist); - float normalizedDistance = - (static_cast(dist) - static_cast(minValidDist)) / - static_cast(maxValidDist-minValidDist); - //Serial.printf("Norm dist: %f\n", normalizedDistance); - - return {.distance = static_cast(dist), - .distanceNormalized = normalizedDistance, - .validReading = validReading}; + float normalizedDistance = + (static_cast(dist) - static_cast(minValidDist)) / + static_cast(maxValidDist - minValidDist); + // Serial.printf("Norm dist: %f\n", normalizedDistance); + + return {.distance = static_cast(dist), + .distanceNormalized = normalizedDistance, + .validReading = validReading}; } void evalDist(SensorData data) { - /************************************************ - * Toggle: Place hand still above for > time x * - * Dim: Place hand above and move up/down * - ************************************************/ - unsigned long t = millis(); + /************************************************ + * Toggle: Place hand still above for > time x * + * Dim: Place hand above and move up/down * + ************************************************/ + unsigned long t = millis(); - static float lastDist = data.distance; + static float lastDist = data.distance; - const float stillThreshold = 20.0f; // Distance in mm - const float releaseThreshold = 70.0f; // If value increases more than this it is considered a release + const float stillThreshold = 20.0f; // Distance in mm + const float releaseThreshold = + 70.0f; // If value increases more than this it is considered a release - float distDelta = data.distance - lastDist; - float distDeltaAbs = fabs(distDelta); - - auto evalToggle = [&]() -> void - { - static uint8_t stillCounter = {}; // If get similar value a few times we are "holding" the hand above - const uint8_t stillCounterThreshold = 10; // # samples to consider a command - - static unsigned long lastToggleTime = t; - const unsigned long toggleTimeThresthold = 3000; // No toggle action is allowed directly after another + float distDelta = data.distance - lastDist; + float distDeltaAbs = fabs(distDelta); - if ( data.validReading - && t - lastToggleTime > toggleTimeThresthold - && distDeltaAbs < stillThreshold) - { - if (++stillCounter > stillCounterThreshold) - { - lastToggleTime = t; - myLed.toggle(); - stillCounter = 0; - } - } - else - { - stillCounter = 0; - } - }; + auto evalToggle = [&]() -> void { + static uint8_t stillCounter = {}; // If get similar value a few times we + // are "holding" the hand above + const uint8_t stillCounterThreshold = + 10; // # samples to consider a command - auto evalDim = [&]() -> void - { - const unsigned long minTime = 200; // Don't register dimming before this - static unsigned long timeOfHandPresent = t; + static unsigned long lastToggleTime = t; + const unsigned long toggleTimeThresthold = + 3000; // No toggle action is allowed directly after another - if (myLed.getOnState() && data.validReading) - { - if (t - timeOfHandPresent > minTime) - { - const float dimFactor = 0.0025; + if (data.validReading && t - lastToggleTime > toggleTimeThresthold && + distDeltaAbs < stillThreshold) + { + if (++stillCounter > stillCounterThreshold) + { + lastToggleTime = t; + myLed.toggle(); + stillCounter = 0; + } + } else + { + stillCounter = 0; + } + }; - if (distDeltaAbs < releaseThreshold && distDeltaAbs > stillThreshold * 2) - myLed.adjustBrightness((data.distance - lastDist) * dimFactor); - } - } - else - { - timeOfHandPresent = t; - } - }; + auto evalDim = [&]() -> void { + const unsigned long minTime = 200; // Don't register dimming before this + static unsigned long timeOfHandPresent = t; - evalToggle(); - evalDim(); + if (myLed.getOnState() && data.validReading) + { + if (t - timeOfHandPresent > minTime) + { + const float dimFactor = 0.0025; - lastDist = data.distance; + if (distDeltaAbs < releaseThreshold && + distDeltaAbs > stillThreshold * 2) + myLed.adjustBrightness((data.distance - lastDist) * + dimFactor); + } + } else + { + timeOfHandPresent = t; + } + }; + + evalToggle(); + evalDim(); + + lastDist = data.distance; } -void loop() { - - SensorData data = getDist(); +void loop() +{ - evalDist(data); + SensorData data = getDist(); - //if (data.validReading) - // Serial.printf("Value: %f\n", data.distance); + evalDist(data); - delay(50); + // if (data.validReading) + // Serial.printf("Value: %f\n", data.distance); + delay(50); } \ No newline at end of file diff --git a/src/myled.cpp b/src/myled.cpp index 02bfcc0..c53c012 100644 --- a/src/myled.cpp +++ b/src/myled.cpp @@ -1,7 +1,7 @@ #include "myled.h" namespace { - const float minBrightness = 0.2; +const float minBrightness = 0.2; } MyLed::MyLed(int ledPin, int ledCount) @@ -10,14 +10,13 @@ MyLed::MyLed(int ledPin, int ledCount) , _isOn(true) , _defColor({255.0f, 147.0f, 41.0f}) , _color(_defColor) -{ - -} +{} void MyLed::initialize() { _strip.begin(); - _strip.fill(_strip.Color(_defColor.at(0), _defColor.at(1), _defColor.at(2))); + _strip.fill( + _strip.Color(_defColor.at(0), _defColor.at(1), _defColor.at(2))); _strip.setBrightness(255); // The max brightness. Set once! _strip.show(); } @@ -56,9 +55,8 @@ void MyLed::adjustBrightness(float diff) void MyLed::_setBrightness(float brightness) { _strip.fill( - _strip.Color( - static_cast(_defColor.at(0) * brightness), - static_cast(_defColor.at(1) * brightness), - static_cast(_defColor.at(2) * brightness))); + _strip.Color(static_cast(_defColor.at(0) * brightness), + static_cast(_defColor.at(1) * brightness), + static_cast(_defColor.at(2) * brightness))); _strip.show(); } \ No newline at end of file