Skip to content

Commit

Permalink
fixes and improvements for the 7-segment clock
Browse files Browse the repository at this point in the history
  • Loading branch information
NimmLor committed Nov 16, 2020
1 parent 3c009b9 commit 0c35ae8
Showing 1 changed file with 49 additions and 21 deletions.
70 changes: 49 additions & 21 deletions esp8266-fastled-iot-webserver.ino
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,10 @@ extern "C" {
#elif DEVICE_TYPE == 2 // 7-Segment Clock
#define NTP_REFRESH_INTERVAL_SECONDS 600 // 10 minutes
const char* ntpServerName = "at.pool.ntp.org"; // Austrian ntp-timeserver
int t_offset = 0; // offset added to the time from the ntp server
int t_offset = 1; // offset added to the time from the ntp server
bool updateColorsEverySecond = false; // if set to false it will update colors every minute (time patterns only)
const int NTP_PACKET_SIZE = 48;
bool switchedTimePattern = true;
#define NUM_LEDS 30
#define Digit1 0
#define Digit2 7
Expand Down Expand Up @@ -142,7 +144,7 @@ extern "C" {
//---------------------------------------------------------------------------------------------------------//
//#define ACCESS_POINT_MODE // the esp8266 will create a wifi-access point instead of connecting to one, credentials must be in Secrets.h

#define ENABLE_OTA_SUPPORT // requires ArduinoOTA - library, not working on esp's with 1MB memory (esp-01, Wemos D1 lite ...)
//#define ENABLE_OTA_SUPPORT // requires ArduinoOTA - library, not working on esp's with 1MB memory (esp-01, Wemos D1 lite ...)
//#define OTA_PASSWORD "passwd123" // password that is required to update the esp's firmware wireless

#define ENABLE_MULTICAST_DNS // allows to access the UI via "http://<HOSTNAME>.local/", implemented by GitHub/WarDrake
Expand Down Expand Up @@ -449,11 +451,10 @@ unsigned long autoPlayTimeout = 0;
uint8_t currentPaletteIndex = 0;

uint8_t gHue = 0; // rotating "base color" used by many of the patterns
uint8_t slowHue = 0; // slower gHue

CRGB solidColor = CRGB::Blue;

bool switchedTimePattern = false;

// scale the brightness of all pixels down
void dimAll(byte value)
{
Expand Down Expand Up @@ -1086,6 +1087,13 @@ void setup() {

Serial.println("HTTP web server started");

#if DEVICE_TYPE == 2
bool sucess = false;
while (!sucess) {
sucess = GetTime();
if (!sucess) delay(300);
}
#endif
// webSocketsServer.begin();
// webSocketsServer.onEvent(webSocketEvent);
// Serial.println("Web socket server started");
Expand Down Expand Up @@ -1249,6 +1257,7 @@ void loop() {
// slowly blend the current palette to the next
nblendPaletteTowardPalette(gCurrentPalette, gTargetPalette, 8);
gHue++; // slowly cycle the "base color" through the rainbow
if (gHue % 16 == 0)slowHue++;
}

if (autoplay && (millis() > autoPlayTimeout)) {
Expand Down Expand Up @@ -2312,6 +2321,17 @@ unsigned long sendNTPpacket(IPAddress& address)
udpTime.endPacket();
}

void PrintTime() {
if (hours < 10)Serial.print("0");
Serial.print(hours);
Serial.print(':');
if (mins < 10)Serial.print("0");
Serial.print(mins);
Serial.print(':');
if (secs < 10)Serial.print("0");
Serial.println(secs);
}


bool GetTime()
{
Expand Down Expand Up @@ -2346,23 +2366,15 @@ bool GetTime()

Serial.println("Requesting time");

if (hours < 10)Serial.print("0");
Serial.print(hours);
Serial.print(':');
if (mins < 10)Serial.print("0");
Serial.print(mins);
Serial.print(':');
if (secs < 10)Serial.print("0");
Serial.println(secs);
PrintTime();
return true;
}
}

bool shouldUpdateNTP()
{
if (switchedTimePattern || (millis() - ntp_timestamp) > (NTP_REFRESH_INTERVAL_SECONDS * 1000)) {
Serial.println(millis() - ntp_timestamp);
switchedTimePattern = true;
switchedTimePattern = false;
return true;
}
return false;
Expand All @@ -2378,7 +2390,7 @@ void DrawDots(int r, int g, int b, int hueMode)
{
for (int i = 2 * Digit2; i < Digit3; i++) {
if (hueMode != 0) {
int hue = map(i, 0, NUM_LEDS, 0, (int)((double)255 / (double)hueMode)) + gHue;
int hue = map(i, 0, NUM_LEDS, 0, (int)((double)255 / (double)hueMode)) + slowHue;
if (hue >= 255) hue -= 255;
leds[i] = CHSV(hue, 255,255);
}
Expand Down Expand Up @@ -2427,7 +2439,7 @@ void displayTimeRainbow()
}
if (fresh_update || shouldUpdateTime())
{
if (incrementTime() || fresh_update)
if (incrementTime() || fresh_update)
{
displayTime();
}
Expand All @@ -2450,6 +2462,11 @@ void displayTimeColorful()
DrawDots(x.r, x.g, x.b, 1);
}
}
else if (updateColorsEverySecond) {
CRGB x = CRGB(255, 0, 0);
DrawTime(x.r, x.g, x.b, 1);
DrawDots(x.r, x.g, x.b, 1);
}
}

void displayTimeGradient()
Expand All @@ -2468,6 +2485,11 @@ void displayTimeGradient()
DrawDots(x.r, x.g, x.b, 5);
}
}
else if(updateColorsEverySecond){
CRGB x = CRGB(255, 0, 0);
DrawTime(x.r, x.g, x.b, 5);
DrawDots(x.r, x.g, x.b, 5);
}
}

void displayTimeGradientLarge()
Expand All @@ -2486,27 +2508,33 @@ void displayTimeGradientLarge()
DrawDots(x.r, x.g, x.b, 3);
}
}
else if (updateColorsEverySecond) {
CRGB x = CRGB(255, 0, 0);
DrawTime(x.r, x.g, x.b, 3);
DrawDots(x.r, x.g, x.b, 3);
}
}

bool incrementTime()
{
bool retval = false;
secs++;
last_diff = millis() - update_timestamp - 1000;
update_timestamp = millis();
if (secs >= 60)
{
secs = 0;
secs -= 60;
mins++;
retval = true;
}
if (mins >= 60)
{
mins = 0;
mins -= 60;
hours++;
retval = true;
}
if (hours >= 24) hours = 0;
if (hours >= 24) hours -= 24;
PrintTime();
last_diff = millis() - update_timestamp - 1000;
return retval;
}

Expand All @@ -2531,7 +2559,7 @@ void dDHelper(int offset, int seg, int segmentLedCount, int hueMode, CRGB rgb =
for (int i = 0; i < segmentLedCount; i++)
{
int pos = offset + seg + i + seg * (segmentLedCount - 1);
int hue = map(pos, 0, NUM_LEDS, 0, (int)((double)255 / (double)hueMode)) + gHue;
int hue = map(pos, 0, NUM_LEDS, 0, (int)((double)255 / (double)hueMode)) + slowHue;
if (hue >= 255) hue -= 255;
CHSV col = CHSV(hue, 255, 255);
leds[pos] = col;
Expand Down

0 comments on commit 0c35ae8

Please sign in to comment.