binary_clock/src/main.cpp

156 lines
3.5 KiB
C++
Executable File

#include <Adafruit_NeoPixel.h>
#include <NTPClient.h>
#include <ArduinoOTA.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <SPI.h>
#include <RTClib.h>
#include "config.h"
#include "colors.h"
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
WiFiUDP ntpUDP;
RTC_DS1307 rtc;
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()
{
rtc.begin();
pixels.begin();
pixels.clear();
Serial.begin(9600);
WiFi.hostname("BinaryClock2");
WiFi.begin(SSID, PASSWORD);
int i=0;
int j=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;
j++;
pixels.clear();
if(rtc.isrunning() && j == 2)
break;
}
}
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();
rtc.adjust(DateTime(2024, 1, 1, timeClient.getHours(), timeClient.getMinutes(), timeClient.getSeconds()));
}
auto now = rtc.now();
int seconds = now.second();
int minutes = now.minute();
int hours = now.hour();
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);
}