206 lines
5.7 KiB
C++
206 lines
5.7 KiB
C++
#include "hardware/adc.h"
|
|
#include "hardware/clocks.h"
|
|
#include "hardware/pwm.h"
|
|
#include "hardware/rtc.h"
|
|
#include "hardware/spi.h"
|
|
#include "lwip/apps/sntp.h"
|
|
#include "lwip/err.h"
|
|
#include "lwip/ip_addr.h"
|
|
#include "pico/cyw43_arch.h"
|
|
#include "pico/stdlib.h"
|
|
#include "pico/util/datetime.h"
|
|
#include "wifi_conf.h"
|
|
#include <lwip/arch.h>
|
|
#include <pico/stdio.h>
|
|
#include <stdio.h>
|
|
|
|
#include "control.h"
|
|
#include "display.h"
|
|
#include "lwip/dns.h"
|
|
#include "lwip/tcp.h"
|
|
#include "macros.h"
|
|
#include "multicore_events.h"
|
|
#include "net_utils.h"
|
|
#include "pico/multicore.h"
|
|
#include "pins.h"
|
|
#include "sound.h"
|
|
#include "spi.h"
|
|
#include "timezones.h"
|
|
#include "ui.h"
|
|
|
|
#define NTP_DELTA 2208988800UL
|
|
|
|
volatile u16_t timezone_index;
|
|
|
|
extern "C" void sync_system_time(unsigned int sec, unsigned int usec) {
|
|
if (sec == 0) {
|
|
printf("SNTP: Invalid timestamp received\n");
|
|
return;
|
|
}
|
|
|
|
printf("SNTP callback received: %lu\n", (unsigned long)sec);
|
|
|
|
// Convert NTP timestamp to Unix timestamp
|
|
time_t unix_time = (time_t)(sec - NTP_DELTA);
|
|
|
|
struct tm *time_info = gmtime(&unix_time);
|
|
if (time_info == NULL) {
|
|
printf("SNTP: Invalid timestamp %lu\n", (unsigned long)sec);
|
|
return;
|
|
}
|
|
|
|
printf("Got SNTP response: %02d/%02d/%04d %02d:%02d:%02d\n",
|
|
time_info->tm_mday, time_info->tm_mon + 1, time_info->tm_year + 1900,
|
|
time_info->tm_hour, time_info->tm_min, time_info->tm_sec);
|
|
|
|
datetime_t dt = {.year = static_cast<int16_t>(time_info->tm_year + 1900),
|
|
.month = static_cast<int8_t>(time_info->tm_mon + 1),
|
|
.day = static_cast<int8_t>(time_info->tm_mday),
|
|
.dotw = static_cast<int8_t>(time_info->tm_wday),
|
|
.hour = static_cast<int8_t>(time_info->tm_hour),
|
|
.min = static_cast<int8_t>(time_info->tm_min),
|
|
.sec = static_cast<int8_t>(time_info->tm_sec)};
|
|
|
|
timezone_offset(timezone_index, dt);
|
|
|
|
// Set RTC
|
|
if (rtc_set_datetime(&dt)) {
|
|
printf("SNTP: Time synchronized to %04d-%02d-%02d %02d:%02d:%02d %s\n",
|
|
dt.year, dt.month, dt.day, dt.hour, dt.min, dt.sec, "Local");
|
|
} else {
|
|
printf("SNTP: Failed to set RTC\n");
|
|
}
|
|
|
|
// Sanity checks
|
|
if (dt.year < 2020 || dt.year > 2100 || dt.month < 1 || dt.month > 12 ||
|
|
dt.day < 1 || dt.day > 31 || dt.hour > 23 || dt.min > 59 || dt.sec > 59) {
|
|
printf("SNTP: Invalid date/time components\n");
|
|
return;
|
|
}
|
|
|
|
// Set RTC with error handling
|
|
if (rtc_set_datetime(&dt)) {
|
|
printf("SNTP: Time synchronized to %04d-%02d-%02d %02d:%02d:%02d %s\n",
|
|
dt.year, dt.month, dt.day, dt.hour, dt.min, dt.sec, "Local");
|
|
} else {
|
|
printf("SNTP: Failed to set RTC\n");
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
stdio_init_all();
|
|
if (cyw43_arch_init()) {
|
|
return 1;
|
|
}
|
|
rtc_init();
|
|
gpio_init(KNOB_PW_PIN);
|
|
gpio_set_dir(KNOB_PW_PIN, GPIO_OUT);
|
|
gpio_put(KNOB_PW_PIN, 1);
|
|
|
|
gpio_init(BUTTON_PRIMARY_PW);
|
|
gpio_init(BUTTON_SECONDARY_PW);
|
|
gpio_init(BUTTON_PRIMARY_IN);
|
|
gpio_init(BUTTON_SECONDARY_IN);
|
|
|
|
gpio_set_dir(BUTTON_PRIMARY_PW, GPIO_OUT);
|
|
gpio_set_dir(BUTTON_SECONDARY_PW, GPIO_OUT);
|
|
gpio_put(BUTTON_PRIMARY_PW, 1);
|
|
gpio_put(BUTTON_SECONDARY_PW, 1);
|
|
|
|
gpio_set_dir(BUTTON_PRIMARY_IN, GPIO_IN);
|
|
gpio_init(BUTTON_PRIMARY_IN);
|
|
|
|
adc_init();
|
|
adc_gpio_init(KNOB_SIG_PIN);
|
|
adc_select_input(0);
|
|
|
|
setup_pwm_audio();
|
|
|
|
timezone_index = 353; // todo load from flash
|
|
gpio_init(LED_PIN);
|
|
gpio_set_dir(LED_PIN, GPIO_OUT);
|
|
|
|
// sign of life
|
|
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, true);
|
|
|
|
display_start();
|
|
|
|
// Timer example code - This example fires off the callback after 2000ms
|
|
// add_alarm_in_ms(2000, alarm_callback, NULL, false);
|
|
|
|
printf("connecting to wifi\n");
|
|
print_time(); // show NO SYNC ERROR
|
|
// connect to wifi
|
|
cyw43_arch_enable_sta_mode();
|
|
if (cyw43_arch_wifi_connect_timeout_ms(wifi_ssid, wifi_pass,
|
|
CYW43_AUTH_WPA2_AES_PSK, 30000)) {
|
|
return 1;
|
|
}
|
|
|
|
printf("IP: %s\n", ip4addr_ntoa(netif_ip_addr4(netif_default)));
|
|
printf("Mask: %s\n", ip4addr_ntoa(netif_ip_netmask4(netif_default)));
|
|
printf("Gateway: %s\n", ip4addr_ntoa(netif_ip_gw4(netif_default)));
|
|
|
|
{
|
|
ip_addr_t ip_addr;
|
|
IP_ADDR4(&ip_addr, 1, 1, 1, 1);
|
|
dns_setserver(0, &ip_addr);
|
|
|
|
IP_ADDR4(&ip_addr, 8, 8, 8, 8);
|
|
dns_setserver(1, &ip_addr);
|
|
|
|
printf("Initializing SNTP\n");
|
|
sntp_setoperatingmode(SNTP_OPMODE_POLL);
|
|
sntp_init();
|
|
|
|
dns_resolve_sync("ntp.cesnet.cz", &ip_addr, 10000);
|
|
sntp_setserver(0, &ip_addr);
|
|
|
|
dns_resolve_sync("ntp0.fau.de", &ip_addr, 10000);
|
|
sntp_setserver(1, &ip_addr);
|
|
}
|
|
|
|
printf("SNTP initialized, waiting for time sync...\n");
|
|
|
|
uint8_t page_selected = 1;
|
|
|
|
multicore_launch_core1(page_functions[page_selected]);
|
|
|
|
uint8_t knob_position = get_knob_percentage();
|
|
while (true) {
|
|
if (gpio_get(BUTTON_SECONDARY_IN)) {
|
|
while (gpio_get(BUTTON_SECONDARY_IN)) {
|
|
tight_loop_contents();
|
|
} // await until button is released
|
|
|
|
if (!secondary_button_override) {
|
|
page_selected =
|
|
page_selected + 1 >= page_functions.size() ? 0 : page_selected + 1;
|
|
multicore_reset_core1();
|
|
clear_display();
|
|
multicore_launch_core1(page_functions[page_selected]);
|
|
} else {
|
|
multicore_fifo_push_timeout_us(SECONDARY_BUTTON_PRESSED, 10);
|
|
}
|
|
}
|
|
if (gpio_get(BUTTON_PRIMARY_IN)) {
|
|
while (gpio_get(BUTTON_PRIMARY_IN)) {
|
|
tight_loop_contents();
|
|
}
|
|
multicore_fifo_push_timeout_us(PRIMARY_BUTTON_PRESSED, 10);
|
|
}
|
|
|
|
{
|
|
uint8_t knob_now = get_knob_percentage();
|
|
if (knob_now != knob_position) {
|
|
knob_position = knob_now;
|
|
multicore_fifo_push_timeout_us(KNOB_CHANGE, 10);
|
|
}
|
|
}
|
|
|
|
sleep_ms(10);
|
|
}
|
|
cyw43_arch_lwip_end();
|
|
return 0;
|
|
}
|