From baf5547cbd8fcec7e0ada4750b48a0fc17f6c885 Mon Sep 17 00:00:00 2001 From: GHOSCHT <31184695+GHOSCHT@users.noreply.github.com> Date: Sat, 5 Feb 2022 20:09:48 +0100 Subject: [PATCH] Seperate light control logic into own class --- .../lib/LightController/LightController.cpp | 96 +++++++++++++++++++ .../lib/LightController/LightController.h | 20 ++++ 2 files changed, 116 insertions(+) create mode 100644 Devices/Control/Firmware/lib/LightController/LightController.cpp create mode 100644 Devices/Control/Firmware/lib/LightController/LightController.h diff --git a/Devices/Control/Firmware/lib/LightController/LightController.cpp b/Devices/Control/Firmware/lib/LightController/LightController.cpp new file mode 100644 index 0000000..e40c78f --- /dev/null +++ b/Devices/Control/Firmware/lib/LightController/LightController.cpp @@ -0,0 +1,96 @@ +#include +#include +#include +#include + +LightController::LightController(int *bjtPins, __SIZE_TYPE__ bjtCount) +{ + this->bjtCount = bjtCount; + this->bjtPins = bjtPins; + this->bjtState = new int[bjtCount]; +} + +void LightController::updateState(const char data[], __SIZE_TYPE__ dataLength, int steps) +{ + for (__SIZE_TYPE__ i = 0; i < bjtCount; i++) + { + parseRelativeState(data, i, steps); + setAbsoluteState(data, i); + commitState(i); + } +} + +void LightController::parseRelativeState(const char data[], int index, int steps) +{ + char numChar[2]; + itoa(index, numChar, 10); + + if (data[0] == numChar[0]) + { + if (data[1] == 't') + { + if (bjtState[index] != 0) + { + bjtState[index] = 0; + } + else + { + bjtState[index] = 255; + } + } + else if (data[1] == 'i') + { + bjtState[index] += steps; + } + else if (data[1] == 'd') + { + bjtState[index] -= steps; + } + } +} + +void LightController::setAbsoluteState(const char data[], int index) +{ + if (!strcmp(data, "off")) + { + bjtState[index] = 0; + } + + if (!strcmp(data, "on")) + { + bjtState[index] = 255; + } +} + +int LightController::clampState(int stateValue) +{ + int clampedState = 0; + if (stateValue > 255) + { + clampedState = 255; + } + else if (stateValue < 0) + { + clampedState = 0; + } + return clampedState; +} + +void LightController::commitState(int index) +{ + analogWrite(bjtPins[index], bjtState[index]); + EEPROM.update(index, bjtState[index]); +} + +int LightController::getBjtCount() +{ + return bjtCount; +} +int *LightController::getBjtPins() +{ + return bjtPins; +} +int *LightController::getBjtState() +{ + return bjtState; +} diff --git a/Devices/Control/Firmware/lib/LightController/LightController.h b/Devices/Control/Firmware/lib/LightController/LightController.h new file mode 100644 index 0000000..0604985 --- /dev/null +++ b/Devices/Control/Firmware/lib/LightController/LightController.h @@ -0,0 +1,20 @@ +class LightController +{ +protected: + int bjtCount; + int *bjtPins; + int *bjtState; + +private: + void setAbsoluteState(const char data[], int index); + void parseRelativeState(const char data[], int index, int steps); + int clampState(int stateValue); + void commitState(int index); + +public: + LightController(int *bjtPins, __SIZE_TYPE__ bjtCount); + void updateState(const char data[], __SIZE_TYPE__ dataLength, int steps); + int getBjtCount(); + int *getBjtPins(); + int *getBjtState(); +};