174 lines
4.4 KiB
C++
174 lines
4.4 KiB
C++
#include <WiFi.h>
|
|
#include "time.h"
|
|
#include <FastLED.h>
|
|
#include <WiFiManager.h>
|
|
|
|
#define MOD(a,b) ((((a)%(b))+(b))%(b))
|
|
// LDR (GL5516 ) is connected to GPIO 34 (Analog ADC1_CH6
|
|
// over a 56k voltage divider . Dark (0.5MOhm) , 10Lux (5-10kOhm)
|
|
const int ldrPin = 34;
|
|
// How many leds in your strip?
|
|
#define NUM_LEDS 60
|
|
#define BRIGHTNESS 50
|
|
#define COLOR_ORDER GRB
|
|
#define DATA_PIN 5
|
|
// Define the array of leds
|
|
CRGB leds[NUM_LEDS];
|
|
|
|
|
|
volatile signed int interruptCounter;
|
|
hw_timer_t * timer = NULL;
|
|
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
|
|
|
|
void IRAM_ATTR onTimer() {
|
|
portENTER_CRITICAL_ISR(&timerMux);
|
|
interruptCounter++;
|
|
portEXIT_CRITICAL_ISR(&timerMux);
|
|
}
|
|
|
|
|
|
volatile int currentSec = 59;
|
|
volatile int currentMin = 1;
|
|
volatile int currentHour = 1;
|
|
|
|
// variable for storing the potentiometer value
|
|
volatile int ldrValue = 0;
|
|
|
|
WiFiManager wifiManager;
|
|
const char* ssid = "koelner Gastzugang";
|
|
const char* password = "deltaforce!";
|
|
|
|
const char* ntpServer = "pool.ntp.org";
|
|
const long gmtOffset_sec = 3600;
|
|
const int daylightOffset_sec = 3600;
|
|
struct tm timeinfo;
|
|
|
|
void printLocalTime()
|
|
{
|
|
if (!getLocalTime(&timeinfo)) {
|
|
Serial.println("Failed to obtain time");
|
|
return;
|
|
}
|
|
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
|
|
}
|
|
|
|
void syncNTP() {
|
|
Serial.println(WiFi.getMode());
|
|
WiFi.mode(WIFI_STA);
|
|
delay(1000);
|
|
WiFi.enableAP(false);
|
|
WiFi.enableSTA(true);
|
|
//connect to WiFi
|
|
Serial.printf("Connecting to %s ", ssid);
|
|
WiFi.begin(ssid, password);
|
|
int i = 0;
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
Serial.print(".");
|
|
if (i == 10) {
|
|
break;
|
|
}
|
|
i++;
|
|
}
|
|
Serial.println(" CONNECTED");
|
|
|
|
//init and get the time
|
|
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
|
|
printLocalTime();
|
|
//inverted logic - remaining time
|
|
currentSec = 60 - timeinfo.tm_sec;
|
|
currentMin = 60 - timeinfo.tm_min;
|
|
currentHour = 12 - (timeinfo.tm_hour % 12);
|
|
Serial.println("NTP mapped: " + String(currentHour) + ":" + String(currentMin) + ":" + String(currentSec));
|
|
//disconnect WiFi as it's no longer needed
|
|
WiFi.disconnect(true);
|
|
WiFi.mode(WIFI_OFF);
|
|
}
|
|
void setup()
|
|
{
|
|
//interrupt for one second@80Mhz!
|
|
timer = timerBegin(0, 80, true);
|
|
timerAttachInterrupt(timer, &onTimer, true);
|
|
timerAlarmWrite(timer, 1000000, true);
|
|
timerAlarmEnable(timer);
|
|
|
|
Serial.begin(9600);
|
|
|
|
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
|
|
|
|
FastLED.setBrightness( BRIGHTNESS );
|
|
FastLED.clear();
|
|
|
|
syncNTP();
|
|
//currentSec = 40; currentMin = 1; currentHour = 2;
|
|
}
|
|
void LDR() {
|
|
//brigthness measurement
|
|
ldrValue = analogRead(ldrPin);
|
|
if (ldrValue == 0) {
|
|
FastLED.setBrightness( BRIGHTNESS );
|
|
}
|
|
else {
|
|
FastLED.setBrightness( int(BRIGHTNESS - BRIGHTNESS / log(ldrValue * 10)) );
|
|
//Serial.println("Brightness: " + String( int(BRIGHTNESS - BRIGHTNESS / log(ldrValue * 10))));
|
|
}
|
|
|
|
}
|
|
void loop()
|
|
{
|
|
if (interruptCounter > 0) {
|
|
|
|
portENTER_CRITICAL(&timerMux);
|
|
interruptCounter--;
|
|
currentSec--;
|
|
portEXIT_CRITICAL(&timerMux);
|
|
|
|
if (currentSec < 0) {
|
|
currentSec = 59;
|
|
currentMin--;
|
|
LDR();
|
|
if (currentMin < 0) {
|
|
currentMin = 59;
|
|
currentHour--;
|
|
if (currentHour < 0) {
|
|
currentHour = 11;
|
|
//timerAlarmDisable(timer);
|
|
//ESP.restart();
|
|
syncNTP();
|
|
//timerAlarmEnable(timer);
|
|
}
|
|
}
|
|
//Serial.println(String(interruptCounter) + " | Ring Index: " + String(currentHour * 5) + ":" + String(currentMin) + ":" + String(currentSec));
|
|
}
|
|
|
|
//reset color
|
|
leds[(currentSec + 1) % NUM_LEDS] = CRGB(30, 48, 40);
|
|
leds[(currentMin + 1) % NUM_LEDS] = CRGB(30, 48, 40);
|
|
leds[((currentHour + 1) * 5 - 1) % NUM_LEDS] = CRGB(30, 48, 40);
|
|
leds[((currentHour + 1) * 5 + 1) % NUM_LEDS] = CRGB(30, 48, 40);
|
|
|
|
//1 dot hour marking
|
|
for (int dot = 0; dot < NUM_LEDS; dot++) {
|
|
if (dot % 5 == 0) {
|
|
leds[dot] = CRGB::OrangeRed;
|
|
}
|
|
}
|
|
//3 dots hour
|
|
if (currentHour == 0) {
|
|
leds[59 % NUM_LEDS] = CRGB::Gold;
|
|
}
|
|
else {
|
|
leds[(currentHour * 5 - 1) % NUM_LEDS] = CRGB::Gold;
|
|
}
|
|
leds[(currentHour * 5) % NUM_LEDS] = CRGB::Gold;
|
|
leds[(currentHour * 5 + 1) % NUM_LEDS] = CRGB::Gold;
|
|
|
|
leds[0] = CRGB::Blue; //define high noon
|
|
//leds[NUM_LEDS / 2] = CRGB::Green; //define half noon
|
|
|
|
leds[(currentSec + 0) % NUM_LEDS] = CRGB::Black;
|
|
leds[(currentMin + 0) % NUM_LEDS] = CRGB::Orange;
|
|
FastLED.show();
|
|
}
|
|
}
|