From 2453e9d74bc11a614e8a8b0b29d165b7a4b9ee42 Mon Sep 17 00:00:00 2001 From: coelner Date: Thu, 28 Jan 2021 19:00:15 +0100 Subject: [PATCH] esp8266 ifdef --- .gitignore | 1 + 60LED_WS2812B_NTP_Clock.ino | 67 +++++++++++++++++++++++++++++++++---- 2 files changed, 61 insertions(+), 7 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a8a0dce --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.bin diff --git a/60LED_WS2812B_NTP_Clock.ino b/60LED_WS2812B_NTP_Clock.ino index d796a21..b1b9df5 100644 --- a/60LED_WS2812B_NTP_Clock.ino +++ b/60LED_WS2812B_NTP_Clock.ino @@ -1,6 +1,4 @@ -#include #include "time.h" - #include @@ -9,8 +7,17 @@ #include // UpdateServer includes #ifdef ESP8266 +#include # include +#include +#define MYTZ TZ_Europe_Berlin +#include // settimeofday_cb() +//#include +//#include +#include // struct timeval +#include // sntp_servermode_dhcp() #elif defined(ESP32) +#include // For ESP32 IotWebConf provides a drop-in replacement for UpdateServer. # include #endif @@ -23,9 +30,15 @@ Dark Resistance: 0.5 Mohm over a 56k voltage divider . Dark (0.5MOhm) , 10Lux (5-10kOhm) */ +#ifdef ESP8266 +//#define LDR_PIN 4 +#define IOTWEBCONF_DEBUG_DISABLED +#define DATA_PIN 5 //DMA RDX0/GPIO3 | Uart1 TXD1/GPIO2 | UART0 GPIO1 +#elif defined(ESP32) #define LDR_PIN A6 -#define NUM_LEDS 60 #define DATA_PIN 17 +#endif +#define NUM_LEDS 60 #define SERIAL_BAUD 115200 #define RGBW @@ -43,8 +56,13 @@ void brightnessAdjustmentCallback(); #define MINIMAL_BRIGHTNESS 5 #define LDR_SCALE 16 #define colorSaturation 255 -//NeoPixelBrightnessBus strip(NUM_LEDS, DATA_PIN); //SK6812 -NeoPixelBrightnessBus strip(NUM_LEDS, DATA_PIN); +#ifdef ESP8266 +NeoPixelBrightnessBus strip(NUM_LEDS);//RDX0 GPIO3 +//NeoPixelBrightnessBus strip(NUM_LEDS); +//NeoPixelBrightnessBus strip(NUM_LEDS); +#elif defined(ESP32) +NeoPixelBrightnessBus strip(NUM_LEDS, DATA_PIN); //ESP32 +#endif RgbwColor red(colorSaturation, 0, 0, 0); RgbwColor green(0, colorSaturation, 0, 0); RgbwColor blue(0, 0, colorSaturation, 0); @@ -138,17 +156,19 @@ void transformHtmltoStrip(RgbColor outputColor, char* sInput) { #endif -//NTP handler //const char* ntpServer = "pool.ntp.org"; const long gmtOffset_sec = 3600; //ToDo changable from user const int daylightOffset_sec = 3600; struct tm timeinfo; + volatile int currentSec = 59; volatile int currentMin = 1; volatile int currentHour = 2; +#ifdef LDR_PIN // variable for storing the potentiometer value volatile unsigned int ldrValue = 0; +#endif volatile int MAX_BRIGHTNESS = 200; // WebPortal @@ -286,12 +306,31 @@ CustomHtmlFormatProvider customHtmlFormatProvider; Task bootAnim(200, TASK_FOREVER, &bootAnimCallback); Task clockTick(1000, TASK_FOREVER, &clockTickCallback); Task ledRefresh(200, TASK_FOREVER, &ledRefreshCallback); +#ifdef LDR_PIN Task brightness(10000, TASK_FOREVER, &brightnessAdjustmentCallback); +#endif //Task iotwebconfLoop(200, TASK_FOREVER, &iotWebConf.doLoop); Scheduler runner; +#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 + String printLocalTime() { - if (!getLocalTime(&timeinfo)) { + if (!getLocalTime(&timeinfo, 200)) { Serial.println("Failed to obtain time"); return ""; } @@ -451,18 +490,26 @@ void syncNTP() { bootAnim.disable(); ledRefresh.enable(); clockTick.enable(); +#ifdef LDR_PIN brightness.enable(); +#endif } } void setup() { +#ifdef LDR_PIN pinMode(LDR_PIN, INPUT_PULLUP); +#endif pinMode(DATA_PIN, OUTPUT); Serial.begin(SERIAL_BAUD); Serial.flush(); Serial.print("\n\n\nCPU Frequency is: "); +#ifdef ESP8266 + Serial.print(ESP.getCpuFreqMHz()); +#elif defined(ESP32) Serial.print(getCpuFrequencyMhz()); //Get CPU clock +#endif Serial.println(" Mhz"); Serial.print("MAC address: "); Serial.println(WiFi.macAddress()); //Get CPU clock @@ -481,7 +528,9 @@ void setup() { runner.init(); runner.addTask(ledRefresh); runner.addTask(clockTick); +#ifdef LDR_PIN runner.addTask(brightness); +#endif runner.addTask(bootAnim); bootAnim.enable(); @@ -525,7 +574,9 @@ void ledRefreshCallback() { // Scheduler void clockTickCallback() { +#ifdef LDR_PIN ldrValue = (ldrValue + analogRead(LDR_PIN)); +#endif if (currentSec < 0) { currentSec = 59; currentMin--; @@ -571,6 +622,7 @@ void bootAnimCallback() { strip.Show(); } +#ifdef LDR_PIN void brightnessAdjustmentCallback() { //Serial.println(ldrValue); //brigthness begin @@ -587,3 +639,4 @@ void brightnessAdjustmentCallback() { ldrValue = 0; strip.Show(); } +#endif