Implement state memory

This commit is contained in:
GHOSCHT 2022-02-05 20:50:33 +01:00
parent baf5547cbd
commit 55a7d2cd0c
No known key found for this signature in database
GPG key ID: A35BD466B8871994
2 changed files with 30 additions and 12 deletions

View file

@ -3,16 +3,28 @@
#include <Arduino.h> #include <Arduino.h>
#include <EEPROM.h> #include <EEPROM.h>
LightController::LightController(int *bjtPins, __SIZE_TYPE__ bjtCount) LightController::LightController(const int bjtPins[], int bjtCount)
{ {
this->bjtCount = bjtCount; this->bjtCount = bjtCount;
this->bjtPins = bjtPins; this->bjtPins = bjtPins;
this->bjtState = new int[bjtCount]; this->bjtState = new int[bjtCount];
for (int i = 0; i < bjtCount; i++)
{
bjtState[i] = 0;
}
for (int i = 0; i < bjtCount; i++)
{
pinMode(bjtPins[i], OUTPUT);
bjtState[i] = EEPROM.read(i);
commitPinState(i);
}
} }
void LightController::updateState(const char data[], __SIZE_TYPE__ dataLength, int steps) void LightController::updateState(const char data[], int steps)
{ {
for (__SIZE_TYPE__ i = 0; i < bjtCount; i++) for (int i = 0; i < bjtCount; i++)
{ {
parseRelativeState(data, i, steps); parseRelativeState(data, i, steps);
setAbsoluteState(data, i); setAbsoluteState(data, i);
@ -40,11 +52,11 @@ void LightController::parseRelativeState(const char data[], int index, int steps
} }
else if (data[1] == 'i') else if (data[1] == 'i')
{ {
bjtState[index] += steps; bjtState[index] = clampState(bjtState[index] + steps);
} }
else if (data[1] == 'd') else if (data[1] == 'd')
{ {
bjtState[index] -= steps; bjtState[index] = clampState(bjtState[index] - steps);
} }
} }
} }
@ -64,7 +76,7 @@ void LightController::setAbsoluteState(const char data[], int index)
int LightController::clampState(int stateValue) int LightController::clampState(int stateValue)
{ {
int clampedState = 0; int clampedState = stateValue;
if (stateValue > 255) if (stateValue > 255)
{ {
clampedState = 255; clampedState = 255;
@ -78,15 +90,20 @@ int LightController::clampState(int stateValue)
void LightController::commitState(int index) void LightController::commitState(int index)
{ {
analogWrite(bjtPins[index], bjtState[index]); commitPinState(index);
EEPROM.update(index, bjtState[index]); EEPROM.update(index, bjtState[index]);
} }
void LightController::commitPinState(int index)
{
analogWrite(bjtPins[index], bjtState[index]);
}
int LightController::getBjtCount() int LightController::getBjtCount()
{ {
return bjtCount; return bjtCount;
} }
int *LightController::getBjtPins() const int *LightController::getBjtPins()
{ {
return bjtPins; return bjtPins;
} }

View file

@ -2,7 +2,7 @@ class LightController
{ {
protected: protected:
int bjtCount; int bjtCount;
int *bjtPins; const int *bjtPins;
int *bjtState; int *bjtState;
private: private:
@ -10,11 +10,12 @@ private:
void parseRelativeState(const char data[], int index, int steps); void parseRelativeState(const char data[], int index, int steps);
int clampState(int stateValue); int clampState(int stateValue);
void commitState(int index); void commitState(int index);
void commitPinState(int index);
public: public:
LightController(int *bjtPins, __SIZE_TYPE__ bjtCount); LightController(const int bjtPins[], int bjtCount);
void updateState(const char data[], __SIZE_TYPE__ dataLength, int steps); void updateState(const char data[], int steps);
int getBjtCount(); int getBjtCount();
int *getBjtPins(); const int *getBjtPins();
int *getBjtState(); int *getBjtState();
}; };