// ESP32 steuert eine touch table Matrix mit WS2816
// Anton Burger Design 2023
#include <WiFi.h>
#include <Wire.h>
#include <FastLED.h>
// #include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
#define TRIGGER_PIN 23
// FASTLED
#define NUM_SEGMENTS 7
#define NUM_LEDS_PER_SEGMENTS 5
#define NUM_DIGITS 4 // 0-3
#define NUM_DOUBLE_POINTS 4
#define NUM_LEDS ( NUM_SEGMENTS * NUM_LEDS_PER_SEGMENTS * NUM_DIGITS ) + NUM_DOUBLE_POINTS
#define DATA_PIN 13
#define MAX_BRIGHTNESS 200
#define MIN_BRIGHTNESS 50
#define FRAMES_PER_SECOND 60
CRGB leds[NUM_LEDS];
const bool digits[14][7] =
{ // G B A F E D C
{0,1,1,1,1,1,1}, //0
{0,1,0,0,0,0,1}, //1
{1,1,1,0,1,1,0}, //2
{1,1,1,0,0,1,1}, //3
{1,1,0,1,0,0,1}, //4
{1,0,1,1,0,1,1}, //5
{1,0,1,1,1,1,1}, //6
{0,1,1,0,0,0,1}, //7
{1,1,1,1,1,1,1}, //8
{1,1,1,1,0,1,1}, //9
{1,0,1,1,1,1,0}, //E
{1,0,0,0,1,0,0}, //r
{1,0,0,0,1,1,1}, //o
{0,0,0,0,0,0,0} //blank
};
#define NTP_SERVER "pool.ntp.org"
#define UTC_OFFSET 3600
#define UTC_OFFSET_DST 3600
//---------------------------------------------------------------------------
void displayTime() {
//static uint8_t lastMinute;
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Connection Err");
displayErr();
return;
}
uint8_t hours = timeinfo.tm_hour;
uint8_t minutes = timeinfo.tm_min;
uint8_t secounds = timeinfo.tm_sec;
/*
if (timeinfo.tm_min==59){
for (int i=0; i<100; i++) {
}
}
*/
display(hours, 0);
display(minutes, 2);
//display(secounds, 4);
Serial.print(&timeinfo, "%H:%M:%S ");
Serial.println(&timeinfo, "%d.%m.%Y");
}
//---------------------------------------------------------------------------
void display(uint8_t zahl, uint8_t digit) {
int z = (zahl/10)%10;
if (z==0) z=13; //Eine führende Null nicht anzeigen
displayDigit (digit, z ); //Zehner
displayDigit (digit+1, zahl%10 ); //Einer
}
//---------------------------------------------------------------------------
// SETUP
//---------------------------------------------------------------------------
void setup() {
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(MAX_BRIGHTNESS);
Serial.begin(115200);
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
configTime(UTC_OFFSET, UTC_OFFSET_DST, NTP_SERVER);
displayErr();
delay(1000);
}
//---------------------------------------------------------------------------
void fillStrip(const struct CRGB &color) {
fill_solid(leds, NUM_LEDS, color);
FastLED.show();
}
//---------------------------------------------------------------------------
void displayDigit(uint8_t digit, uint8_t number) {
// digit 0-3
if (digit >= NUM_DIGITS ) {
Serial.println("displayDigit: value of digit to high!");
return;
}
int pos = NUM_LEDS_PER_SEGMENTS * NUM_SEGMENTS * digit;
for (int i=0; i<NUM_SEGMENTS; i++) {
if(digits[number][i]==1)
for (int x=0; x<NUM_LEDS_PER_SEGMENTS; x++) { leds[pos++]=CRGB::Red;}
else
for (int x=0; x<NUM_LEDS_PER_SEGMENTS; x++) { leds[pos++]=CRGB::Black;}
}
}
//---------------------------------------------------------------------------
void displayDoubleDot(boolean sec_switch) {
// Punkte zwischen Stunden und Minuten
int pos = NUM_LEDS_PER_SEGMENTS * NUM_SEGMENTS * NUM_DIGITS;
for (int i=0; i < NUM_DOUBLE_POINTS; i++) {
if(sec_switch)
leds[pos++] = CRGB::Red;
else
leds[pos++] = CRGB(0, 0, 0);
}
}
//---------------------------------------------------------------------------
void clearDisplay() {
fill_solid(leds, NUM_LEDS, CRGB(0, 0, 0));
FastLED.show();
}
//---------------------------------------------------------------------------
void displayErr() {
Serial.println("Err");
clearDisplay();
displayDigit(0, 10);
displayDigit(1, 11);
displayDigit(2, 11);
displayDigit(3, 12);
displayDigit(4, 11);
FastLED.show();
}
//---------------------------------------------------------------------------
void loop() {
static boolean sec_switch = false;
displayTime();
displayDoubleDot(sec_switch);
sec_switch = !sec_switch;
FastLED.show();
delay(1000);
}