Removal of ntp_client_plus files, refactoring to use time C lib instead.

This commit is contained in:
2024-04-01 03:32:54 +02:00
parent 52c7794d59
commit 73aa168152
5 changed files with 139 additions and 885 deletions

View File

@@ -29,15 +29,14 @@
#include <EEPROM.h> //from ESP8266 Arduino Core (automatically installed when ESP8266 was installed via Boardmanager)
#include <ESP8266WebServer.h>
#include <ESP8266WiFi.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager WiFi Configuration Magic
#include <WiFiUdp.h>
#include <time.h>
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager WiFi Configuration Magic
// own libraries
#include "animation_functions.h"
#include "base64_wrapper.h" // copied from https://github.com/Xander-Electronics/Base64
#include "led_matrix.h"
#include "littlefs_wrapper.h"
#include "ntp_client_plus.h"
#include "ota_functions.h"
#include "pong.h"
#include "render_functions.h"
@@ -49,14 +48,15 @@
// ----------------------------------------------------------------------------------
// GLOBAL VARIABLES
// ----------------------------------------------------------------------------------
UDPLogger logger; // Logger
Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(MATRIX_WIDTH, MATRIX_HEIGHT + 1, NEOPIXEL_PIN,
NEO_MATRIX_TOP + NEO_MATRIX_LEFT + NEO_MATRIX_ROWS + NEO_MATRIX_ZIGZAG,
NEO_GRB + NEO_KHZ800); // NeoMatrix
UDPLogger logger; // Logger
char strftime_buf[64]; // Time string buffer
ESP8266WebServer webserver(HTTP_PORT); // Webserver
LEDMatrix led_matrix = LEDMatrix(&matrix, DEFAULT_BRIGHTNESS, &logger); // NeoMatrix wrapper
WiFiUDP wifi_udp;
NTPClientPlus ntp_client = NTPClientPlus(wifi_udp, NTP_SERVER_URL, 1, true);
struct tm timeinfo; // Structure tm holds time information
time_t now; // Seconds since Epoch (1970) - UTC
// ----------------------------------------------------------------------------------
// STATIC VARIABLES
@@ -70,6 +70,8 @@ static Pong pong = Pong(&led_matrix, &logger);
static Snake snake = Snake(&led_matrix, &logger);
static Tetris tetris = Tetris(&led_matrix, &logger);
static uint32 last_ntp_update_us = 0; // time of last NTP update
static bool flg_night_mode = false; // state of nightmode
static bool flg_reset_wifi_creds = false; // used to reset stored wifi credentials
static bool spiral_direction = false;
@@ -87,7 +89,7 @@ static const uint32_t period_timings[NUM_STATES] = {PERIOD_CLOCK_UPDATE_US, PERI
PERIOD_ANIMATION_US, PERIOD_TETRIS_US, PERIOD_SNAKE_US,
PERIOD_PONG_US, PERIOD_ANIMATION_US};
// Quarterly brightness factor for dynamic brightness
// Quarterly brightness factor for dynamic brightness (4 quarters a 24 hours)
static const float qtly_brightness_factor[96] = {
0.0f, 0.0f, 0.0f, 0.001f, 0.003f, 0.007f, 0.014f, 0.026f, 0.044f, 0.069f, 0.101f, 0.143f, 0.194f, 0.253f, 0.32f,
0.392f, 0.468f, 0.545f, 0.62f, 0.691f, 0.755f, 0.811f, 0.858f, 0.896f, 0.927f, 0.949f, 0.966f, 0.978f, 0.986f,
@@ -212,17 +214,13 @@ void setup()
}
// setup NTP
ntp_client.setup_ntp_client();
logger.log_string("NTP running");
logger.log_string("Time: " + ntp_client.get_formatted_time());
logger.log_string("TimeOffset (seconds): " + String(ntp_client.get_time_offset()));
configTime(MY_TZ, NTP_SERVER_URL);
ntp_time_update(&last_ntp_update_us); // NTP time update
// show the current time for short time in words
int hours = ntp_client.get_hours_24();
int minutes = ntp_client.get_minutes();
String timeMessage = time_to_string(hours, minutes);
String timeMessage = time_to_string(timeinfo.tm_hour, timeinfo.tm_min);
show_string_on_clock(timeMessage, main_color_clock);
draw_minute_indicator(minutes, main_color_clock);
draw_minute_indicator(timeinfo.tm_min, main_color_clock);
led_matrix.draw_on_matrix_smooth(filter_factor);
// init all animation modes
@@ -233,7 +231,7 @@ void setup()
// init random tetris
random_tetris(true);
// Range limits
// Set range limits
limit_value_ranges();
logger.log_string("Nightmode starts at: " + String(night_mode_times_ps->start_hour) + ":" + String(night_mode_times_ps->start_min));
@@ -256,7 +254,6 @@ void loop()
static uint32 last_matrix_update_us = 0; // time of last Matrix update
static uint32 last_heartbeat_us = 0; // time of last heartbeat sending
static uint32 last_nightmode_check_us = 0; // time of last nightmode check
static uint32 last_ntp_update_us = 0; // time of last NTP update
handleOTA(); // handle OTA
@@ -313,69 +310,95 @@ void loop()
// OTHER FUNCTIONS
// ----------------------------------------------------------------------------------
/**
* @brief Updates the NTP time
*
* @return boolean - true if NTP update was successful, false otherwise
*/
bool get_ntp_time(uint32 sec)
{
uint32 start = system_get_time() / 1000; // ms
do
{
time(&now);
localtime_r(&now, &timeinfo);
delay(10);
} while (((system_get_time() / 1000 - start) <= (1000 * sec)) && (timeinfo.tm_year < (2023 - 1900)));
if (timeinfo.tm_year <= (2023 - 1900))
{
return false; // the NTP call was not successful
}
else
{
return true;
}
}
void log_time(tm local_time)
{
strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
logger.log_string(String(strftime_buf));
}
void handle_current_state()
{
switch (current_state)
{
case ST_CLOCK: // state clock
{
int hours = ntp_client.get_hours_24();
int minutes = ntp_client.get_minutes();
(void)show_string_on_clock(time_to_string((uint8_t)hours, (uint8_t)minutes), main_color_clock);
draw_minute_indicator((uint8_t)minutes, main_color_clock);
break;
}
case ST_DICLOCK: // state diclock
{
int hours = ntp_client.get_hours_24();
int minutes = ntp_client.get_minutes();
show_digital_clock((uint8_t)hours, (uint8_t)minutes, main_color_clock);
break;
}
case ST_SPIRAL: // state spiral
{
int res = draw_spiral(false, spiral_direction, MATRIX_WIDTH - 2);
if ((bool)res && spiral_direction == 0)
case ST_CLOCK: // state clock
{
// change spiral direction to closing (draw empty leds)
spiral_direction = true;
// init spiral with new spiral direction
draw_spiral(true, spiral_direction, MATRIX_WIDTH - 1);
(void)show_string_on_clock(time_to_string((uint8_t)timeinfo.tm_hour, (uint8_t)timeinfo.tm_min), main_color_clock);
draw_minute_indicator((uint8_t)timeinfo.tm_min, main_color_clock);
break;
}
else if (res && spiral_direction == 1)
case ST_DICLOCK: // state diclock
{
// reset spiral direction to normal drawing leds
spiral_direction = false;
// init spiral with new spiral direction
draw_spiral(true, spiral_direction, MATRIX_WIDTH - 1);
show_digital_clock((uint8_t)timeinfo.tm_hour, (uint8_t)timeinfo.tm_min, main_color_clock);
break;
}
case ST_SPIRAL: // state spiral
{
int res = draw_spiral(false, spiral_direction, MATRIX_WIDTH - 2);
if ((bool)res && spiral_direction == 0)
{
// change spiral direction to closing (draw empty leds)
spiral_direction = true;
// init spiral with new spiral direction
draw_spiral(true, spiral_direction, MATRIX_WIDTH - 1);
}
else if (res && spiral_direction == 1)
{
// reset spiral direction to normal drawing leds
spiral_direction = false;
// init spiral with new spiral direction
draw_spiral(true, spiral_direction, MATRIX_WIDTH - 1);
}
break;
}
case ST_TETRIS: // state tetris
{
tetris.loopCycle();
break;
}
case ST_SNAKE: // state snake
{
snake.loopCycle();
break;
}
case ST_PINGPONG: // state ping pong
{
pong.loopCycle();
break;
}
case ST_HEARTS:
{
draw_heart_animation();
break;
}
default:
{
break;
}
break;
}
case ST_TETRIS: // state tetris
{
tetris.loopCycle();
break;
}
case ST_SNAKE: // state snake
{
snake.loopCycle();
break;
}
case ST_PINGPONG: // state ping pong
{
pong.loopCycle();
break;
}
case ST_HEARTS:
{
draw_heart_animation();
break;
}
default:
{
break;
}
}
}
@@ -424,8 +447,8 @@ void check_wifi_status()
void check_night_mode()
{
// check if nightmode need to be activated
int hours = ntp_client.get_hours_24();
int minutes = ntp_client.get_minutes();
int hours = timeinfo.tm_hour;
int minutes = timeinfo.tm_min;
if ((hours == night_mode_times_ps->start_hour) && (minutes == night_mode_times_ps->start_min))
{
@@ -445,44 +468,19 @@ void check_night_mode()
void ntp_time_update(uint32 *last_ntp_update_us)
{
// NTP time update
int ntp_retval = ntp_client.update_ntp();
bool ntp_retval = get_ntp_time(*last_ntp_update_us / 10000000);
switch (ntp_retval)
if (ntp_retval == true)
{
case NTP_UPDATE_SUCCESS:
{
ntp_client.calc_date();
logger.log_string("NTP-Update successful, Time: " + ntp_client.get_formatted_time());
log_time(timeinfo);
*last_ntp_update_us = system_get_time();
watchdog_counter = 30;
break;
}
case NTP_UPDATE_TIMEOUT:
else
{
logger.log_string("NTP-Update not successful. Reason: Timeout");
logger.log_string("NTP-Update was not successful.");
*last_ntp_update_us += 10000000;
watchdog_counter--;
break;
}
case NTP_UPDATE_DIFFTOOHIGH:
{
logger.log_string("NTP-Update not successful. Reason: Too large time difference");
logger.log_string("Time: " + ntp_client.get_formatted_time());
logger.log_string("Date: " + ntp_client.get_formatted_date());
logger.log_string("TimeOffset (seconds): " + String(ntp_client.get_time_offset()));
logger.log_string("Summertime: " + String(ntp_client.check_daylight_saving_time()));
*last_ntp_update_us += 10000000;
watchdog_counter--;
break;
}
case NTP_UPDATE_TIME_INVALID:
default:
{
logger.log_string("NTP-Update not successful. Reason: NTP time not valid (<1970)");
*last_ntp_update_us += 10000000;
watchdog_counter--;
break;
}
}
logger.log_string("Watchdog counter: " + String(watchdog_counter));
@@ -504,34 +502,34 @@ void on_state_entry(uint8_t state)
filter_factor = 0.5f;
switch (state)
{
case ST_SPIRAL:
{
spiral_direction = 0; // Init spiral with normal drawing mode
draw_spiral(true, spiral_direction, MATRIX_WIDTH - 1);
break;
}
case ST_TETRIS:
{
filter_factor = 1.0f; // no smoothing
tetris.ctrlStart();
break;
}
case ST_SNAKE:
{
filter_factor = 1.0f; // no smoothing
snake.initGame();
break;
}
case ST_PINGPONG:
{
filter_factor = 1.0f; // no smoothing
pong.initGame(1);
break;
}
default:
{
break;
}
case ST_SPIRAL:
{
spiral_direction = 0; // Init spiral with normal drawing mode
draw_spiral(true, spiral_direction, MATRIX_WIDTH - 1);
break;
}
case ST_TETRIS:
{
filter_factor = 1.0f; // no smoothing
tetris.ctrlStart();
break;
}
case ST_SNAKE:
{
filter_factor = 1.0f; // no smoothing
snake.initGame();
break;
}
case ST_PINGPONG:
{
filter_factor = 1.0f; // no smoothing
pong.initGame(1);
break;
}
default:
{
break;
}
}
}
@@ -1032,9 +1030,9 @@ uint8_t update_brightness()
{
new_brightness = calculate_dynamic_brightness(brightness_ps->dyn_brightness_min,
brightness_ps->dyn_brightness_max,
ntp_client.get_hours_24(),
ntp_client.get_minutes(),
ntp_client.check_daylight_saving_time());
timeinfo.tm_hour,
timeinfo.tm_min,
timeinfo.tm_isdst);
}
else // use static brightness
{