led-ring-clock-ntp/led-ring-clock.ino

370 lines
11 KiB
Arduino
Raw Normal View History

2016-11-26 05:01:55 +01:00
//
// WS2812 LED Analog Clock Firmware
2018-09-09 04:56:52 +02:00
// Copyright (c) 2016-2018 jackw01
// NTP Changes: 2019 Commander1024
2016-11-26 05:01:55 +01:00
// This code is distrubuted under the MIT License, see LICENSE for details
//
2018-09-09 04:56:52 +02:00
#include <math.h>
2016-11-26 05:01:55 +01:00
#include <FastLED.h>
#include <EEPROM.h>
#include <TimeLib.h>
#include <NtpClientLib.h>
#include <SPI.h>
#include <EthernetUdp.h>
#include <Ethernet.h>
#include <Dns.h>
#include <Dhcp.h>
2016-11-26 05:01:55 +01:00
#include "constants.h"
2016-11-26 05:01:55 +01:00
// LED ring size
2017-09-29 02:06:10 +02:00
CRGB leds[ledRingSize];
2016-11-26 05:01:55 +01:00
2018-09-09 04:56:52 +02:00
// Globals to keep track of state
int clockMode, colorScheme;
2018-09-10 01:31:26 +02:00
uint32_t lastLoopTime = 0;
uint32_t lastButtonClickTime = 0;
uint32_t lastDebugMessageTime = 0;
2018-09-09 04:56:52 +02:00
uint8_t currentBrightness;
uint8_t previousBrightness[16];
int lastSecondsValue = 0;
2018-09-10 01:31:26 +02:00
uint32_t lastMillisecondsSetTime = 0;
int milliseconds;
2016-11-26 05:01:55 +01:00
void setup() {
2018-09-09 04:56:52 +02:00
// Begin serial port
Serial.begin(serialPortBaudRate);
2016-11-26 05:01:55 +01:00
// Initialize Network and NTP
if (Ethernet.begin (mac) == 0) {
Serial.println ("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for (;;)
;
}
NTP.onNTPSyncEvent ([](NTPSyncEvent_t error) {
if (error) {
Serial.print ("Time Sync error: ");
if (error == noResponse)
Serial.println ("NTP server not reachable");
else if (error == invalidAddress)
Serial.println ("Invalid NTP server address");
} else {
Serial.print ("Got NTP time: ");
Serial.println (NTP.getTimeDateString (NTP.getLastNTPSync ()));
}
});
NTP.setInterval (60, 900);
NTP.setNTPTimeout (1000);
NTP.begin (ntp_server, 1, true);
2016-11-26 05:01:55 +01:00
// Init FastLED
2017-09-29 02:06:10 +02:00
FastLED.addLeds<NEOPIXEL, pinLeds>(leds, ledRingSize);
2016-11-26 05:01:55 +01:00
FastLED.setTemperature(Halogen);
FastLED.show();
2018-09-09 04:56:52 +02:00
// Set button pin
pinMode(pinButton, INPUT_PULLUP);
2016-11-26 05:01:55 +01:00
2018-09-09 04:56:52 +02:00
// Read saved config from EEPROM
colorScheme = EEPROM.read(eepromAddrColorScheme);
clockMode = EEPROM.read(eepromAddrClockMode);
2017-09-26 23:16:01 +02:00
2018-09-09 04:56:52 +02:00
// If button is pressed at startup, light all LEDs
2017-09-26 23:16:01 +02:00
if (digitalRead(pinButton) == LOW) {
2017-09-29 02:06:10 +02:00
for (int i = 0; i < ledRingSize; i++) leds[i] = white;
2017-09-26 23:16:01 +02:00
FastLED.show();
delay(10000);
2017-09-26 23:16:01 +02:00
}
2016-11-26 05:01:55 +01:00
}
static int i = 0;
static int last = 0;
2016-11-26 05:01:55 +01:00
void loop() {
2018-09-10 01:31:26 +02:00
uint32_t currentTime = millis();
2018-09-09 04:56:52 +02:00
if (currentTime - lastLoopTime > runLoopIntervalMs) {
lastLoopTime = currentTime;
2018-09-09 04:56:52 +02:00
// Handle button
if (digitalRead(pinButton) == LOW && currentTime - lastButtonClickTime > buttonClickRepeatDelayMs) {
delay(buttonLongPressDelayMs);
2018-09-10 01:31:26 +02:00
// Long press: clock mode, short press: color scheme
2018-09-09 04:56:52 +02:00
if (digitalRead(pinButton) == LOW) {
lastButtonClickTime = currentTime;
colorScheme ++;
if (colorScheme >= colorSchemeCount + 2) colorScheme = 0; // 2 special color schemes
2018-09-10 01:31:26 +02:00
EEPROM.write(eepromAddrColorScheme, colorScheme);
2018-09-09 04:56:52 +02:00
} else {
clockMode ++;
if (clockMode >= ClockModeCount) clockMode = 0;
2018-09-10 01:31:26 +02:00
EEPROM.write(eepromAddrClockMode, clockMode);
2018-09-09 04:56:52 +02:00
}
}
// Print debug message
if (currentTime > lastDebugMessageTime + debugMessageIntervalMs) {
lastDebugMessageTime = currentTime;
printDebugMessage();
}
// Update brightness - do a moving average to smooth out noisy potentiometers
int sum = 0;
for (uint8_t i = 15; i > 0; i--) {
previousBrightness[i] = previousBrightness[i - 1];
sum += previousBrightness[i];
}
previousBrightness[0] = map(analogRead(pinBrightness), 0, 1023, minBrightness, 255);
sum += previousBrightness[0];
currentBrightness = sum / 16;
FastLED.setBrightness(currentBrightness);
// Get time and calculate milliseconds value that is synced with the RTC's second count
int currentSeconds = second(now());
if (currentSeconds != lastSecondsValue) {
lastSecondsValue = currentSeconds;
milliseconds = 0;
}
currentTime = millis();
milliseconds = (milliseconds + currentTime - lastMillisecondsSetTime);
lastMillisecondsSetTime = currentTime;
// Show clock
2018-09-09 04:56:52 +02:00
clearLeds();
showClock();
// Check/Renew DHCP
Ethernet.maintain ();
2018-09-09 04:56:52 +02:00
}
2016-11-26 05:01:55 +01:00
}
2018-09-09 04:56:52 +02:00
// Display the current clock
2016-11-26 05:01:55 +01:00
void showClock() {
2018-09-09 04:56:52 +02:00
switch (clockMode) {
case ClockModeRingClock:
drawRingClock();
2018-09-09 04:56:52 +02:00
break;
case ClockModeDotClock:
drawDotClock();
2018-09-09 04:56:52 +02:00
break;
case ClockModeDotClockTrail:
drawDotClockTrail();
2018-09-09 04:56:52 +02:00
break;
2018-09-10 01:31:26 +02:00
case ClockModeDotClockGlow:
drawDotClockGlow();
2018-09-09 04:56:52 +02:00
break;
}
2016-11-26 05:01:55 +01:00
}
2018-09-09 04:56:52 +02:00
// Print debugging info over serial
2017-09-26 23:16:01 +02:00
void printDebugMessage() {
Serial.print("Current date/time: ");
Serial.print(year(now()), DEC);
2017-09-26 23:16:01 +02:00
Serial.print("/");
Serial.print(month(now()), DEC);
2017-09-26 23:16:01 +02:00
Serial.print("/");
Serial.print(day(now()), DEC);
2017-09-26 23:16:01 +02:00
Serial.print(" ");
Serial.print(hour(now()), DEC);
2017-09-26 23:16:01 +02:00
Serial.print(":");
Serial.print(minute(now()), DEC);
2017-09-26 23:16:01 +02:00
Serial.print(":");
Serial.print(second(now()), DEC);
2017-09-26 23:16:01 +02:00
Serial.println();
Serial.print("Display mode: ");
Serial.println(clockMode);
Serial.print("Color scheme: ");
Serial.println(colorScheme);
Serial.print("Brightness: ");
2017-09-29 02:06:10 +02:00
Serial.println(currentBrightness);
2018-09-09 04:56:52 +02:00
Serial.println("");
2017-09-26 23:16:01 +02:00
}
2016-11-26 05:01:55 +01:00
// Show a ring clock
void drawRingClock() {
int h = hourPosition();
2018-09-09 04:56:52 +02:00
int m = minutePosition();
2018-09-10 01:31:26 +02:00
float s = secondPosition();
2016-11-26 05:01:55 +01:00
2018-09-09 04:56:52 +02:00
if (m > h) {
for (int i = 0; i < m; i++) setLed(i, minuteColor(), BlendModeOver, 1.0);
for (int i = 0; i < h; i++) setLed(i, hourColor(), BlendModeOver, 1.0);
2016-11-26 05:01:55 +01:00
} else {
for (int i = 0; i < h; i++) setLed(i, hourColor(), BlendModeOver, 1.0);
for (int i = 0; i < m; i++) setLed(i, minuteColor(), BlendModeOver, 1.0);
2016-11-26 05:01:55 +01:00
}
if (showSecondHand) setLed(s, secondColor(), BlendModeAlpha, 1.0);
2016-11-26 05:01:55 +01:00
FastLED.show();
}
// Show a more traditional dot clock
void drawDotClock() {
2018-09-10 01:31:26 +02:00
float h = hourPosition();
float m = minutePosition();
float s = secondPosition();
2016-11-26 05:01:55 +01:00
for (float i = h - 1.0; i < h + 2.0; i++) setLed(i, hourColor(), BlendModeAlpha, 1.0);
setLed(m, minuteColor(), BlendModeAlpha, 1.0);
if (showSecondHand) setLed(s, secondColor(), BlendModeAlpha, 1.0);
2016-11-26 05:01:55 +01:00
FastLED.show();
}
2018-09-10 01:31:26 +02:00
// Show a dot clock where the hands have a glowing trail behing them
void drawDotClockTrail() {
2018-09-10 01:31:26 +02:00
float h = hourPosition();
float m = minutePosition();
float s = secondPosition();
2016-11-26 05:01:55 +01:00
2018-09-10 04:12:13 +02:00
for (float i = -hourTrailLength; i < 1.0; i++) setLed(h + i, hourColor(), BlendModeAdd, mapFloat(i, -hourTrailLength, 1.0, 0.1, 1.0));
for (float i = -minuteTrailLength; i < 1.0; i++) setLed(m + i, minuteColor(), BlendModeAdd, mapFloat(i, -minuteTrailLength, 1.0, 0.1, 1.0));
if (showSecondHand) {
for (float i = -secondTrailLength; i < 1.0; i++) setLed(s + i, secondColor(), BlendModeAdd, mapFloat(i, -secondTrailLength, 1.0, 0.1, 1.0));
}
2016-11-26 05:01:55 +01:00
FastLED.show();
}
2018-09-10 01:31:26 +02:00
// Show a dot clock where the hands glow outwards from their position
void drawDotClockGlow() {
float h = hourPosition();
float m = minutePosition();
float s = secondPosition();
2016-11-26 05:01:55 +01:00
2018-09-10 01:31:26 +02:00
for (float i = h - hourGlowWidth; i <= h + hourGlowWidth; i++) {
setLed(i, hourColor(), BlendModeAdd, mapFloat(fabs(h - i), 0.0, hourGlowWidth, 1.0, 0.1));
}
for (float i = m - minuteGlowWidth; i <= m + minuteGlowWidth; i++) {
setLed(i, minuteColor(), BlendModeAdd, mapFloat(fabs(m - i), 0.0, minuteGlowWidth, 1.0, 0.1));
}
if (showSecondHand) {
for (float i = s - secondGlowWidth; i <= s + secondGlowWidth; i++) {
setLed(i, secondColor(), BlendModeAdd, mapFloat(fabs(s - i), 0.0, secondGlowWidth, 1.0, 0.1));
}
}
2016-11-26 05:01:55 +01:00
FastLED.show();
}
// Get floating point hour representation
float floatHour() {
return (float)hour(now()) + mapFloat(minute(now()) + mapFloat(second(now()), 0.0, 59.0, 0.0, 1.0), 0.0, 59.0, 0.0, 1.0);
}
2018-09-09 04:56:52 +02:00
// Get positions mapped to ring size
2018-09-10 01:31:26 +02:00
float hourPosition() {
if (twelveHour) {
int hourt;
if (hour(now()) > 12) hourt = (hour(now()) - 12) * (ledRingSize / 12);
else hourt = hour(now()) * (ledRingSize / 12);
return hourt + mapFloat(minute(now()) + 0.001, 0.0, 59.0, 0.0, (ledRingSize / 12.0) - 1.0);
} else {
int hourt = hour(now()) * (ledRingSize / 24);
return hourt + mapFloat(minute(now()) + 0.001, 0, 59, 0, (ledRingSize / 24.0) - 1.0);
}
}
2018-09-10 01:31:26 +02:00
float minutePosition() {
return mapFloat(
(float)minute(now()) + ((0.001 + 1.0 / 60.0) * (float)second(now())), 0.0, 59.0, 0.0, (float)ledRingSize
);
}
2018-09-10 01:31:26 +02:00
float secondPosition() {
return mapFloat(
second(now()) + (0.001 * milliseconds), 0.0, 60.0, 0.0, (float)ledRingSize
);
}
2018-09-09 04:56:52 +02:00
// Get colors
CRGB hourColor() {
if (colorScheme < colorSchemeCount) return colorSchemes[colorScheme][0];
else if (colorScheme == colorSchemeCount + 0) {
return CHSV(map(hour(now()), 0, 24, 0, 255), 255, 255);
} else if (colorScheme == colorSchemeCount + 1) {
return CHSV((uint8_t)mapFloat(fmod(20.0 - floatHour(), 24.0), 0.0, 24.0, 0.0, 255.0), 255, 255);
}
2018-09-09 04:56:52 +02:00
}
2017-03-26 23:16:25 +02:00
2018-09-09 04:56:52 +02:00
CRGB minuteColor() {
if (colorScheme < colorSchemeCount) return colorSchemes[colorScheme][1];
else if (colorScheme == colorSchemeCount + 0) {
return CHSV(map(minute(now()), 0, 59, 0, 255), 255, 255);
} else if (colorScheme == colorSchemeCount + 1) {
return CHSV((uint8_t)mapFloat(fmod(20.0 - floatHour(), 24.0), 0.0, 24.0, 0.0, 255.0), 255, 255);
}
2018-09-09 04:56:52 +02:00
}
2017-03-26 23:16:25 +02:00
2018-09-09 04:56:52 +02:00
CRGB secondColor() {
if (colorScheme < colorSchemeCount) return colorSchemes[colorScheme][2];
else if (colorScheme == colorSchemeCount + 0) {
return CHSV(map(second(now()), 0, 59, 0, 255), 255, 255);
} else if (colorScheme == colorSchemeCount + 1) {
return CHSV((uint8_t)mapFloat(fmod(20.0 - floatHour(), 24.0), 0.0, 24.0, 0.0, 255.0), 255, 255);
}
2017-03-26 23:16:25 +02:00
}
2018-09-09 04:56:52 +02:00
// Clear the LED ring
2016-11-26 05:01:55 +01:00
void clearLeds() {
2018-09-10 01:31:26 +02:00
for (int i = 0; i < ledRingSize; i++) leds[i] = CRGB(0, 0, 0);
2016-11-26 05:01:55 +01:00
}
// Set LED(s) at a position with enhanced rendering
void setLed(float position, CRGB color, BlendMode blendMode, float factor) {
if (useEnhancedRenderer) {
int low = floor(position);
int high = ceil(position);
float lowFactor = ((float)high - position);
float highFactor = (position - (float)low);
if (blendMode == BlendModeAdd) {
blendAdd(wrap(low), color, lowFactor * factor);
blendAdd(wrap(high), color, highFactor * factor);
} else if (blendMode == BlendModeAlpha) {
blendAlpha(wrap(low), color, lowFactor * factor);
blendAlpha(wrap(high), color, highFactor * factor);
} else if (blendMode == BlendModeOver) {
blendOver(wrap(low), color, lowFactor * factor);
blendOver(wrap(high), color, highFactor * factor);
}
} else {
leds[wrap((int)position)] = color;
}
}
// Additive blending
2018-09-09 04:56:52 +02:00
void blendAdd(int position, CRGB color, float factor) {
leds[position].r += min(color.r * factor, 255 - leds[position].r);
leds[position].g += min(color.g * factor, 255 - leds[position].g);
leds[position].b += min(color.b * factor, 255 - leds[position].b);
}
// Alpha blending (factor is the alpha value)
void blendAlpha(int position, CRGB color, float factor) {
leds[position].r = (uint8_t)mapFloat(factor, 0.0, 1.0, leds[position].r, color.r);
leds[position].g = (uint8_t)mapFloat(factor, 0.0, 1.0, leds[position].g, color.g);
leds[position].b = (uint8_t)mapFloat(factor, 0.0, 1.0, leds[position].b, color.b);
}
// Overlay/replace blending
void blendOver(int position, CRGB color, float factor) {
leds[position].r = color.r * factor;
leds[position].g = color.g * factor;
leds[position].b = color.b * factor;
leds[position] = color;
2016-11-26 05:01:55 +01:00
}
// Wrap around LED ring
int wrap(int i) {
2017-09-29 02:06:10 +02:00
if (i >= ledRingSize) return i - ledRingSize;
else if (i < 0) return ledRingSize + i;
2016-11-26 05:01:55 +01:00
else return i;
}
// Because Arduino does not
2018-09-09 04:56:52 +02:00
float mapFloat(float x, float inMin, float inMax, float outMin, float outMax) {
return (x - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
2016-11-26 05:01:55 +01:00
}