Skip to content

Commit

Permalink
Improved display (#5)
Browse files Browse the repository at this point in the history
* Show only during daylight

* Create schedule class

* Fix schedule

* Remove hide/show
  • Loading branch information
matthewturner authored Sep 5, 2021
1 parent 2d68db6 commit ecbce45
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 61 deletions.
2 changes: 0 additions & 2 deletions lib/Display/Display.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ class Display
void setLedRange(byte first, byte last, bool show);
void clear();
void setAll();
void hide();
void show();
void setSeparator(bool show);
bool updateFrom(Display *other);

Expand Down
1 change: 1 addition & 0 deletions lib/Schedule/Schedule.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "Schedule.h"
44 changes: 44 additions & 0 deletions lib/Schedule/Schedule.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifndef Schedule_h
#define Schedule_h

#include "common.h"

const byte HOURS_IN_DAY = 24;

template <class T>
class Schedule
{
public:
Schedule(T defaultValue)
{
_defaultValue = defaultValue;
reset();
}

void reset()
{
for (byte i = 0; i < HOURS_IN_DAY; i++)
{
_valuesForHour[i] = _defaultValue;
}
}

T valueFor(byte hour)
{
return _valuesForHour[hour];
}

void setup(byte min, byte max, T value)
{
for (byte i = min; i <= max; i++)
{
_valuesForHour[i] = value;
}
}

private:
T _defaultValue;
T _valuesForHour[HOURS_IN_DAY];
};

#endif
106 changes: 57 additions & 49 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,17 @@ void setup()
Serial.begin(9600);

FastLED.addLeds<APA102, DATA_PIN, CLOCK_PIN, BGR>(physicalLeds, NUM_LEDS);
FastLED.setBrightness(10);
FastLED.setBrightness(5);

while (!Serial)
;

if (!clock.begin())
{
Serial.println("Couldn't find Realtime Clock");
Serial.flush();
abort();
}
setupRealtimeClock();
setupColorCodes();
setupDisplayHours();
setupTest();

if (clock.lostPower())
{
Serial.println("Realtime Clock lost power, setting the time...");
clock.adjust(DateTime(F(__DATE__), F(__TIME__)));
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}

Serial.println("Populating colors...");
populateColorCodes();

Serial.println("Testing display 88...");
display.setPart(0, 88, false);
show();
delay(200);
display.clear();
display.setSeparator(true);
show();
delay(200);
display.clear();
display.setPart(1, 88, false);
show();
delay(200);
display.clear();

mgr.addListener(new EvtTimeListener(500, true, (EvtAction)showTime));
mgr.addListener(new EvtTimeListener(500, true, (EvtAction)update));

Serial.println("Setup complete. Continuing...");
}
Expand All @@ -51,21 +25,28 @@ void loop()
mgr.loopIteration();
}

bool showTime()
bool update()
{
now = clock.now();

if (!displaySchedule.valueFor(now.hour()))
{
display.clear();
render();
return false;
}

display.setPart(1, now.hour(), false);
display.setPart(0, now.minute(), true);
display.setSeparator(true);
show();
render();

return false;
}

void show()
void render()
{
CRGB::HTMLColorCode colorCode = determineColorCode();
CRGB::HTMLColorCode colorCode = colorSchedule.valueFor(now.hour());

for (byte i = 0; i < NUM_LEDS; i++)
{
Expand All @@ -82,26 +63,53 @@ void show()
FastLED.show();
}

CRGB::HTMLColorCode determineColorCode()
void setupColorCodes()
{
byte hour = now.hour();
return colorCodesForHour[hour];
Serial.println("Populating colors...");
// daylight
colorSchedule.setup(8, 20, CRGB::Blue);
// morning
colorSchedule.setup(6, 7, CRGB::Orange);
// evening
colorSchedule.setup(21, 21, CRGB::Purple);
}

void setupDisplayHours()
{
Serial.println("Setting up display hours...");
displaySchedule.setup(6, 21, true);
}

void populateColorCodes()
void setupRealtimeClock()
{
// default night to red
for (byte i = 0; i < HOURS_IN_DAY; i++)
if (!clock.begin())
{
colorCodesForHour[i] = CRGB::Red;
Serial.println("Couldn't find Realtime Clock");
Serial.flush();
abort();
}

// daylight
for (byte i = 8; i <= 20; i++)
if (clock.lostPower())
{
colorCodesForHour[i] = CRGB::Blue;
Serial.println("Realtime Clock lost power, setting the time...");
clock.adjust(DateTime(F(__DATE__), F(__TIME__)));
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
// morning
colorCodesForHour[6] = CRGB::Orange;
colorCodesForHour[7] = CRGB::Green;
}

void setupTest()
{
Serial.println("Testing display 88...");
display.setPart(0, 88, false);
render();
delay(200);
display.clear();
display.setSeparator(true);
render();
delay(200);
display.clear();
display.setPart(1, 88, false);
render();
delay(200);
display.clear();
}
21 changes: 11 additions & 10 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,27 @@
#include <Arduino.h>
#include <FastLED.h>
#include "Display.h"
#include "Schedule.h"
#include <RTClib.h>
#include <Eventually.h>

const byte DATA_PIN = 3;
const byte CLOCK_PIN = 13;

const byte HOUR = 0;
const byte MINUTE = 1;
const byte NOTHING = 2;
const byte HOURS_IN_DAY = 24;

CRGB physicalLeds[NUM_LEDS];
Display display;
RTC_DS3231 clock;
EvtManager mgr;
DateTime now;
Schedule<CRGB::HTMLColorCode> colorSchedule(CRGB::Red);
Schedule<bool> displaySchedule(false);

void render();
bool update();

void setupColorCodes();
void setupDisplayHours();
void setupRealtimeClock();
void setupTest();

void show();
bool showTime();
CRGB::HTMLColorCode determineColorCode();
void populateColorCodes();
CRGB::HTMLColorCode colorCodesForHour[HOURS_IN_DAY];
#endif
43 changes: 43 additions & 0 deletions test/test_schedule/test_schedule.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <unity.h>
#include "Schedule.h"

Schedule<byte> target(10);

void setUp(void)
{
target.reset();
}

void test_initializes(void)
{
for (byte i = 0; i < HOURS_IN_DAY; i++)
{
TEST_ASSERT_EQUAL(10, target.valueFor(i));
}
}

void test_setup_range(void)
{
target.setup(5, 6, 12);
for (byte i = 0; i < HOURS_IN_DAY; i++)
{
if (i == 5 || i == 6)
{
TEST_ASSERT_EQUAL(12, target.valueFor(i));
}
else
{
TEST_ASSERT_EQUAL(10, target.valueFor(i));
}
}
}

int main(int argc, char **argv)
{
UNITY_BEGIN();
RUN_TEST(test_initializes);
RUN_TEST(test_setup_range);
UNITY_END();

return 0;
}

0 comments on commit ecbce45

Please sign in to comment.