// C++ code
//
#include <WiFi.h>
#include <Adafruit_NeoPixel.h>
#include "text.h"
#define NTP_SERVER1 "ntp.aliyun.com"
#define NTP_SERVER2 "ntp.tencent.com"
#define NTP_SERVER3 "ntp.sjtu.edu.cn"
#define UTC_OFFSET 8 * 3600
#define UTC_OFFSET_DST 0
#define LED_PIN 21
#define LED_COUNT 224
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
uint32_t color = strip.Color(255, 0, 0);
bool showDot = true;
int brightness = 255;
void setup()
{
Serial.begin(115200);
strip.begin();
strip.setBrightness(brightness);
display_text(set_wifi);
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
display_text(connect);
delay(500);
Serial.print("WiFi status:");
Serial.println(WiFi.status());
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.println("online");
display_text(online);
delay(1000);
Serial.println("get time");
// // LCD.println("Updating time...");
configTime(UTC_OFFSET, UTC_OFFSET_DST, NTP_SERVER1, NTP_SERVER2, NTP_SERVER3);
delay(2000);
}
void loop()
{
delay(500);
showTime();
}
void showTime()
{
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
display_text(connect_err);
Serial.println("Connection Err");
return;
}
strip.clear();
int hour1 = timeinfo.tm_hour / 10; // 小时十位数字
int hour2 = timeinfo.tm_hour % 10; // 小时个位数字
int minute1 = timeinfo.tm_min / 10; // 分钟十位数字
int minute2 = timeinfo.tm_min % 10; // 分钟个位数字
// 显示小时十位数字
display_pixel(0, (int**)digit[hour1], 5);
// 显示小时个位数字
display_pixel(6, (int**)digit[hour2], 5);
// 显示分钟十位数字
display_pixel(15, (int**)digit[minute1], 5);
// 显示分钟个位数字
display_pixel(21, (int**)digit[minute2], 5);
// 显示冒号
display_pixel(12, showDot ? (int**)dot_show : (int**)dot_hide, 2);
showDot = !showDot;
// 显示星期
display_pixel(27, (int**)weeks[timeinfo.tm_wday], 5);
strip.show();
Serial.print(timeinfo.tm_hour);
Serial.print(":");
Serial.print(timeinfo.tm_min);
Serial.print(":");
Serial.print(timeinfo.tm_sec);
Serial.print(" ");
Serial.println(timeinfo.tm_wday);
}
void display_pixel(int start_col, int** data, int cols)
{
for(int row = 0; row < 7; row++)
{
for(int col = 0; col < cols; col++)
{
int index = 32 * row + col + start_col;
if(*((int*)data + row * cols + col) == 1)
{
strip.setPixelColor(index, color);
}
}
}
}
// void display_dot(int data[7][2]){
// for(int row = 0; row < 7; row++)
// {
// for(int col = 0; col < 2; col++)
// {
// int index = 32 * row + col + 12;
// if(data[row][col] == 1)
// {
// strip.setPixelColor(index, color);
// }
// }
// }
// }
// void display_week(int week){
// for(int row = 0; row < 7; row++)
// {
// for(int col = 0; col < 5; col++)
// {
// int index = 32 * row + col + 27;
// if(weeks[week - 1][row][col] == 1)
// {
// strip.setPixelColor(index, color);
// }
// }
// }
// }
void display_text(int data[7][32]){
strip.clear();
for(int row = 0; row < 7; row++)
{
for(int col = 0; col < 32; col++)
{
int index = 32 * row + col;
if(data[row][col] == 1)
{
strip.setPixelColor(index, color);
}
}
}
strip.show();
}