From 6776269b8ad2b03a28261d007a03a8a61b74c4a0 Mon Sep 17 00:00:00 2001 From: coelner Date: Sun, 27 Sep 2020 08:29:14 +0200 Subject: [PATCH] real SK6812 hardware support --- 60LED_WS2812B_NTP_Clock.ino | 88 +++++++++++++++++++++++-------------- 1 file changed, 54 insertions(+), 34 deletions(-) diff --git a/60LED_WS2812B_NTP_Clock.ino b/60LED_WS2812B_NTP_Clock.ino index f87e418..9ded248 100644 --- a/60LED_WS2812B_NTP_Clock.ino +++ b/60LED_WS2812B_NTP_Clock.ino @@ -1,20 +1,37 @@ #include #include "time.h" -#include +#include #include #define MOD(a,b) ((((a)%(b))+(b))%(b)) // 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) -const int ldrPin = 34; +#define LDR_PIN A6 // How many leds in your strip? -#define MINIMAL_BRIGHTNESS 5 +#define colorSaturation 128 +#define MINIMAL_BRIGHTNESS 2 #define NUM_LEDS 60 #define COLOR_ORDER GRB -#define DATA_PIN 5 +#define DATA_PIN 17 #define SERIAL_BAUD 9600 // Define the array of leds -CRGB leds[NUM_LEDS]; + +NeoPixelBrightnessBus strip(NUM_LEDS, DATA_PIN); +//NeoPixelBus strip(NUM_LEDS, DATA_PIN); +//NeoPixelBus strip(NUM_LEDS, DATA_PIN); + +RgbwColor red(colorSaturation, 0, 0, 0); +RgbwColor green(0, colorSaturation, 0, 0); +RgbwColor blue(0, 0, colorSaturation, 0); +RgbwColor white(colorSaturation); +RgbwColor black(0); +RgbwColor unknown(30, 48, 40, 0); //darkish dirt +RgbwColor gold(HtmlColor( 0xFFD700 ) ); +RgbwColor orangered(HtmlColor( 0xFF4500 ) ); +RgbwColor orange(HtmlColor( 0xFFA500 ) ); + volatile signed int interruptCounter; @@ -60,8 +77,7 @@ IotWebConfSeparator separator1 = IotWebConfSeparator("LED settings"); IotWebConfParameter brightnessParam = IotWebConfParameter("BRIGHTNESS", "brightness", brightnessParamValue, NUMBER_LEN, "number", "10..254", "100", "min='10' max='254' step='1'"); -void printLocalTime() -{ +void printLocalTime() { if (!getLocalTime(&timeinfo)) { Serial.println("Failed to obtain time"); return; @@ -72,8 +88,7 @@ void printLocalTime() /** Handle web requests to "/" path. */ -void handleRoot() -{ +void handleRoot() { // -- Let IotWebConf test and handle captive portal requests. if (iotWebConf.handleCaptivePortal()) { @@ -159,37 +174,41 @@ void syncNTP() { } } -void setup() -{ +void setup() { //interrupt for one second@80Mhz! timer = timerBegin(0, 80, true); timerAttachInterrupt(timer, &onTimer, true); timerAlarmWrite(timer, 1000000, true); timerAlarmEnable(timer); + pinMode(LDR_PIN, INPUT); + pinMode(DATA_PIN, OUTPUT); + Serial.begin(SERIAL_BAUD); - FastLED.addLeds(leds, NUM_LEDS); - - FastLED.setBrightness(50); - FastLED.clear(); + strip.Begin(); + strip.Show(); //currentSec = 40; currentMin = 1; currentHour = 2; iotWebConf_Setup(); //syncNTP(); } void brightnessSet() { + int BRIGHTNESS = atoi(brightnessParamValue); int x; if (ldrValue == 0) { - FastLED.setBrightness( BRIGHTNESS ); + strip.SetBrightness( BRIGHTNESS ); } else { - x = int(MINIMAL_BRIGHTNESS + ( (BRIGHTNESS - MINIMAL_BRIGHTNESS) / (ldrValue/32) + 1) ); - FastLED.setBrightness( x ); + x = int(MINIMAL_BRIGHTNESS + ( (BRIGHTNESS - MINIMAL_BRIGHTNESS) / (ldrValue / 64) + 1) ); + if (strip.GetBrightness() != x) { + strip.SetBrightness( x ); + } } Serial.println("Brightness: " + String( x ) + "(" + String(ldrValue) + ")"); ldrValue = 0; + } void loop() @@ -203,9 +222,10 @@ void loop() interruptCounter--; currentSec--; portEXIT_CRITICAL(&timerMux); - + //brigthness measurement - ldrValue = (ldrValue + analogRead(ldrPin)) / 2; + ldrValue = (ldrValue + analogRead(LDR_PIN)) / 2; + //Serial.println(ldrValue); if (currentSec < 0) { currentSec = 59; currentMin--; @@ -225,32 +245,32 @@ void loop() } //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); + strip.SetPixelColor((currentSec + 1) % NUM_LEDS, white); + strip.SetPixelColor((currentMin + 1) % NUM_LEDS, white); + strip.SetPixelColor(((currentHour + 1) * 5 - 1) % NUM_LEDS, white); + strip.SetPixelColor(((currentHour + 1) * 5 + 1) % NUM_LEDS, white); //1 dot hour marking for (int dot = 0; dot < NUM_LEDS; dot++) { if (dot % 5 == 0) { - leds[dot] = CRGB::OrangeRed; + strip.SetPixelColor(dot, orangered); } } //3 dots hour if (currentHour == 0) { - leds[59 % NUM_LEDS] = CRGB::Gold; + strip.SetPixelColor(59 % NUM_LEDS, gold); } else { - leds[(currentHour * 5 - 1) % NUM_LEDS] = CRGB::Gold; + strip.SetPixelColor((currentHour * 5 - 1) % NUM_LEDS, gold); } - leds[(currentHour * 5) % NUM_LEDS] = CRGB::Gold; - leds[(currentHour * 5 + 1) % NUM_LEDS] = CRGB::Gold; + strip.SetPixelColor((currentHour * 5) % NUM_LEDS, gold); + strip.SetPixelColor((currentHour * 5 + 1) % NUM_LEDS, gold); - leds[0] = CRGB::Blue; //define high noon - //leds[NUM_LEDS / 2] = CRGB::Green; //define half noon + strip.SetPixelColor(0, blue); //define high noon + //strip.SetPixelColor(NUM_LEDS / 2, CRGB::Green; //define half noon - leds[(currentSec + 0) % NUM_LEDS] = CRGB::Black; - leds[(currentMin + 0) % NUM_LEDS] = CRGB::Orange; - FastLED.show(); + strip.SetPixelColor((currentSec + 0) % NUM_LEDS, black); + strip.SetPixelColor((currentMin + 0) % NUM_LEDS, orange); + strip.Show(); } }