initial commit
This commit is contained in:
143
src/main.cpp
Executable file
143
src/main.cpp
Executable file
@@ -0,0 +1,143 @@
|
||||
#include <Adafruit_NeoPixel.h>
|
||||
#include <NTPClient.h>
|
||||
#include <ArduinoOTA.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <WiFiUdp.h>
|
||||
#include "config.h"
|
||||
#include "colors.h"
|
||||
|
||||
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
|
||||
|
||||
WiFiUDP ntpUDP;
|
||||
NTPClient timeClient(ntpUDP, NTP_SERVER, 60 * 60);
|
||||
|
||||
const int LEDS_HOURS_1[] = { 2, 3, 4, 5 };
|
||||
const int LEDS_HOURS_2[] = { 1, 0 };
|
||||
const int LEDS_MINUTES_1[] = { 9, 10, 11, 12 };
|
||||
const int LEDS_MINUTES_2[] = { 8, 7, 6 };
|
||||
const int LEDS_SECONDS_1[] = { 16, 17, 18, 19 };
|
||||
const int LEDS_SECONDS_2[] = { 15, 14, 13 };
|
||||
|
||||
rgb start = int2rgb(0xff0000);
|
||||
rgb end = int2rgb(0x49cb2c);
|
||||
|
||||
void display_progress(int percentage)
|
||||
{
|
||||
pixels.clear();
|
||||
int activeLedCount = (int)(NUMPIXELS / 100.0 * percentage);
|
||||
|
||||
|
||||
for(int i=0; i<activeLedCount; i++)
|
||||
{
|
||||
auto color = gradient(start, end, i/(double)NUMPIXELS);
|
||||
pixels.setPixelColor(i, color.r, color.g, color.b);
|
||||
}
|
||||
|
||||
pixels.show();
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
pixels.begin();
|
||||
pixels.clear();
|
||||
Serial.begin(9600);
|
||||
|
||||
|
||||
WiFi.hostname("BinaryClock2");
|
||||
WiFi.begin(SSID, PASSWORD);
|
||||
|
||||
int i=0;
|
||||
while(WiFi.status() != WL_CONNECTED)
|
||||
{
|
||||
delay(500);
|
||||
auto color = gradient(start, end, i/(double)NUMPIXELS);
|
||||
pixels.setPixelColor(i++, color.r, color.g, color.b);
|
||||
pixels.show();
|
||||
if(i >= NUMPIXELS)
|
||||
{
|
||||
i = 0;
|
||||
pixels.clear();
|
||||
}
|
||||
}
|
||||
|
||||
if(WiFi.isConnected())
|
||||
{
|
||||
Serial.print(WiFi.localIP().toString());
|
||||
|
||||
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total)
|
||||
{
|
||||
try
|
||||
{
|
||||
display_progress(progress / (total / 100.0));
|
||||
}
|
||||
catch(const std::exception& e)
|
||||
{
|
||||
}
|
||||
});
|
||||
ArduinoOTA.onEnd([]()
|
||||
{
|
||||
for(int i=0; i<NUMPIXELS; i++)
|
||||
{
|
||||
pixels.setPixelColor(i, 0x51, 0x6f, 0xf6);
|
||||
pixels.show();
|
||||
|
||||
delay(40);
|
||||
}
|
||||
pixels.clear();
|
||||
pixels.show();
|
||||
});
|
||||
|
||||
timeClient.begin();
|
||||
ArduinoOTA.begin();
|
||||
}
|
||||
}
|
||||
|
||||
void displayBinary(int value, const int* leds, uint32_t color)
|
||||
{
|
||||
int index = 0;
|
||||
while(value != 0)
|
||||
{
|
||||
bool isOn = value & 0b1;
|
||||
value = value >> 1;
|
||||
if(isOn)
|
||||
pixels.setPixelColor(leds[index], color);
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
if(WiFi.isConnected())
|
||||
{
|
||||
ArduinoOTA.handle();
|
||||
timeClient.update();
|
||||
}
|
||||
|
||||
int seconds = timeClient.getSeconds();
|
||||
int minutes = timeClient.getMinutes();
|
||||
int hours = timeClient.getHours();
|
||||
int hours_1 = hours % 10;
|
||||
int hours_2 = (hours / 10) % 10;
|
||||
int minutes_1 = minutes % 10;
|
||||
int minutes_2 = (minutes / 10) % 10;
|
||||
int seconds_1 = seconds % 10;
|
||||
int seconds_2 = (seconds / 10) % 10;
|
||||
|
||||
int palette_index = hours % PALETTE_COUNT;
|
||||
uint32_t color_hours = PALETTES[palette_index];
|
||||
uint32_t color_minutes = PALETTES[palette_index + 1];
|
||||
uint32_t color_seconds = PALETTES[palette_index + 2];
|
||||
|
||||
pixels.clear();
|
||||
|
||||
displayBinary(hours_1, &LEDS_HOURS_1[0], color_hours);
|
||||
displayBinary(hours_2, &LEDS_HOURS_2[0], color_hours);
|
||||
displayBinary(minutes_1, &LEDS_MINUTES_1[0], color_minutes);
|
||||
displayBinary(minutes_2, &LEDS_MINUTES_2[0], color_minutes);
|
||||
displayBinary(seconds_1, &LEDS_SECONDS_1[0], color_seconds);
|
||||
displayBinary(seconds_2, &LEDS_SECONDS_2[0], color_seconds);
|
||||
|
||||
pixels.show();
|
||||
|
||||
delay(100);
|
||||
}
|
||||
Reference in New Issue
Block a user