1 #ifndef LOGICENGINECONTROLLER32_H
2 #define LOGICENGINECONTROLLER32_H
5 #ifndef LENGINE_DELAY_PIN
6 #define LENGINE_BUTTON_PIN 36
8 #ifndef LENGINE_TRIMPOT_PIN
9 #define LENGINE_TRIMPOT_PIN 39
11 #ifndef LENGINE_STATUS_PIN
12 #define LENGINE_STATUS_PIN 14
18 #include <Preferences.h>
23 static constexpr
unsigned int LogicEngineController_kButtonVals[4]={0,850,1900,2900};
26 #define PREFERENCE_LOGICS "logics"
37 template <
byte kButtonPin = LENGINE_BUTTON_PIN,
byte kTrimpotPin = LENGINE_TRIMPOT_PIN,
byte kLEDStatusPin = LENGINE_STATUS_PIN>
41 template <u
int8_t DATA_PIN = FRONT_LOGIC_PIN>
42 class StatusLED :
public FastLEDPCB<WS2812B, kLEDStatusPin>
50 Preferences& preferences) :
51 fPreferences(preferences),
60 fWifiResetFunction = resetFunction;
65 fWifiToggleFunction = toggleFunction;
71 pinMode(kButtonPin, INPUT);
72 pinMode(kTrimpotPin, INPUT);
73 fPrevButtonValue = analogRead(kButtonPin);
91 fFactorySettings[0] = fSettings[0] = FLD->
getSettings();
95 fFactorySettings[1] = fSettings[1] = RLD->
getSettings();
99 if (fPreferences.getBytes(
PREFERENCE_LOGICS, fSettings,
sizeof(fSettings)) ==
sizeof(fSettings))
115 logicNum = (logicNum & 1);
116 return fSettings[logicNum];
123 logicNum = (logicNum & 1);
126 *settings = fFactorySettings[logicNum];
127 if (logic !=
nullptr)
139 logicNum = (logicNum & 1);
147 settings->
fBri = val;
150 settings->
fHue = val;
153 settings->
fFade = val;
164 if (logic !=
nullptr)
174 return (fPreferences.putBytes(
PREFERENCE_LOGICS, fSettings,
sizeof(fSettings)) ==
sizeof(fSettings));
186 if ((fAdjMode == 1 || fAdjMode == 2) && fLogic[fAdjMode-1] !=
nullptr && fTrimPot.
hasChanged())
188 uint8_t val = 255 - (uint8_t)(0xFF * (fTrimPot.
getValue() / 4096.0f));
194 static constexpr
unsigned kButtonVariance = 300;
195 static constexpr uint32_t kStatusLED_LowDelay = 300;
196 static constexpr uint32_t kStatusLED_NormalDelay = 1000;
198 static constexpr
unsigned numberOfButtons()
200 return sizeof(LogicEngineController_kButtonVals)/
sizeof(LogicEngineController_kButtonVals[0]);
203 Preferences& fPreferences;
204 StatusLED<> fStatusLED;
206 void (*fWifiResetFunction)() =
nullptr;
207 void (*fWifiToggleFunction)() =
nullptr;
211 unsigned fPrevButtonValue;
212 bool fButtonState[numberOfButtons()];
213 bool fPrevButtonState[numberOfButtons()];
214 uint32_t fButtonTime[numberOfButtons()+1];
218 bool fChanges[
sizeof(fLogic)/
sizeof(fLogic[0])] = {};
220 byte fStatusColor = 0;
221 byte fPrevStatusColor = 0;
222 uint32_t fPrevFlipFlopMillis = 0;
223 uint32_t fStatusFlipFlopTime = kStatusLED_NormalDelay;
225 static bool inRange(
int val,
int minimum,
int maximum)
227 return ((minimum <= val) && (val <= maximum));
230 inline void fastStatusMode()
232 fStatusFlipFlopTime = kStatusLED_LowDelay;
235 inline void normalStatusMode()
237 fStatusFlipFlopTime = kStatusLED_NormalDelay;
242 static constexpr uint8_t kStatusColors[5][4][3] = {
243 { { 2, 0, 0} , { 0, 0, 2} , { 2, 0, 0} , { 0, 0, 2} } ,
244 { { 25, 25, 25} , { 16, 16, 16} , { 10, 10, 10} , { 2, 2, 2} } ,
245 { { 2, 0, 0} , { 2, 2, 0} , { 0, 2, 0} , { 0, 0, 2} } ,
246 { { 2, 0, 2} , { 2, 0, 1} , { 2, 0, 0} , { 2, 0, 1} } ,
247 { { 0, 2, 0} , { 0, 2, 0} , { 0, 2, 0} , { 0, 2, 0} }
249 uint32_t timeNow = millis();
250 if (timeNow - fPrevFlipFlopMillis > fStatusFlipFlopTime)
252 fPrevFlipFlopMillis = timeNow;
254 if (fStatusColor == 4) fStatusColor = 0;
255 fStatusLED.fLED[0].r = kStatusColors[fTrimpotMode][fStatusColor][0];
256 fStatusLED.fLED[0].g = kStatusColors[fTrimpotMode][fStatusColor][1];
257 fStatusLED.fLED[0].b = kStatusColors[fTrimpotMode][fStatusColor][2];
268 uint32_t timeNow = millis();
269 unsigned buttonValue = analogRead(kButtonPin);
270 unsigned buttonReleased = numberOfButtons()+1;
271 if (!inRange(buttonValue, fPrevButtonValue - kButtonVariance, fPrevButtonValue + kButtonVariance))
273 for (
byte i = 0; i < numberOfButtons(); i++)
275 if (inRange(buttonValue, LogicEngineController_kButtonVals[i] - kButtonVariance, LogicEngineController_kButtonVals[i] + kButtonVariance))
277 if (!fButtonState[i])
279 fButtonState[i] =
true;
280 fButtonTime[i] = timeNow;
285 fButtonState[i] =
false;
286 if (fPrevButtonState[i])
291 if (timeNow - fButtonTime[i] > 30)
297 fPrevButtonState[i] = fButtonState[i];
300 fPrevButtonValue = buttonValue;
302 if (buttonReleased < numberOfButtons()+1)
305 if (buttonReleased == 0)
307 if (timeNow - fButtonTime[buttonReleased] > 5000 && fWifiResetFunction !=
nullptr)
308 fWifiResetFunction();
309 else if (timeNow - fButtonTime[buttonReleased] > 2000 && fWifiToggleFunction !=
nullptr)
310 fWifiToggleFunction();
312 else if (fLogic[0] !=
nullptr && fAdjMode == 0 && buttonReleased == 1)
314 if (timeNow - fButtonTime[1] > 5000)
319 else if (timeNow - fButtonTime[1] > 1000)
327 else if (fLogic[1] !=
nullptr && fAdjMode == 0 && buttonReleased == 2)
329 if (timeNow - fButtonTime[2] > 5000)
334 else if (timeNow - fButtonTime[2] > 1000)
342 else if ((fAdjMode==1 || fAdjMode==2) && (buttonReleased == 1 || buttonReleased == 2))
344 if (timeNow - fButtonTime[buttonReleased] > 1000)
352 else if (timeNow - fButtonTime[1] > 30)
355 if (fTrimpotMode > 4) fTrimpotMode = 1;
364 if ((fAdjMode == 1 || fAdjMode == 2) && buttonReleased == 3 && timeNow - fButtonTime[4] > 30)
367 if (fLogic[fAdjMode-1] !=
nullptr)
369 changeSetting(fAdjMode-1, 5, fLogic[fAdjMode-1]->getCurrentPalette()+1);