more feature
This commit is contained in:
148
src/hardware/bma.cpp
Normal file
148
src/hardware/bma.cpp
Normal file
@@ -0,0 +1,148 @@
|
||||
#include "config.h"
|
||||
#include <TTGO.h>
|
||||
#include <soc/rtc.h>
|
||||
|
||||
#include "bma.h"
|
||||
#include "powermgm.h"
|
||||
|
||||
#include "gui/statusbar.h"
|
||||
|
||||
EventGroupHandle_t bma_event_handle = NULL;
|
||||
|
||||
void IRAM_ATTR bma_irq( void );
|
||||
|
||||
bma_config_t bma_config[ BMA_CONFIG_NUM ];
|
||||
|
||||
void bma_reload_settings( void );
|
||||
void bma_read_config( void );
|
||||
void bma_save_config( void );
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
void bma_setup( TTGOClass *ttgo ) {
|
||||
|
||||
bma_event_handle = xEventGroupCreate();
|
||||
|
||||
for ( int i = 0 ; i < BMA_CONFIG_NUM ; i++ ) {
|
||||
bma_config[ i ].enable = true;
|
||||
}
|
||||
|
||||
bma_read_config();
|
||||
|
||||
ttgo->bma->begin();
|
||||
ttgo->bma->attachInterrupt();
|
||||
ttgo->bma->direction();
|
||||
|
||||
pinMode( BMA423_INT1, INPUT );
|
||||
attachInterrupt( BMA423_INT1, bma_irq, RISING );
|
||||
|
||||
ttgo->bma->enableStepCountInterrupt( bma_config[ BMA_STEPCOUNTER ].enable );
|
||||
ttgo->bma->enableWakeupInterrupt( bma_config[ BMA_DOUBLECLICK ].enable );
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
void bma_reload_settings( void ) {
|
||||
|
||||
TTGOClass *ttgo = TTGOClass::getWatch();
|
||||
|
||||
ttgo->bma->enableStepCountInterrupt( bma_config[ BMA_STEPCOUNTER ].enable );
|
||||
ttgo->bma->enableWakeupInterrupt( bma_config[ BMA_DOUBLECLICK ].enable );
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
void IRAM_ATTR bma_irq( void ) {
|
||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||
|
||||
xEventGroupSetBitsFromISR( bma_event_handle, BMA_EVENT_INT, &xHigherPriorityTaskWoken );
|
||||
|
||||
if ( xHigherPriorityTaskWoken )
|
||||
{
|
||||
portYIELD_FROM_ISR ();
|
||||
}
|
||||
rtc_clk_cpu_freq_set( RTC_CPU_FREQ_240M );
|
||||
// setCpuFrequencyMhz(240);
|
||||
}
|
||||
|
||||
/*
|
||||
* loop routine for handling IRQ in main loop
|
||||
*/
|
||||
void bma_loop( TTGOClass *ttgo ) {
|
||||
/*
|
||||
* handle IRQ event
|
||||
*/
|
||||
if ( xEventGroupGetBitsFromISR( bma_event_handle ) & BMA_EVENT_INT ) {
|
||||
while( !ttgo->bma->readInterrupt() );
|
||||
if ( ttgo->bma->isDoubleClick() ) {
|
||||
powermgm_set_event( POWERMGM_BMA_WAKEUP );
|
||||
xEventGroupClearBitsFromISR( bma_event_handle, BMA_EVENT_INT );
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if ( !powermgm_get_event( POWERMGM_STANDBY ) && xEventGroupGetBitsFromISR( bma_event_handle ) & BMA_EVENT_INT ) {
|
||||
statusbar_update_stepcounter( ttgo->bma->getCounter() );
|
||||
xEventGroupClearBitsFromISR( bma_event_handle, BMA_EVENT_INT );
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
void bma_save_config( void ) {
|
||||
fs::File file = SPIFFS.open( BMA_COFIG_FILE, FILE_WRITE );
|
||||
|
||||
if ( !file ) {
|
||||
Serial.printf("Can't save file: %s\r\n", BMA_COFIG_FILE );
|
||||
}
|
||||
else {
|
||||
file.write( (uint8_t *)bma_config, sizeof( bma_config ) );
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
void bma_read_config( void ) {
|
||||
fs::File file = SPIFFS.open( BMA_COFIG_FILE, FILE_READ );
|
||||
|
||||
if (!file) {
|
||||
Serial.printf("Can't open file: %s!\r\n", BMA_COFIG_FILE );
|
||||
}
|
||||
else {
|
||||
int filesize = file.size();
|
||||
if ( filesize > sizeof( bma_config ) ) {
|
||||
Serial.printf("Failed to read configfile. Wrong filesize!\r\n" );
|
||||
}
|
||||
else {
|
||||
file.read( (uint8_t *)bma_config, filesize );
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
bool bma_get_config( int config ) {
|
||||
if ( config < BMA_CONFIG_NUM ) {
|
||||
return( bma_config[ config ].enable );
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
void bma_set_config( int config, bool enable ) {
|
||||
if ( config < BMA_CONFIG_NUM ) {
|
||||
bma_config[ config ].enable = enable;
|
||||
bma_save_config();
|
||||
bma_reload_settings();
|
||||
}
|
||||
}
|
||||
24
src/hardware/bma.h
Normal file
24
src/hardware/bma.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#ifndef _BMA_H
|
||||
#define _BMA_H
|
||||
|
||||
#define BMA_EVENT_INT _BV(0)
|
||||
|
||||
typedef struct {
|
||||
bool enable=true;
|
||||
} bma_config_t;
|
||||
|
||||
enum {
|
||||
BMA_STEPCOUNTER,
|
||||
BMA_DOUBLECLICK,
|
||||
BMA_CONFIG_NUM
|
||||
};
|
||||
|
||||
#define BMA_COFIG_FILE "/bma.cfg"
|
||||
|
||||
void bma_setup( TTGOClass *ttgo );
|
||||
void bma_loop( TTGOClass *ttgo );
|
||||
|
||||
bool bma_get_config( int config );
|
||||
void bma_set_config( int config, bool enable );
|
||||
|
||||
#endif // _BMA_H
|
||||
79
src/hardware/display.cpp
Normal file
79
src/hardware/display.cpp
Normal file
@@ -0,0 +1,79 @@
|
||||
#include "config.h"
|
||||
#include <TTGO.h>
|
||||
|
||||
#include "display.h"
|
||||
|
||||
display_config_t display_config;
|
||||
|
||||
void display_read_config( void );
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
void display_setup( TTGOClass *ttgo ) {
|
||||
display_read_config();
|
||||
|
||||
ttgo->openBL();
|
||||
ttgo->bl->adjust( 0 );
|
||||
}
|
||||
|
||||
/*
|
||||
* loop routine for handling IRQ in main loop
|
||||
*/
|
||||
void display_loop( TTGOClass *ttgo ) {
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
void display_save_config( void ) {
|
||||
fs::File file = SPIFFS.open( DISPLAY_COFIG_FILE, FILE_WRITE );
|
||||
|
||||
if ( !file ) {
|
||||
Serial.printf("Can't save file: %s\r\n", DISPLAY_COFIG_FILE );
|
||||
}
|
||||
else {
|
||||
file.write( (uint8_t *)&display_config, sizeof( display_config ) );
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
void display_read_config( void ) {
|
||||
fs::File file = SPIFFS.open( DISPLAY_COFIG_FILE, FILE_READ );
|
||||
|
||||
if (!file) {
|
||||
Serial.printf("Can't open file: %s!\r\n", DISPLAY_COFIG_FILE );
|
||||
}
|
||||
else {
|
||||
int filesize = file.size();
|
||||
if ( filesize > sizeof( display_config ) ) {
|
||||
Serial.printf("Failed to read configfile. Wrong filesize!\r\n" );
|
||||
}
|
||||
else {
|
||||
file.read( (uint8_t *)&display_config, filesize );
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t display_get_timeout( void ) {
|
||||
return( display_config.timeout );
|
||||
}
|
||||
|
||||
void display_set_timeout( uint32_t timeout ) {
|
||||
display_config.timeout = timeout;
|
||||
}
|
||||
|
||||
uint32_t display_get_brightness( void ) {
|
||||
return( display_config.brightness );
|
||||
}
|
||||
|
||||
void display_set_brightness( uint32_t brightness ) {
|
||||
TTGOClass *ttgo = TTGOClass::getWatch();
|
||||
|
||||
display_config.brightness = brightness;
|
||||
ttgo->bl->adjust( brightness );
|
||||
}
|
||||
21
src/hardware/display.h
Normal file
21
src/hardware/display.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#ifndef _DISPLAY_H
|
||||
#define _DISPLAY_H
|
||||
|
||||
typedef struct {
|
||||
uint32_t brightness = 64;
|
||||
uint32_t timeout = 15;
|
||||
} display_config_t;
|
||||
|
||||
#define DISPLAY_COFIG_FILE "/display.cfg"
|
||||
|
||||
void display_setup( TTGOClass *ttgo );
|
||||
void display_loop( TTGOClass *ttgo );
|
||||
|
||||
void display_save_config( void );
|
||||
|
||||
uint32_t display_get_timeout( void );
|
||||
void display_set_timeout( uint32_t timeout );
|
||||
uint32_t display_get_brightness( void );
|
||||
void display_set_brightness( uint32_t brightness );
|
||||
|
||||
#endif // _DISPLAY_H
|
||||
53
src/hardware/motor.cpp
Normal file
53
src/hardware/motor.cpp
Normal file
@@ -0,0 +1,53 @@
|
||||
#include <TTGO.h>
|
||||
#include "motor.h"
|
||||
#include "powermgm.h"
|
||||
|
||||
volatile int DRAM_ATTR motor_run_time_counter=0;
|
||||
hw_timer_t * timer = NULL;
|
||||
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
|
||||
|
||||
bool motor_init = false;
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
void IRAM_ATTR onTimer() {
|
||||
portENTER_CRITICAL_ISR(&timerMux);
|
||||
if ( motor_run_time_counter >0 ) {
|
||||
motor_run_time_counter--;
|
||||
digitalWrite(GPIO_NUM_4, HIGH );
|
||||
}
|
||||
else {
|
||||
digitalWrite(GPIO_NUM_4, LOW );
|
||||
}
|
||||
portEXIT_CRITICAL_ISR(&timerMux);
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
void motor_setup( void ) {
|
||||
if ( motor_init == true )
|
||||
return;
|
||||
|
||||
pinMode(GPIO_NUM_4, OUTPUT);
|
||||
timer = timerBegin(0, 80, true);
|
||||
timerAttachInterrupt(timer, &onTimer, true);
|
||||
timerAlarmWrite(timer, 10000, true);
|
||||
timerAlarmEnable(timer);
|
||||
motor_init = true;
|
||||
|
||||
motor_vibe( 10 );
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
void motor_vibe( int time ) {
|
||||
if ( motor_init == false )
|
||||
return;
|
||||
|
||||
portENTER_CRITICAL(&timerMux);
|
||||
motor_run_time_counter = time;
|
||||
portEXIT_CRITICAL(&timerMux);
|
||||
}
|
||||
14
src/hardware/motor.h
Normal file
14
src/hardware/motor.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#ifndef _MOTOR_H
|
||||
#define _MOTOR_H
|
||||
|
||||
/*
|
||||
* @ brief setup motor I/O
|
||||
*/
|
||||
void motor_setup( void );
|
||||
/*
|
||||
* @brief let vibe motor for n * 10ms
|
||||
* @param time time in 10ms
|
||||
*/
|
||||
void motor_vibe( int time );
|
||||
|
||||
#endif // _MOTOR_H
|
||||
99
src/hardware/pmu.cpp
Normal file
99
src/hardware/pmu.cpp
Normal file
@@ -0,0 +1,99 @@
|
||||
#include "config.h"
|
||||
#include <TTGO.h>
|
||||
#include <soc/rtc.h>
|
||||
|
||||
#include "pmu.h"
|
||||
#include "powermgm.h"
|
||||
#include "motor.h"
|
||||
|
||||
#include "gui/statusbar.h"
|
||||
|
||||
EventGroupHandle_t pmu_event_handle = NULL;
|
||||
|
||||
void IRAM_ATTR pmu_irq( void );
|
||||
|
||||
/*
|
||||
* init the pmu: AXP202
|
||||
*/
|
||||
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->enableIRQ(AXP202_VBUS_REMOVED_IRQ | AXP202_VBUS_CONNECT_IRQ | AXP202_CHARGING_FINISHED_IRQ, AXP202_ON);
|
||||
ttgo->power->clearIRQ();
|
||||
|
||||
// Turn off unused power
|
||||
ttgo->power->setPowerOutPut(AXP202_EXTEN, AXP202_OFF);
|
||||
ttgo->power->setPowerOutPut(AXP202_DCDC2, AXP202_OFF);
|
||||
ttgo->power->setPowerOutPut(AXP202_LDO3, AXP202_OFF);
|
||||
ttgo->power->setPowerOutPut(AXP202_LDO4, AXP202_OFF);
|
||||
|
||||
pinMode(AXP202_INT, INPUT);
|
||||
|
||||
attachInterrupt(AXP202_INT, &pmu_irq , FALLING);
|
||||
}
|
||||
|
||||
/*
|
||||
* IRQ routine AXP202
|
||||
*/
|
||||
void IRAM_ATTR pmu_irq( void ) {
|
||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||
/*
|
||||
* setup an PMU event
|
||||
*/
|
||||
xEventGroupSetBitsFromISR( pmu_event_handle, PMU_EVENT_AXP_INT, &xHigherPriorityTaskWoken );
|
||||
if ( xHigherPriorityTaskWoken ) {
|
||||
portYIELD_FROM_ISR();
|
||||
}
|
||||
/*
|
||||
* fast wake up from IRQ
|
||||
*/
|
||||
rtc_clk_cpu_freq_set(RTC_CPU_FREQ_240M);
|
||||
// setCpuFrequencyMhz(240);
|
||||
}
|
||||
|
||||
/*
|
||||
* loop routine for handling IRQ in main loop
|
||||
*/
|
||||
void pmu_loop( TTGOClass *ttgo ) {
|
||||
static uint64_t nextmillis = 0;
|
||||
bool updatetrigger = false;
|
||||
|
||||
/*
|
||||
* handle IRQ event
|
||||
*/
|
||||
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;
|
||||
}
|
||||
if (ttgo->power->isPEKShortPressIRQ()) {
|
||||
updatetrigger = true;
|
||||
powermgm_set_event( POWERMGM_PMU_BUTTON );
|
||||
ttgo->power->clearIRQ();
|
||||
return;
|
||||
}
|
||||
ttgo->power->clearIRQ();
|
||||
xEventGroupClearBits( pmu_event_handle, PMU_EVENT_AXP_INT );
|
||||
}
|
||||
|
||||
if ( !powermgm_get_event( POWERMGM_STANDBY ) ) {
|
||||
if ( nextmillis < millis() || updatetrigger == true ) {
|
||||
nextmillis = millis() + 1000;
|
||||
statusbar_update_battery( ttgo->power->getBattPercentage(), ttgo->power->isChargeing(), ttgo->power->isVBUSPlug() );
|
||||
}
|
||||
}
|
||||
}
|
||||
19
src/hardware/pmu.h
Normal file
19
src/hardware/pmu.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#ifndef _PMU_H
|
||||
#define _PMU_H
|
||||
|
||||
#define PMU_EVENT_AXP_INT _BV(0)
|
||||
|
||||
/*
|
||||
* @brief setup pmu: axp202
|
||||
*
|
||||
* @param ttgo pointer to an TTGOClass
|
||||
*/
|
||||
void pmu_setup( TTGOClass *ttgo );
|
||||
/*
|
||||
* @brief pmu loop routine, call from powermgm. not for user use
|
||||
*
|
||||
* @param ttgo pointer to an TTGOClass
|
||||
*/
|
||||
void pmu_loop( TTGOClass *ttgo );
|
||||
|
||||
#endif // _PMU_H
|
||||
86
src/hardware/powermgm.cpp
Normal file
86
src/hardware/powermgm.cpp
Normal file
@@ -0,0 +1,86 @@
|
||||
#include "config.h"
|
||||
#include <TTGO.h>
|
||||
#include <soc/rtc.h>
|
||||
#include <WiFi.h>
|
||||
|
||||
#include "pmu.h"
|
||||
#include "bma.h"
|
||||
#include "powermgm.h"
|
||||
#include "wifictl.h"
|
||||
#include "motor.h"
|
||||
|
||||
EventGroupHandle_t powermgm_status = NULL;
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
void powermgm_setup( TTGOClass *ttgo ) {
|
||||
|
||||
powermgm_status = xEventGroupCreate();
|
||||
xEventGroupClearBits( powermgm_status, POWERMGM_STANDBY | POWERMGM_PMU_BUTTON | POWERMGM_PMU_BATTERY );
|
||||
|
||||
pmu_setup( ttgo );
|
||||
bma_setup( ttgo );
|
||||
wifictl_setup();
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
void powermgm_loop( TTGOClass *ttgo ) {
|
||||
|
||||
// event-tripper pmu-button or pmu-battery state change
|
||||
if ( powermgm_get_event( POWERMGM_PMU_BUTTON | POWERMGM_PMU_BATTERY | POWERMGM_BMA_WAKEUP ) ) {
|
||||
// if we are in standby, wake up
|
||||
if ( powermgm_get_event( POWERMGM_STANDBY ) ) {
|
||||
powermgm_clear_event( POWERMGM_STANDBY );
|
||||
ttgo->openBL();
|
||||
ttgo->displayWakeup();
|
||||
ttgo->rtc->syncToSystem();
|
||||
ttgo->startLvglTick();
|
||||
lv_disp_trig_activity(NULL);
|
||||
if ( bma_get_config( BMA_STEPCOUNTER ) )
|
||||
ttgo->bma->enableStepCountInterrupt( true );
|
||||
motor_vibe( 1 );
|
||||
}
|
||||
// if we are nor in stand by, go sleep
|
||||
else {
|
||||
ttgo->bl->adjust( 0 );
|
||||
ttgo->displaySleep();
|
||||
ttgo->closeBL();
|
||||
if ( powermgm_get_event( POWERMGM_WIFI_ACTIVE ) ) wifictl_off();
|
||||
while( powermgm_get_event( POWERMGM_WIFI_ACTIVE | POWERMGM_WIFI_CONNECTED | POWERMGM_WIFI_OFF_REQUEST | POWERMGM_WIFI_ON_REQUEST | POWERMGM_WIFI_SCAN ) ) {}
|
||||
ttgo->stopLvglTick();
|
||||
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);
|
||||
}
|
||||
// clear event
|
||||
powermgm_clear_event( POWERMGM_PMU_BUTTON | POWERMGM_PMU_BATTERY | POWERMGM_BMA_WAKEUP );
|
||||
}
|
||||
pmu_loop( ttgo );
|
||||
bma_loop( ttgo );
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
void powermgm_set_event( EventBits_t bits ) {
|
||||
xEventGroupSetBits( powermgm_status, bits );
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
void powermgm_clear_event( EventBits_t bits ) {
|
||||
xEventGroupClearBits( powermgm_status, bits );
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
EventBits_t powermgm_get_event( EventBits_t bits ) {
|
||||
return( xEventGroupGetBits( powermgm_status ) & bits );
|
||||
}
|
||||
45
src/hardware/powermgm.h
Normal file
45
src/hardware/powermgm.h
Normal file
@@ -0,0 +1,45 @@
|
||||
#ifndef _POWERMGM_H
|
||||
#define _POWERMGM_H
|
||||
|
||||
#define POWERMGM_STANDBY _BV(0)
|
||||
#define POWERMGM_PMU_BUTTON _BV(1)
|
||||
#define POWERMGM_PMU_BATTERY _BV(2)
|
||||
#define POWERMGM_BMA_WAKEUP _BV(3)
|
||||
#define POWERMGM_WIFI_ON_REQUEST _BV(5)
|
||||
#define POWERMGM_WIFI_OFF_REQUEST _BV(6)
|
||||
#define POWERMGM_WIFI_ACTIVE _BV(7)
|
||||
#define POWERMGM_WIFI_SCAN _BV(8)
|
||||
#define POWERMGM_WIFI_CONNECTED _BV(9)
|
||||
|
||||
/*
|
||||
* @brief setp power managment, coordinate managment beween CPU, wifictl, pmu, bma, display, backlight and lvgl
|
||||
*
|
||||
* @param ttgo pointer to an TTGOClass
|
||||
*/
|
||||
void powermgm_setup( TTGOClass *ttgo );
|
||||
/*
|
||||
* @brief power managment loop routine, call from loop. not for user use
|
||||
*
|
||||
* @param ttgo pointer to an TTGOClass
|
||||
*/
|
||||
void powermgm_loop( TTGOClass *ttgo );
|
||||
/*
|
||||
* @brief trigger a power managemt event
|
||||
*
|
||||
* @param bits event to trigger, example: POWERMGM_WIFI_ON_REQUEST for switch an WiFi
|
||||
*/
|
||||
void powermgm_set_event( EventBits_t bits );
|
||||
/*
|
||||
* @brief clear a power managemt event
|
||||
*
|
||||
* @param bits event to trigger, example: POWERMGM_WIFI_ON_REQUEST for switch an WiFi
|
||||
*/
|
||||
void powermgm_clear_event( EventBits_t bits );
|
||||
/*
|
||||
* @brief get a power managemt event state
|
||||
*
|
||||
* @param bits event state, example: POWERMGM_STANDBY to evaluate if the system in standby
|
||||
*/
|
||||
EventBits_t powermgm_get_event( EventBits_t bits );
|
||||
|
||||
#endif // _POWERMGM_H
|
||||
245
src/hardware/wifictl.cpp
Normal file
245
src/hardware/wifictl.cpp
Normal file
@@ -0,0 +1,245 @@
|
||||
#include "config.h"
|
||||
#include <Arduino.h>
|
||||
#include <WiFi.h>
|
||||
|
||||
#include "powermgm.h"
|
||||
#include "wifictl.h"
|
||||
|
||||
#include "gui/statusbar.h"
|
||||
|
||||
bool wifi_init = false;
|
||||
TaskHandle_t _WIFICTL_Task;
|
||||
|
||||
void wifictl_StartTask( void );
|
||||
void wifictl_Task( void * pvParameters );
|
||||
TaskHandle_t _wifictl_Task;
|
||||
|
||||
char *wifiname=NULL;
|
||||
char *wifipassword=NULL;
|
||||
|
||||
struct networklist wifictl_networklist[ NETWORKLIST_ENTRYS ];
|
||||
|
||||
void wifictl_save_network( void );
|
||||
void wifictl_load_network( void );
|
||||
void wifictl_Task( void * pvParameters );
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
void wifictl_setup( void ) {
|
||||
if ( wifi_init == true )
|
||||
return;
|
||||
wifi_init = true;
|
||||
powermgm_clear_event( POWERMGM_WIFI_ACTIVE | POWERMGM_WIFI_OFF_REQUEST | POWERMGM_WIFI_ON_REQUEST | POWERMGM_WIFI_CONNECTED | POWERMGM_WIFI_SCAN );
|
||||
|
||||
// clean network list table
|
||||
for ( int entry = 0 ; entry < NETWORKLIST_ENTRYS ; entry++ ) {
|
||||
wifictl_networklist[ entry ].ssid[ 0 ] = '\0';
|
||||
wifictl_networklist[ entry ].password[ 0 ] = '\0';
|
||||
}
|
||||
|
||||
// load network list from spiff
|
||||
wifictl_load_network();
|
||||
|
||||
// register WiFi events
|
||||
WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info) {
|
||||
powermgm_set_event( POWERMGM_WIFI_ACTIVE );
|
||||
powermgm_clear_event( POWERMGM_WIFI_OFF_REQUEST | POWERMGM_WIFI_ON_REQUEST | POWERMGM_WIFI_SCAN | POWERMGM_WIFI_CONNECTED );
|
||||
statusbar_style_icon( STATUSBAR_WIFI, STATUSBAR_STYLE_GRAY );
|
||||
statusbar_show_icon( STATUSBAR_WIFI );
|
||||
statusbar_wifi_set_state( true, "scan ..." );
|
||||
WiFi.scanNetworks();
|
||||
}, WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
|
||||
|
||||
WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info) {
|
||||
powermgm_set_event( POWERMGM_WIFI_ACTIVE );
|
||||
powermgm_clear_event( POWERMGM_WIFI_OFF_REQUEST | POWERMGM_WIFI_ON_REQUEST | POWERMGM_WIFI_SCAN | POWERMGM_WIFI_CONNECTED );
|
||||
statusbar_style_icon( STATUSBAR_WIFI, STATUSBAR_STYLE_GRAY );
|
||||
statusbar_show_icon( STATUSBAR_WIFI );
|
||||
int len = WiFi.scanComplete();
|
||||
for( int i = 0 ; i < len ; i++ ) {
|
||||
for ( int entry = 0 ; entry < NETWORKLIST_ENTRYS ; entry++ ) {
|
||||
if ( !strcmp( wifictl_networklist[ entry ].ssid, WiFi.SSID(i).c_str() ) ) {
|
||||
wifiname = wifictl_networklist[ entry ].ssid;
|
||||
wifipassword = wifictl_networklist[ entry ].password;
|
||||
statusbar_wifi_set_state( true, "connecting ..." );
|
||||
WiFi.begin( wifiname, wifipassword );
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}, WiFiEvent_t::SYSTEM_EVENT_SCAN_DONE );
|
||||
|
||||
WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info) {
|
||||
powermgm_set_event( POWERMGM_WIFI_CONNECTED | POWERMGM_WIFI_ACTIVE );
|
||||
powermgm_clear_event( POWERMGM_WIFI_OFF_REQUEST | POWERMGM_WIFI_ON_REQUEST | POWERMGM_WIFI_SCAN );
|
||||
statusbar_style_icon( STATUSBAR_WIFI, STATUSBAR_STYLE_WHITE );
|
||||
statusbar_show_icon( STATUSBAR_WIFI );
|
||||
statusbar_wifi_set_state( true, wifiname );
|
||||
}, WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP );
|
||||
|
||||
WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info) {
|
||||
powermgm_set_event( POWERMGM_WIFI_ACTIVE | POWERMGM_WIFI_SCAN );
|
||||
powermgm_clear_event( POWERMGM_WIFI_CONNECTED | POWERMGM_WIFI_OFF_REQUEST | POWERMGM_WIFI_ON_REQUEST );
|
||||
statusbar_style_icon( STATUSBAR_WIFI, STATUSBAR_STYLE_GRAY );
|
||||
statusbar_show_icon( STATUSBAR_WIFI );
|
||||
statusbar_wifi_set_state( true, "scan ..." );
|
||||
WiFi.scanNetworks();
|
||||
}, WiFiEvent_t::SYSTEM_EVENT_WIFI_READY );
|
||||
|
||||
WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info) {
|
||||
powermgm_clear_event( POWERMGM_WIFI_ACTIVE | POWERMGM_WIFI_CONNECTED | POWERMGM_WIFI_OFF_REQUEST | POWERMGM_WIFI_ON_REQUEST | POWERMGM_WIFI_SCAN );
|
||||
statusbar_hide_icon( STATUSBAR_WIFI );
|
||||
statusbar_wifi_set_state( false, "" );
|
||||
}, WiFiEvent_t::SYSTEM_EVENT_STA_STOP );
|
||||
|
||||
// start Wifo controll task
|
||||
xTaskCreatePinnedToCore(
|
||||
wifictl_Task, /* Function to implement the task */
|
||||
"wifictl Task", /* Name of the task */
|
||||
10000, /* Stack size in words */
|
||||
NULL, /* Task input parameter */
|
||||
1, /* Priority of the task */
|
||||
&_wifictl_Task, /* Task handle. */
|
||||
1 ); /* Core where the task should run */
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
void wifictl_save_network( void ) {
|
||||
fs::File file = SPIFFS.open( WIFICTL_CONFIG_FILE, FILE_WRITE );
|
||||
|
||||
if ( !file ) {
|
||||
Serial.printf("Can't save file: %s\r\n", WIFICTL_CONFIG_FILE );
|
||||
}
|
||||
else {
|
||||
file.write( (uint8_t *)wifictl_networklist, sizeof( wifictl_networklist ) );
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
void wifictl_load_network( void ) {
|
||||
fs::File file = SPIFFS.open( WIFICTL_CONFIG_FILE, FILE_READ );
|
||||
|
||||
if (!file) {
|
||||
Serial.printf("Can't open file: %s\r\n", WIFICTL_CONFIG_FILE );
|
||||
}
|
||||
else {
|
||||
int filesize = file.size();
|
||||
if ( filesize > sizeof( wifictl_networklist ) ) {
|
||||
Serial.printf("Failed to read configfile. Wrong filesize!\r\n" );
|
||||
}
|
||||
else {
|
||||
file.read( (uint8_t *)wifictl_networklist, filesize );
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
bool wifictl_is_known( const char* networkname ) {
|
||||
for( int entry = 0 ; entry < NETWORKLIST_ENTRYS; entry++ ) {
|
||||
if( !strcmp( networkname, wifictl_networklist[ entry ].ssid ) ) {
|
||||
return( true );
|
||||
}
|
||||
}
|
||||
return( false );
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
bool wifictl_delete_network( const char *ssid ) {
|
||||
for( int entry = 0 ; entry < NETWORKLIST_ENTRYS; entry++ ) {
|
||||
if( !strcmp( ssid, wifictl_networklist[ entry ].ssid ) ) {
|
||||
wifictl_networklist[ entry ].ssid[ 0 ] = '\0';
|
||||
wifictl_networklist[ entry ].password[ 0 ] = '\0';
|
||||
wifictl_save_network();
|
||||
return( true );
|
||||
}
|
||||
}
|
||||
return( false );
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
bool wifictl_insert_network( const char *ssid, const char *password ) {
|
||||
// check if existin
|
||||
for( int entry = 0 ; entry < NETWORKLIST_ENTRYS; entry++ ) {
|
||||
if( !strcmp( ssid, wifictl_networklist[ entry ].ssid ) ) {
|
||||
strncpy( wifictl_networklist[ entry ].ssid, ssid, sizeof( wifictl_networklist[ entry ].ssid ) );
|
||||
wifictl_save_network();
|
||||
WiFi.scanNetworks();
|
||||
powermgm_set_event( POWERMGM_WIFI_SCAN );
|
||||
return( true );
|
||||
}
|
||||
}
|
||||
// check for an emty entry
|
||||
for( int entry = 0 ; entry < NETWORKLIST_ENTRYS; entry++ ) {
|
||||
if( strlen( wifictl_networklist[ entry ].ssid ) == 0 ) {
|
||||
Serial.printf("wifictl: insert network\r\n");
|
||||
strncpy( wifictl_networklist[ entry ].ssid, ssid, sizeof( wifictl_networklist[ entry ].ssid ) );
|
||||
strncpy( wifictl_networklist[ entry ].password, password, sizeof( wifictl_networklist[ entry ].password ) );
|
||||
wifictl_save_network();
|
||||
WiFi.scanNetworks();
|
||||
powermgm_set_event( POWERMGM_WIFI_SCAN );
|
||||
return( true );
|
||||
}
|
||||
}
|
||||
return( false );
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
void wifictl_on( void ) {
|
||||
if ( wifi_init == false )
|
||||
return;
|
||||
vTaskResume( _wifictl_Task );
|
||||
powermgm_set_event( POWERMGM_WIFI_ON_REQUEST );
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
void wifictl_off( void ) {
|
||||
if ( wifi_init == false )
|
||||
return;
|
||||
powermgm_set_event( POWERMGM_WIFI_OFF_REQUEST );
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
void wifictl_Task( void * pvParameters ) {
|
||||
if ( wifi_init == false )
|
||||
return;
|
||||
|
||||
while( true ) {
|
||||
vTaskDelay( 50 );
|
||||
if ( powermgm_get_event( POWERMGM_STANDBY ) ) {
|
||||
vTaskSuspend( _wifictl_Task );
|
||||
}
|
||||
else {
|
||||
if ( powermgm_get_event( POWERMGM_WIFI_ON_REQUEST ) ) {
|
||||
Serial.printf("wlan on request\r\n");
|
||||
statusbar_wifi_set_state( true, "activate" );
|
||||
WiFi.mode( WIFI_STA );
|
||||
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");
|
||||
statusbar_wifi_set_state( false, "" );
|
||||
WiFi.mode( WIFI_OFF );
|
||||
powermgm_clear_event( POWERMGM_WIFI_OFF_REQUEST | POWERMGM_WIFI_ACTIVE | POWERMGM_WIFI_CONNECTED | POWERMGM_WIFI_SCAN | POWERMGM_WIFI_ON_REQUEST );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
49
src/hardware/wifictl.h
Normal file
49
src/hardware/wifictl.h
Normal file
@@ -0,0 +1,49 @@
|
||||
#ifndef _WIFICTL_H
|
||||
#define _WIFICTL_H
|
||||
|
||||
#define WIFICTL_DELAY 10
|
||||
#define NETWORKLIST_ENTRYS 20
|
||||
#define WIFICTL_CONFIG_FILE "/wifilist.cfg"
|
||||
|
||||
struct networklist {
|
||||
char ssid[64]="";
|
||||
char password[64]="";
|
||||
};
|
||||
|
||||
/*
|
||||
* @brief setup wifi controller routine
|
||||
*/
|
||||
void wifictl_setup( void );
|
||||
/*
|
||||
* @brief check if networkname known
|
||||
*
|
||||
* @param networkname network name to check
|
||||
*/
|
||||
bool wifictl_is_known( const char* networkname );
|
||||
/*
|
||||
* @brief insert or add an new ssid/password to the known network list
|
||||
*
|
||||
* @param ssid pointer to an network name
|
||||
* @param password pointer to the password
|
||||
*
|
||||
* @return true if was success or false if fail
|
||||
*/
|
||||
bool wifictl_insert_network( const char *ssid, const char *password );
|
||||
/*
|
||||
* @brief delete ssid from network list
|
||||
*
|
||||
* @param ssid pointer to an network name
|
||||
*
|
||||
* @return true if was success or false if fail
|
||||
*/
|
||||
bool wifictl_delete_network( const char *ssid );
|
||||
/*
|
||||
* @brief switch on wifi
|
||||
*/
|
||||
void wifictl_on( void );
|
||||
/*
|
||||
* @brief switch off wifi
|
||||
*/
|
||||
void wifictl_off( void );
|
||||
|
||||
#endif // _WIFICTL_H
|
||||
Reference in New Issue
Block a user