Skip to content

Commit

Permalink
Merge pull request #287 from laszloh/feature/led-brightness
Browse files Browse the repository at this point in the history
Modify CMD_DIMM_LEDS_NIGHTMODE to toggle night light mode
  • Loading branch information
biologist79 committed Jan 14, 2024
2 parents ec7e56b + 724ead4 commit 39ad764
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 90 deletions.
2 changes: 1 addition & 1 deletion src/AudioPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1037,7 +1037,7 @@ void AudioPlayer_TrackQueueDispatcher(const char *_itemToPlay, const uint32_t _l
gPlayProperties.sleepAfterCurrentTrack = true;
gPlayProperties.playUntilTrackNumber = 0;
gPlayProperties.numberOfTracks = 1; // Limit number to 1 even there are more entries in the playlist
Led_ResetToNightBrightness();
Led_SetNightmode(true);
Log_Println(modeSingleTrackRandom, LOGLEVEL_NOTICE);
AudioPlayer_RandomizePlaylist(musicFiles, gPlayProperties.numberOfTracks);
xQueueSend(gTrackQueue, &(musicFiles), 0);
Expand Down
99 changes: 28 additions & 71 deletions src/Cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@
#include "System.h"
#include "Wlan.h"

static void Cmd_HandleSleepAction(bool enable, const char *enLogMsg, const char *enMqttMsg) {
Led_SetNightmode(enable);
if (enable) {
Log_Println(enLogMsg, LOGLEVEL_INFO);
#ifdef MQTT_ENABLE
publishMqtt(topicSleepTimerState, enMqttMsg, false);
#endif
} else {
System_DisableSleepTimer();
Log_Println(modificatorSleepd, LOGLEVEL_INFO);
#ifdef MQTT_ENABLE
publishMqtt(topicSleepTimerState, "0", false);
#endif
}
}

void Cmd_Action(const uint16_t mod) {
switch (mod) {
case CMD_LOCK_BUTTONS_MOD: { // Locks/unlocks all buttons
Expand Down Expand Up @@ -81,27 +97,9 @@ void Cmd_Action(const uint16_t mod) {

gPlayProperties.sleepAfterPlaylist = false;
gPlayProperties.playUntilTrackNumber = 0;
gPlayProperties.sleepAfterCurrentTrack = !gPlayProperties.sleepAfterCurrentTrack;

if (gPlayProperties.sleepAfterCurrentTrack) {
gPlayProperties.sleepAfterCurrentTrack = false;
Log_Println(modificatorSleepAtEOTd, LOGLEVEL_NOTICE);
#ifdef MQTT_ENABLE
publishMqtt(topicSleepTimerState, "0", false);
#endif
Led_ResetToInitialBrightness();
} else {
System_DisableSleepTimer();
gPlayProperties.sleepAfterCurrentTrack = true;
Log_Println(modificatorSleepAtEOT, LOGLEVEL_NOTICE);
#ifdef MQTT_ENABLE
publishMqtt(topicSleepTimerState, "EOT", false);
#endif
Led_ResetToNightBrightness();
}

#ifdef MQTT_ENABLE
publishMqtt(topicLedBrightnessState, Led_GetBrightness(), false);
#endif
Cmd_HandleSleepAction(gPlayProperties.sleepAfterCurrentTrack, modificatorSleepAtEOT, "EOT");
System_IndicateOk();
break;
}
Expand All @@ -112,28 +110,11 @@ void Cmd_Action(const uint16_t mod) {
System_IndicateError();
return;
}
if (gPlayProperties.sleepAfterPlaylist) {
System_DisableSleepTimer();
gPlayProperties.sleepAfterPlaylist = false;
#ifdef MQTT_ENABLE
publishMqtt(topicSleepTimerState, "0", false);
#endif
Led_ResetToInitialBrightness();
Log_Println(modificatorSleepAtEOPd, LOGLEVEL_NOTICE);
} else {
gPlayProperties.sleepAfterPlaylist = true;
Led_ResetToNightBrightness();
Log_Println(modificatorSleepAtEOP, LOGLEVEL_NOTICE);
#ifdef MQTT_ENABLE
publishMqtt(topicSleepTimerState, "EOP", false);
#endif
}

gPlayProperties.sleepAfterCurrentTrack = false;
gPlayProperties.playUntilTrackNumber = 0;
#ifdef MQTT_ENABLE
publishMqtt(topicLedBrightnessState, Led_GetBrightness(), false);
#endif
gPlayProperties.sleepAfterPlaylist = !gPlayProperties.sleepAfterPlaylist;

Cmd_HandleSleepAction(gPlayProperties.sleepAfterCurrentTrack, modificatorSleepAtEOP, "EOP");
System_IndicateOk();
break;
}
Expand All @@ -147,36 +128,16 @@ void Cmd_Action(const uint16_t mod) {

gPlayProperties.sleepAfterCurrentTrack = false;
gPlayProperties.sleepAfterPlaylist = false;
System_DisableSleepTimer();
gPlayProperties.sleepAfter5Tracks = !gPlayProperties.sleepAfter5Tracks;

if (gPlayProperties.sleepAfter5Tracks) {
gPlayProperties.sleepAfter5Tracks = false;
gPlayProperties.playUntilTrackNumber = 0;
#ifdef MQTT_ENABLE
publishMqtt(topicSleepTimerState, "0", false);
#endif
Led_ResetToInitialBrightness();
Log_Println(modificatorSleepd, LOGLEVEL_NOTICE);
} else {
gPlayProperties.sleepAfter5Tracks = true;
if (gPlayProperties.currentTrackNumber + 5 > gPlayProperties.numberOfTracks) { // If currentTrack + 5 exceeds number of tracks in playlist, sleep after end of playlist
gPlayProperties.sleepAfterPlaylist = true;
#ifdef MQTT_ENABLE
publishMqtt(topicSleepTimerState, "EOP", false);
#endif
} else {
gPlayProperties.playUntilTrackNumber = gPlayProperties.currentTrackNumber + 5;
#ifdef MQTT_ENABLE
publishMqtt(topicSleepTimerState, "EO5T", false);
#endif
if (gPlayProperties.currentTrackNumber + 5 > gPlayProperties.numberOfTracks) {
// execute a sleep after end of playlist
Cmd_Action(CMD_SLEEP_AFTER_END_OF_PLAYLIST);
break;
}
Led_ResetToNightBrightness();
Log_Println(sleepTimerEO5, LOGLEVEL_NOTICE);
}

#ifdef MQTT_ENABLE
publishMqtt(topicLedBrightnessState, Led_GetBrightness(), false);
#endif
Cmd_HandleSleepAction(gPlayProperties.sleepAfter5Tracks, sleepTimerEO5, "EO5T");
System_IndicateOk();
break;
}
Expand Down Expand Up @@ -220,11 +181,7 @@ void Cmd_Action(const uint16_t mod) {
}

case CMD_DIMM_LEDS_NIGHTMODE: {
#ifdef MQTT_ENABLE
publishMqtt(topicLedBrightnessState, Led_GetBrightness(), false);
#endif
Log_Println(ledsDimmedToNightmode, LOGLEVEL_INFO);
Led_ResetToNightBrightness();
Led_ToggleNightmode();
System_IndicateOk();
break;
}
Expand Down
35 changes: 35 additions & 0 deletions src/Led.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "Bluetooth.h"
#include "Button.h"
#include "Log.h"
#include "Mqtt.h"
#include "Port.h"
#include "System.h"
#include "Wlan.h"
Expand Down Expand Up @@ -45,6 +46,9 @@ static bool Led_Pause = false; // Used to pause Neopixel-signalisation (while NV
static uint8_t Led_InitialBrightness = LED_INITIAL_BRIGHTNESS;
static uint8_t Led_Brightness = LED_INITIAL_BRIGHTNESS;
static uint8_t Led_NightBrightness = LED_INITIAL_NIGHT_BRIGHTNESS;
static bool Led_NightMode = false;
static uint8_t Led_savedBrightness;

constexpr uint8_t Led_IdleDotDistance = NUM_INDICATOR_LEDS / NUM_LEDS_IDLE_DOTS;

static CRGBArray<NUM_INDICATOR_LEDS + NUM_CONTROL_LEDS> leds;
Expand Down Expand Up @@ -170,9 +174,40 @@ void Led_SetBrightness(uint8_t value) {
#ifdef BUTTONS_LED
Port_Write(BUTTONS_LED, value <= Led_NightBrightness ? LOW : HIGH, false);
#endif

#ifdef MQTT_ENABLE
publishMqtt(topicLedBrightnessState, Led_Brightness, false);
#endif
#endif
}

void Led_SetNightmode(bool enabled) {
if (Led_NightMode == enabled) {
// we don't need to do anything
return;
}

const char *msg = ledsBrightnessRestored;
uint8_t newValue = Led_savedBrightness;
if (enabled) {
// we are switching to night mode
Led_savedBrightness = Led_Brightness;
msg = ledsDimmedToNightmode;
newValue = Led_NightBrightness;
}
Led_NightMode = enabled;
Led_SetBrightness(newValue);
Log_Println(msg, LOGLEVEL_INFO);
}

bool Led_GetNightmode() {
return Led_NightMode;
}

void Led_ToggleNightmode() {
Led_SetNightmode(!Led_NightMode);
}

// Calculates physical address for a virtual LED address. This handles reversing the rotation direction of the ring and shifting the starting LED
#ifdef NEOPIXEL_ENABLE
uint8_t Led_Address(uint8_t number) {
Expand Down
4 changes: 4 additions & 0 deletions src/Led.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,7 @@ uint8_t Led_GetBrightness(void);
void Led_SetBrightness(uint8_t value);
void Led_TaskPause(void);
void Led_TaskResume(void);

void Led_SetNightmode(bool enabled);
bool Led_GetNightmode();
void Led_ToggleNightmode();
3 changes: 1 addition & 2 deletions src/LogMessages_DE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,10 @@ const char modificatorSleepTimer60[] = "Modifikator: Sleep-Timer per RFID aktivi
const char modificatorSleepTimer120[] = "Modifikator: Sleep-Timer per RFID aktiviert (2 Stunden).";
const char ledsDimmedToNightmode[] = "LEDs wurden auf Nachtmodus gedimmt.";
const char ledsDimmedToInitialValue[] = "LEDs wurden auf initiale Helligkeit gedimmt.";
const char ledsBrightnessRestored[] = "LED Helligkeit wieder hergestellt.";
const char modificatorNotallowedWhenIdle[] = "Modifikator kann bei nicht aktivierter Playlist nicht angewendet werden.";
const char modificatorSleepAtEOT[] = "Modifikator: Sleep-Timer am Ende des Titels aktiviert.";
const char modificatorSleepAtEOTd[] = "Modifikator: Sleep-Timer am Ende des Titels deaktiviert.";
const char modificatorSleepAtEOP[] = "Modifikator: Sleep-Timer am Ende der Playlist aktiviert.";
const char modificatorSleepAtEOPd[] = "Modifikator: Sleep-Timer am Ende der Playlist deaktiviert.";
const char modificatorAllTrackAlphSortedLoop[] = "Modifikator: Alle Titel (alphabetisch sortiert) in Endlosschleife.";
const char modificatorAllTrackRandomLoop[] = "Modifikator: Alle Titel (zufällige Reihenfolge) in Endlosschleife.";
const char modificatorCurTrackLoop[] = "Modifikator: Aktueller Titel in Endlosschleife.";
Expand Down
3 changes: 1 addition & 2 deletions src/LogMessages_EN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,10 @@ const char modificatorSleepTimer60[] = "Modificator: sleep-Timer enabled via RFI
const char modificatorSleepTimer120[] = "Modificator: sleep-Timer enabled via RFID (2 hours).";
const char ledsDimmedToNightmode[] = "Dimmed LEDs to nightmode.";
const char ledsDimmedToInitialValue[] = "Dimmed LEDs to initial value.";
const char ledsBrightnessRestored[] = "LED brightness restored.";
const char modificatorNotallowedWhenIdle[] = "Modificator cannot be applied while playlist is inactive.";
const char modificatorSleepAtEOT[] = "Modificator: adjusted sleep-timer to after end of current track.";
const char modificatorSleepAtEOTd[] = "Modificator: disabled sleep-timer after end of current track.";
const char modificatorSleepAtEOP[] = "Modificator: adjusted sleep-timer to after end of playlist.";
const char modificatorSleepAtEOPd[] = "Modificator: disabled sleep-timer after end of playlist.";
const char modificatorAllTrackAlphSortedLoop[] = "Modificator: adjusted to all tracks (in alph. order) as infinite loop.";
const char modificatorAllTrackRandomLoop[] = "Modificator: adjusted to all tracks (in random order) as infinite loop.";
const char modificatorCurTrackLoop[] = "Modificator: adjusted to current track as infinite loop.";
Expand Down
12 changes: 4 additions & 8 deletions src/Mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,17 +320,15 @@ void Mqtt_ClientCallback(const char *topic, const byte *payload, uint32_t length
gPlayProperties.sleepAfterPlaylist = true;
Log_Println(sleepTimerEOP, LOGLEVEL_NOTICE);
publishMqtt(topicSleepTimerState, "EOP", false);
Led_ResetToNightBrightness();
publishMqtt(topicLedBrightnessState, Led_GetBrightness(), false);
Led_SetNightmode(true);
System_IndicateOk();
free(receivedString);
return;
} else if (strcmp(receivedString, "EOT") == 0) {
gPlayProperties.sleepAfterCurrentTrack = true;
Log_Println(sleepTimerEOT, LOGLEVEL_NOTICE);
publishMqtt(topicSleepTimerState, "EOT", false);
Led_ResetToNightBrightness();
publishMqtt(topicLedBrightnessState, Led_GetBrightness(), false);
Led_SetNightmode(true);
System_IndicateOk();
free(receivedString);
return;
Expand All @@ -342,8 +340,7 @@ void Mqtt_ClientCallback(const char *topic, const byte *payload, uint32_t length
}
Log_Println(sleepTimerEO5, LOGLEVEL_NOTICE);
publishMqtt(topicSleepTimerState, "EO5T", false);
Led_ResetToNightBrightness();
publishMqtt(topicLedBrightnessState, Led_GetBrightness(), false);
Led_SetNightmode(true);
System_IndicateOk();
free(receivedString);
return;
Expand All @@ -352,8 +349,8 @@ void Mqtt_ClientCallback(const char *topic, const byte *payload, uint32_t length
System_DisableSleepTimer();
Log_Println(sleepTimerStop, LOGLEVEL_NOTICE);
System_IndicateOk();
Led_SetNightmode(false);
publishMqtt(topicSleepState, 0, false);
publishMqtt(topicLedBrightnessState, Led_GetBrightness(), false);
gPlayProperties.sleepAfterPlaylist = false;
gPlayProperties.sleepAfterCurrentTrack = false;
gPlayProperties.playUntilTrackNumber = 0;
Expand Down Expand Up @@ -447,7 +444,6 @@ void Mqtt_ClientCallback(const char *topic, const byte *payload, uint32_t length
// Check if LEDs should be dimmed
else if (strcmp_P(topic, topicLedBrightnessCmnd) == 0) {
Led_SetBrightness(strtoul(receivedString, NULL, 10));
publishMqtt(topicLedBrightnessState, Led_GetBrightness(), false);
}

// Requested something that isn't specified?
Expand Down
7 changes: 3 additions & 4 deletions src/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ bool System_SetSleepTimer(uint8_t minutes) {
if (System_SleepTimerStartTimestamp && (System_SleepTimer == minutes)) {
System_SleepTimerStartTimestamp = 0u;
System_SleepTimer = 0u;
Led_ResetToInitialBrightness();
Led_SetNightmode(false);
Log_Println(modificatorSleepd, LOGLEVEL_NOTICE);
} else {
System_SleepTimerStartTimestamp = millis();
System_SleepTimer = minutes;
sleepTimerEnabled = true;

Led_ResetToNightBrightness();
Led_SetNightmode(true);
if (minutes == 15) {
Log_Println(modificatorSleepTimer15, LOGLEVEL_NOTICE);
} else if (minutes == 30) {
Expand All @@ -95,15 +95,14 @@ bool System_SetSleepTimer(uint8_t minutes) {

#ifdef MQTT_ENABLE
publishMqtt(topicSleepTimerState, System_GetSleepTimer(), false);
publishMqtt(topicLedBrightnessState, Led_GetBrightness(), false);
#endif

return sleepTimerEnabled;
}

void System_DisableSleepTimer(void) {
System_SleepTimerStartTimestamp = 0u;
Led_ResetToInitialBrightness();
Led_SetNightmode(false);
}

bool System_IsSleepTimerEnabled(void) {
Expand Down
3 changes: 1 addition & 2 deletions src/logmessages.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,10 @@ extern const char modificatorSleepTimer60[];
extern const char modificatorSleepTimer120[];
extern const char ledsDimmedToNightmode[];
extern const char ledsDimmedToInitialValue[];
extern const char ledsBrightnessRestored[];
extern const char modificatorNotallowedWhenIdle[];
extern const char modificatorSleepAtEOT[];
extern const char modificatorSleepAtEOTd[];
extern const char modificatorSleepAtEOP[];
extern const char modificatorSleepAtEOPd[];
extern const char modificatorAllTrackAlphSortedLoop[];
extern const char modificatorAllTrackRandomLoop[];
extern const char modificatorCurTrackLoop[];
Expand Down

0 comments on commit 39ad764

Please sign in to comment.