Compare commits

...

3 Commits

Author SHA1 Message Date
Dirk Jahnke b773437d45 Changed default clock mode to Realclock 2019-06-04 21:43:48 +02:00
Dirk Jahnke 22df009807 Added color Yellow. 2019-06-04 21:43:09 +02:00
Dirk Jahnke 07984234ab Added NTP client to get real time.
Added support for 3 clock modes: demo / real / fast
2019-06-04 21:33:13 +02:00
4 changed files with 86 additions and 19 deletions

View File

@ -17,4 +17,5 @@ upload_port = /dev/cu.wchusbserial1420
[lib_deps] [lib_deps]
library = library =
WifiManager, WifiManager,
Adafruit NeoPixel Adafruit NeoPixel,
NTPClient

View File

@ -193,10 +193,12 @@ void SevenSegmentClock::displaySeperator(char seperatorCharacter) {
void SevenSegmentClock::displayTime(int hour, int minute) { void SevenSegmentClock::displayTime(int hour, int minute) {
clockHour = hour; if (clockHour != hour || clockMinute != minute) {
clockMinute = minute; clockHour = hour;
Serial.print("SevenSegmentClock: new time "); clockMinute = minute;
Serial.print(clockHour); Serial.print(":"); Serial.println(clockMinute); Serial.print("SevenSegmentClock: new time ");
Serial.print(clockHour); Serial.print(":"); Serial.println(clockMinute);
}
displayUpdate(); displayUpdate();
}; };
@ -281,7 +283,7 @@ void SevenSegmentClock::displayUpdate(void) {
} }
} }
uint32_t SevenSegmentClock::red, SevenSegmentClock::green, SevenSegmentClock::blue, SevenSegmentClock::white, SevenSegmentClock::black; uint32_t SevenSegmentClock::red, SevenSegmentClock::green, SevenSegmentClock::blue, SevenSegmentClock::white, SevenSegmentClock::black, SevenSegmentClock::yellow;
uint8_t SevenSegmentClock::LedDataPin; uint8_t SevenSegmentClock::LedDataPin;
Adafruit_NeoPixel *SevenSegmentClock::strip; Adafruit_NeoPixel *SevenSegmentClock::strip;
@ -291,6 +293,7 @@ void SevenSegmentClock::initColors(uint8_t _brightness) {
SevenSegmentClock::blue = strip->Color(0, 0, _brightness); SevenSegmentClock::blue = strip->Color(0, 0, _brightness);
SevenSegmentClock::white = strip->Color(_brightness, _brightness, _brightness); SevenSegmentClock::white = strip->Color(_brightness, _brightness, _brightness);
SevenSegmentClock::black = strip->Color(0, 0, 0); SevenSegmentClock::black = strip->Color(0, 0, 0);
SevenSegmentClock::yellow = strip->Color(_brightness, _brightness, 0);
SevenSegmentClock::setColor(SevenSegmentClock::getColor()); // reset color to enforce reclaculation SevenSegmentClock::setColor(SevenSegmentClock::getColor()); // reset color to enforce reclaculation
} }
@ -302,6 +305,7 @@ void SevenSegmentClock::setColor(Color color) {
case Red: currentColor = SevenSegmentClock::red; break; case Red: currentColor = SevenSegmentClock::red; break;
case Green: currentColor = SevenSegmentClock::green; break; case Green: currentColor = SevenSegmentClock::green; break;
case White: currentColor = SevenSegmentClock::white; break; case White: currentColor = SevenSegmentClock::white; break;
case Yellow: currentColor = SevenSegmentClock::yellow; break;
} }
} }

View File

@ -19,10 +19,9 @@ public:
enum BlinkMode { NoBlinking, ClockBlinking, SeperatorBlinking, DecimalPointBlinking }; enum BlinkMode { NoBlinking, ClockBlinking, SeperatorBlinking, DecimalPointBlinking };
void setBlinkMode(BlinkMode _blinkMode) { blinkMode = _blinkMode; }; void setBlinkMode(BlinkMode _blinkMode) { blinkMode = _blinkMode; };
void setClockHalted(bool halted) { clockHalted = halted; }; void setClockHalted(bool halted) { clockHalted = halted; };
enum Color { Black, Red, Green, Blue, White }; enum Color { Black, Red, Green, Blue, White, Yellow };
void setColor(Color color); void setColor(Color color);
Color getColor(void) { return currentColorHandle; }; Color getColor(void) { return currentColorHandle; };
static uint32_t red, green, blue, white, black;
enum ClockDisplayStatus { Off, Booting, Halted, StandardClock, FastClock }; enum ClockDisplayStatus { Off, Booting, Halted, StandardClock, FastClock };
void displayDigit(unsigned int digitNum, char c); void displayDigit(unsigned int digitNum, char c);
void displaySeperator(char seperatorCharacter); void displaySeperator(char seperatorCharacter);
@ -34,6 +33,7 @@ private:
static Adafruit_NeoPixel *strip; static Adafruit_NeoPixel *strip;
static BlinkMode blinkMode; static BlinkMode blinkMode;
static uint8_t brightness; static uint8_t brightness;
static uint32_t red, green, blue, white, black, yellow;
ClockDisplayStatus displayStatus; ClockDisplayStatus displayStatus;
int clockHour; int clockHour;
int clockMinute; int clockMinute;

