Skip to content

Commit 9d846bb

Browse files
committed
Clean up fade
1 parent 3bd6c63 commit 9d846bb

File tree

8 files changed

+70
-72
lines changed

8 files changed

+70
-72
lines changed

src/Config.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ bool ConfigClass::initialize()
3131
return parseJson(json);
3232
}
3333

34-
bool ConfigClass::parseJson(char *input)
34+
bool ConfigClass::parseJson(const char *input)
3535
{
3636
DynamicJsonDocument doc(2048);
3737
DeserializationError error = deserializeJson(doc, input);

src/Config.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@ class ConfigClass
1919
// Alarm functionality enabled
2020
bool alarmEnabled = false;
2121
/// Alarm duration in minutes
22-
uint16_t alarmDuration = 0;
22+
uint16_t alarmDuration = 1;
2323
/// Alarm time hour
2424
uint8_t alarmHour = 0;
2525
/// Alarm time minute
2626
uint8_t alarmMinute = 0;
2727
/// Animation during alarm duration (while the brightness increases)
28-
String alarmAnimation = "";
28+
String alarmAnimation = "Color";
2929
/// Animation triggered after the alarm duration has ended
30-
String postAlarmAnimation = "";
30+
String postAlarmAnimation = "Color";
3131
/// Sunset functionality enabled
3232
bool sunsetEnabled = false;
3333
/// Sunset duration in minutes
34-
uint16_t sunsetDuration = 0;
34+
uint16_t sunsetDuration = 1;
3535
/// Sunset time hour
3636
int8_t sunsetHour = 0;
3737
/// Sunset time minute
@@ -40,11 +40,11 @@ class ConfigClass
4040
/// automatically obtained local sunset time
4141
int16_t sunsetOffset = 0;
4242
/// Sunset animation
43-
String sunsetAnimation = "";
43+
String sunsetAnimation = "Color";
4444
/// Startup animation that gets triggered when powering up the device
4545
String startupAnimation = "";
4646
/// Lastly used color for Color animation
47-
String color = "";
47+
String color = "ffffff";
4848
/// Slider values
4949
LinkedList<int16_t> sliderValues;
5050

@@ -61,7 +61,7 @@ class ConfigClass
6161
/// config state.
6262
/// @param input JSON char array
6363
/// @return True if successfull
64-
bool parseJson(char *input);
64+
bool parseJson(const char *input);
6565

6666
/// Get config state contained in a DynamicJsonDocument
6767
/// @return DynamicJsonDocument containing config

src/Fade.cpp

Lines changed: 35 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@
1313
namespace Fade
1414
{
1515

16-
FadeMode currentFade = FadeMode::NONE;
16+
FadeMode mode = FadeMode::NONE;
1717
uint16_t fadeBrightness = 0;
18-
uint16_t sunsetMaximumBrightness = 0;
18+
uint16_t targetBrightness = 0;
1919
Ticker fadeTicker;
20-
Ticker hasBeenStartedResetTicker;
21-
bool hasBeenStarted = false;
20+
Ticker debounce;
2221

2322
void initialize()
2423
{
@@ -28,59 +27,31 @@ void initialize()
2827

2928
void handle()
3029
{
31-
// Return if fade is running
32-
if (currentFade != FadeMode::NONE || hasBeenStarted)
30+
if (mode != FadeMode::NONE || debounce.active())
3331
return;
3432

35-
// Get current time
36-
time_t n = time(nullptr);
37-
if (!n)
33+
int8_t hour, minute;
34+
if (!getTime(&hour, &minute))
3835
return;
39-
struct tm *now = gmtime(&n);
40-
int8_t hour = (now->tm_hour + Config.timeZone + Config.summerTime) % 24;
4136

42-
// Check for alarm
43-
if (Config.alarmEnabled && hour == Config.alarmHour && now->tm_min == Config.alarmMinute)
37+
if (Config.alarmEnabled && hour == Config.alarmHour && minute == Config.alarmMinute)
4438
{
4539
Fade::begin(Fade::FadeMode::ALARM);
4640
}
47-
// Check for sunset
48-
else if (Config.sunsetEnabled && hour == Config.sunsetHour && now->tm_min == Config.sunsetMinute)
41+
else if (Config.sunsetEnabled && hour == Config.sunsetHour && minute == Config.sunsetMinute && FastLEDHub.isDim())
4942
{
50-
// Only start sunset if all leds are off
51-
if (FastLEDHub.brightness10 == 0)
52-
{
53-
Fade::begin(Fade::FadeMode::SUNSET);
54-
}
55-
else // brightness10 > 0
56-
{
57-
bool ledsIlluminated = false;
58-
for (uint16_t i = 0; i < FastLEDHub.numLeds; i++)
59-
{
60-
if (FastLEDHub.hardwareLeds[i] != CRGB(0, 0, 0))
61-
{
62-
ledsIlluminated = true;
63-
break;
64-
}
65-
}
66-
67-
if (!ledsIlluminated)
68-
Fade::begin(Fade::FadeMode::SUNSET);
69-
}
43+
Fade::begin(Fade::FadeMode::SUNSET);
7044
}
7145
}
7246

7347
void begin(FadeMode fadeMode)
7448
{
75-
// Set fade starting point
76-
fadeBrightness = 1;
49+
mode = fadeMode;
50+
targetBrightness = FastLEDHub.brightness10;
51+
fadeBrightness = 0;
7752
FastLEDHub.show(fadeBrightness);
7853

79-
currentFade = fadeMode;
80-
81-
// Prevent starting fade multiple times
82-
hasBeenStarted = true;
83-
hasBeenStartedResetTicker.attach(90, [&]() { hasBeenStarted = false; }); // TODO: Should only be called once?
54+
debounce.once(61, [&](){ debounce.detach(); });
8455

8556
if (fadeMode == Fade::FadeMode::ALARM)
8657
{
@@ -91,42 +62,38 @@ void begin(FadeMode fadeMode)
9162
else if (fadeMode == Fade::FadeMode::SUNSET)
9263
{
9364
FastLEDHub.begin(FastLEDHub.getAnimation(Config.sunsetAnimation));
94-
sunsetMaximumBrightness = FastLEDHub.brightness10;
95-
fadeTicker.attach_ms(Config.sunsetDuration * 60 * 1000 / sunsetMaximumBrightness, tick);
65+
fadeTicker.attach_ms(Config.sunsetDuration * 60 * 1000 / targetBrightness, tick);
9666
PRINTLN("[FastLEDHub] Start fade 'Sunset'");
9767
}
9868
}
9969

10070
void stop()
10171
{
10272
fadeTicker.detach();
103-
currentFade = FadeMode::NONE;
73+
mode = FadeMode::NONE;
10474
}
10575

10676
void tick()
10777
{
10878
if (FastLEDHub.status == PAUSED)
10979
return;
11080

111-
if (currentFade == Fade::FadeMode::ALARM && fadeBrightness == 1023)
81+
if (mode == Fade::FadeMode::ALARM && fadeBrightness == 1023)
11282
{
11383
if (Config.postAlarmAnimation != Config.alarmAnimation)
11484
FastLEDHub.begin(FastLEDHub.getAnimation(Config.postAlarmAnimation));
115-
fadeTicker.detach();
85+
86+
stop();
11687
PRINTLN("[FastLEDHub] End fade 'Alarm'");
11788
}
118-
else if (currentFade == Fade::FadeMode::SUNSET && fadeBrightness == sunsetMaximumBrightness)
89+
else if (mode == Fade::FadeMode::SUNSET && fadeBrightness == targetBrightness)
11990
{
120-
fadeTicker.detach();
91+
stop();
12192
PRINTLN("[FastLEDHub] End fade 'Sunset'");
12293
}
12394
else
12495
{
125-
if (fadeBrightness < 1023)
126-
fadeBrightness++;
127-
else
128-
fadeTicker.detach();
129-
96+
fadeBrightness++;
13097
PRINTLN("[FastLEDHub] Fade brightness: " + String(fadeBrightness));
13198
}
13299

@@ -166,4 +133,18 @@ void getSunsetTime()
166133
}
167134
}
168135

136+
bool getTime(int8_t *hour, int8_t *minute)
137+
{
138+
time_t n = time(nullptr);
139+
140+
if (!n)
141+
return false;
142+
143+
tm *now = gmtime(&n);
144+
*hour = (now->tm_hour + Config.timeZone + Config.summerTime) % 24;
145+
*minute = now->tm_min;
146+
147+
return true;
148+
}
149+
169150
} // namespace Fade

src/Fade.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ enum class FadeMode
1717
SUNSET
1818
};
1919

20-
extern FadeMode currentFade;
20+
extern FadeMode mode;
2121

2222
void handle();
2323
void begin(FadeMode fadeMode);
2424
void stop();
2525
void tick();
2626
void initialize();
2727
void getSunsetTime();
28+
bool getTime(int8_t *hour, int8_t *minute);
2829

2930
} // namespace Fade

