Skip to content

Commit 4a6ff64

Browse files
add more macro/timer slots (#5140)
* convert to vectors * Update wled00/cfg.cpp Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * UI fixes converted preset entry to select box for ease of use * no eeprom compatibility * tighten up code * fix sunrise/sunset issue * fix issue with sunrise/sunset/regular selector * preset dropdowns * sort before use * delete timer when preset = 0 * truncate long names and add sort options * html updates * fix save bug * make boxes a bit darker * add fallback if localStorage is empty * remove sort option * code rabbit suggested fixes * remove css * restyling * common * dry preset loading * revert missing const * update deep sleep usermod with new timer struct * tabs * Revert "tabs" This reverts commit fdc01f2. * remove unneeded clamp checks * leave index alone * remove useless check * fix up await functions * only fetch localstorage * inline single line function --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent f6a43f4 commit 4a6ff64

11 files changed

Lines changed: 472 additions & 239 deletions

File tree

‎usermods/deep_sleep/deep_sleep.cpp‎

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -89,28 +89,29 @@ class DeepSleepUsermod : public Usermod {
8989
int currentWeekday = weekdayMondayFirst(); // 1=Monday ... 7=Sunday
9090
int minDifference = INT_MAX;
9191

92-
for (uint8_t i = 0; i < 8; i++) {
93-
// check if timer is enabled and date is in range, also wakes up if no macro is used
94-
if ((timerWeekday[i] & 0x01) && isTodayInDateRange(((timerMonth[i] >> 4) & 0x0F), timerDay[i], timerMonth[i] & 0x0F, timerDayEnd[i])) {
92+
for (size_t i = 0; i < timers.size(); i++) {
93+
const Timer& t = timers[i];
94+
// only regular enabled timers with valid date range can be used for wake scheduling
95+
if (!t.isEnabled() || !t.isRegular()) continue;
96+
if (!isTodayInDateRange(t.monthStart, t.dayStart, t.monthEnd, t.dayEnd)) continue;
9597

96-
// if timer is enabled (bit0 of timerWeekday) and date is in range, check all weekdays it is set for
97-
for (int dayOffset = 0; dayOffset < 7; dayOffset++) {
98-
int checkWeekday = ((currentWeekday + dayOffset) % 7); // 1-7, check all weekdays starting from today
99-
if (checkWeekday == 0) {
100-
checkWeekday = 7; // sunday is 7 not 0
101-
}
98+
// check all weekdays for the current timer, starting from today
99+
for (int dayOffset = 0; dayOffset < 7; dayOffset++) {
100+
int checkWeekday = (currentWeekday + dayOffset) % 7; // 1-7, check all weekdays starting from today
101+
if (checkWeekday == 0) {
102+
checkWeekday = 7; // sunday is 7 not 0
103+
}
102104

103-
int targetHour = timerHours[i];
104-
int targetMinute = timerMinutes[i];
105-
if ((timerWeekday[i] >> (checkWeekday)) & 0x01) {
106-
if (dayOffset == 0 && (targetHour < currentHour || (targetHour == currentHour && targetMinute <= currentMinute)))
107-
continue; // skip if time has already passed today
105+
int targetHour = t.hour;
106+
int targetMinute = t.minute;
107+
if ((t.weekdays >> checkWeekday) & 0x01) {
108+
if (dayOffset == 0 && (targetHour < currentHour || (targetHour == currentHour && targetMinute <= currentMinute)))
109+
continue; // skip if time has already passed today
108110

109-
int timeDifference = calculateTimeDifference(currentHour, currentMinute, targetHour + (dayOffset * 24), targetMinute);
110-
if (timeDifference < minDifference) {
111-
minDifference = timeDifference;
112-
wakeupPreset = timerMacro[i];
113-
}
111+
int timeDifference = calculateTimeDifference(currentHour, currentMinute, targetHour + (dayOffset * 24), targetMinute);
112+
if (timeDifference < minDifference) {
113+
minDifference = timeDifference;
114+
wakeupPreset = t.preset;
114115
}
115116
}
116117
}

‎wled00/cfg.cpp‎

Lines changed: 36 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -690,37 +690,28 @@ bool deserializeConfig(JsonObject doc, bool fromFS) {
690690
CJSON(macroCountdown, cntdwn["macro"]);
691691
setCountdown();
692692

693-
JsonArray timers = tm["ins"];
694-
uint8_t it = 0;
695-
for (JsonObject timer : timers) {
696-
if (it > 9) break;
697-
if (it<8 && timer[F("hour")]==255) it=8; // hour==255 -> sunrise/sunset
698-
CJSON(timerHours[it], timer[F("hour")]);
699-
CJSON(timerMinutes[it], timer["min"]);
700-
CJSON(timerMacro[it], timer["macro"]);
701-
702-
byte dowPrev = timerWeekday[it];
703-
//note: act is currently only 0 or 1.
704-
//the reason we are not using bool is that the on-disk type in 0.11.0 was already int
705-
int actPrev = timerWeekday[it] & 0x01;
706-
CJSON(timerWeekday[it], timer[F("dow")]);
707-
if (timerWeekday[it] != dowPrev) { //present in JSON
708-
timerWeekday[it] <<= 1; //add active bit
709-
int act = timer["en"] | actPrev;
710-
if (act) timerWeekday[it]++;
711-
}
712-
if (it<8) {
713-
JsonObject start = timer["start"];
714-
byte startm = start["mon"];
715-
if (startm) timerMonth[it] = (startm << 4);
716-
CJSON(timerDay[it], start["day"]);
717-
JsonObject end = timer["end"];
718-
CJSON(timerDayEnd[it], end["day"]);
719-
byte endm = end["mon"];
720-
if (startm) timerMonth[it] += endm & 0x0F;
721-
if (!(timerMonth[it] & 0x0F)) timerMonth[it] += 12; //default end month to 12
693+
JsonArray timersArray = tm["ins"];
694+
if (!timersArray.isNull()) {
695+
clearTimers();
696+
for (JsonObject timer : timersArray) {
697+
uint8_t h = timer[F("hour")] | 0;
698+
int8_t m = timer[F("min")] | 0;
699+
uint8_t p = timer[F("macro")] | 0;
700+
uint8_t dow = timer[F("dow")] | 127;
701+
uint8_t wd = (dow << 1) | ((timer[F("en")] | 0) ? 1 : 0);
702+
uint8_t ms = 1, me = 12, ds = 1, de = 31;
703+
JsonObject start = timer[F("start")];
704+
if (!start.isNull()) {
705+
ms = start[F("mon")] | 1;
706+
ds = start[F("day")] | 1;
707+
}
708+
JsonObject end = timer[F("end")];
709+
if (!end.isNull()) {
710+
me = end[F("mon")] | 12;
711+
de = end[F("day")] | 31;
712+
}
713+
addTimer(p, h, m, wd, ms, me, ds, de);
722714
}
723-
it++;
724715
}
725716

726717
JsonObject ota = doc["ota"];
@@ -1217,23 +1208,21 @@ void serializeConfig(JsonObject root) {
12171208
cntdwn["macro"] = macroCountdown;
12181209

12191210
JsonArray timers_ins = timers.createNestedArray("ins");
1220-
1221-
for (unsigned i = 0; i < 10; i++) {
1222-
if (timerMacro[i] == 0 && timerHours[i] == 0 && timerMinutes[i] == 0) continue; // sunrise/sunset get saved always (timerHours=255)
1223-
JsonObject timers_ins0 = timers_ins.createNestedObject();
1224-
timers_ins0["en"] = (timerWeekday[i] & 0x01);
1225-
timers_ins0[F("hour")] = timerHours[i];
1226-
timers_ins0["min"] = timerMinutes[i];
1227-
timers_ins0["macro"] = timerMacro[i];
1228-
timers_ins0[F("dow")] = timerWeekday[i] >> 1;
1229-
if (i<8) {
1230-
JsonObject start = timers_ins0.createNestedObject("start");
1231-
start["mon"] = (timerMonth[i] >> 4) & 0xF;
1232-
start["day"] = timerDay[i];
1233-
JsonObject end = timers_ins0.createNestedObject("end");
1234-
end["mon"] = timerMonth[i] & 0xF;
1235-
end["day"] = timerDayEnd[i];
1236-
}
1211+
for (size_t i = 0; i < ::timers.size(); i++) {
1212+
const Timer& t = ::timers[i];
1213+
if (t.preset == 0 && t.hour == 0 && t.minute == 0) continue;
1214+
JsonObject ti = timers_ins.createNestedObject();
1215+
ti[F("en")] = t.isEnabled() ? 1 : 0;
1216+
ti[F("hour")] = t.hour;
1217+
ti[F("min")] = t.minute;
1218+
ti[F("macro")] = t.preset;
1219+
ti[F("dow")] = t.weekdays >> 1;
1220+
JsonObject start = ti.createNestedObject(F("start"));
1221+
start[F("mon")] = t.monthStart;
1222+
start[F("day")] = t.dayStart;
1223+
JsonObject end = ti.createNestedObject(F("end"));
1224+
end[F("mon")] = t.monthEnd;
1225+
end[F("day")] = t.dayEnd;
12371226
}
12381227

12391228
JsonObject ota = root.createNestedObject("ota");

‎wled00/const.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ constexpr size_t FIXED_PALETTE_COUNT = DYNAMIC_PALETTE_COUNT + FASTLED_PALETTE_C
5959
#define WLED_MAX_RMT_CHANNELS 0 // ESP8266 does not have RMT nor I2S
6060
#define WLED_MAX_I2S_CHANNELS 0
6161
#define WLED_MAX_ANALOG_CHANNELS 5
62+
#define WLED_MAX_TIMERS 16 // reduced limit for ESP8266 due to memory constraints
6263
#define WLED_PLATFORM_ID 0 // used in UI to distinguish ESP types, needs a proper fix!
6364
#else
6465
#if !defined(LEDC_CHANNEL_MAX) || !defined(LEDC_SPEED_MODE_MAX)
@@ -86,6 +87,7 @@ constexpr size_t FIXED_PALETTE_COUNT = DYNAMIC_PALETTE_COUNT + FASTLED_PALETTE_C
8687
//#define WLED_MAX_ANALOG_CHANNELS 16
8788
#define WLED_PLATFORM_ID 4 // used in UI to distinguish ESP type in UI, needs a proper fix!
8889
#endif
90+
#define WLED_MAX_TIMERS 64 // maximum number of timers
8991
#define WLED_MAX_DIGITAL_CHANNELS (WLED_MAX_RMT_CHANNELS + WLED_MAX_I2S_CHANNELS)
9092
#endif
9193
// WLED_MAX_BUSSES was used to define the size of busses[] array which is no longer needed

‎wled00/data/common.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ function loadJS(FILE_URL, async = true, preGetV = undefined, postGetV = undefine
9090
scE.setAttribute("type", "text/javascript");
9191
scE.setAttribute("async", async);
9292
d.body.appendChild(scE);
93-
// success event
93+
// success event
9494
scE.addEventListener("load", () => {
9595
//console.log("File loaded");
9696
if (preGetV) preGetV();

0 commit comments

Comments
 (0)