diff --git a/vertushka_esp32s3/vertushka_esp32s3.ino b/vertushka_esp32s3/vertushka_esp32s3.ino index 981195e..fc73ee2 100644 --- a/vertushka_esp32s3/vertushka_esp32s3.ino +++ b/vertushka_esp32s3/vertushka_esp32s3.ino @@ -64,6 +64,26 @@ static uint16_t calib_pwm[CALIB_MAX]; static uint8_t calib_count = CALIB_DEFAULT_N; static uint16_t rpm_min = RPM_MIN_DEFAULT; +// ==== Расписание (7 дней недели, индекс 0=Пн, 6=Вс) ==== +struct DaySchedule { + uint8_t enabled; // 0/1 — активен ли этот день + uint8_t step_idx; // на какой ступени крутить + uint8_t start_hh; + uint8_t start_mm; + uint8_t end_hh; + uint8_t end_mm; +}; // 6 байт; 7×6 = 42 байт NVS-blob + +#define SCHEDULE_DAYS 7 +static DaySchedule schedule[SCHEDULE_DAYS]; +static bool schedule_global_on = false; + +// Состояние исполнения (RAM, не сохраняется): какое правило сейчас активно +// (мотор крутится по расписанию), и в какой день была последняя проверка. +// Нужно чтобы не запускать дважды в одну минуту. +static int8_t schedule_active_day = -1; // -1 = нет; 0..6 = индекс дня +static int8_t last_checked_day = -1; + static inline uint16_t calibRpm(uint8_t idx) { return rpm_min + idx * RPM_STEP; } #define INIT_VER 2200 // PWM "стоп" — ESC в нейтрали @@ -675,6 +695,35 @@ static void calibSave() { Serial.printf("calib saved: %u steps from %u rpm\n", calib_count, rpm_min); } +// ==== Расписание: NVS load/save ==== +static void scheduleSetDefaults() { + for (uint8_t d = 0; d < SCHEDULE_DAYS; d++) { + schedule[d].enabled = 0; + schedule[d].step_idx = 0; + schedule[d].start_hh = 9; + schedule[d].start_mm = 0; + schedule[d].end_hh = 10; + schedule[d].end_mm = 0; + } + schedule_global_on = false; +} +static void scheduleLoad() { + size_t sz = prefs.getBytesLength("sch_data"); + if (sz == sizeof(schedule)) { + prefs.getBytes("sch_data", schedule, sz); + schedule_global_on = prefs.getUChar("sch_on", 0) != 0; + Serial.printf("schedule loaded, global=%d\n", (int)schedule_global_on); + } else { + scheduleSetDefaults(); + Serial.println("schedule: defaults"); + } +} +static void scheduleSave() { + prefs.putBytes("sch_data", schedule, sizeof(schedule)); + prefs.putUChar("sch_on", schedule_global_on ? 1 : 0); + Serial.println("schedule saved"); +} + // ==== Setup ==== void setup() { Serial.begin(115200); @@ -685,6 +734,7 @@ void setup() { prefs.begin("vertushka", false); step_idx = prefs.getUChar("idx", 0); calibLoad(); + scheduleLoad(); if (step_idx >= calib_count) step_idx = 0; Serial.printf("Loaded step_idx=%d, calib_count=%u, rpm_min=%u\n", step_idx, calib_count, rpm_min);