Skip to content

Commit

Permalink
Merge branch 'stable' of https://github.com/blazoncek/WLED into stable
Browse files Browse the repository at this point in the history
  • Loading branch information
blazoncek committed Dec 16, 2024
2 parents eb748f5 + 634a15a commit ff04547
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 115 deletions.
1 change: 1 addition & 0 deletions usermods/BME280_v2/usermod_bme280.h
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ class UsermodBME280 : public Usermod
configComplete &= getJsonValue(top[F("PublishAlways")], PublishAlways, false);
configComplete &= getJsonValue(top[F("UseCelsius")], UseCelsius, true);
configComplete &= getJsonValue(top[F("HomeAssistantDiscovery")], HomeAssistantDiscovery, false);
tempScale = UseCelsius ? "°C" : "°F";

DEBUGUM_PRINT(FPSTR(_name));
if (!initDone) {
Expand Down
33 changes: 19 additions & 14 deletions wled00/data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,9 @@ function pName(i)

function isPlaylist(i)
{
return pJson[i].playlist && pJson[i].playlist.ps;
if (isNumeric(i)) return pJson[i].playlist && pJson[i].playlist.ps;
if (isObj(i)) return i.playlist && i.playlist.ps;
return false;
}

function papiVal(i)
Expand Down Expand Up @@ -1899,15 +1901,16 @@ function resetUtil(off=false)
if (lSeg>2) d.querySelectorAll("#Segments .pop").forEach((e)=>{e.classList.remove("hide");});
}

function makePlSel(el, incPl=false)
function makePlSel(p, el)
{
var plSelContent = "";
delete pJson["0"]; // remove filler preset
Object.entries(pJson).sort(cmpP).forEach((a)=>{
var n = a[1].n ? a[1].n : "Preset " + a[0];
if (isPlaylist(a[1])) n += ' ▶'; // mark playlist
if (cfg.comp.idsort) n = a[0] + ' ' + n;
if (!(!incPl && a[1].playlist && a[1].playlist.ps)) // skip playlists, sub-playlists not yet supported
plSelContent += `<option value="${a[0]}" ${a[0]==el?"selected":""}>${n}</option>`;
// skip endless playlists and itself
if (!isPlaylist(a[1]) || (a[1].playlist.repeat > 0 && a[0]!=p)) plSelContent += `<option value="${a[0]}" ${a[0]==el?"selected":""}>${n}</option>`;
});
return plSelContent;
}
Expand All @@ -1932,21 +1935,23 @@ function refreshPlE(p)
});
}

// p: preset ID, i: ps index
// p: preset ID, i: playlist item index
function addPl(p,i)
{
plJson[p].ps.splice(i+1,0,0);
plJson[p].dur.splice(i+1,0,plJson[p].dur[i]);
plJson[p].transition.splice(i+1,0,plJson[p].transition[i]);
const pl = plJson[p];
pl.ps.splice(i+1,0,1);
pl.dur.splice(i+1,0,pl.dur[i]);
pl.transition.splice(i+1,0,pl.transition[i]);
refreshPlE(p);
}

function delPl(p,i)
{
if (plJson[p].ps.length < 2) return;
plJson[p].ps.splice(i,1);
plJson[p].dur.splice(i,1);
plJson[p].transition.splice(i,1);
const pl = plJson[p];
if (pl.ps.length < 2) return;
pl.ps.splice(i,1);
pl.dur.splice(i,1);
pl.transition.splice(i,1);
refreshPlE(p);
}

Expand Down Expand Up @@ -2011,7 +2016,7 @@ function makeP(i,pl)
<div class="sel-p"><select class="sel-ple" id="pl${i}selEnd" onchange="plR(${i})" data-val=${plJson[i].end?plJson[i].end:0}>
<option value="0">None</option>
<option value="255" ${plJson[i].end && plJson[i].end==255?"selected":""}>Restore preset</option>
${makePlSel(plJson[i].end?plJson[i].end:0, true)}
${makePlSel(i, plJson[i].end?plJson[i].end:0)}
</select></div></div>
</div>
<div class="c"><button class="btn btn-p" onclick="testPl(${i}, this)"><i class='icons btn-icon'>&#xe139;</i>Test</button></div>`;
Expand Down Expand Up @@ -2090,7 +2095,7 @@ function makePlEntry(p,i)
<tr>
<td width="80%" colspan=2>
<div class="sel-p"><select class="sel-pl" onchange="plePs(${p},${i},this)" data-val="${plJson[p].ps[i]}" data-index="${i}">
${makePlSel(plJson[p].ps[i])}
${makePlSel(p, plJson[p].ps[i])}
</select></div>
</td>
<td class="c"><button class="btn btn-pl-add" onclick="addPl(${p},${i})"><i class="icons btn-icon">&#xe18a;</i></button></td>
Expand Down
19 changes: 18 additions & 1 deletion wled00/fcn_declare.h
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ void serializePlaylist(JsonObject obj);