View File

@ -1,6 +1,8 @@
#include <Arduino.h> #include <Arduino.h>
#include <FS.h> //this needs to be first, or it all crashes and burns... #include <FS.h> //this needs to be first, or it all crashes and burns...
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino #include <ESP8266WiFi.h>
#include <NTPClient.h> //https://github.com/esp8266/Arduino
#include <WiFiUdp.h>
//needed for library //needed for library
#include <DNSServer.h> #include <DNSServer.h>
@ -11,6 +13,19 @@
#include <SPI.h> #include <SPI.h>
#include "SevenSegmentClock.h" #include "SevenSegmentClock.h"
// NTP
WiFiUDP ntpUDP;
// You can specify the time server pool and the offset (in seconds, can be
// changed later with setTimeOffset() ). Additionaly you can specify the
// update interval (in milliseconds, can be changed using setUpdateInterval() ).
NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 3600, 60000);
#define MODE_DEMO 1
#define MODE_REALCLOCK 2
#define MODE_FASTCLOCK 3
static int appMode = MODE_REAL;
static const char *appName = "FastclockClient7Seg"; static const char *appName = "FastclockClient7Seg";
@ -38,7 +53,8 @@ static struct ColorSelection {
{ 2, SevenSegmentClock::Blue, "blue" }, { 2, SevenSegmentClock::Blue, "blue" },
{ 3, SevenSegmentClock::Red, "red" }, { 3, SevenSegmentClock::Red, "red" },
{ 4, SevenSegmentClock::Green, "green" }, { 4, SevenSegmentClock::Green, "green" },
{ 5, SevenSegmentClock::White, "white" } { 5, SevenSegmentClock::White, "white" },
{ 6, SevenSegmentClock::Yellow, "yellow" }
}; };
static const String getColorName(uint8_t color) { static const String getColorName(uint8_t color) {
@ -174,13 +190,19 @@ const char _SCRIPT[] PROGMEM = "<script>function c(l){document.getEleme
const char _HEAD_END[] PROGMEM = "</head><body><div style='text-align:left;display:inline-block;min-width:260px;'>"; const char _HEAD_END[] PROGMEM = "</head><body><div style='text-align:left;display:inline-block;min-width:260px;'>";
const char _PORTAL_OPTIONS[] PROGMEM = "<form action=\"/wifi\" method=\"get\"><button>Configure WiFi</button></form><br/><form action=\"/0wifi\" method=\"get\"><button>Configure WiFi (No Scan)</button></form><br/><form action=\"/i\" method=\"get\"><button>Info</button></form><br/><form action=\"/r\" method=\"post\"><button>Reset</button></form>"; const char _PORTAL_OPTIONS[] PROGMEM = "<form action=\"/wifi\" method=\"get\"><button>Configure WiFi</button></form><br/><form action=\"/0wifi\" method=\"get\"><button>Configure WiFi (No Scan)</button></form><br/><form action=\"/i\" method=\"get\"><button>Info</button></form><br/><form action=\"/r\" method=\"post\"><button>Reset</button></form>";
const char _ITEM[] PROGMEM = "<div><a href='#p' onclick='c(this)'>{v}</a>&nbsp;<span class='q {i}'>{r}%</span></div>"; const char _ITEM[] PROGMEM = "<div><a href='#p' onclick='c(this)'>{v}</a>&nbsp;<span class='q {i}'>{r}%</span></div>";
const char _FORM_START[] PROGMEM = "<form method='get' action='configsave'><label for='n'>Fastclock name</label><input id='n' name='n' length=32 placeholder='clock name'><br/>"; const char _FORM_START[] PROGMEM = "<form method='get' action='configsave'>";
const char _FORM_CLOCKNAME[] PROGMEM = "<label for='n'>Fastclock name</label><input id='n' name='n' length=32 placeholder='clock name'><br/>";
const char _FORM_CLOCKMODE_HEADLINE[] PROGMEM = "<br/>Clock mode:<br/>";
const char _FORM_CLOCKMODE_DEMO[] PROGMEM = "<input class='r' id='md' name='m' type='radio' value='demo' {check}><label for='md'>Demo</label><br/>";
const char _FORM_CLOCKMODE_REAL[] PROGMEM = "<input class='r' id='mr' name='m' type='radio' value='real' {check}><label for='md'>Real Clock</label><br/>";
const char _FORM_CLOCKMODE_FAST[] PROGMEM = "<input class='r' id='mf' name='m' type='radio' value='fast' {check}><label for='md'>Fast Clock</label><br/>";
const char _FORM_PARAM[] PROGMEM = "<br/><input id='{i}' name='{n}' maxlength={l} placeholder='{p}' value='{v}' {c}>"; const char _FORM_PARAM[] PROGMEM = "<br/><input id='{i}' name='{n}' maxlength={l} placeholder='{p}' value='{v}' {c}>";
const char _FORM_COLOR_HEADLINE[] PROGMEM = "<br/>Display color:<br/>"; const char _FORM_COLOR_HEADLINE[] PROGMEM = "<br/>Display color:<br/>";
const char _FORM_COLOR_BLUE[] PROGMEM = "<input class='r' id='cb' name='c' type='radio' value='blue' {check}><label for='cb'>Blue</label><br/>"; const char _FORM_COLOR_BLUE[] PROGMEM = "<input class='r' id='cb' name='c' type='radio' value='blue' {check}><label for='cb'>Blue</label><br/>";
const char _FORM_COLOR_RED[] PROGMEM = "<input class='r' id='cr' name='c' type='radio' value='red' {check}><label for='cr'>Red</label><br/>"; const char _FORM_COLOR_RED[] PROGMEM = "<input class='r' id='cr' name='c' type='radio' value='red' {check}><label for='cr'>Red</label><br/>";
const char _FORM_COLOR_GREEN[] PROGMEM = "<input class='r' id='cg' name='c' type='radio' value='green' {check}><label for='cg'>Green</label><br/>"; const char _FORM_COLOR_GREEN[] PROGMEM = "<input class='r' id='cg' name='c' type='radio' value='green' {check}><label for='cg'>Green</label><br/>";
const char _FORM_COLOR_WHITE[] PROGMEM = "<input class='r' id='cw' name='c' type='radio' value='white' {check}><label for='cw'>White</label><br/>"; const char _FORM_COLOR_WHITE[] PROGMEM = "<input class='r' id='cw' name='c' type='radio' value='white' {check}><label for='cw'>White</label><br/>";
const char _FORM_COLOR_YELLOW[] PROGMEM = "<input class='r' id='cy' name='c' type='radio' value='yellow' {check}><label for='cy'>Yellow</label><br/>";
const char _FORM_BRIGHTNESS[] PROGMEM = "<br/><label for='b'>Brightness:</label><input id='b' name='b' type='range' min='10' max='255' value='{bright}'><br/>"; const char _FORM_BRIGHTNESS[] PROGMEM = "<br/><label for='b'>Brightness:</label><input id='b' name='b' type='range' min='10' max='255' value='{bright}'><br/>";
const char _FORM_END[] PROGMEM = "<br/><button type='submit'>save</button></form>"; const char _FORM_END[] PROGMEM = "<br/><button type='submit'>save</button></form>";
const char _SCAN_LINK[] PROGMEM = "<br/><div class=\"c\"><a href=\"/wifi\">Scan</a></div>"; const char _SCAN_LINK[] PROGMEM = "<br/><div class=\"c\"><a href=\"/wifi\">Scan</a></div>";
@ -203,6 +225,17 @@ void appConfig() {
page += String(F("<h3>Clock Options</h3>")); page += String(F("<h3>Clock Options</h3>"));
//page += FPSTR(_PORTAL_OPTIONS); //page += FPSTR(_PORTAL_OPTIONS);
page += FPSTR(_FORM_START); page += FPSTR(_FORM_START);
page += FPSTR(_FORM_CLOCKMODE_HEADLINE);
input = FPSTR(_FORM_CLOCKMODE_DEMO);
input.replace("{check}", (appMode == MODE_DEMO) ? "checked" : "");
page += input;
input = FPSTR(_FORM_CLOCKMODE_REAL);
input.replace("{check}", (appMode == MODE_REALCLOCK) ? "checked" : "");
page += input;
input = FPSTR(_FORM_CLOCKMODE_FAST);
input.replace("{check}", (appMode == MODE_FASTCLOCK) ? "checked" : "");
page += input;
page += FPSTR(_FORM_CLOCKNAME);
page += FPSTR(_FORM_COLOR_HEADLINE); page += FPSTR(_FORM_COLOR_HEADLINE);
input = FPSTR(_FORM_COLOR_BLUE); input = FPSTR(_FORM_COLOR_BLUE);
input.replace("{check}", (clockColor == SevenSegmentClock::Blue) ? "checked" : ""); input.replace("{check}", (clockColor == SevenSegmentClock::Blue) ? "checked" : "");
@ -213,6 +246,9 @@ void appConfig() {
input = FPSTR(_FORM_COLOR_GREEN); input = FPSTR(_FORM_COLOR_GREEN);
input.replace("{check}", (clockColor == SevenSegmentClock::Green) ? "checked" : ""); input.replace("{check}", (clockColor == SevenSegmentClock::Green) ? "checked" : "");
page += input; page += input;
input = FPSTR(_FORM_COLOR_YELLOW);
input.replace("{check}", (clockColor == SevenSegmentClock::Yellow) ? "checked" : "");
page += input;
input = FPSTR(_FORM_COLOR_WHITE); input = FPSTR(_FORM_COLOR_WHITE);
input.replace("{check}", (clockColor == SevenSegmentClock::White) ? "checked" : ""); input.replace("{check}", (clockColor == SevenSegmentClock::White) ? "checked" : "");
page += input; page += input;
@ -245,6 +281,16 @@ void appConfigSave() {
SevenSegmentClock::Color colorHandle = getColorHandleByName(server->arg("c")); SevenSegmentClock::Color colorHandle = getColorHandleByName(server->arg("c"));
sevenSegmentClock.setColor(colorHandle); sevenSegmentClock.setColor(colorHandle);
} }
if (server->hasArg("m")) {
Serial.print("setting clock mode to "); Serial.println(server->arg("m"));
if (server->arg("m").equals("real")) appMode = MODE_REALCLOCK;
else if (server->arg("m").equals("fast")) appMode = MODE_FASTCLOCK;
else if (server->arg("m").equals("demo")) appMode = MODE_DEMO;
else {
Serial.println("ERROR: Unknown application mode, going into demo mode");
appMode = MODE_DEMO;
}
}
page.replace("{v}", "7Seg Config"); page.replace("{v}", "7Seg Config");
page += FPSTR(_SCRIPT); page += FPSTR(_SCRIPT);
page += FPSTR(_STYLE); page += FPSTR(_STYLE);
@ -352,6 +398,8 @@ void setup() {
pinMode(POWER_OFF_PIN, INPUT); pinMode(POWER_OFF_PIN, INPUT);
*/ */
setupWifiConnection(); setupWifiConnection();
Serial.println("Starting NTP Client");
timeClient.begin();
Serial.println("Have following configuration:"); Serial.println("Have following configuration:");
Serial.print(" Clock name: "); Serial.println(clockName); Serial.print(" Clock name: "); Serial.println(clockName);
@ -372,15 +420,29 @@ int hours = 0, minutes = 0;
uint32_t nextUpdate_ms = 0; uint32_t nextUpdate_ms = 0;
void loop() { void loop() {
if (millis() > nextUpdate_ms) { timeClient.update();
nextUpdate_ms = millis() + 1000;
minutes++; //Serial.println(timeClient.getFormattedTime());
if (minutes > 99) { minutes = 0; }
if (minutes % 5 == 0) hours++; switch (appMode) {
if (hours > 99) hours = 0; case MODE_DEMO:
sevenSegmentClock.displayTime(hours, minutes); if (millis() > nextUpdate_ms) {
if (hours % 4 == 0) sevenSegmentClock.setBlinkMode(SevenSegmentClock::SeperatorBlinking); else sevenSegmentClock.setBlinkMode(SevenSegmentClock::NoBlinking); nextUpdate_ms = millis() + 1000;
minutes++;
if (minutes > 99) { minutes = 0; }
if (minutes % 5 == 0) hours++;
if (hours > 99) hours = 0;
sevenSegmentClock.displayTime(hours, minutes);
if (hours % 4 == 0) sevenSegmentClock.setBlinkMode(SevenSegmentClock::SeperatorBlinking); else sevenSegmentClock.setBlinkMode(SevenSegmentClock::NoBlinking);
}
break;
case MODE_REALCLOCK:
sevenSegmentClock.displayTime(timeClient.getHours(), timeClient.getMinutes());
break;
case MODE_FASTCLOCK:
break;
} }
sevenSegmentClock.displayUpdate(); sevenSegmentClock.displayUpdate();
server->handleClient(); server->handleClient();
} }