hardware driver separation

This commit is contained in:
sharandac
2020-08-26 14:32:11 +02:00
parent 2b33f61b32
commit c335328ab4
20 changed files with 207 additions and 52 deletions

View File

@@ -27,12 +27,11 @@
#define LILYGO_WATCH_2020_V1 //To use T-Watch2020, please uncomment this line #define LILYGO_WATCH_2020_V1 //To use T-Watch2020, please uncomment this line
#define LILYGO_WATCH_LVGL //To use LVGL, you need to enable the macro LVGL #define LILYGO_WATCH_LVGL //To use LVGL, you need to enable the macro LVGL
#define TWATCH_USE_PSRAM_ALLOC_LVGL #define TWATCH_USE_PSRAM_ALLOC_LVGL
#define ENABLE_LVGL_FLUSH_DMA
#include <LilyGoWatch.h> #include <LilyGoWatch.h>
/* /*
* firmeware version string * firmeware version string
*/ */
#define __FIRMWARE__ "2020082601" #define __FIRMWARE__ "2020082603"
#endif // _CONFIG_H #endif // _CONFIG_H

View File

@@ -52,15 +52,12 @@ LV_IMG_DECLARE(bg2)
*/ */
void gui_setup(void) void gui_setup(void)
{ {
lv_coord_t hres = lv_disp_get_hor_res(NULL);
lv_coord_t vres = lv_disp_get_ver_res(NULL);
//Create wallpaper //Create wallpaper
lv_obj_t *img_bin = lv_img_create( lv_scr_act() , NULL); /*Create an image object*/ lv_obj_t *img_bin = lv_img_create( lv_scr_act() , NULL );
lv_img_set_src(img_bin, &bg2 ); lv_img_set_src( img_bin, &bg2 );
lv_obj_set_width( img_bin, hres ); lv_obj_set_width( img_bin, lv_disp_get_hor_res( NULL ) );
lv_obj_set_height( img_bin, vres ); lv_obj_set_height( img_bin, lv_disp_get_ver_res( NULL ) );
lv_obj_align(img_bin, NULL, LV_ALIGN_CENTER, 0, 0); lv_obj_align( img_bin, NULL, LV_ALIGN_CENTER, 0, 0 );
mainbar_setup(); mainbar_setup();
/* add the four mainbar screens */ /* add the four mainbar screens */
@@ -79,7 +76,7 @@ void gui_setup(void)
update_tile_setup(); update_tile_setup();
statusbar_setup(); statusbar_setup();
lv_disp_trig_activity(NULL); lv_disp_trig_activity( NULL );
keyboard_setup(); keyboard_setup();

View File

@@ -196,10 +196,9 @@ static void exit_battery_view_event_cb( lv_obj_t * obj, lv_event_t event ) {
void battery_view_update_task( lv_task_t *task ) { void battery_view_update_task( lv_task_t *task ) {
char temp[16]=""; char temp[16]="";
TTGOClass *ttgo = TTGOClass::getWatch();
if ( pmu_get_battery_percent( ttgo ) >= 0 ) { if ( pmu_get_battery_percent( ) >= 0 ) {
snprintf( temp, sizeof( temp ), "%0.1fmAh", ttgo->power->getCoulombData() ); snprintf( temp, sizeof( temp ), "%0.1fmAh", pmu_get_coulumb_data() );
} }
else { else {
snprintf( temp, sizeof( temp ), "unknown" ); snprintf( temp, sizeof( temp ), "unknown" );
@@ -211,19 +210,19 @@ void battery_view_update_task( lv_task_t *task ) {
lv_label_set_text( battery_view_design_cap, temp ); lv_label_set_text( battery_view_design_cap, temp );
lv_obj_align( battery_view_design_cap, lv_obj_get_parent( battery_view_design_cap ), LV_ALIGN_IN_RIGHT_MID, -5, 0 ); lv_obj_align( battery_view_design_cap, lv_obj_get_parent( battery_view_design_cap ), LV_ALIGN_IN_RIGHT_MID, -5, 0 );
snprintf( temp, sizeof( temp ), "%0.2fV", ttgo->power->getBattVoltage() / 1000 ); snprintf( temp, sizeof( temp ), "%0.2fV", pmu_get_battery_voltage() / 1000 );
lv_label_set_text( battery_view_voltage, temp ); lv_label_set_text( battery_view_voltage, temp );
lv_obj_align( battery_view_voltage, lv_obj_get_parent( battery_view_voltage ), LV_ALIGN_IN_RIGHT_MID, -5, 0 ); lv_obj_align( battery_view_voltage, lv_obj_get_parent( battery_view_voltage ), LV_ALIGN_IN_RIGHT_MID, -5, 0 );
snprintf( temp, sizeof( temp ), "%0.1fmA", ttgo->power->getBattChargeCurrent() ); snprintf( temp, sizeof( temp ), "%0.1fmA", pmu_get_battery_charge_current() );
lv_label_set_text( charge_view_current, temp ); lv_label_set_text( charge_view_current, temp );
lv_obj_align( charge_view_current, lv_obj_get_parent( charge_view_current ), LV_ALIGN_IN_RIGHT_MID, -5, 0 ); lv_obj_align( charge_view_current, lv_obj_get_parent( charge_view_current ), LV_ALIGN_IN_RIGHT_MID, -5, 0 );
snprintf( temp, sizeof( temp ), "%0.1fmA", ttgo->power->getBattDischargeCurrent() ); snprintf( temp, sizeof( temp ), "%0.1fmA", pmu_get_battery_discharge_current() );
lv_label_set_text( discharge_view_current, temp ); lv_label_set_text( discharge_view_current, temp );
lv_obj_align( discharge_view_current, lv_obj_get_parent( discharge_view_current ), LV_ALIGN_IN_RIGHT_MID, -5, 0 ); lv_obj_align( discharge_view_current, lv_obj_get_parent( discharge_view_current ), LV_ALIGN_IN_RIGHT_MID, -5, 0 );
snprintf( temp, sizeof( temp ), "%0.2fV", ttgo->power->getVbusVoltage() / 1000 ); snprintf( temp, sizeof( temp ), "%0.2fV", pmu_get_vbus_voltage() / 1000 );
lv_label_set_text( vbus_view_voltage, temp ); lv_label_set_text( vbus_view_voltage, temp );
lv_obj_align( vbus_view_voltage, lv_obj_get_parent( vbus_view_voltage ), LV_ALIGN_IN_RIGHT_MID, -5, 0 ); lv_obj_align( vbus_view_voltage, lv_obj_get_parent( vbus_view_voltage ), LV_ALIGN_IN_RIGHT_MID, -5, 0 );
} }

View File

@@ -82,5 +82,6 @@ void splash_screen_stage_finish( TTGOClass *ttgo ) {
delay(1); delay(1);
} }
lv_obj_del( preload ); lv_obj_del( preload );
lv_obj_del( preload_label );
lv_task_handler(); lv_task_handler();
} }

View File

@@ -41,7 +41,8 @@ void IRAM_ATTR bma_irq( void );
/* /*
* *
*/ */
void bma_setup( TTGOClass *ttgo ) { void bma_setup( void ) {
TTGOClass *ttgo = TTGOClass::getWatch();
bma_event_handle = xEventGroupCreate(); bma_event_handle = xEventGroupCreate();
@@ -120,7 +121,8 @@ void IRAM_ATTR bma_irq( void ) {
/* /*
* loop routine for handling IRQ in main loop * loop routine for handling IRQ in main loop
*/ */
void bma_loop( TTGOClass *ttgo ) { void bma_loop( void ) {
TTGOClass *ttgo = TTGOClass::getWatch();
/* /*
* handle IRQ event * handle IRQ event
*/ */

View File

@@ -42,11 +42,11 @@
* *
* @param ttgo pointer to an TTGOClass * @param ttgo pointer to an TTGOClass
*/ */
void bma_setup( TTGOClass *ttgo ); void bma_setup( void );
/* /*
* @brief loop function for activity measurement * @brief loop function for activity measurement
*/ */
void bma_loop( TTGOClass *ttgo ); void bma_loop( void );
/* /*
* @brief put bma into standby, depending on ther config * @brief put bma into standby, depending on ther config
*/ */

View File

@@ -0,0 +1,94 @@
#include "i2c_bus.h"
#include "Wire.h"
#include <Arduino.h>
void I2C_Bus::scan(void)
{
uint8_t err, addr;
int nDevices = 0;
for (addr = 1; addr < 127; addr++) {
_port->beginTransmission(addr);
err = _port->endTransmission();
if (err == 0) {
Serial.print("I2C device found at address 0x");
if (addr < 16)
Serial.print("0");
Serial.print(addr, HEX);
Serial.println(" !");
nDevices++;
} else if (err == 4) {
Serial.print("Unknow error at address 0x");
if (addr < 16)
Serial.print("0");
Serial.println(addr, HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");
}
uint16_t I2C_Bus::readBytes(uint8_t addr, uint8_t *data, uint16_t len, uint16_t delay_ms)
{
uint16_t ret = 0;
xSemaphoreTakeRecursive(_i2c_mux, portMAX_DELAY);
uint8_t cnt = _port->requestFrom(addr, (uint8_t)len, (uint8_t)1);
if (!cnt) {
ret = 1 << 13;
}
uint16_t index = 0;
while (_port->available()) {
if (index > len)return 1 << 14;
if (delay_ms)delay(delay_ms);
data[index++] = _port->read();
}
xSemaphoreGiveRecursive(_i2c_mux);
return ret;
}
uint16_t I2C_Bus::readBytes(uint8_t addr, uint8_t reg, uint8_t *data, uint16_t len)
{
uint16_t ret = 0;
xSemaphoreTakeRecursive(_i2c_mux, portMAX_DELAY);
_port->beginTransmission(addr);
_port->write(reg);
_port->endTransmission(false);
uint8_t cnt = _port->requestFrom(addr, (uint8_t)len, (uint8_t)1);
if (!cnt) {
ret = 1 << 13;
}
uint16_t index = 0;
while (_port->available()) {
if (index > len)return 1 << 14;
data[index++] = _port->read();
}
xSemaphoreGiveRecursive(_i2c_mux);
return ret;
}
uint16_t I2C_Bus::writeBytes(uint8_t addr, uint8_t reg, uint8_t *data, uint16_t len)
{
uint16_t ret = 0;
xSemaphoreTakeRecursive(_i2c_mux, portMAX_DELAY);
_port->beginTransmission(addr);
_port->write(reg);
for (uint16_t i = 0; i < len; i++) {
_port->write(data[i]);
}
ret = _port->endTransmission();
xSemaphoreGiveRecursive(_i2c_mux);
return ret ? 1 << 12 : ret;
}
bool I2C_Bus::deviceProbe(uint8_t addr)
{
uint16_t ret = 0;
xSemaphoreTakeRecursive(_i2c_mux, portMAX_DELAY);
_port->beginTransmission(addr);
ret = _port->endTransmission();
xSemaphoreGiveRecursive(_i2c_mux);
return (ret == 0);
}

View File

@@ -0,0 +1,26 @@
#ifndef I2C_BUS_H
#define I2C_BUS_H
#include <Wire.h>
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
class I2C_Bus {
public:
I2C_Bus(TwoWire &port = Wire, int sda = 21, int scl = 22)
{
_port = &port;
_port->begin(sda, scl);
_i2c_mux = xSemaphoreCreateRecursiveMutex();
};
void scan();
uint16_t readBytes(uint8_t addr, uint8_t *data, uint16_t len, uint16_t delay_ms = 0);
uint16_t readBytes(uint8_t addr, uint8_t reg, uint8_t *data, uint16_t len);
uint16_t writeBytes(uint8_t addr, uint8_t reg, uint8_t *data, uint16_t len);
bool deviceProbe(uint8_t addr);
private:
TwoWire *_port;
SemaphoreHandle_t _i2c_mux = NULL;
};
#endif // I2C_BUS_H

0
src/hardware/driver/rtc Normal file
View File

View File

@@ -13,19 +13,19 @@
#include "gui/statusbar.h" #include "gui/statusbar.h"
EventGroupHandle_t pmu_event_handle = NULL; EventGroupHandle_t pmu_event_handle = NULL;
void IRAM_ATTR pmu_irq( void ); void IRAM_ATTR pmu_irq( void );
pmu_config_t pmu_config; pmu_config_t pmu_config;
/* /*
* init the pmu: AXP202 * init the pmu: AXP202
*/ */
void pmu_setup( TTGOClass *ttgo ) { void pmu_setup( void ) {
pmu_event_handle = xEventGroupCreate(); pmu_event_handle = xEventGroupCreate();
pmu_read_config(); pmu_read_config();
TTGOClass *ttgo = TTGOClass::getWatch();
// Turn on the IRQ used // 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_TIMER_TIMEOUT_IRQ, AXP202_ON ); ttgo->power->enableIRQ( AXP202_VBUS_REMOVED_IRQ | AXP202_VBUS_CONNECT_IRQ | AXP202_CHARGING_FINISHED_IRQ | AXP202_TIMER_TIMEOUT_IRQ, AXP202_ON );
@@ -249,10 +249,12 @@ void pmu_set_experimental_power_save( bool value ) {
/* /*
* loop routine for handling IRQ in main loop * loop routine for handling IRQ in main loop
*/ */
void pmu_loop( TTGOClass *ttgo ) { void pmu_loop( void ) {
static uint64_t nextmillis = 0; static uint64_t nextmillis = 0;
bool updatetrigger = false; bool updatetrigger = false;
TTGOClass *ttgo = TTGOClass::getWatch();
/* /*
* handle IRQ event * handle IRQ event
*/ */
@@ -294,13 +296,15 @@ void pmu_loop( TTGOClass *ttgo ) {
if ( !powermgm_get_event( POWERMGM_STANDBY ) ) { if ( !powermgm_get_event( POWERMGM_STANDBY ) ) {
if ( nextmillis < millis() || updatetrigger == true ) { if ( nextmillis < millis() || updatetrigger == true ) {
nextmillis = millis() + 1000; nextmillis = millis() + 1000;
statusbar_update_battery( pmu_get_battery_percent( ttgo ), ttgo->power->isChargeing(), ttgo->power->isVBUSPlug() ); statusbar_update_battery( pmu_get_battery_percent(), ttgo->power->isChargeing(), ttgo->power->isVBUSPlug() );
blectl_update_battery( pmu_get_battery_percent( ttgo ), ttgo->power->isChargeing(), ttgo->power->isVBUSPlug() ); blectl_update_battery( pmu_get_battery_percent(), ttgo->power->isChargeing(), ttgo->power->isVBUSPlug() );
} }
} }
} }
int32_t pmu_get_battery_percent( TTGOClass *ttgo ) { int32_t pmu_get_battery_percent( void ) {
TTGOClass *ttgo = TTGOClass::getWatch();
if ( ttgo->power->getBattChargeCoulomb() < ttgo->power->getBattDischargeCoulomb() || ttgo->power->getBattVoltage() < 3200 ) { if ( ttgo->power->getBattChargeCoulomb() < ttgo->power->getBattDischargeCoulomb() || ttgo->power->getBattVoltage() < 3200 ) {
ttgo->power->ClearCoulombcounter(); ttgo->power->ClearCoulombcounter();
} }
@@ -311,4 +315,29 @@ int32_t pmu_get_battery_percent( TTGOClass *ttgo ) {
else { else {
return( ttgo->power->getBattPercentage() ); return( ttgo->power->getBattPercentage() );
} }
} }
float pmu_get_battery_voltage( void ) {
TTGOClass *ttgo = TTGOClass::getWatch();
return( ttgo->power->getBattVoltage() );
}
float pmu_get_battery_charge_current( void ) {
TTGOClass *ttgo = TTGOClass::getWatch();
return( ttgo->power->getBattChargeCurrent() );
}
float pmu_get_battery_discharge_current( void ) {
TTGOClass *ttgo = TTGOClass::getWatch();
return( ttgo->power->getBattDischargeCurrent() );
}
float pmu_get_vbus_voltage( void ) {
TTGOClass *ttgo = TTGOClass::getWatch();
return( ttgo->power->getVbusVoltage() );
}
float pmu_get_coulumb_data( void ) {
TTGOClass *ttgo = TTGOClass::getWatch();
return( ttgo->power->getCoulombData() );
}

View File

@@ -48,13 +48,13 @@
* *
* @param ttgo pointer to an TTGOClass * @param ttgo pointer to an TTGOClass
*/ */
void pmu_setup( TTGOClass *ttgo ); void pmu_setup( void );
/* /*
* @brief pmu loop routine, call from powermgm. not for user use * @brief pmu loop routine, call from powermgm. not for user use
* *
* @param ttgo pointer to an TTGOClass * @param ttgo pointer to an TTGOClass
*/ */
void pmu_loop( TTGOClass *ttgo ); void pmu_loop( void );
/* /*
* *
* @brief get the charge of battery in percent * @brief get the charge of battery in percent
@@ -63,7 +63,7 @@
* *
* @return charge in percent or -1 if unknown * @return charge in percent or -1 if unknown
*/ */
int32_t pmu_get_battery_percent( TTGOClass *ttgo ); int32_t pmu_get_battery_percent( void );
/* /*
* @brief set the axp202 in standby * @brief set the axp202 in standby
*/ */
@@ -99,5 +99,10 @@
int32_t pmu_get_designed_battery_cap( void ); int32_t pmu_get_designed_battery_cap( void );
bool pmu_get_silence_wakeup( void ); bool pmu_get_silence_wakeup( void );
void pmu_set_silence_wakeup( bool value ); void pmu_set_silence_wakeup( bool value );
float pmu_get_battery_voltage( void );
float pmu_get_battery_charge_current( void );
float pmu_get_battery_discharge_current( void );
float pmu_get_vbus_voltage( void );
float pmu_get_coulumb_data( void );
#endif // _PMU_H #endif // _PMU_H

View File

@@ -48,23 +48,25 @@ portMUX_TYPE powermgmMux = portMUX_INITIALIZER_UNLOCKED;
/* /*
* *
*/ */
void powermgm_setup( TTGOClass *ttgo ) { void powermgm_setup( void ) {
powermgm_status = xEventGroupCreate(); powermgm_status = xEventGroupCreate();
pmu_setup( ttgo ); pmu_setup();
bma_setup( ttgo ); bma_setup();
wifictl_setup(); wifictl_setup();
blectl_read_config(); blectl_read_config();
timesync_setup( ttgo ); timesync_setup();
touch_setup( ttgo ); touch_setup();
sound_setup(); sound_setup();
} }
/* /*
* *
*/ */
void powermgm_loop( TTGOClass *ttgo ) { void powermgm_loop( void ) {
TTGOClass *ttgo = TTGOClass::getWatch();
// check if a button or doubleclick was release // check if a button or doubleclick was release
if( powermgm_get_event( POWERMGM_PMU_BUTTON | POWERMGM_BMA_DOUBLECLICK ) ) { if( powermgm_get_event( POWERMGM_PMU_BUTTON | POWERMGM_BMA_DOUBLECLICK ) ) {
@@ -162,12 +164,12 @@ void powermgm_loop( TTGOClass *ttgo ) {
if ( powermgm_get_event( POWERMGM_STANDBY ) ) { if ( powermgm_get_event( POWERMGM_STANDBY ) ) {
vTaskDelay( 100 ); vTaskDelay( 100 );
pmu_loop( ttgo ); pmu_loop();
bma_loop( ttgo ); bma_loop();
} }
else { else {
pmu_loop( ttgo ); pmu_loop();
bma_loop( ttgo ); bma_loop();
display_loop( ttgo ); display_loop( ttgo );
} }
} }

View File

@@ -38,13 +38,13 @@
* *
* @param ttgo pointer to an TTGOClass * @param ttgo pointer to an TTGOClass
*/ */
void powermgm_setup( TTGOClass *ttgo ); void powermgm_setup( void );
/* /*
* @brief power managment loop routine, call from loop. not for user use * @brief power managment loop routine, call from loop. not for user use
* *
* @param ttgo pointer to an TTGOClass * @param ttgo pointer to an TTGOClass
*/ */
void powermgm_loop( TTGOClass *ttgo ); void powermgm_loop( void );
/* /*
* @brief trigger a power managemt event * @brief trigger a power managemt event
* *

View File

@@ -37,7 +37,7 @@ timesync_config_t timesync_config;
void timesync_wifictl_event_cb( EventBits_t event, char* msg ); void timesync_wifictl_event_cb( EventBits_t event, char* msg );
void timesync_setup( TTGOClass *ttgo ) { void timesync_setup( void ) {
timesync_read_config(); timesync_read_config();
time_event_handle = xEventGroupCreate(); time_event_handle = xEventGroupCreate();

View File

@@ -40,7 +40,7 @@
* *
* @param ttgo pointer to an TTGOClass * @param ttgo pointer to an TTGOClass
*/ */
void timesync_setup( TTGOClass *ttgo ); void timesync_setup( void );
/* /*
* @brief save config for timesync to spiffs * @brief save config for timesync to spiffs
*/ */

View File

@@ -29,13 +29,14 @@ lv_indev_t *touch_indev = NULL;
static bool touch_read(lv_indev_drv_t * drv, lv_indev_data_t*data); static bool touch_read(lv_indev_drv_t * drv, lv_indev_data_t*data);
static bool touch_getXY( int16_t &x, int16_t &y ); static bool touch_getXY( int16_t &x, int16_t &y );
void touch_setup( TTGOClass *ttgo ) { void touch_setup( void ) {
touch_indev = lv_indev_get_next( NULL ); touch_indev = lv_indev_get_next( NULL );
touch_indev->driver.read_cb = touch_read; touch_indev->driver.read_cb = touch_read;
} }
static bool touch_getXY( int16_t &x, int16_t &y ) { static bool touch_getXY( int16_t &x, int16_t &y ) {
TTGOClass *ttgo = TTGOClass::getWatch(); TTGOClass *ttgo = TTGOClass::getWatch();
TP_Point p; TP_Point p;
static bool touch_press = false; static bool touch_press = false;

View File

@@ -29,6 +29,6 @@
* *
* @param ttgo pointer to an TTGOClass * @param ttgo pointer to an TTGOClass
*/ */
void touch_setup( TTGOClass *ttgo ); void touch_setup( void );
#endif // _TOUCH_H #endif // _TOUCH_H

View File

@@ -74,7 +74,7 @@ void setup()
ttgo->rtc->syncToSystem(); ttgo->rtc->syncToSystem();
splash_screen_stage_update( "init powermgm", 60 ); splash_screen_stage_update( "init powermgm", 60 );
powermgm_setup( ttgo ); powermgm_setup();
splash_screen_stage_update( "init gui", 80 ); splash_screen_stage_update( "init gui", 80 );
gui_setup(); gui_setup();
@@ -112,5 +112,5 @@ void loop()
{ {
delay(5); delay(5);
gui_loop( ttgo ); gui_loop( ttgo );
powermgm_loop( ttgo ); powermgm_loop();
} }

Binary file not shown.

View File

@@ -1 +1 @@
{"version":"2020082601","host":"http://www.neo-guerillaz.de","file":"ttgo-t-watch2020_v1.ino.bin"} {"version":"2020082603","host":"http://www.neo-guerillaz.de","file":"ttgo-t-watch2020_v1.ino.bin"}