add timesync and timezone setup

This commit is contained in:
sharandac
2020-07-21 00:26:48 +02:00
parent 078ad1505b
commit 1221662cc0
15 changed files with 397 additions and 56 deletions

View File

@@ -5,8 +5,6 @@
display_config_t display_config;
void display_read_config( void );
/*
*
*/
@@ -27,10 +25,10 @@ void display_loop( TTGOClass *ttgo ) {
*
*/
void display_save_config( void ) {
fs::File file = SPIFFS.open( DISPLAY_COFIG_FILE, FILE_WRITE );
fs::File file = SPIFFS.open( DISPLAY_CONFIG_FILE, FILE_WRITE );
if ( !file ) {
Serial.printf("Can't save file: %s\r\n", DISPLAY_COFIG_FILE );
Serial.printf("Can't save file: %s\r\n", DISPLAY_CONFIG_FILE );
}
else {
file.write( (uint8_t *)&display_config, sizeof( display_config ) );
@@ -42,10 +40,10 @@ void display_save_config( void ) {
*
*/
void display_read_config( void ) {
fs::File file = SPIFFS.open( DISPLAY_COFIG_FILE, FILE_READ );
fs::File file = SPIFFS.open( DISPLAY_CONFIG_FILE, FILE_READ );
if (!file) {
Serial.printf("Can't open file: %s!\r\n", DISPLAY_COFIG_FILE );
Serial.printf("Can't open file: %s!\r\n", DISPLAY_CONFIG_FILE );
}
else {
int filesize = file.size();

View File

@@ -12,7 +12,7 @@
uint32_t timeout = DISPLAY_MIN_TIMEOUT;
} display_config_t;
#define DISPLAY_COFIG_FILE "/display.cfg"
#define DISPLAY_CONFIG_FILE "/display.cfg"
/*
* @brief setup display
@@ -27,9 +27,13 @@
*/
void display_loop( TTGOClass *ttgo );
/*
* @brief save config for display to spiff
* @brief save config for display to spiffs
*/
void display_save_config( void );
/*
* @brief read config for display from spiffs
*/
void display_read_config( void );
/*
* @brief read the timeout from config
*

View File

@@ -19,7 +19,7 @@ void pmu_setup( TTGOClass *ttgo ) {
pmu_event_handle = xEventGroupCreate();
// Turn on the IRQ used
ttgo->power->adc1Enable( AXP202_BATT_VOL_ADC1 | AXP202_BATT_CUR_ADC1 | AXP202_VBUS_VOL_ADC1 | AXP202_VBUS_CUR_ADC1, AXP202_ON );
ttgo->power->adc1Enable( AXP202_BATT_VOL_ADC1 | AXP202_BATT_CUR_ADC1 | AXP202_VBUS_VOL_ADC1 | AXP202_VBUS_CUR_ADC1, AXP202_ON);
ttgo->power->enableIRQ( AXP202_VBUS_REMOVED_IRQ | AXP202_VBUS_CONNECT_IRQ | AXP202_CHARGING_FINISHED_IRQ, AXP202_ON );
ttgo->power->clearIRQ();
@@ -57,7 +57,7 @@ void IRAM_ATTR pmu_irq( void ) {
* fast wake up from IRQ
*/
// rtc_clk_cpu_freq_set(RTC_CPU_FREQ_240M);
setCpuFrequencyMhz( 240 );
setCpuFrequencyMhz(240);
}
/*
@@ -73,14 +73,17 @@ void pmu_loop( TTGOClass *ttgo ) {
if ( xEventGroupGetBitsFromISR( pmu_event_handle ) & PMU_EVENT_AXP_INT ) {
ttgo->power->readIRQ();
if (ttgo->power->isVbusPlugInIRQ()) {
powermgm_set_event( POWERMGM_PMU_BATTERY );
motor_vibe( 1 );
updatetrigger = true;
}
if (ttgo->power->isVbusRemoveIRQ()) {
powermgm_set_event( POWERMGM_PMU_BATTERY );
motor_vibe( 1 );
updatetrigger = true;
}
if (ttgo->power->isChargingDoneIRQ()) {
powermgm_set_event( POWERMGM_PMU_BATTERY );
motor_vibe( 1 );
updatetrigger = true;
}
@@ -107,7 +110,7 @@ uint32_t pmu_get_byttery_percent( TTGOClass *ttgo ) {
if ( ttgo->power->getBattChargeCoulomb() < ttgo->power->getBattDischargeCoulomb() )
ttgo->power->ClearCoulombcounter();
printf("Coulumb data: %0.1fmAh %0.1f%% (charge current: %0.1fmA, discharge current: %0.1fmA)\r",ttgo->power->getCoulombData(), (ttgo->power->getCoulombData()/380)*100, ttgo->power->getBattChargeCurrent(), ttgo->power->getBattDischargeCurrent() );
// printf("Coulumb data: %0.1fmAh %0.1f%% (charge current: %0.1fmA, discharge current: %0.1fmA)\r",ttgo->power->getCoulombData(), (ttgo->power->getCoulombData()/380)*100, ttgo->power->getBattChargeCurrent(), ttgo->power->getBattDischargeCurrent() );
return( ( ttgo->power->getCoulombData() / PMU_BATTERY_CAP ) * 100 );
}

View File

@@ -7,6 +7,7 @@
#include "bma.h"
#include "powermgm.h"
#include "wifictl.h"
#include "timesync.h"
#include "motor.h"
EventGroupHandle_t powermgm_status = NULL;
@@ -22,6 +23,7 @@ void powermgm_setup( TTGOClass *ttgo ) {
pmu_setup( ttgo );
bma_setup( ttgo );
wifictl_setup();
timesync_setup( ttgo );
}
/*
@@ -54,8 +56,11 @@ void powermgm_loop( TTGOClass *ttgo ) {
if ( bma_get_config( BMA_STEPCOUNTER ) )
ttgo->bma->enableStepCountInterrupt( false );
powermgm_set_event( POWERMGM_STANDBY );
// rtc_clk_cpu_freq_set(RTC_CPU_FREQ_2M);
setCpuFrequencyMhz( 2 );
setCpuFrequencyMhz( 10 );
gpio_wakeup_enable ((gpio_num_t)AXP202_INT, GPIO_INTR_LOW_LEVEL);
gpio_wakeup_enable ((gpio_num_t)BMA423_INT1, GPIO_INTR_HIGH_LEVEL);
esp_sleep_enable_gpio_wakeup ();
esp_light_sleep_start();
}
// clear event
powermgm_clear_event( POWERMGM_PMU_BUTTON | POWERMGM_PMU_BATTERY | POWERMGM_BMA_WAKEUP );

112
src/hardware/timesync.cpp Normal file
View File

@@ -0,0 +1,112 @@
#include "config.h"
#include "timesync.h"
#include <WiFi.h>
EventGroupHandle_t time_event_handle = NULL;
TaskHandle_t _timesync_Task;
void timesync_Task( void * pvParameters );
timesync_config_t timesync_config;
void timesync_setup( TTGOClass *ttgo ) {
timesync_read_config();
WiFi.onEvent( [](WiFiEvent_t event, WiFiEventInfo_t info) {
xEventGroupSetBits( time_event_handle, TIME_SYNC_REQUEST );
vTaskResume( _timesync_Task );
}, WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP );
time_event_handle = xEventGroupCreate();
xEventGroupClearBits( time_event_handle, TIME_SYNC_REQUEST );
xTaskCreate(
timesync_Task, /* Function to implement the task */
"timesync Task", /* Name of the task */
10000, /* Stack size in words */
NULL, /* Task input parameter */
1, /* Priority of the task */
&_timesync_Task ); /* Task handle. */
}
void timesync_save_config( void ) {
fs::File file = SPIFFS.open( TIMESYNC_CONFIG_FILE, FILE_WRITE );
if ( !file ) {
Serial.printf("Can't save file: %s\r\n", TIMESYNC_CONFIG_FILE );
}
else {
file.write( (uint8_t *)&timesync_config, sizeof( timesync_config ) );
file.close();
}
}
void timesync_read_config( void ) {
fs::File file = SPIFFS.open( TIMESYNC_CONFIG_FILE, FILE_READ );
if (!file) {
Serial.printf("Can't open file: %s!\r\n", TIMESYNC_CONFIG_FILE );
}
else {
int filesize = file.size();
if ( filesize > sizeof( timesync_config ) ) {
Serial.printf("Failed to read configfile. Wrong filesize!\r\n" );
}
else {
file.read( (uint8_t *)&timesync_config, filesize );
}
file.close();
}
}
bool timesync_get_timesync( void ) {
return( timesync_config.timesync );
}
void timesync_set_timesync( bool timesync ) {
timesync_config.timesync = timesync;
timesync_save_config();
}
bool timesync_get_daylightsave( void ) {
return( timesync_config.daylightsave );
}
void timesync_set_daylightsave( bool daylightsave ) {
timesync_config.daylightsave = daylightsave;
timesync_save_config();
}
int32_t timesync_get_timezone( void ) {
return( timesync_config.timezone );
}
void timesync_set_timezone( int32_t timezone ) {
timesync_config.timezone = timezone;
timesync_save_config();
}
void timesync_Task( void * pvParameters ) {
while( true ) {
vTaskDelay( 50 );
if ( xEventGroupGetBits( time_event_handle ) & TIME_SYNC_REQUEST ) {
struct tm timeinfo;
TTGOClass *ttgo = TTGOClass::getWatch();
long gmtOffset_sec = timesync_config.timezone * 3600;
configTime( gmtOffset_sec, 0, "pool.ntp.org" );
if( !getLocalTime( &timeinfo ) ) {
Serial.println( "Failed to obtain time\r\n" );
}
ttgo->rtc->syncToRtc();
xEventGroupClearBits( time_event_handle, TIME_SYNC_REQUEST );
}
vTaskSuspend( _timesync_Task );
}
}

67
src/hardware/timesync.h Normal file
View File

@@ -0,0 +1,67 @@
#ifndef _TIME_SYNC_H
#define _TIME_SYNC_H
#include <TTGO.h>
#define TIME_SYNC_REQUEST _BV(0)
#define TIMESYNC_CONFIG_FILE "/timesync.cfg"
typedef struct {
bool timesync = true;
bool daylightsave = false;
int32_t timezone = 0;
} timesync_config_t;
/*
* @brief setup display
*
* @param ttgo pointer to an TTGOClass
*/
void timesync_setup( TTGOClass *ttgo );
/*
* @brief save config for timesync to spiffs
*/
void timesync_save_config( void );
/*
* @brief read config for timesync from spiffs
*/
void timesync_read_config( void );
/*
* @brief get the status if timesync enable/disable
*
* @return true or false
*/
bool timesync_get_timesync( void );
/*
* @brief enable/disable automatic timesync when wifi is connected
*
* @param timesync true or false
*/
void timesync_set_timesync( bool timesync );
/*
* @brief get the status if daylightsave enable/disable
*
* @return true or false
*/
bool timesync_get_daylightsave( void );
/*
* @brief enable/disable daylight saving
*
* @param daylightsave true or false
*/
void timesync_set_daylightsave( bool daylightsave );
/*
* @brief get the current timezone
*
* @return timezone from UTC-12 to UTC+12
*/
int32_t timesync_get_timezone( void );
/*
* @brief set the current timezone
*
* @param timezone timezone from UTC-12 to UTC+12
*/
void timesync_set_timezone( int32_t timezone );
#endif // _TIME_SYNC_H

View File

@@ -1,6 +1,7 @@
#include "config.h"
#include <Arduino.h>
#include <WiFi.h>
#include <esp_wifi.h>
#include "powermgm.h"
#include "wifictl.h"
@@ -237,9 +238,10 @@ void wifictl_Task( void * pvParameters ) {
powermgm_clear_event( POWERMGM_WIFI_OFF_REQUEST | POWERMGM_WIFI_ACTIVE | POWERMGM_WIFI_CONNECTED | POWERMGM_WIFI_SCAN | POWERMGM_WIFI_ON_REQUEST );
}
else if ( powermgm_get_event( POWERMGM_WIFI_OFF_REQUEST ) ) {
Serial.printf("wlan of request\r\n");
Serial.printf("wlan off request\r\n");
statusbar_wifi_set_state( false, "" );
WiFi.mode( WIFI_OFF );
esp_wifi_stop();
powermgm_clear_event( POWERMGM_WIFI_OFF_REQUEST | POWERMGM_WIFI_ACTIVE | POWERMGM_WIFI_CONNECTED | POWERMGM_WIFI_SCAN | POWERMGM_WIFI_ON_REQUEST );
}
}