maybe a better countdown algorithm?

This commit is contained in:
Neale Pickett 2020-12-19 16:46:43 -07:00
parent 13bcd0a98c
commit e702145635
4 changed files with 32 additions and 18 deletions

View File

@ -1,4 +1,5 @@
FQBN = adafruit:samd:adafruit_trinket_m0 #FQBN = adafruit:samd:adafruit_trinket_m0
FQBN = adafruit:avr:protrinket5
install: holiday-lights.ino install: holiday-lights.ino
arduino --upload --board $(FQBN) $@ arduino --upload --board $(FQBN) $@

View File

@ -61,7 +61,7 @@
// I'm guessing 10 minutes a day. // I'm guessing 10 minutes a day.
#define SNOSSLOSS_DAY (DURATION_DAY - (10 * DURATION_MINUTE)) #define SNOSSLOSS_DAY (DURATION_DAY - (10 * DURATION_MINUTE))
#define ON_FOR (5 * DURATION_HOUR) #define ON_FOR (6 * DURATION_HOUR)
#define ARK "\x03\x04" #define ARK "\x03\x04"
const char *message = ( const char *message = (
@ -102,8 +102,8 @@ uint8_t RandomHue() {
Pulse mainPulse = Pulse(DELAY_MS); Pulse mainPulse = Pulse(DELAY_MS);
bool strandUpdate(bool white) { bool strandUpdate(unsigned long now, bool white) {
if (!mainPulse.Ticked()) { if (!mainPulse.Ticked(now)) {
return false; return false;
} }
@ -130,8 +130,8 @@ bool strandUpdate(bool white) {
return true; return true;
} }
bool black() { bool black(unsigned long now) {
if (!mainPulse.Ticked()) { if (!mainPulse.Ticked(now)) {
return false; return false;
} }
@ -140,12 +140,12 @@ bool black() {
return true; return true;
} }
bool morseUpdate() { bool morseUpdate(unsigned long now) {
static MorseEncoder enc; static MorseEncoder enc;
static Pulse pulse = Pulse(DIT_DURATION_MS); static Pulse pulse = Pulse(DIT_DURATION_MS);
bool ret = false; bool ret = false;
if (pulse.Ticked()) { if (pulse.Ticked(now)) {
if (!enc.Tick()) { if (!enc.Tick()) {
enc.SetText(message); enc.SetText(message);
} }
@ -156,10 +156,18 @@ bool morseUpdate() {
return ret; return ret;
} }
bool millisUpdate() { bool timeUpdate(unsigned long now, unsigned long timeLeft) {
unsigned long now = millis(); unsigned int hoursLeft = timeLeft / DURATION_HOUR;
for (int i = 0; i < sizeof(unsigned long) * 8; ++i) { unsigned int minutesLeft = (timeLeft / DURATION_MINUTE) % 60;
leds[i] = CHSV(0, 255, bitRead(now, i) ? 32 : 0);
int i;
for (i = 0; i < hoursLeft; ++i) {
leds[i] = CHSV(0, 255, 32);
}
if ((timeLeft / DELAY_MS) % minutesLeft == 0) {
leds[i]= CHSV(0, 255, 32);
} else {
leds[i] = CRGB::Black;
} }
return false; return false;
} }
@ -167,16 +175,18 @@ bool millisUpdate() {
void loop() { void loop() {
bool white = false; bool white = false;
bool update = false; bool update = false;
unsigned long now = millis(); // Everybody uses the same time so we don't do spurious updates 5ms apart
unsigned long timeOfDay = now % DURATION_DAY;
button.read(); button.read();
white = (button.count() % 2 == 1); white = (button.count() % 2 == 1);
if (FOREVER || white || (millis() % SNOSSLOSS_DAY < ON_FOR)) { if (FOREVER || white || (timeOfDay < ON_FOR)) {
update |= strandUpdate(white); update |= strandUpdate(now, white);
update |= morseUpdate(); update |= morseUpdate(now);
} else { } else {
update |= black(); update |= black(now);
update |= millisUpdate(); update |= timeUpdate(now, DURATION_DAY - timeOfDay);
} }
if (update) { if (update) {

View File

@ -8,8 +8,10 @@ Pulse::Pulse(unsigned long period) {
} }
bool Pulse::Ticked() { bool Pulse::Ticked() {
unsigned long now = millis(); return Ticked(millis());
}
bool Pulse::Ticked(unsigned long now) {
if (now >= nextEventMillis) { if (now >= nextEventMillis) {
Until(period, now); Until(period, now);
return true; return true;

View File

@ -7,6 +7,7 @@ public:
/** Ticked tells you if a period has elapsed. */ /** Ticked tells you if a period has elapsed. */
bool Ticked(); bool Ticked();
bool Ticked(unsigned long now);
/** Until sets the duration of the next period. */ /** Until sets the duration of the next period. */
void Until(unsigned long next); void Until(unsigned long next);