2019-12-11 08:03:32 +00:00
|
|
|
#include "time.h"
|
2020-12-28 15:41:01 +00:00
|
|
|
#include <TaskScheduler.h>
|
|
|
|
|
|
|
|
|
2020-09-27 06:29:14 +00:00
|
|
|
#include <NeoPixelBrightnessBus.h>
|
2020-05-03 12:53:41 +00:00
|
|
|
#include <IotWebConf.h>
|
2021-01-01 15:44:50 +00:00
|
|
|
#include <IotWebConfUsing.h>
|
|
|
|
// UpdateServer includes
|
|
|
|
#ifdef ESP8266
|
2021-01-28 18:00:15 +00:00
|
|
|
#include <ESP8266WiFi.h>
|
2021-01-01 15:44:50 +00:00
|
|
|
# include <ESP8266HTTPUpdateServer.h>
|
2021-01-28 18:00:15 +00:00
|
|
|
#include <TZ.h>
|
|
|
|
#define MYTZ TZ_Europe_Berlin
|
|
|
|
#include <coredecls.h> // settimeofday_cb()
|
|
|
|
//#include <Schedule.h>
|
|
|
|
//#include <PolledTimeout.h>
|
|
|
|
#include <sys/time.h> // struct timeval
|
|
|
|
#include <sntp.h> // sntp_servermode_dhcp()
|
2021-01-01 15:44:50 +00:00
|
|
|
#elif defined(ESP32)
|
2021-01-28 18:00:15 +00:00
|
|
|
#include <WiFi.h>
|
2021-01-01 15:44:50 +00:00
|
|
|
// For ESP32 IotWebConf provides a drop-in replacement for UpdateServer.
|
|
|
|
# include <IotWebConfESP32HTTPUpdateServer.h>
|
|
|
|
#endif
|
|
|
|
|
2019-12-11 08:03:32 +00:00
|
|
|
|
2019-12-12 18:06:56 +00:00
|
|
|
#define MOD(a,b) ((((a)%(b))+(b))%(b))
|
2020-12-26 15:00:29 +00:00
|
|
|
/* Model I
|
|
|
|
LDR (GL5516 ) is connected to GPIO 34 (Analog ADC1_CH6, 12bit resolution default
|
|
|
|
Light Resistance (at 10 Lux): 5-10 Kohm
|
|
|
|
Dark Resistance: 0.5 Mohm
|
|
|
|
over a 56k voltage divider . Dark (0.5MOhm) , 10Lux (5-10kOhm)
|
2021-05-06 19:13:50 +00:00
|
|
|
-----------------
|
|
|
|
ESP8266 24LED
|
|
|
|
5V--10k-+-LDR--GND -ADC
|
|
|
|
- 150k--220k-+-100k--GND
|
2020-12-26 15:00:29 +00:00
|
|
|
*/
|
2021-01-28 18:00:15 +00:00
|
|
|
#ifdef ESP8266
|
2021-05-06 19:13:50 +00:00
|
|
|
#define LDR_PIN A0
|
2021-01-28 18:00:15 +00:00
|
|
|
#define IOTWEBCONF_DEBUG_DISABLED
|
2021-05-06 19:13:50 +00:00
|
|
|
#define DATA_PIN D7 //DMA RDX0/GPIO3 | Uart1 TXD1/GPIO2 | UART0 GPIO1
|
2021-01-28 18:00:15 +00:00
|
|
|
#elif defined(ESP32)
|
2020-09-27 06:29:14 +00:00
|
|
|
#define LDR_PIN A6
|
|
|
|
#define DATA_PIN 17
|
2021-01-28 18:00:15 +00:00
|
|
|
#endif
|
2021-05-06 20:41:54 +00:00
|
|
|
/* useful preselection
|
|
|
|
* NO following hour
|
|
|
|
* NO three block hour marking
|
|
|
|
* quarter hour marking
|
|
|
|
*/
|
|
|
|
#define NUM_LEDS 60 //24
|
2020-12-26 15:00:29 +00:00
|
|
|
#define SERIAL_BAUD 115200
|
|
|
|
|
2021-05-06 19:13:50 +00:00
|
|
|
#define RGBW
|
2021-05-06 20:41:54 +00:00
|
|
|
const bool clockwiseRing = false;
|
|
|
|
volatile bool singleSecond = false; //show seconds
|
2021-01-08 20:10:16 +00:00
|
|
|
volatile bool allDotsOn = true; //lighten up all leds
|
2021-05-06 20:41:54 +00:00
|
|
|
#if NUM_LEDS == 60
|
2021-01-08 20:10:16 +00:00
|
|
|
volatile bool followingHour = true; //move hour like an analog one
|
2021-05-06 20:41:54 +00:00
|
|
|
#endif
|
|
|
|
#if NUM_LEDS == 24
|
|
|
|
volatile bool followingHour = false; //move hour like an analog one
|
|
|
|
#endif
|
2021-01-08 20:10:16 +00:00
|
|
|
volatile int hourOffset = 0;
|
2020-12-26 15:00:29 +00:00
|
|
|
|
2020-12-28 15:41:01 +00:00
|
|
|
void bootAnimCallback();
|
|
|
|
void clockTickCallback();
|
|
|
|
void ledRefreshCallback();
|
|
|
|
void brightnessAdjustmentCallback();
|
|
|
|
|
2020-12-26 15:00:29 +00:00
|
|
|
#ifdef RGBW
|
|
|
|
#define MINIMAL_BRIGHTNESS 5
|
|
|
|
#define LDR_SCALE 16
|
|
|
|
#define colorSaturation 255
|
2021-01-28 18:00:15 +00:00
|
|
|
#ifdef ESP8266
|
2021-05-06 19:13:50 +00:00
|
|
|
//NeoPixelBrightnessBus<NeoGrbwFeature, Neo800KbpsMethod> strip(NUM_LEDS);//RDX0 GPIO3
|
2021-01-28 18:00:15 +00:00
|
|
|
//NeoPixelBrightnessBus<NeoGrbwFeature, NeoEsp8266AsyncUart0Sk6812Method> strip(NUM_LEDS);
|
|
|
|
//NeoPixelBrightnessBus<NeoGrbwFeature, NeoEsp8266Uart0Sk6812Method> strip(NUM_LEDS);
|
2021-05-06 19:13:50 +00:00
|
|
|
NeoPixelBrightnessBus<NeoGrbwFeature, NeoEsp8266BitBang800KbpsMethod> strip(NUM_LEDS, DATA_PIN);
|
2021-01-28 18:00:15 +00:00
|
|
|
#elif defined(ESP32)
|
|
|
|
NeoPixelBrightnessBus<NeoGrbwFeature, NeoEsp32I2s1800KbpsMethod> strip(NUM_LEDS, DATA_PIN); //ESP32
|
|
|
|
#endif
|
2020-09-27 06:29:14 +00:00
|
|
|
RgbwColor red(colorSaturation, 0, 0, 0);
|
|
|
|
RgbwColor green(0, colorSaturation, 0, 0);
|
|
|
|
RgbwColor blue(0, 0, colorSaturation, 0);
|
2020-12-28 15:41:01 +00:00
|
|
|
RgbwColor realWhite(colorSaturation);
|
2021-01-08 20:10:16 +00:00
|
|
|
RgbwColor white(0, 0, 0, 40); //darkish dirt
|
2020-12-28 15:41:01 +00:00
|
|
|
RgbwColor whiter(0, 0, 0, 120); //darkish dirt
|
2021-01-01 15:44:50 +00:00
|
|
|
RgbwColor white12(0, 0, 0, 192); //darkish dirt
|
2020-09-27 06:29:14 +00:00
|
|
|
RgbwColor black(0);
|
|
|
|
RgbwColor gold(HtmlColor( 0xFFD700 ) );
|
|
|
|
RgbwColor orangered(HtmlColor( 0xFF4500 ) );
|
|
|
|
RgbwColor orange(HtmlColor( 0xFFA500 ) );
|
2021-01-27 16:27:23 +00:00
|
|
|
RgbwColor darkred(HtmlColor( 0x800000) );
|
|
|
|
RgbwColor darkgreen(HtmlColor( 0x006400) );
|
|
|
|
RgbwColor lightgreen(HtmlColor( 0x30ee30) );
|
2020-12-26 15:00:29 +00:00
|
|
|
RgbwColor temp;
|
2021-01-30 13:04:23 +00:00
|
|
|
RgbwColor secondsColor = black;
|
2021-01-26 20:44:46 +00:00
|
|
|
RgbwColor minuteColor = darkred;
|
|
|
|
RgbwColor hourColor = gold;
|
|
|
|
RgbwColor highnoonColor = white12;
|
|
|
|
RgbwColor backlightColor = white;
|
|
|
|
RgbwColor hourMarkingColor = whiter;
|
2021-01-27 16:27:23 +00:00
|
|
|
|
|
|
|
void transformtoHtmlColor (char* sOutput, RgbwColor* inputColor) {
|
|
|
|
//Serial.printf("InputColor: R:%i G:%i B:%i W:%i\n", inputColor.R, inputColor.G, inputColor.B, inputColor.W);
|
|
|
|
|
|
|
|
if (inputColor->IsMonotone())
|
|
|
|
{
|
|
|
|
//Serial.print("White: "); Serial.println(inputColor->W);
|
2021-01-30 13:04:23 +00:00
|
|
|
HtmlColor(RgbColor(inputColor->W, inputColor->W, inputColor->W)).ToNumericalString(sOutput, 12);
|
2021-01-27 16:27:23 +00:00
|
|
|
//Serial.println((char*)sOutput);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
//Serial.print("Color: "); Serial.print(inputColor->R); Serial.print(inputColor->G); Serial.println(inputColor->B);
|
2021-01-30 13:04:23 +00:00
|
|
|
HtmlColor(RgbColor(inputColor->R, inputColor->G, inputColor->B)).ToNumericalString(sOutput, 12);
|
2021-01-27 16:27:23 +00:00
|
|
|
//Serial.println((char*)sOutput);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void transformHtmltoStrip(RgbwColor* outputColor, char* sInput) {
|
|
|
|
HtmlColor htmlTemp;
|
|
|
|
//Serial.print("HtmltoStrip ");
|
|
|
|
//Serial.println((char*)sInput);
|
|
|
|
htmlTemp.Parse<HtmlColorNames>( sInput );
|
|
|
|
RgbwColor stripColor( htmlTemp );
|
|
|
|
//Serial.printf("StripColor: R:%i G:%i B:%i W:%i\n", stripColor.R, stripColor.G, stripColor.B, stripColor.W);
|
|
|
|
memcpy(outputColor, &stripColor, sizeof(stripColor));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-26 15:00:29 +00:00
|
|
|
#else
|
|
|
|
#define MINIMAL_BRIGHTNESS 20
|
|
|
|
#define LDR_SCALE 16
|
|
|
|
#define colorSaturation 192
|
2021-05-06 19:13:50 +00:00
|
|
|
#ifdef ESP8266
|
|
|
|
//NeoPixelBrightnessBus<NeoGrbFeature, Neo800KbpsMethod> strip(NUM_LEDS);//RDX0 GPIO3 Broken due IoTWebConf
|
|
|
|
NeoPixelBrightnessBus<NeoGrbFeature, NeoEsp8266Uart1800KbpsMethod> strip(NUM_LEDS);
|
|
|
|
//NeoPixelBrightnessBus<NeoGrbFeature, NeoEsp8266BitBang800KbpsMethod> strip(NUM_LEDS, DATA_PIN);
|
|
|
|
#elif defined(ESP32)
|
|
|
|
NeoPixelBrightnessBus<NeoGrbFeature, NeoEsp32I2s1800KbpsMethod> strip(NUM_LEDS, DATA_PIN); //ESP32
|
|
|
|
#endif
|
2020-12-26 15:00:29 +00:00
|
|
|
RgbColor red(colorSaturation, 0, 0);
|
|
|
|
RgbColor green(0, colorSaturation, 0);
|
|
|
|
RgbColor blue(0, 0, colorSaturation);
|
|
|
|
RgbColor realWhite(colorSaturation);
|
|
|
|
RgbColor black(0);
|
|
|
|
RgbColor white(30, 40, 35); //darkish dirt
|
|
|
|
RgbColor whiter(120, 120, 120); //darkish dirt
|
|
|
|
RgbColor white12(255, 255, 255); //darkish dirt
|
2021-01-27 16:27:23 +00:00
|
|
|
RgbColor gold(HtmlColor( 0xFFD700 ));
|
|
|
|
RgbColor orangered(HtmlColor( 0xFF4500 ));
|
|
|
|
RgbColor orange(HtmlColor( 0xFFA500 ));
|
|
|
|
RgbColor darkred(HtmlColor( 0x800000 ));
|
|
|
|
RgbColor darkgreen(HtmlColor( 0x006400 ));
|
|
|
|
RgbColor lightgreen(HtmlColor( 0x90ee90 ));
|
2020-12-26 15:00:29 +00:00
|
|
|
RgbColor temp;
|
2021-01-30 13:04:23 +00:00
|
|
|
RgbColor secondsColor = black;
|
2021-01-26 20:44:46 +00:00
|
|
|
RgbColor minuteColor = darkred;
|
|
|
|
RgbColor hourColor = gold;
|
|
|
|
RgbColor highnoonColor = white12;
|
|
|
|
RgbColor backlightColor = white;
|
|
|
|
RgbColor hourMarkingColor = whiter;
|
2021-01-27 16:27:23 +00:00
|
|
|
|
2021-01-30 13:04:23 +00:00
|
|
|
void transformtoHtmlColor (char* sOutput, RgbColor* inputColor) {
|
|
|
|
HtmlColor(RgbColor(inputColor->R, inputColor->G, inputColor->B)).ToNumericalString(sOutput, 12);
|
2021-01-27 16:27:23 +00:00
|
|
|
}
|
|
|
|
|
2021-01-30 13:04:23 +00:00
|
|
|
void transformHtmltoStrip(RgbColor* outputColor, char* sInput) {
|
2021-01-27 16:27:23 +00:00
|
|
|
HtmlColor htmlTemp;
|
|
|
|
//Serial.print("HtmltoStrip ");
|
|
|
|
//Serial.println((char*)sInput);
|
2021-01-30 13:04:23 +00:00
|
|
|
htmlTemp.Parse<HtmlColorNames>( sInput );
|
2021-01-27 16:27:23 +00:00
|
|
|
RgbColor stripColor( htmlTemp );
|
2021-01-30 13:04:23 +00:00
|
|
|
//Serial.printf("StripColor: R:%i G:%i B:%i W:%i\n", stripColor.R, stripColor.G, stripColor.B, stripColor.W);
|
2021-01-27 16:27:23 +00:00
|
|
|
memcpy(outputColor, &stripColor, sizeof(stripColor));
|
|
|
|
}
|
|
|
|
|
2020-12-26 15:00:29 +00:00
|
|
|
#endif
|
2019-12-11 08:03:32 +00:00
|
|
|
|
2020-05-03 12:53:41 +00:00
|
|
|
//const char* ntpServer = "pool.ntp.org";
|
2020-12-26 15:00:29 +00:00
|
|
|
const long gmtOffset_sec = 3600; //ToDo changable from user
|
2020-05-03 12:53:41 +00:00
|
|
|
const int daylightOffset_sec = 3600;
|
|
|
|
struct tm timeinfo;
|
2021-01-28 18:00:15 +00:00
|
|
|
|
2019-12-11 08:03:32 +00:00
|
|
|
volatile int currentSec = 59;
|
|
|
|
volatile int currentMin = 1;
|
2021-01-01 15:44:50 +00:00
|
|
|
volatile int currentHour = 2;
|
2021-01-30 13:04:23 +00:00
|
|
|
volatile bool NTPreachable = false;
|
2019-12-11 08:03:32 +00:00
|
|
|
|
2021-01-28 18:00:15 +00:00
|
|
|
#ifdef LDR_PIN
|
2019-12-11 08:03:32 +00:00
|
|
|
// variable for storing the potentiometer value
|
2020-12-26 15:00:29 +00:00
|
|
|
volatile unsigned int ldrValue = 0;
|
2021-01-28 18:00:15 +00:00
|
|
|
#endif
|
2021-01-08 20:10:16 +00:00
|
|
|
volatile int MAX_BRIGHTNESS = 200;
|
2019-12-11 08:03:32 +00:00
|
|
|
|
2020-05-03 12:53:41 +00:00
|
|
|
// WebPortal
|
|
|
|
const char thingName[] = "NTP-Clock-RGBLED";
|
2020-12-26 15:00:29 +00:00
|
|
|
const char wifiInitialApPassword[] = "12345678";
|
2020-05-03 12:53:41 +00:00
|
|
|
#define STRING_LEN 63
|
|
|
|
#define NUMBER_LEN 4
|
2021-01-26 20:44:46 +00:00
|
|
|
// -- Maximal length the input-range attributes can have.
|
|
|
|
#define COLOR_ATTR_LENGTH 60
|
2020-05-03 12:53:41 +00:00
|
|
|
// -- Configuration specific key. The value should be modified if config structure was changed.
|
2021-05-06 20:41:54 +00:00
|
|
|
#define CONFIG_VERSION "V1.1.5"
|
2021-01-01 15:44:50 +00:00
|
|
|
|
|
|
|
const char CUSTOMHTML_SCRIPT_INNER[] PROGMEM = "\n\
|
2021-01-26 20:44:46 +00:00
|
|
|
function colorCh(id)\n\
|
|
|
|
{\n\
|
|
|
|
var x=document.getElementById(id);\n\
|
|
|
|
var s=document.getElementById(id + 'Val');\n\
|
|
|
|
s.innerHTML = x.value;\n\
|
|
|
|
}\n\n\
|
|
|
|
document.addEventListener('DOMContentLoaded', function(event) {\n\
|
|
|
|
let elements = document.querySelectorAll('input[type=\"password\"]');\n\
|
|
|
|
for (let p of elements) {\n\
|
|
|
|
let btn = document.createElement('INPUT'); btn.type = 'button'; btn.value = '🔓'; btn.style.width = 'auto'; p.style.width = '80%'; p.parentNode.insertBefore(btn,p.nextSibling);\n\
|
|
|
|
btn.onclick = function() { if (p.type === 'password') { p.type = 'text'; btn.value = '🔒'; } else { p.type = 'password'; btn.value = '🔓'; } }\n\
|
|
|
|
};\n\
|
|
|
|
});\n";
|
|
|
|
const char IOTWEBCONF_HTML_FORM_COLOR_PARAM[] PROGMEM =
|
|
|
|
"<div class='{s}' '><label for='{i}'>{b}</label> (<span id='{i}Val'>{v}</span>)"
|
|
|
|
"<input type='{t}' id='{i}' "
|
|
|
|
"name='{i}' style=\"padding: unset\" maxlength={l} placeholder='{p}' value='{v}' {c}/>"
|
|
|
|
"<div class='em'>{e}</div></div>\n";
|
|
|
|
|
|
|
|
// -- Our custom class declaration. You should move it to a .h fine in your project.
|
|
|
|
class ColorWithValueParameter : public iotwebconf::NumberParameter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ColorWithValueParameter(
|
|
|
|
const char* label, const char* id, char* valueBuffer, int length,
|
|
|
|
const char* defaultValue
|
|
|
|
) : iotwebconf::NumberParameter(
|
|
|
|
label, id, valueBuffer, length, defaultValue)
|
|
|
|
{
|
|
|
|
snprintf(
|
|
|
|
this->_colorAttr, COLOR_ATTR_LENGTH,
|
|
|
|
"oninput='colorCh(this.id)'");
|
|
|
|
|
|
|
|
this->customHtml = this->_colorAttr;
|
|
|
|
};
|
|
|
|
protected:
|
|
|
|
// Overrides
|
|
|
|
virtual String renderHtml(
|
|
|
|
bool dataArrived, bool hasValueFromPost, String valueFromPost) override
|
|
|
|
{
|
|
|
|
return TextParameter::renderHtml("color", hasValueFromPost, valueFromPost);
|
|
|
|
};
|
|
|
|
virtual String getHtmlTemplate()
|
|
|
|
{
|
|
|
|
return FPSTR(IOTWEBCONF_HTML_FORM_COLOR_PARAM);
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
char _colorAttr[COLOR_ATTR_LENGTH];
|
|
|
|
};
|
|
|
|
|
|
|
|
// -- We need to create our custom HtmlFormatProvider to add some javasripts.
|
|
|
|
class CustomHtmlFormatProvider : public iotwebconf::HtmlFormatProvider
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
String getScriptInner() override
|
|
|
|
{
|
|
|
|
return
|
|
|
|
HtmlFormatProvider::getScriptInner() +
|
|
|
|
String(FPSTR(CUSTOMHTML_SCRIPT_INNER));
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
String getBodyInner() override
|
|
|
|
{
|
|
|
|
return
|
|
|
|
String(FPSTR(CUSTOMHTML_BODY_INNER)) +
|
|
|
|
HtmlFormatProvider::getBodyInner();
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
};
|
|
|
|
// -- Javascript block will be added to the header.
|
|
|
|
/*
|
|
|
|
const char CUSTOMHTML_SCRIPT_INNER[] PROGMEM = "\n\
|
|
|
|
document.addEventListener('DOMContentLoaded', function(event) {\n\
|
2021-01-01 15:44:50 +00:00
|
|
|
let elements = document.querySelectorAll('input[type=\"password\"]');\n\
|
|
|
|
for (let p of elements) {\n\
|
2021-01-16 16:46:34 +00:00
|
|
|
let btn = document.createElement('INPUT'); btn.type = 'button'; btn.value = '🔓'; btn.style.width = 'auto'; p.style.width = '83%'; p.parentNode.insertBefore(btn,p.nextSibling);\n\
|
|
|
|
btn.onclick = function() { if (p.type === 'password') { p.type = 'text'; btn.value = '🔒'; } else { p.type = 'password'; btn.value = '🔓'; } }\n\
|
2021-01-01 15:44:50 +00:00
|
|
|
};\n\
|
2021-01-26 20:44:46 +00:00
|
|
|
});\n";
|
|
|
|
*/
|
2021-01-01 15:44:50 +00:00
|
|
|
//const char CUSTOMHTML_BODY_INNER[] PROGMEM = " <link rel='icon' src='data:image/png;base64,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'/>";
|
2020-05-03 12:53:41 +00:00
|
|
|
DNSServer dnsServer;
|
|
|
|
WebServer server(80);
|
2021-01-01 15:44:50 +00:00
|
|
|
#ifdef ESP8266
|
|
|
|
ESP8266HTTPUpdateServer httpUpdater;
|
|
|
|
#elif defined(ESP32)
|
|
|
|
HTTPUpdateServer httpUpdater;
|
|
|
|
#endif
|
2020-05-03 12:53:41 +00:00
|
|
|
|
2021-01-01 15:44:50 +00:00
|
|
|
char ntpServerParamValue[STRING_LEN];
|
|
|
|
char maxBrightnessParamValue[NUMBER_LEN];
|
|
|
|
char singleSecondParamValue[STRING_LEN];
|
|
|
|
char allDotsOnParamValue[STRING_LEN];
|
|
|
|
char followingHourParamValue[STRING_LEN];
|
2021-01-26 20:44:46 +00:00
|
|
|
char hourColorParamValue[COLOR_ATTR_LENGTH];
|
|
|
|
char minuteColorParamValue[COLOR_ATTR_LENGTH];
|
|
|
|
char secondsColorParamValue[COLOR_ATTR_LENGTH];
|
|
|
|
char highnoonColorParamValue[COLOR_ATTR_LENGTH];
|
|
|
|
char backlightColorParamValue[COLOR_ATTR_LENGTH];
|
|
|
|
char hourMarkingColorParamValue[COLOR_ATTR_LENGTH];
|
2020-05-03 12:53:41 +00:00
|
|
|
|
|
|
|
IotWebConf iotWebConf(thingName, &dnsServer, &server, wifiInitialApPassword, CONFIG_VERSION);
|
2021-01-08 20:10:16 +00:00
|
|
|
IotWebConfParameterGroup timeGroup = IotWebConfParameterGroup("Time", "Time settings");
|
2021-01-01 15:44:50 +00:00
|
|
|
IotWebConfTextParameter ntpServerParam = IotWebConfTextParameter("NTP Server", "ntpServer", ntpServerParamValue, STRING_LEN, "pool.ntp.org");
|
|
|
|
IotWebConfParameterGroup ledGroup = IotWebConfParameterGroup("LED", "LED settings");
|
|
|
|
IotWebConfNumberParameter maxBrightnessParam = IotWebConfNumberParameter("Max BRIGHTNESS", "max brightness", maxBrightnessParamValue, NUMBER_LEN, "200", "20..254", "min='20' max='254' step='1'");
|
2021-05-06 20:41:54 +00:00
|
|
|
#if NUM_LEDS == 60
|
2021-01-01 15:44:50 +00:00
|
|
|
IotWebConfCheckboxParameter singleSecondParam = IotWebConfCheckboxParameter("Show Seconds", "singleSecond", singleSecondParamValue, STRING_LEN, true);
|
2021-05-06 20:41:54 +00:00
|
|
|
#endif
|
|
|
|
#if NUM_LEDS == 24
|
|
|
|
IotWebConfCheckboxParameter singleSecondParam = IotWebConfCheckboxParameter("Show Seconds", "singleSecond", singleSecondParamValue, STRING_LEN, false);
|
|
|
|
#endif
|
2021-01-26 20:44:46 +00:00
|
|
|
IotWebConfCheckboxParameter allDotsOnParam = IotWebConfCheckboxParameter("all Dots lighten on", "allDotsOn", allDotsOnParamValue, STRING_LEN, true);
|
2021-05-06 20:41:54 +00:00
|
|
|
#if NUM_LEDS == 60
|
2021-01-26 20:44:46 +00:00
|
|
|
IotWebConfCheckboxParameter followingHourParam = IotWebConfCheckboxParameter("following Hour", "followingHour", followingHourParamValue, STRING_LEN, true);
|
2021-05-06 20:41:54 +00:00
|
|
|
#endif
|
|
|
|
#if NUM_LEDS == 24
|
|
|
|
IotWebConfCheckboxParameter followingHourParam = IotWebConfCheckboxParameter("following Hour", "followingHour", followingHourParamValue, STRING_LEN, false);
|
|
|
|
#endif
|
2021-01-26 20:44:46 +00:00
|
|
|
ColorWithValueParameter hourColorParam = ColorWithValueParameter("Stundenfarbe", "hourColorParam", hourColorParamValue, COLOR_ATTR_LENGTH, "#FFD700");
|
|
|
|
ColorWithValueParameter minuteColorParam = ColorWithValueParameter("Minutenfarbe", "minuteColorParam", minuteColorParamValue, COLOR_ATTR_LENGTH, "#800000");
|
2021-01-30 13:04:23 +00:00
|
|
|
ColorWithValueParameter secondsColorParam = ColorWithValueParameter("Sekundenfarbe", "secondsColorParam", secondsColorParamValue, COLOR_ATTR_LENGTH, "#000000");
|
|
|
|
ColorWithValueParameter highnoonColorParam = ColorWithValueParameter("12Uhr Farbe", "highnoonColorParam", highnoonColorParamValue, COLOR_ATTR_LENGTH, "#C0C0C0");
|
2021-01-26 20:44:46 +00:00
|
|
|
ColorWithValueParameter backlightColorParam = ColorWithValueParameter("Hintergrundfarbe", "backlightColorParam", backlightColorParamValue, COLOR_ATTR_LENGTH, "#1E2823");
|
|
|
|
ColorWithValueParameter hourMarkingColorParam = ColorWithValueParameter("Stundenmarkierung", "hourMarkingColorParam", hourMarkingColorParamValue, COLOR_ATTR_LENGTH, "#787878");
|
|
|
|
|
2021-01-01 15:44:50 +00:00
|
|
|
|
|
|
|
// -- An instance must be created from the class defined above.
|
|
|
|
CustomHtmlFormatProvider customHtmlFormatProvider;
|
2019-12-11 08:03:32 +00:00
|
|
|
|
2020-12-28 15:41:01 +00:00
|
|
|
Task bootAnim(200, TASK_FOREVER, &bootAnimCallback);
|
|
|
|
Task clockTick(1000, TASK_FOREVER, &clockTickCallback);
|
|
|
|
Task ledRefresh(200, TASK_FOREVER, &ledRefreshCallback);
|
2021-01-28 18:00:15 +00:00
|
|
|
#ifdef LDR_PIN
|
2020-12-28 15:41:01 +00:00
|
|
|
Task brightness(10000, TASK_FOREVER, &brightnessAdjustmentCallback);
|
2021-01-28 18:00:15 +00:00
|
|
|
#endif
|
2020-12-28 15:41:01 +00:00
|
|
|
//Task iotwebconfLoop(200, TASK_FOREVER, &iotWebConf.doLoop);
|
|
|
|
Scheduler runner;
|
|
|
|
|
2021-01-28 18:00:15 +00:00
|
|
|
#ifdef ESP8266
|
|
|
|
bool getLocalTime(struct tm * info, uint32_t ms)
|
|
|
|
{
|
|
|
|
uint32_t start = millis();
|
|
|
|
time_t now;
|
|
|
|
while ((millis() - start) <= ms) {
|
|
|
|
time(&now);
|
|
|
|
localtime_r(&now, info);
|
|
|
|
if (info->tm_year > (2016 - 1900)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
delay(10);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-12-26 15:00:29 +00:00
|
|
|
String printLocalTime() {
|
2021-01-28 18:00:15 +00:00
|
|
|
if (!getLocalTime(&timeinfo, 200)) {
|
2019-12-11 08:03:32 +00:00
|
|
|
Serial.println("Failed to obtain time");
|
2021-01-30 13:04:23 +00:00
|
|
|
NTPreachable = false;
|
2020-12-26 15:00:29 +00:00
|
|
|
return "";
|
2019-12-11 08:03:32 +00:00
|
|
|
}
|
2020-12-26 15:00:29 +00:00
|
|
|
//Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
|
|
|
|
|
|
|
|
char timeStringBuff[50]; //50 chars should be enough
|
|
|
|
strftime(timeStringBuff, sizeof(timeStringBuff), "%A, %B %d %Y %H:%M:%S", &timeinfo);
|
|
|
|
//Serial.println(timeStringBuff);
|
2021-01-30 13:04:23 +00:00
|
|
|
NTPreachable = true;
|
2020-12-26 15:00:29 +00:00
|
|
|
return timeStringBuff;
|
|
|
|
|
2019-12-11 08:03:32 +00:00
|
|
|
}
|
|
|
|
|
2020-05-03 12:53:41 +00:00
|
|
|
/**
|
|
|
|
Handle web requests to "/" path.
|
|
|
|
*/
|
2021-01-01 15:44:50 +00:00
|
|
|
void iotWebConfHandleRoot() {
|
2020-05-03 12:53:41 +00:00
|
|
|
// -- Let IotWebConf test and handle captive portal requests.
|
|
|
|
if (iotWebConf.handleCaptivePortal())
|
|
|
|
{
|
|
|
|
// -- Captive portal request were already served.
|
|
|
|
return;
|
|
|
|
}
|
2021-01-26 20:44:46 +00:00
|
|
|
char sTemp[12];
|
2020-05-03 12:53:41 +00:00
|
|
|
String s = "<!DOCTYPE html><html lang=\"en\"><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=no\"/>";
|
2020-12-26 15:00:29 +00:00
|
|
|
s += "<meta http-equiv=\"refresh\" content=\"30\">";
|
2021-01-01 15:44:50 +00:00
|
|
|
//s += CUSTOMHTML_BODY_INNER;
|
|
|
|
s += "<title>RGB LED Clock</title></head><body>";
|
|
|
|
s += "RGB LED Clock";
|
2020-12-26 15:00:29 +00:00
|
|
|
s += "<p>";
|
|
|
|
s += "<table>";
|
|
|
|
s += "<tr><td>Current Time:</td><td>";
|
|
|
|
s += printLocalTime();
|
2021-01-27 16:27:23 +00:00
|
|
|
s += "</td></tr></table><table>";
|
|
|
|
s += "<tr></tr><tr><td>NTP Server:</td><td>";
|
2020-05-03 12:53:41 +00:00
|
|
|
s += ntpServerParamValue;
|
2020-12-26 15:00:29 +00:00
|
|
|
s += "</td><tr><tr><td>Current Brightness value: </td><td>";
|
|
|
|
s += String(strip.GetBrightness());
|
|
|
|
s += "</td><tr><tr><td>Max Brightness value: </td><td>";
|
|
|
|
s += atoi(maxBrightnessParamValue);
|
|
|
|
s += "</td></tr><tr><td>Show Seconds: </td><td>";
|
|
|
|
s += (singleSecond ? "Yes" : "No");
|
|
|
|
s += "</td></tr><tr><td>All Dots On: </td><td>";
|
|
|
|
s += (allDotsOn ? "Yes" : "No");
|
|
|
|
s += "</td></tr><tr><td>Following Hour: </td><td>";
|
|
|
|
s += (followingHour ? "Yes" : "No");
|
2021-01-26 20:44:46 +00:00
|
|
|
s += "</td></tr><tr><td>";
|
|
|
|
s += "<tr><td>High noon: </td><td style=\"border: 1px solid #000000; background-color:";
|
2021-01-27 16:27:23 +00:00
|
|
|
transformtoHtmlColor ((char*)&sTemp, &highnoonColor);
|
2021-01-26 20:44:46 +00:00
|
|
|
s += sTemp;
|
|
|
|
s += ";\"><td></tr>";
|
|
|
|
s += "<tr><td>Hour color: </td><td style=\"border: 1px solid #000000; background-color:";
|
2021-01-27 16:27:23 +00:00
|
|
|
transformtoHtmlColor ((char*)&sTemp, &hourColor);
|
2021-01-26 20:44:46 +00:00
|
|
|
s += sTemp;
|
|
|
|
s += ";\"><td></tr>";
|
|
|
|
s += "<tr><td>Minute color: </td><td style=\"border: 1px solid #000000; background-color:";
|
2021-01-27 16:27:23 +00:00
|
|
|
transformtoHtmlColor ((char*)&sTemp, &minuteColor);
|
2021-01-26 20:44:46 +00:00
|
|
|
s += sTemp;
|
|
|
|
s += ";\"><td></tr>";
|
2021-01-27 16:27:23 +00:00
|
|
|
if (singleSecond) {
|
2021-01-30 13:04:23 +00:00
|
|
|
s += "<tr><td>Seconds color: </td><td style=\"border: 1px solid #000000; background-color:";
|
2021-01-27 16:27:23 +00:00
|
|
|
transformtoHtmlColor ((char*)&sTemp, &secondsColor);
|
|
|
|
s += sTemp;
|
|
|
|
s += ";\"><td></tr>";
|
|
|
|
}
|
|
|
|
if (allDotsOn) {
|
|
|
|
s += "<tr><td>Backlight: </td><td style=\"border: 1px solid #000000; background-color:";
|
|
|
|
transformtoHtmlColor ((char*)&sTemp, &backlightColor);
|
|
|
|
s += sTemp;
|
|
|
|
s += ";\"><td></tr>";
|
|
|
|
}
|
2021-01-26 20:44:46 +00:00
|
|
|
s += "<tr><td>Hour Marking: </td><td style=\"border: 1px solid #000000; background-color:";
|
2021-01-27 16:27:23 +00:00
|
|
|
transformtoHtmlColor ((char*)&sTemp, &hourMarkingColor);
|
2021-01-26 20:44:46 +00:00
|
|
|
s += sTemp;
|
|
|
|
s += ";\"><td></tr></table>";
|
2020-12-26 15:00:29 +00:00
|
|
|
s += "<p>";
|
2020-05-03 12:53:41 +00:00
|
|
|
s += "Go to <a href='config'>configure page</a> to change values.";
|
|
|
|
s += "</body></html>\n";
|
|
|
|
|
2021-01-01 15:44:50 +00:00
|
|
|
server.send(200, "text/html; charset=UTF-8", s);
|
2020-05-03 12:53:41 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 15:44:50 +00:00
|
|
|
void iotWebConfConfigSaved()
|
2020-05-03 12:53:41 +00:00
|
|
|
{
|
2020-12-26 15:00:29 +00:00
|
|
|
//ToDo ntpServerParamValue;
|
|
|
|
MAX_BRIGHTNESS = atoi(maxBrightnessParamValue);
|
2021-01-01 15:44:50 +00:00
|
|
|
singleSecond = singleSecondParam.isChecked() ? true : false;
|
|
|
|
//Serial.println(singleSecondParam.isChecked() ? "true" : "false");
|
|
|
|
allDotsOn = allDotsOnParam.isChecked() ? true : false;
|
|
|
|
//Serial.println(allDotsOnParam.isChecked() ? "true" : "false");
|
2021-05-06 20:41:54 +00:00
|
|
|
#if NUM_LEDS == 60
|
2021-01-01 15:44:50 +00:00
|
|
|
followingHour = followingHourParam.isChecked() ? true : false;
|
2021-05-06 20:41:54 +00:00
|
|
|
#endif
|
2021-01-01 15:44:50 +00:00
|
|
|
//Serial.println(followingHourParam.isChecked() ? "true" : "false");
|
|
|
|
|
2021-01-27 16:27:23 +00:00
|
|
|
transformHtmltoStrip(&highnoonColor, (char*)&highnoonColorParamValue);
|
|
|
|
transformHtmltoStrip(&hourColor, (char*)&hourColorParamValue);
|
|
|
|
transformHtmltoStrip(&minuteColor, (char*)&minuteColorParamValue);
|
|
|
|
transformHtmltoStrip(&secondsColor, (char*)&secondsColorParamValue);
|
|
|
|
transformHtmltoStrip(&backlightColor, (char*)&backlightColorParamValue);
|
|
|
|
transformHtmltoStrip(&hourMarkingColor, (char*)&hourMarkingColorParamValue);
|
2021-01-26 20:44:46 +00:00
|
|
|
Serial.println("Configuration was updated.");
|
2020-05-03 12:53:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void iotWebConf_Setup() {
|
2021-01-01 15:44:50 +00:00
|
|
|
|
|
|
|
timeGroup.addItem(&ntpServerParam);
|
|
|
|
|
|
|
|
ledGroup.addItem(&maxBrightnessParam);
|
|
|
|
ledGroup.addItem(&singleSecondParam);
|
|
|
|
ledGroup.addItem(&allDotsOnParam);
|
2021-05-06 20:41:54 +00:00
|
|
|
#if NUM_LEDS == 60
|
2021-01-01 15:44:50 +00:00
|
|
|
ledGroup.addItem(&followingHourParam);
|
2021-05-06 20:41:54 +00:00
|
|
|
#endif
|
2021-01-26 20:44:46 +00:00
|
|
|
ledGroup.addItem(&hourColorParam);
|
|
|
|
ledGroup.addItem(&minuteColorParam);
|
|
|
|
ledGroup.addItem(&secondsColorParam);
|
|
|
|
ledGroup.addItem(&highnoonColorParam);
|
|
|
|
ledGroup.addItem(&backlightColorParam);
|
|
|
|
ledGroup.addItem(&hourMarkingColorParam);
|
2021-01-01 15:44:50 +00:00
|
|
|
|
|
|
|
iotWebConf.addParameterGroup(&timeGroup);
|
|
|
|
iotWebConf.addParameterGroup(&ledGroup);
|
|
|
|
|
|
|
|
iotWebConf.setConfigSavedCallback(&iotWebConfConfigSaved);
|
2020-12-26 15:00:29 +00:00
|
|
|
iotWebConf.setWifiConnectionCallback(&syncNTP); //NTP call after connection established
|
2021-01-16 16:46:34 +00:00
|
|
|
//iotWebConf.setFormValidator(&iotWebConfFormValidator);
|
2020-05-03 12:53:41 +00:00
|
|
|
iotWebConf.getApTimeoutParameter()->visible = true;
|
2021-01-01 15:44:50 +00:00
|
|
|
iotWebConf.setupUpdateServer(
|
2021-01-08 20:10:16 +00:00
|
|
|
[](const char* updatePath) {
|
|
|
|
httpUpdater.setup(&server, updatePath);
|
|
|
|
},
|
|
|
|
[](const char* userName, char* password) {
|
|
|
|
httpUpdater.updateCredentials(userName, password);
|
|
|
|
});
|
2020-05-03 12:53:41 +00:00
|
|
|
|
2021-01-01 15:44:50 +00:00
|
|
|
iotWebConf.setHtmlFormatProvider(&customHtmlFormatProvider);
|
2020-05-03 12:53:41 +00:00
|
|
|
// -- Initializing the configuration.
|
|
|
|
iotWebConf.init();
|
|
|
|
|
|
|
|
// -- Set up required URL handlers on the web server.
|
2021-01-01 15:44:50 +00:00
|
|
|
server.on("/", iotWebConfHandleRoot);
|
2020-05-03 12:53:41 +00:00
|
|
|
server.on("/config", [] { iotWebConf.handleConfig(); });
|
|
|
|
server.onNotFound([]() {
|
|
|
|
iotWebConf.handleNotFound();
|
|
|
|
});
|
|
|
|
|
|
|
|
Serial.println("Ready.");
|
2020-12-26 15:00:29 +00:00
|
|
|
Serial.flush();
|
2020-05-03 12:53:41 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 08:03:32 +00:00
|
|
|
void syncNTP() {
|
2021-01-16 17:01:47 +00:00
|
|
|
if (iotWebConf.getState() == IOTWEBCONF_STATE_ONLINE) {
|
2020-05-03 12:53:41 +00:00
|
|
|
//init and get the time
|
2021-05-06 19:13:50 +00:00
|
|
|
#ifdef ESP8266
|
|
|
|
//sntp_servermode_dhcp(0);
|
|
|
|
configTime(MYTZ, ntpServerParamValue);
|
|
|
|
#endif
|
|
|
|
#ifdef ESP32
|
|
|
|
configTime(0, 0, ntpServer);
|
|
|
|
// TZ string information: https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html
|
|
|
|
setenv("TZ", "AEST-10", 1);
|
|
|
|
tzset(); // save the TZ variable
|
|
|
|
//setTimeZone(long offset, int daylight);
|
|
|
|
//configTime(gmtOffset_sec, daylightOffset_sec, ntpServerParamValue);
|
|
|
|
#endif
|
2020-12-26 15:00:29 +00:00
|
|
|
Serial.println(printLocalTime());
|
2021-01-30 13:04:23 +00:00
|
|
|
if (NTPreachable) {
|
2021-05-06 20:41:54 +00:00
|
|
|
if (clockwiseRing) {
|
|
|
|
currentSec = timeinfo.tm_sec;
|
|
|
|
currentMin = timeinfo.tm_min;
|
|
|
|
currentHour = MOD(timeinfo.tm_hour, 12);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
//inverted logic - remaining time
|
|
|
|
currentSec = 60 - timeinfo.tm_sec;
|
|
|
|
currentMin = 60 - timeinfo.tm_min;
|
|
|
|
currentHour = 12 - MOD(timeinfo.tm_hour, 12);
|
|
|
|
}
|
|
|
|
Serial.println("NTP mapped: " + String(int(currentHour * NUM_LEDS / 12)) + ":" + String(int(currentMin * NUM_LEDS / 60)) + ":" + String(int(currentSec * NUM_LEDS / 60)));
|
2021-01-30 13:04:23 +00:00
|
|
|
bootAnim.disable();
|
|
|
|
ledRefresh.enable();
|
|
|
|
clockTick.enable();
|
2021-01-28 18:00:15 +00:00
|
|
|
#ifdef LDR_PIN
|
2021-01-30 13:04:23 +00:00
|
|
|
brightness.enable();
|
2021-01-28 18:00:15 +00:00
|
|
|
#endif
|
2021-01-30 13:04:23 +00:00
|
|
|
}
|
2019-12-11 08:03:32 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-03 12:53:41 +00:00
|
|
|
|
2020-09-27 06:29:14 +00:00
|
|
|
void setup() {
|
2021-01-28 18:00:15 +00:00
|
|
|
#ifdef LDR_PIN
|
2020-12-26 15:00:29 +00:00
|
|
|
pinMode(LDR_PIN, INPUT_PULLUP);
|
2021-01-28 18:00:15 +00:00
|
|
|
#endif
|
2020-09-27 06:29:14 +00:00
|
|
|
pinMode(DATA_PIN, OUTPUT);
|
2019-12-11 08:03:32 +00:00
|
|
|
|
2020-09-27 06:29:14 +00:00
|
|
|
Serial.begin(SERIAL_BAUD);
|
2020-12-26 15:00:29 +00:00
|
|
|
Serial.flush();
|
2020-12-28 15:41:01 +00:00
|
|
|
Serial.print("\n\n\nCPU Frequency is: ");
|
2021-01-28 18:00:15 +00:00
|
|
|
#ifdef ESP8266
|
|
|
|
Serial.print(ESP.getCpuFreqMHz());
|
|
|
|
#elif defined(ESP32)
|
2020-12-26 15:00:29 +00:00
|
|
|
Serial.print(getCpuFrequencyMhz()); //Get CPU clock
|
2021-01-28 18:00:15 +00:00
|
|
|
#endif
|
2020-12-27 13:35:16 +00:00
|
|
|
Serial.println(" Mhz");
|
2021-01-08 20:10:16 +00:00
|
|
|
Serial.print("MAC address: ");
|
|
|
|
Serial.println(WiFi.macAddress()); //Get CPU clock
|
2019-12-11 08:03:32 +00:00
|
|
|
|
2020-09-27 06:29:14 +00:00
|
|
|
strip.Begin();
|
2020-12-26 15:00:29 +00:00
|
|
|
strip.ClearTo(white);
|
2021-05-06 19:13:50 +00:00
|
|
|
strip.SetBrightness( MINIMAL_BRIGHTNESS );
|
2020-09-27 06:29:14 +00:00
|
|
|
strip.Show();
|
2020-12-28 15:41:01 +00:00
|
|
|
|
|
|
|
runner.init();
|
|
|
|
runner.addTask(ledRefresh);
|
|
|
|
runner.addTask(clockTick);
|
2021-01-28 18:00:15 +00:00
|
|
|
#ifdef LDR_PIN
|
2020-12-28 15:41:01 +00:00
|
|
|
runner.addTask(brightness);
|
2021-01-28 18:00:15 +00:00
|
|
|
#endif
|
2020-12-28 15:41:01 +00:00
|
|
|
runner.addTask(bootAnim);
|
|
|
|
bootAnim.enable();
|
2019-12-11 08:03:32 +00:00
|
|
|
|
2020-05-03 12:53:41 +00:00
|
|
|
iotWebConf_Setup();
|
2020-12-26 15:00:29 +00:00
|
|
|
//Serial.end();
|
2019-12-11 08:03:32 +00:00
|
|
|
}
|
2020-05-03 12:53:41 +00:00
|
|
|
|
2019-12-11 08:03:32 +00:00
|
|
|
void loop()
|
|
|
|
{
|
2020-12-28 15:41:01 +00:00
|
|
|
runner.execute();
|
2020-05-03 12:53:41 +00:00
|
|
|
iotWebConf.doLoop();
|
2020-12-27 13:35:16 +00:00
|
|
|
/*
|
|
|
|
while (Serial.available())
|
2020-12-26 15:00:29 +00:00
|
|
|
Serial.read();
|
2020-12-27 13:35:16 +00:00
|
|
|
*/
|
2020-12-28 15:41:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ledRefreshCallback() {
|
2021-01-26 20:44:46 +00:00
|
|
|
temp = allDotsOn ? backlightColor : black;
|
2020-12-28 15:41:01 +00:00
|
|
|
strip.ClearTo(temp);
|
|
|
|
for (int dot = 0; dot < NUM_LEDS; dot++) {
|
2021-05-06 20:41:54 +00:00
|
|
|
#if NUM_LEDS == 60
|
|
|
|
//1 dot hour marking
|
2020-12-28 15:41:01 +00:00
|
|
|
if (MOD(dot, 5) == 0) {
|
2021-01-26 20:44:46 +00:00
|
|
|
strip.SetPixelColor(dot, hourMarkingColor);
|
2020-12-26 15:00:29 +00:00
|
|
|
}
|
2021-05-06 20:41:54 +00:00
|
|
|
#endif
|
|
|
|
#if NUM_LEDS == 24
|
|
|
|
//1 dot quarter marking
|
|
|
|
if (MOD(dot, 6) == 0) {
|
|
|
|
strip.SetPixelColor(dot, hourMarkingColor);
|
|
|
|
}
|
|
|
|
#endif
|
2020-12-26 15:00:29 +00:00
|
|
|
}
|
2021-05-06 20:41:54 +00:00
|
|
|
#if NUM_LEDS == 60
|
2020-12-28 15:41:01 +00:00
|
|
|
//3 dots hour
|
2021-01-26 20:44:46 +00:00
|
|
|
strip.SetPixelColor(MOD((currentHour * 5 - 1 + hourOffset), NUM_LEDS), hourColor);
|
|
|
|
strip.SetPixelColor(MOD((currentHour * 5 + 0 + hourOffset), NUM_LEDS), hourColor);
|
|
|
|
strip.SetPixelColor(MOD((currentHour * 5 + 1 + hourOffset), NUM_LEDS), hourColor);
|
2021-05-06 20:41:54 +00:00
|
|
|
#endif
|
2021-01-26 20:44:46 +00:00
|
|
|
strip.SetPixelColor(0, highnoonColor); //define high noon
|
2019-12-11 08:03:32 +00:00
|
|
|
|
2020-12-28 15:41:01 +00:00
|
|
|
if (singleSecond) {
|
2021-05-06 20:41:54 +00:00
|
|
|
strip.SetPixelColor(MOD(int(currentSec * NUM_LEDS / 60 + 0), NUM_LEDS), secondsColor);
|
2020-12-28 15:41:01 +00:00
|
|
|
}
|
2021-05-06 20:41:54 +00:00
|
|
|
strip.SetPixelColor(MOD(int(currentHour * NUM_LEDS / 12 + 0), NUM_LEDS) , hourColor);
|
|
|
|
strip.SetPixelColor(MOD(int(currentMin * NUM_LEDS / 60 + 0), NUM_LEDS), minuteColor);
|
2020-12-28 15:41:01 +00:00
|
|
|
strip.Show();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scheduler
|
|
|
|
void clockTickCallback() {
|
2021-01-28 18:00:15 +00:00
|
|
|
#ifdef LDR_PIN
|
2020-12-28 15:41:01 +00:00
|
|
|
ldrValue = (ldrValue + analogRead(LDR_PIN));
|
2021-01-28 18:00:15 +00:00
|
|
|
#endif
|
2021-05-06 20:41:54 +00:00
|
|
|
if (clockwiseRing) {
|
|
|
|
if (currentSec >= 60) {
|
|
|
|
currentSec = 0;
|
|
|
|
currentMin++;
|
|
|
|
if (!NTPreachable) {
|
2020-12-28 15:41:01 +00:00
|
|
|
syncNTP();
|
2019-12-12 18:06:56 +00:00
|
|
|
}
|
2021-05-06 20:41:54 +00:00
|
|
|
if (currentMin >= 60) {
|
|
|
|
currentMin = 0;
|
|
|
|
currentHour++;
|
|
|
|
if (currentHour >= 12) {
|
|
|
|
currentHour = 0;
|
|
|
|
syncNTP();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (followingHour) {
|
|
|
|
hourOffset = int (currentMin * NUM_LEDS / 60 / 12); //negative value
|
|
|
|
//Serial.println("hourOffset: " + String(hourOffset));
|
|
|
|
}
|
|
|
|
//Serial.println(String(interruptCounter) + " | Ring Index: " + String(currentHour / (12 / NUM_LEDS)) + ":" + String(currentMin / (60 / NUM_LEDS)) + ":" + String(currentSec / (60 / NUM_LEDS)));
|
2019-12-11 08:03:32 +00:00
|
|
|
}
|
2021-05-06 20:41:54 +00:00
|
|
|
currentSec++;
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (currentSec < 0) {
|
|
|
|
currentSec = 59;
|
|
|
|
currentMin--;
|
|
|
|
if (!NTPreachable) {
|
|
|
|
syncNTP();
|
|
|
|
}
|
|
|
|
if (currentMin < 0) {
|
|
|
|
currentMin = 59;
|
|
|
|
currentHour--;
|
|
|
|
if (currentHour < 0) {
|
|
|
|
currentHour = 11;
|
|
|
|
syncNTP();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (followingHour) {
|
|
|
|
hourOffset = 0 + int((60 - currentMin * NUM_LEDS / 60) / 12); //negative value
|
|
|
|
//Serial.println("hourOffset: " + String(hourOffset));
|
|
|
|
}
|
|
|
|
//Serial.println(String(interruptCounter) + " | Ring Index: " + String(currentHour / (12 / NUM_LEDS)) + ":" + String(currentMin / (60 / NUM_LEDS)) + ":" + String(currentSec / (60 / NUM_LEDS)));
|
2019-12-12 18:06:56 +00:00
|
|
|
}
|
2021-05-06 20:41:54 +00:00
|
|
|
currentSec--;
|
2020-12-28 15:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-11 08:03:32 +00:00
|
|
|
|
2020-12-28 15:41:01 +00:00
|
|
|
void bootAnimCallback() {
|
|
|
|
strip.ClearTo(black);
|
2021-05-06 19:13:50 +00:00
|
|
|
int tail = -1;
|
2021-05-06 20:41:54 +00:00
|
|
|
if (clockwiseRing) {
|
|
|
|
currentSec++;
|
|
|
|
if (currentSec >= NUM_LEDS) {
|
|
|
|
currentSec = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2021-05-06 19:13:50 +00:00
|
|
|
currentSec--;
|
2021-05-06 20:41:54 +00:00
|
|
|
tail = 1;
|
2021-05-06 19:13:50 +00:00
|
|
|
if (currentSec < 0) {
|
2021-05-06 20:41:54 +00:00
|
|
|
currentSec = NUM_LEDS - 1;
|
2021-05-06 19:13:50 +00:00
|
|
|
}
|
2021-05-06 20:41:54 +00:00
|
|
|
}
|
2021-01-30 13:04:23 +00:00
|
|
|
|
|
|
|
if (iotWebConf.getState() == IOTWEBCONF_STATE_ONLINE && !NTPreachable) {
|
|
|
|
strip.ClearTo(white);
|
|
|
|
strip.SetPixelColor(MOD((currentSec - 0), NUM_LEDS), black);
|
2021-05-06 19:13:50 +00:00
|
|
|
strip.SetPixelColor(MOD((currentSec - 1 * tail), NUM_LEDS), black);
|
|
|
|
strip.SetPixelColor(MOD((currentSec - 2 * tail), NUM_LEDS), black);
|
2021-01-30 13:04:23 +00:00
|
|
|
}
|
2021-01-16 17:01:47 +00:00
|
|
|
if (iotWebConf.getState() == IOTWEBCONF_STATE_CONNECTING) {
|
2021-01-08 20:10:16 +00:00
|
|
|
strip.SetPixelColor(MOD((currentSec - 0), NUM_LEDS), white);
|
2021-05-06 19:13:50 +00:00
|
|
|
strip.SetPixelColor(MOD((currentSec - 1 * tail), NUM_LEDS), whiter);
|
|
|
|
strip.SetPixelColor(MOD((currentSec - 2 * tail), NUM_LEDS), white12);
|
2021-01-08 20:10:16 +00:00
|
|
|
}
|
2021-01-16 17:01:47 +00:00
|
|
|
if (iotWebConf.getState() == IOTWEBCONF_STATE_AP_MODE) {
|
2021-01-17 17:50:21 +00:00
|
|
|
strip.SetPixelColor(MOD((currentSec - 0), NUM_LEDS), lightgreen);
|
2021-05-06 19:13:50 +00:00
|
|
|
strip.SetPixelColor(MOD((currentSec - 1 * tail), NUM_LEDS), green);
|
|
|
|
strip.SetPixelColor(MOD((currentSec - 2 * tail), NUM_LEDS), darkgreen);
|
2021-01-08 20:10:16 +00:00
|
|
|
}
|
2021-01-16 17:01:47 +00:00
|
|
|
if (iotWebConf.getState() == IOTWEBCONF_STATE_BOOT || iotWebConf.getState() == IOTWEBCONF_STATE_NOT_CONFIGURED) {
|
2021-01-08 20:10:16 +00:00
|
|
|
strip.SetPixelColor(MOD((currentSec - 0), NUM_LEDS), orangered);
|
2021-05-06 19:13:50 +00:00
|
|
|
strip.SetPixelColor(MOD((currentSec - 1 * tail), NUM_LEDS), red);
|
|
|
|
strip.SetPixelColor(MOD((currentSec - 2 * tail), NUM_LEDS), darkred);
|
2021-01-08 20:10:16 +00:00
|
|
|
}
|
|
|
|
|
2020-12-28 15:41:01 +00:00
|
|
|
strip.Show();
|
|
|
|
}
|
2020-12-26 15:00:29 +00:00
|
|
|
|
2021-01-28 18:00:15 +00:00
|
|
|
#ifdef LDR_PIN
|
2020-12-28 15:41:01 +00:00
|
|
|
void brightnessAdjustmentCallback() {
|
|
|
|
//Serial.println(ldrValue);
|
|
|
|
//brigthness begin
|
|
|
|
ldrValue = ldrValue / 10;
|
|
|
|
int x;
|
|
|
|
if (ldrValue == 0) {
|
|
|
|
strip.SetBrightness( MAX_BRIGHTNESS );
|
2019-12-12 18:06:56 +00:00
|
|
|
}
|
2020-12-28 15:41:01 +00:00
|
|
|
else {
|
|
|
|
x = int(MINIMAL_BRIGHTNESS + (MAX_BRIGHTNESS - MINIMAL_BRIGHTNESS) / ((ldrValue / LDR_SCALE) + 1) );
|
|
|
|
strip.SetBrightness(x);
|
|
|
|
}
|
2021-01-27 16:27:23 +00:00
|
|
|
//Serial.println("Brightness: " + String(strip.GetBrightness()) + "(" + String(ldrValue) + ")");
|
2020-12-28 15:41:01 +00:00
|
|
|
ldrValue = 0;
|
|
|
|
strip.Show();
|
2019-12-11 08:03:32 +00:00
|
|
|
}
|
2021-01-28 18:00:15 +00:00
|
|
|
#endif
|