From 74f728b6b2f0b861010d81787aa4d8e803a54d71 Mon Sep 17 00:00:00 2001 From: Matt Turner Date: Fri, 30 Dec 2022 20:33:27 +0000 Subject: [PATCH] Simplify schedules (#27) --- lib/Display/Display.cpp | 13 +--- lib/Display/Display.h | 6 +- lib/Schedule/Schedule.h | 8 +-- src/main.cpp | 93 ++++++++++++---------------- src/main.h | 11 ++-- test/test_display/test_display.cpp | 29 ++------- test/test_schedule/test_schedule.cpp | 54 +--------------- 7 files changed, 60 insertions(+), 154 deletions(-) diff --git a/lib/Display/Display.cpp b/lib/Display/Display.cpp index dd23457..83a1e3f 100644 --- a/lib/Display/Display.cpp +++ b/lib/Display/Display.cpp @@ -69,16 +69,11 @@ void Display::setPart(byte part, byte value, Flags flags) } byte tens = (value - (value % 10)) / 10; byte unit = value % 10; - bool superMinimalMode = ((flags & Flags::SUPER_MINIMAL) == Flags::SUPER_MINIMAL); bool minimalMode = ((flags & Flags::MINIMAL) == Flags::MINIMAL); - if (superMinimalMode) + if (minimalMode) { setDots(0 + offset, 0); } - else if (minimalMode) - { - setDots(0 + offset, unit); - } else { setDigit(0 + offset, unit); @@ -86,11 +81,7 @@ void Display::setPart(byte part, byte value, Flags flags) bool leadingZero = ((flags & Flags::LEADING_ZERO) == Flags::LEADING_ZERO); if (leadingZero || tens > 0) { - if (superMinimalMode) - { - setDots(1 + offset, tens); - } - else if (minimalMode) + if (minimalMode) { setDots(1 + offset, tens); } diff --git a/lib/Display/Display.h b/lib/Display/Display.h index 1bb2acd..de9b728 100644 --- a/lib/Display/Display.h +++ b/lib/Display/Display.h @@ -12,9 +12,11 @@ const byte NUM_COMPONENTS = 5; typedef enum { NONE = 0, - LEADING_ZERO = 1, + SEPARATOR = 1, MINIMAL = 2, - SUPER_MINIMAL = 4 + BRIGHT = 4, + LEADING_ZERO = 8, + STANDARD = SEPARATOR } Flags; class Display diff --git a/lib/Schedule/Schedule.h b/lib/Schedule/Schedule.h index 3411f62..1eb4740 100644 --- a/lib/Schedule/Schedule.h +++ b/lib/Schedule/Schedule.h @@ -9,12 +9,8 @@ const byte BLOCKS = HOURS_IN_DAY * BLOCKS_PER_HOUR; typedef enum { BNONE = 0, - FIRST = 1, - SECOND = 2, - THIRD = 4, - LAST = 8, - FIRST_HALF = FIRST | SECOND, - SECOND_HALF = THIRD | LAST, + FIRST_HALF = 1, + SECOND_HALF = 2, ALL = FIRST_HALF | SECOND_HALF } BlockFlags; diff --git a/src/main.cpp b/src/main.cpp index 90be7c5..80a319c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,8 +18,6 @@ void setup() setupRealtimeClock(); setupColorSchedule(); setupDisplaySchedule(); - setupBrightnessSchedule(); - setupMinimalModeSchedule(); setupTimezones(); setupTest(); @@ -43,7 +41,7 @@ void setup() bool idle() { - // Serial.println(F("Sleeping...")); + Serial.println(F("Sleeping...")); Serial.flush(); LowPower.idle(SLEEP_8S, ADC_OFF, TIMER2_OFF, TIMER1_OFF, TIMER0_OFF, SPI_OFF, USART0_OFF, TWI_OFF); @@ -52,15 +50,15 @@ bool idle() bool show() { - // Serial.println(F("Command: SHOW")); + Serial.println(F("Command: SHOW")); stateMachine.transition(SHOWING); return true; } bool set(EvtListener *, EvtContext *, long data) { - // Serial.print(F("Command: SET ")); - // Serial.println(data); + Serial.print(F("Command: SET ")); + Serial.println(data); clock.adjust(DateTime(data)); stateMachine.transition(SHOWING); return true; @@ -68,8 +66,8 @@ bool set(EvtListener *, EvtContext *, long data) bool setSchedule(EvtListener *, EvtContext *, long data) { - // Serial.print(F("Command: SET SCHEDULE ")); - // Serial.println(data); + Serial.print(F("Command: SET SCHEDULE ")); + Serial.println(data); displaySchedule.update(data); stateMachine.transition(SHOWING); return true; @@ -77,14 +75,14 @@ bool setSchedule(EvtListener *, EvtContext *, long data) bool status() { - // Serial.println(F("Command: STATUS")); + Serial.println(F("Command: STATUS")); reportStatus(); return true; } bool updating() { - // Serial.println(F("Updating...")); + Serial.println(F("Updating...")); DateTime now = DateTime(); if (CLOCK_IS_ENABLED) @@ -92,22 +90,22 @@ bool updating() now = toLocal(clock.now()); } - if (displaySchedule.valueFor(now.hour(), now.minute())) + Flags mode = displaySchedule.valueFor(now.hour(), now.minute()); + if (mode == Flags::NONE) { - Flags mode = minimalSchedule.valueFor(now.hour()); - pendingDisplay.setPart(1, now.hour(), mode); - pendingDisplay.setPart(0, now.minute(), (Flags)(mode | Flags::LEADING_ZERO)); - pendingDisplay.setSeparator(mode == Flags::NONE); + pendingDisplay.clear(); } else { - pendingDisplay.clear(); + pendingDisplay.setPart(1, now.hour(), mode); + pendingDisplay.setPart(0, now.minute(), (Flags)(mode | Flags::LEADING_ZERO)); + pendingDisplay.setSeparator((mode & Flags::SEPARATOR) == Flags::SEPARATOR); } CRGB::HTMLColorCode colorCode = colorSchedule.valueFor(now.hour()); pendingDisplay.setColor(colorCode); - byte brightness = brightnessSchedule.valueFor(now.hour()); - pendingDisplay.setBrightness(brightness); + + pendingDisplay.setBrightness(brightnessFrom(mode)); if (display.updateFrom(&pendingDisplay)) { @@ -117,9 +115,18 @@ bool updating() return true; } +byte brightnessFrom(Flags mode) +{ + if ((mode & Flags::BRIGHT) == Flags::BRIGHT) + { + return BRIGHTNESS_BRIGHT; + } + return BRIGHTNESS_DIM; +} + bool showing() { - // Serial.println(F("Showing temporarily...")); + Serial.println(F("Showing temporarily...")); DateTime now = DateTime(); if (CLOCK_IS_ENABLED) @@ -127,14 +134,15 @@ bool showing() now = toLocal(clock.now()); } - display.setPart(1, now.hour(), Flags::NONE); + display.setPart(1, now.hour(), Flags::STANDARD); display.setPart(0, now.minute(), Flags::LEADING_ZERO); display.setSeparator(true); CRGB::HTMLColorCode colorCode = colorSchedule.valueFor(now.hour()); display.setColor(colorCode); - byte brightness = brightnessSchedule.valueFor(now.hour()); - display.setBrightness(brightness); + + Flags mode = displaySchedule.valueFor(now.hour(), now.minute()); + display.setBrightness(brightnessFrom(mode)); render(); @@ -196,7 +204,7 @@ void reportStatus() void setupColorSchedule() { - // Serial.println(F("Setup color schedule...")); + Serial.println(F("Setup color schedule...")); // daylight colorSchedule.setup(8, 20, CRGB::Blue); // morning @@ -207,28 +215,13 @@ void setupColorSchedule() void setupDisplaySchedule() { - // Serial.println(F("Setting up display schedule...")); + Serial.println(F("Setting up display schedule...")); - separatorSchedule.setup(6, 7, true); - displaySchedule.setup(6, 7, true); - - separatorSchedule.setup(20, 20, true); - displaySchedule.setup(20, 20, true); - - // separatorSchedule.setValueFor(20, BlockFlags::SECOND_HALF, true); - // displaySchedule.setValueFor(20, BlockFlags::SECOND_HALF, true); -} - -void setupBrightnessSchedule() -{ - // Serial.println(F("Setting up brightness schedule...")); - brightnessSchedule.setup(10, 18, 40); -} - -void setupMinimalModeSchedule() -{ - // Serial.println(F("Setting up minimal mode schedule...")); - minimalSchedule.setup(6, 6, Flags::SUPER_MINIMAL); + displaySchedule.setup(6, 6, Flags::MINIMAL); + displaySchedule.setup(7, 7, Flags::STANDARD); + displaySchedule.setup(10, 18, Flags::BRIGHT); + displaySchedule.setup(20, 20, Flags::STANDARD); + // displaySchedule.setValueFor(20, BlockFlags::SECOND_HALF, Flags::STANDARD); } void setupRealtimeClock() @@ -254,20 +247,10 @@ void setupRealtimeClock() if (clock.lostPower()) { - Serial.println(F("Realtime Clock lost power, setting the time...")); + Serial.println(F("RTC lost power...")); display.setText("lost"); render(); delay(1000); - clock.adjust(DateTime(F(__DATE__), F(__TIME__))); - // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0)); - } - if (digitalRead(SHOW_PIN) == LOW) - { - // Serial.println(F("Manual override: setting the time...")); - display.setText("rset"); - render(); - delay(1000); - clock.adjust(DateTime(F(__DATE__), F(__TIME__))); } } diff --git a/src/main.h b/src/main.h index 69c9270..527b805 100644 --- a/src/main.h +++ b/src/main.h @@ -28,6 +28,9 @@ const byte UPDATING = 1; const byte PROCESSING = 2; const byte SHOWING = 3; +const byte BRIGHTNESS_DIM = 5; +const byte BRIGHTNESS_BRIGHT = 40; + CRGB physicalLeds[NUM_LEDS]; Display display; Display pendingDisplay; @@ -35,10 +38,7 @@ RTC_DS3231 clock; EvtManager mgr; Timezone *timezone; Schedule colorSchedule(CRGB::Red); -Schedule displaySchedule(false, 1); -Schedule separatorSchedule(false); -Schedule brightnessSchedule(5); -Schedule minimalSchedule(Flags::NONE); +Schedule displaySchedule(Flags::NONE, 1); SoftwareSerial bluetoothSerial(RECEIVE_PIN, TRANSMIT_PIN); EvtCommandListener commandListener(&bluetoothSerial, 20); EvtStateMachineListener stateMachine; @@ -53,15 +53,14 @@ bool show(); bool set(EvtListener *, EvtContext *, long data); bool setSchedule(EvtListener *, EvtContext *, long data); bool status(); +byte brightnessFrom(Flags mode); void reportStatus(); DateTime toLocal(DateTime utc); void setupColorSchedule(); void setupDisplaySchedule(); -void setupBrightnessSchedule(); void setupRealtimeClock(); -void setupMinimalModeSchedule(); void setupTimezones(); void setupTest(); diff --git a/test/test_display/test_display.cpp b/test/test_display/test_display.cpp index b24180f..a1a87e5 100644 --- a/test/test_display/test_display.cpp +++ b/test/test_display/test_display.cpp @@ -127,21 +127,6 @@ void test_set_text(void) assert(); } -void test_set_three_in_minimal_mode(void) -{ - expected.setLedRange(15, 17, true); - target.setPart(0, 3, Flags::MINIMAL); - assert(); -} - -void test_set_thirty_three_in_minimal_mode(void) -{ - expected.setLedRange(15, 17, true); - expected.setLedRange(36, 38, true); - target.setPart(0, 33, Flags::MINIMAL); - assert(); -} - void test_flags_multiple(void) { Flags f = (Flags)(Flags::MINIMAL | Flags::LEADING_ZERO); @@ -156,16 +141,16 @@ void test_flags_single(void) TEST_ASSERT_FALSE((f & Flags::LEADING_ZERO) == Flags::LEADING_ZERO); } -void test_super_minimal_mode_includes_tens(void) +void test_minimal_mode_includes_tens(void) { expected.setLedRange(36, 38, true); - target.setPart(0, 33, Flags::SUPER_MINIMAL); + target.setPart(0, 33, Flags::MINIMAL); assert(); } -void test_super_minimal_mode_excludes_units(void) +void test_minimal_mode_excludes_units(void) { - target.setPart(0, 6, Flags::SUPER_MINIMAL); + target.setPart(0, 6, Flags::MINIMAL); assert(); } @@ -187,12 +172,10 @@ int main(int argc, char **argv) RUN_TEST(test_set_all_parts_max); RUN_TEST(test_set_all_parts_max_with_separator); RUN_TEST(test_set_text); - RUN_TEST(test_set_three_in_minimal_mode); RUN_TEST(test_flags_multiple); RUN_TEST(test_flags_single); - RUN_TEST(test_set_thirty_three_in_minimal_mode); - RUN_TEST(test_super_minimal_mode_includes_tens); - RUN_TEST(test_super_minimal_mode_excludes_units); + RUN_TEST(test_minimal_mode_includes_tens); + RUN_TEST(test_minimal_mode_excludes_units); UNITY_END(); return 0; diff --git a/test/test_schedule/test_schedule.cpp b/test/test_schedule/test_schedule.cpp index 417876c..43e9842 100644 --- a/test/test_schedule/test_schedule.cpp +++ b/test/test_schedule/test_schedule.cpp @@ -69,7 +69,7 @@ void test_does_not_update_invalid_quarter_flag(void) TEST_ASSERT_FALSE(actual); } -void test_updates_only_specified_first_quarter(void) +void test_updates_only_specified_first_half(void) { bool actual = target.update(114012); @@ -80,7 +80,7 @@ void test_updates_only_specified_first_quarter(void) TEST_ASSERT_EQUAL(2, target.valueFor(14, 59)); } -void test_updates_only_specified_second_quarter(void) +void test_updates_only_specified_second_half(void) { bool actual = target.update(114022); @@ -91,50 +91,6 @@ void test_updates_only_specified_second_quarter(void) TEST_ASSERT_EQUAL(10, target.valueFor(14, 59)); } -void test_updates_only_specified_third_quarter(void) -{ - bool actual = target.update(114042); - - TEST_ASSERT_TRUE(actual); - TEST_ASSERT_EQUAL(10, target.valueFor(14, 14)); - TEST_ASSERT_EQUAL(10, target.valueFor(14, 29)); - TEST_ASSERT_EQUAL(10, target.valueFor(14, 44)); - TEST_ASSERT_EQUAL(10, target.valueFor(14, 59)); -} - -void test_updates_only_specified_last_quarter(void) -{ - bool actual = target.update(114082); - - TEST_ASSERT_TRUE(actual); - TEST_ASSERT_EQUAL(10, target.valueFor(14, 14)); - TEST_ASSERT_EQUAL(10, target.valueFor(14, 29)); - TEST_ASSERT_EQUAL(10, target.valueFor(14, 44)); - TEST_ASSERT_EQUAL(10, target.valueFor(14, 59)); -} - -void test_updates_only_specified_first_half(void) -{ - bool actual = target.update(114032); - - TEST_ASSERT_TRUE(actual); - TEST_ASSERT_EQUAL(2, target.valueFor(14, 14)); - TEST_ASSERT_EQUAL(2, target.valueFor(14, 29)); - TEST_ASSERT_EQUAL(2, target.valueFor(14, 44)); - TEST_ASSERT_EQUAL(2, target.valueFor(14, 59)); -} - -void test_updates_only_specified_last_half(void) -{ - bool actual = target.update(114122); - - TEST_ASSERT_TRUE(actual); - TEST_ASSERT_EQUAL(10, target.valueFor(14, 14)); - TEST_ASSERT_EQUAL(10, target.valueFor(14, 29)); - TEST_ASSERT_EQUAL(10, target.valueFor(14, 44)); - TEST_ASSERT_EQUAL(10, target.valueFor(14, 59)); -} - int main(int argc, char **argv) { UNITY_BEGIN(); @@ -145,12 +101,8 @@ int main(int argc, char **argv) RUN_TEST(test_update_single_bool_value); RUN_TEST(test_does_not_update_invalid_hour); RUN_TEST(test_does_not_update_invalid_quarter_flag); - RUN_TEST(test_updates_only_specified_first_quarter); - RUN_TEST(test_updates_only_specified_second_quarter); - RUN_TEST(test_updates_only_specified_third_quarter); - RUN_TEST(test_updates_only_specified_last_quarter); RUN_TEST(test_updates_only_specified_first_half); - RUN_TEST(test_updates_only_specified_last_half); + RUN_TEST(test_updates_only_specified_second_half); UNITY_END(); return 0;