//presets.cpp
const char *getPresetsFileName(bool persistent = true);
bool presetNeedsSaving();
void initPresetsFile();
void handlePresets();
bool applyPreset(byte index, byte callMode = CALL_MODE_DIRECT_CHANGE);
Expand All @@ -317,7 +318,23 @@ void deletePreset(byte index);
bool getPresetName(byte index, String& name);

//remote.cpp
void handleRemote(uint8_t *data, size_t len);
void handleRemote();
// This is kind of an esoteric strucure because it's pulled from the "Wizmote"
// product spec. That remote is used as the baseline for behavior and availability
// since it's broadly commercially available and works out of the box as a drop-in
typedef struct WizMoteMessageStructure {
uint8_t program; // 0x91 for ON button, 0x81 for all others
uint8_t seq[4]; // Incremetal sequence number 32 bit unsigned integer LSB first
uint8_t dt1; // Button Data Type (0x32)
uint8_t button; // Identifies which button is being pressed
uint8_t dt2; // Battery Level Data Type (0x01)
uint8_t batLevel; // Battery Level 0-100

uint8_t byte10; // Unknown, maybe checksum
uint8_t byte11; // Unknown, maybe checksum
uint8_t byte12; // Unknown, maybe checksum
uint8_t byte13; // Unknown, maybe checksum
} message_structure_t;

//set.cpp
bool isAsterisksOnly(const char* str, byte maxLen);
Expand Down
7 changes: 4 additions & 3 deletions wled00/led.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,11 @@ void stateUpdated(byte callMode) {
}
}

unsigned long now = millis();
if (callMode != CALL_MODE_NO_NOTIFY && nightlightActive && (nightlightMode == NL_MODE_FADE || nightlightMode == NL_MODE_COLORFADE)) {
briNlT = bri;
nightlightDelayMs -= (millis() - nightlightStartTime);
nightlightStartTime = millis();
nightlightDelayMs -= (now - nightlightStartTime);
nightlightStartTime = now;
}
if (briT == 0) {
if (callMode != CALL_MODE_NOTIFICATION) strip.resetTimebase(); //effect start from beginning
Expand All @@ -143,7 +144,7 @@ void stateUpdated(byte callMode) {
} else
strip.setTransitionMode(true); // force all segments to transition mode
transitionActive = true;
transitionStartTime = millis();
transitionStartTime = now;
}


Expand Down
46 changes: 34 additions & 12 deletions wled00/playlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ typedef struct PlaylistEntry {
uint16_t tr; //Duration of the transition TO this entry (in tenths of seconds)
} ple;

byte playlistRepeat = 1; //how many times to repeat the playlist (0 = infinitely)
byte playlistEndPreset = 0; //what preset to apply after playlist end (0 = stay on last preset)
byte playlistOptions = 0; //bit 0: shuffle playlist after each iteration. bits 1-7 TBD
static byte playlistRepeat = 1; //how many times to repeat the playlist (0 = infinitely)
static byte playlistEndPreset = 0; //what preset to apply after playlist end (0 = stay on last preset)
static byte playlistOptions = 0; //bit 0: shuffle playlist after each iteration. bits 1-7 TBD

PlaylistEntry *playlistEntries = nullptr;
byte playlistLen; //number of playlist entries
int8_t playlistIndex = -1;
uint16_t playlistEntryDur = 0; //duration of the current entry in tenths of seconds
static PlaylistEntry *playlistEntries = nullptr;
static byte playlistLen; //number of playlist entries
static int8_t playlistIndex = -1;
static uint16_t playlistEntryDur = 0; //duration of the current entry in tenths of seconds

//values we need to keep about the parent playlist while inside sub-playlist
//int8_t parentPlaylistIndex = -1;
//byte parentPlaylistRepeat = 0;
//byte parentPlaylistPresetId = 0; //for re-loading
static int16_t parentPlaylistIndex = -1;
static byte parentPlaylistRepeat = 0;
static byte parentPlaylistPresetId = 0; //for re-loading


