Seperate light control logic into own class

This commit is contained in:
GHOSCHT 2022-02-05 20:09:48 +01:00
parent 620cdd25ac
commit baf5547cbd
No known key found for this signature in database
GPG key ID: A35BD466B8871994
2 changed files with 116 additions and 0 deletions

View file

@ -0,0 +1,96 @@
#include <LightController.h>
#include <string.h>
#include <Arduino.h>
#include <EEPROM.h>
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;
}

View file

@ -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();
};