refacturing
This commit is contained in:
parent
d98c72cb9f
commit
225f9eb989
4 changed files with 54 additions and 24 deletions
|
@ -2,6 +2,7 @@
|
|||
|
||||
set(SOURCES
|
||||
ws2812.cpp
|
||||
ntp_client.cpp
|
||||
abfall.cpp
|
||||
)
|
||||
|
||||
|
|
|
@ -11,8 +11,8 @@
|
|||
#include "pico/util/datetime.h"
|
||||
|
||||
#include "lwip/apps/http_client.h"
|
||||
#include "lwip/apps/sntp.h"
|
||||
|
||||
#include "ntp_client.h"
|
||||
#include "ws2812.h"
|
||||
|
||||
using std::string;
|
||||
|
@ -97,22 +97,7 @@ err_t body_callback(void *arg, struct altcp_pcb *conn, struct pbuf *p, err_t err
|
|||
return ERR_OK;
|
||||
}
|
||||
|
||||
// Called by lwip sntp in order to set the time
|
||||
extern "C" void set_system_time(u32_t sec) {
|
||||
time_t epoch = sec;
|
||||
struct tm *utc = gmtime(&epoch);
|
||||
datetime_t datetime;
|
||||
datetime.year = utc->tm_year + 1900;
|
||||
datetime.month = utc->tm_mon + 1;
|
||||
datetime.day = utc->tm_mday;
|
||||
datetime.hour = utc->tm_hour;
|
||||
datetime.min = utc->tm_min;
|
||||
datetime.sec = utc->tm_sec;
|
||||
datetime.dotw = utc->tm_wday;
|
||||
if (rtc_set_datetime(&datetime) == true) {
|
||||
printf("RTC successfully set.\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
const string ssid{"Apis cerana"};
|
||||
|
@ -147,13 +132,7 @@ int main() {
|
|||
&settings, body_callback, nullptr, nullptr);
|
||||
// printf("Status %d\n", err);
|
||||
|
||||
sntp_setoperatingmode(SNTP_OPMODE_POLL);
|
||||
sntp_init();
|
||||
|
||||
// It takes some time for the system to set the RTC. Wait, until the RTC runs properly.
|
||||
while (!rtc_running()) {
|
||||
sleep_ms(100);
|
||||
}
|
||||
NtpClient::setDateTime();
|
||||
|
||||
char datetime_buf[256];
|
||||
char *datetime_str = &datetime_buf[0];
|
||||
|
|
33
src/ntp_client.cpp
Normal file
33
src/ntp_client.cpp
Normal file
|
@ -0,0 +1,33 @@
|
|||
#include "ntp_client.h"
|
||||
|
||||
#include "hardware/rtc.h"
|
||||
|
||||
#include "pico/util/datetime.h"
|
||||
|
||||
|
||||
// Called by lwip sntp in order to set the time
|
||||
void set_system_time(u32_t sec) {
|
||||
time_t epoch = sec;
|
||||
struct tm *utc = gmtime(&epoch);
|
||||
datetime_t datetime;
|
||||
datetime.year = utc->tm_year + 1900;
|
||||
datetime.month = utc->tm_mon + 1;
|
||||
datetime.day = utc->tm_mday;
|
||||
datetime.hour = utc->tm_hour;
|
||||
datetime.min = utc->tm_min;
|
||||
datetime.sec = utc->tm_sec;
|
||||
datetime.dotw = utc->tm_wday;
|
||||
if (rtc_set_datetime(&datetime) == true) {
|
||||
printf("RTC successfully set.\n");
|
||||
}
|
||||
}
|
||||
|
||||
void NtpClient::setDateTime() {
|
||||
sntp_setoperatingmode(SNTP_OPMODE_POLL);
|
||||
sntp_init();
|
||||
|
||||
// It takes some time for the system to set the RTC. Wait, until the RTC runs properly.
|
||||
while (!rtc_running()) {
|
||||
sleep_ms(500);
|
||||
}
|
||||
}
|
17
src/ntp_client.h
Normal file
17
src/ntp_client.h
Normal file
|
@ -0,0 +1,17 @@
|
|||
#pragma once
|
||||
|
||||
#include <ctime>
|
||||
|
||||
#include "pico/stdlib.h"
|
||||
|
||||
#include "lwip/apps/sntp.h"
|
||||
|
||||
extern "C" void set_system_time(u32_t sec);
|
||||
|
||||
class NtpClient {
|
||||
public:
|
||||
static void setDateTime();
|
||||
|
||||
private:
|
||||
NtpClient() {} // Disallow creating an instance of this object
|
||||
};
|
Loading…
Reference in a new issue