Skip to content

Commit

Permalink
Simplify schedules (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewturner authored Dec 30, 2022
1 parent e7f60b7 commit 74f728b
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 154 deletions.
13 changes: 2 additions & 11 deletions lib/Display/Display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,28 +69,19 @@ 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);
}
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);
}
Expand Down
6 changes: 4 additions & 2 deletions lib/Display/Display.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 2 additions & 6 deletions lib/Schedule/Schedule.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
93 changes: 38 additions & 55 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ void setup()
setupRealtimeClock();
setupColorSchedule();
setupDisplaySchedule();
setupBrightnessSchedule();
setupMinimalModeSchedule();
setupTimezones();
setupTest();

Expand All @@ -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);
Expand All @@ -52,62 +50,62 @@ 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;
}

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;
}

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)
{
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))
{
Expand All @@ -117,24 +115,34 @@ 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)
{
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();

Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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__)));
}
}

Expand Down
11 changes: 5 additions & 6 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ 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;
RTC_DS3231 clock;
EvtManager mgr;
Timezone *timezone;
Schedule<CRGB::HTMLColorCode> colorSchedule(CRGB::Red);
Schedule<bool> displaySchedule(false, 1);
Schedule<bool> separatorSchedule(false);
Schedule<byte> brightnessSchedule(5);
Schedule<Flags> minimalSchedule(Flags::NONE);
Schedule<Flags> displaySchedule(Flags::NONE, 1);
SoftwareSerial bluetoothSerial(RECEIVE_PIN, TRANSMIT_PIN);
EvtCommandListener commandListener(&bluetoothSerial, 20);
EvtStateMachineListener stateMachine;
Expand All @@ -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();

Expand Down
29 changes: 6 additions & 23 deletions test/test_display/test_display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
}

Expand All @@ -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;
Expand Down
Loading

0 comments on commit 74f728b

Please sign in to comment.