src/FastLEDHub.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ void FastLEDHubClass::initialize(String projectName, uint16_t numberOfLeds)
3333
numLeds = numberOfLeds;
3434
leds = new CRGB[numLeds];
3535
hardwareLeds = new CRGB[numLeds];
36-
brightness10 = 1023;
3736
Config.initialize();
3837
if (WiFi.status() == WL_CONNECTED)
3938
{
@@ -144,6 +143,22 @@ void FastLEDHubClass::clear(bool writeData)
144143
show();
145144
}
146145

146+
bool FastLEDHubClass::isDim()
147+
{
148+
if (brightness10 == 0)
149+
return true;
150+
151+
for (uint16_t i = 0; i < numLeds; i++)
152+
{
153+
if (hardwareLeds[i] != CRGB(0, 0, 0))
154+
{
155+
return false;
156+
}
157+
}
158+
159+
return true;
160+
}
161+
147162
void FastLEDHubClass::delay(uint16_t ms)
148163
{
149164
unsigned long start = micros();
@@ -210,7 +225,7 @@ Slider* FastLEDHubClass::getSlider(uint8_t i)
210225

211226
void FastLEDHubClass::handleInput()
212227
{
213-
if (potentiometerPin >= 0 && Fade::currentFade == Fade::FadeMode::NONE)
228+
if (potentiometerPin >= 0 && Fade::mode == Fade::FadeMode::NONE)
214229
{
215230
// Adjust the range slightly so low and high adc values
216231
// span the whole 10bit brightness range

src/FastLEDHub.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ class FastLEDHubClass : public CFastLED
9393
/// @param scale Brightness scale to set LEDs to (0 - 255)
9494
void showColor(const struct CRGB &color, uint8_t scale);
9595

96+
/// Check if all leds are turned off (i.e. either brightness is zero or
97+
/// all leds are black)
98+
bool isDim();
99+
96100
/// Clear all pixels. This function should be used in combination with
97101
/// FastLEDHub rather than FastLED.clear().
98102
/// @param writeData Wether to write out the data

src/WebSocket.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ void handle(uint8_t id, WStype_t type, uint8_t *payload, size_t length)
4343
break;
4444
case WStype_TEXT:
4545
PRINTF_VERBOSE("[%u] Got text: %s\n", id, payload);
46-
handleText(byteArray2string(payload), id);
46+
handleText(byteArray2String(payload), id);
4747
break;
4848
case WStype_BIN:
4949
PRINTF_VERBOSE("[%u] Got binary: %s\n", id, payload);
@@ -56,10 +56,7 @@ void handle(uint8_t id, WStype_t type, uint8_t *payload, size_t length)
5656

5757
void handleText(String text, uint8_t id)
5858
{
59-
char textArray[text.length()];
60-
text.toCharArray(textArray, text.length());
61-
62-
if (Config.parseJson(textArray))
59+
if (Config.parseJson(text.c_str()))
6360
Config.save();
6461
}
6562

@@ -122,7 +119,7 @@ void handleBinary(uint8_t *binary, uint8_t id)
122119
}
123120
}
124121

125-
String byteArray2string(uint8_t *bytes)
122+
String byteArray2String(uint8_t *bytes)
126123
{
127124
String s = "";
128125
for (uint16_t i = 0; bytes[i] != '\0'; i++)

src/WebSocket.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ void broadcastStatus();
3535
/// Convert a byte array to String
3636
/// @param bytes Byte array
3737
/// @return Converted String
38-
String byteArray2string(uint8_t *bytes);
38+
String byteArray2String(uint8_t *bytes);
3939

4040
} // namespace Websocket

0 commit comments

Comments
 (0)