void shufflePlaylist() {
Expand Down Expand Up @@ -54,6 +54,12 @@ void unloadPlaylist() {


int16_t loadPlaylist(JsonObject playlistObj, byte presetId) {
if (currentPlaylist > 0 && parentPlaylistPresetId > 0) return -1; // we are already in nested playlist, do nothing
if (currentPlaylist > 0) {
parentPlaylistIndex = playlistIndex;
parentPlaylistRepeat = playlistRepeat;
parentPlaylistPresetId = currentPlaylist;
}
unloadPlaylist();

JsonArray presets = playlistObj["ps"];
Expand Down Expand Up @@ -117,6 +123,19 @@ int16_t loadPlaylist(JsonObject playlistObj, byte presetId) {
shuffle = shuffle || playlistObj["r"];
if (shuffle) playlistOptions |= PL_OPTION_SHUFFLE;

if (parentPlaylistPresetId == 0 && parentPlaylistIndex > -1) {
// we are re-loading playlist when returning from nested playlist
playlistIndex = parentPlaylistIndex;
playlistRepeat = parentPlaylistRepeat;
parentPlaylistIndex = -1;
parentPlaylistRepeat = 0;
} else if (rep == 0) {
// endless playlist will never return to parent so erase parent information if it was called from it
parentPlaylistPresetId = 0;
parentPlaylistIndex = -1;
parentPlaylistRepeat = 0;
}

currentPlaylist = presetId;
DEBUG_PRINTLN(F("Playlist loaded."));
return currentPlaylist;
Expand All @@ -127,7 +146,7 @@ void handlePlaylist() {
static unsigned long presetCycledTime = 0;
if (currentPlaylist < 0 || playlistEntries == nullptr) return;

if (millis() - presetCycledTime > (100 * playlistEntryDur) || doAdvancePlaylist) {
if (millis() - presetCycledTime > (100 * playlistEntryDur) || doAdvancePlaylist) {
presetCycledTime = millis();
if (bri == 0 || nightlightActive) return;

Expand All @@ -137,7 +156,10 @@ if (millis() - presetCycledTime > (100 * playlistEntryDur) || doAdvancePlaylist)
if (!playlistIndex) {
if (playlistRepeat == 1) { //stop if all repetitions are done
unloadPlaylist();
if (playlistEndPreset) applyPresetFromPlaylist(playlistEndPreset);
if (parentPlaylistPresetId > 0) {
applyPresetFromPlaylist(parentPlaylistPresetId); // reload previous playlist (unfortunately asynchronous)
parentPlaylistPresetId = 0; // reset previous playlist but do not reset Index or Repeat (they will be loaded & reset in loadPlaylist())
} else if (playlistEndPreset) applyPresetFromPlaylist(playlistEndPreset);
return;
}
if (playlistRepeat > 1) playlistRepeat--; // decrease repeat count on each index reset if not an endless playlist
Expand Down
6 changes: 5 additions & 1 deletion wled00/presets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ const char *getPresetsFileName(bool persistent) {
return persistent ? presets_json : tmp_json;
}

bool presetNeedsSaving() {
return presetToSave;
}

static void doSaveState() {
bool persist = (presetToSave < 251);

Expand Down Expand Up @@ -264,7 +268,7 @@ void savePreset(byte index, const char* pname, JsonObject sObj)
quickLoad = nullptr;
} else {
// store playlist
// WARNING: playlist will be loaded in json.cpp after this call and will have repeat counter increased by 1
// WARNING: playlist will be loaded in json.cpp after this call and will have repeat counter increased by 1 it will also be randomised if selected
includeBri = true; // !sObj["on"].isNull();
playlistSave = true;
}
Expand Down
59 changes: 11 additions & 48 deletions wled00/remote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,8 @@
#define WIZ_SMART_BUTTON_BRIGHT_UP 102
#define WIZ_SMART_BUTTON_BRIGHT_DOWN 103

// This is kind of an esoteric strucure because it's pulled from the "Wizmote"
// product spec. That remote is used as the baseline for behavior and availability
// since it's broadly commercially available and works out of the box as a drop-in
typedef struct WizMoteMessageStructure {
uint8_t program; // 0x91 for ON button, 0x81 for all others
uint8_t seq[4]; // Incremetal sequence number 32 bit unsigned integer LSB first
uint8_t dt1; // Button Data Type (0x32)
uint8_t button; // Identifies which button is being pressed
uint8_t dt2; // Battery Level Data Type (0x01)
uint8_t batLevel; // Battery Level 0-100

uint8_t byte10; // Unknown, maybe checksum
uint8_t byte11; // Unknown, maybe checksum
uint8_t byte12; // Unknown, maybe checksum
uint8_t byte13; // Unknown, maybe checksum
} message_structure_t;

static uint32_t last_seq = UINT32_MAX;
#define ESPNOW_DELAY_PROCESSING 24 // one frame delay

static int brightnessBeforeNightMode = NIGHT_MODE_DEACTIVATED;

// Pulled from the IR Remote logic but reduced to 10 steps with a constant of 3
Expand Down Expand Up @@ -117,6 +101,9 @@ static bool remoteJson(int button)
char objKey[10];
bool parsed = false;

unsigned long start = millis();
while (strip.isUpdating() && millis()-start < ESPNOW_DELAY_PROCESSING) delay(1); // we are not in ISR/callback

if (!requestJSONBufferLock(22)) return false;

sprintf_P(objKey, PSTR("\"%d\":"), button);
Expand Down Expand Up @@ -176,34 +163,10 @@ static bool remoteJson(int button)
}

// Callback function that will be executed when data is received
void handleRemote(uint8_t *incomingData, size_t len) {
message_structure_t *incoming = reinterpret_cast<message_structure_t *>(incomingData);

if (strcmp(last_signal_src, linked_remote) != 0) {
DEBUG_PRINT(F("ESP Now Message Received from Unlinked Sender: "));
DEBUG_PRINTLN(last_signal_src);
return;
}

if (len != sizeof(message_structure_t)) {
DEBUG_PRINTF_P(PSTR("Unknown incoming ESP Now message received of length %u\n"), len);
return;
}

uint32_t cur_seq = incoming->seq[0] | (incoming->seq[1] << 8) | (incoming->seq[2] << 16) | (incoming->seq[3] << 24);
if (cur_seq == last_seq) {
return;
}

DEBUG_PRINT(F("Incoming ESP Now Packet ["));
DEBUG_PRINT(cur_seq);
DEBUG_PRINT(F("] from sender ["));
DEBUG_PRINT(last_signal_src);
DEBUG_PRINT(F("] button: "));
DEBUG_PRINTLN(incoming->button);

if (!remoteJson(incoming->button))
switch (incoming->button) {
void handleRemote() {
if (wizMoteButton == -1) return;
if (!remoteJson(wizMoteButton))
switch (wizMoteButton) {
case WIZMOTE_BUTTON_ON : setOn(); break;
case WIZMOTE_BUTTON_OFF : setOff(); break;
case WIZMOTE_BUTTON_ONE : presetWithFallback(1, FX_MODE_STATIC, 0); break;
Expand All @@ -219,9 +182,9 @@ void handleRemote(uint8_t *incomingData, size_t len) {
case WIZ_SMART_BUTTON_BRIGHT_DOWN : brightnessDown(); break;
default: break;
}
last_seq = cur_seq;
wizMoteButton = -1;
}

#else
void handleRemote(uint8_t *incomingData, size_t len) {}
void handleRemote() {}
#endif
15 changes: 13 additions & 2 deletions wled00/udp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,10 @@ void espNowReceiveCB(uint8_t* address, uint8_t* data, uint8_t len, signed int rs

#ifdef WLED_DEBUG
DEBUG_PRINT(F("ESP-NOW: ")); DEBUG_PRINT(last_signal_src); DEBUG_PRINT(F(" -> ")); DEBUG_PRINTLN(len);
for (int i=0; i<len; i++) DEBUG_PRINTF_P(PSTR("%02x "), data[i]);
for (int i=0; i<len; i++) {
if (!(i % 32)) DEBUG_PRINTLN();
DEBUG_PRINTF_P(PSTR("%02x "), data[i]);
}
DEBUG_PRINTLN();
#endif

Expand All @@ -959,7 +962,15 @@ void espNowReceiveCB(uint8_t* address, uint8_t* data, uint8_t len, signed int rs

// handle WiZ Mote data
if (data[0] == 0x91 || data[0] == 0x81 || data[0] == 0x80) {
handleRemote(data, len);
static uint32_t last_seq = UINT32_MAX;
message_structure_t *incoming = reinterpret_cast<message_structure_t *>(data);
if (len == sizeof(message_structure_t)) {
uint32_t cur_seq = incoming->seq[0] | (incoming->seq[1] << 8) | (incoming->seq[2] << 16) | (incoming->seq[3] << 24);
if (cur_seq == last_seq) return;
DEBUG_PRINTF_P(PSTR("Incoming Wiz Mote Packet [%u]; button: %d\n"), cur_seq, (int)incoming->button);
wizMoteButton = incoming->button;
last_seq = cur_seq;
} else DEBUG_PRINTF_P(PSTR("Unknown Wiz Mote message [%uB]\n"), len);
return;
}

Expand Down
Loading

0 comments on commit ff04547

Please sign in to comment.