From ba9342e249d70a7e1de81967ad2576ae5d1f4878 Mon Sep 17 00:00:00 2001 From: Th3maz1ng Date: Fri, 3 Feb 2023 13:38:29 +0100 Subject: [PATCH] Updated source code because of changes done in the previous commits --- .../watch_peripherals/watch_peripherals.c | 45 ++++++++++++++++--- .../watch_peripherals/watch_peripherals.h | 39 ++++++++++++++-- src/W800 SDK v1.00.08/app/gfx/gfx_task.c | 20 +++++---- src/W800 SDK v1.00.08/include/wm_type_def.h | 5 +++ 4 files changed, 90 insertions(+), 19 deletions(-) diff --git a/src/W800 SDK v1.00.08/app/app_drivers/watch_peripherals/watch_peripherals.c b/src/W800 SDK v1.00.08/app/app_drivers/watch_peripherals/watch_peripherals.c index 23407e8..2ccd709 100644 --- a/src/W800 SDK v1.00.08/app/app_drivers/watch_peripherals/watch_peripherals.c +++ b/src/W800 SDK v1.00.08/app/app_drivers/watch_peripherals/watch_peripherals.c @@ -7,7 +7,7 @@ #include "wm_pwm.h" -static uint8_t _adc_offset = 0; +static int8_t _adc_offset = 0; static uint8_t _vibration_motor_timer_id = WM_TIMER_ID_INVALID; static void vibration_motor_timer_irq_cb(void *p) @@ -20,7 +20,7 @@ static void vibration_motor_timer_irq_cb(void *p) APP_LOG_DEBUG("Vibration stopped"); } -void watch_peripherals_io_init(void) +static void watch_peripherals_io_init(void) { /* We initialize the ADC input as well as the gpio used to enabled the voltage divider bridge */ wm_adc_config(BATTERY_VOLTAGE_ADC_CHANNEL); @@ -28,11 +28,11 @@ void watch_peripherals_io_init(void) tls_gpio_write(BATTERY_VOLTAGE_DIVIDER_ENABLE, 0); /* We initialize the output pin for the vibration motor enable pin */ - /*tls_gpio_cfg(VIBRATION_MOTOR_ENABLE, WM_GPIO_DIR_OUTPUT, WM_GPIO_ATTR_FLOATING); - tls_gpio_write(VIBRATION_MOTOR_ENABLE, 0);*/ + tls_gpio_cfg(VIBRATION_MOTOR_ENABLE, WM_GPIO_DIR_OUTPUT, WM_GPIO_ATTR_FLOATING); + tls_gpio_write(VIBRATION_MOTOR_ENABLE, 0); } -void watch_peripherals_init(uint8_t adcOffset) +void watch_peripherals_init(int8_t adcOffset) { watch_peripherals_io_init(); _adc_offset = adcOffset; @@ -56,12 +56,43 @@ void watch_peripherals_init(uint8_t adcOffset) } } -uint16_t watch_peripherals_get_battery_voltage(void) +uint16_t watch_peripherals_get_battery_voltage(Battery_Unit_e unit) { tls_gpio_write(BATTERY_VOLTAGE_DIVIDER_ENABLE, 1); int batteryVoltage = adc_get_inputVolt(BATTERY_VOLTAGE_ADC_CHANNEL, 1, 1) * 2 + _adc_offset; tls_gpio_write(BATTERY_VOLTAGE_DIVIDER_ENABLE, 0); - return batteryVoltage < 0 ? 0 : batteryVoltage; + + switch (unit) + { + case Battery_Unit_mV: + return batteryVoltage < 0 ? 0 : batteryVoltage; + break; + case Battery_Unit_percent: + return battery_voltage_to_percentage(batteryVoltage < 0 ? 0 : batteryVoltage); + break; + default: + return 0; + break; + } +} + +uint8_t battery_voltage_to_percentage(uint16_t voltage_in_mV) +{ + const uint16_t BATTERY_VOLTAGE_MV_TABLE[20] = + { + 3270, 3610, 3690, 3710, 3730, 3750, 3770, 3790, 3800, 3820, + 3840, 3850, 3870, 3910, 3950, 3980, 4020, 4080, 4110, 4150, + }; + + uint8_t i = 19; + + do + { + if(voltage_in_mV > BATTERY_VOLTAGE_MV_TABLE[i]) + break; + }while(i-- > 0); + + return (i+1) * 5; } void watch_peripherals_vibrate(uint8_t strength, uint32_t durationMs) diff --git a/src/W800 SDK v1.00.08/app/app_drivers/watch_peripherals/watch_peripherals.h b/src/W800 SDK v1.00.08/app/app_drivers/watch_peripherals/watch_peripherals.h index e3eaff9..0cd99ce 100644 --- a/src/W800 SDK v1.00.08/app/app_drivers/watch_peripherals/watch_peripherals.h +++ b/src/W800 SDK v1.00.08/app/app_drivers/watch_peripherals/watch_peripherals.h @@ -6,14 +6,45 @@ #define WATCH_PERIPHERALS_H #include "wm_type_def.h" -void watch_peripherals_io_init(void); +typedef enum Battery_Unit +{ + Battery_Unit_mV = 0, + Battery_Unit_percent +} Battery_Unit_e; -void watch_peripherals_init(uint8_t adcOffset); +/** + * @brief Inits the watch peripherals driver. + * This must be called before using the API. + * + * @param adcOffset an offset in mV to apply to the battery voltage reading. + */ +void watch_peripherals_init(int8_t adcOffset); -uint16_t watch_peripherals_get_battery_voltage(void); +/** + * @brief Reads and returns the current battery voltage in mV or it's corresponding capacity in percentage. + * This function takes into account the adcOffset parameter provided in the @ref watch_peripherals_init + * function. + * /!\ The capacity in % returned is for a single cell battery LiPo only. + * + * @param unit the unit of the data to be returned : mV or corresponding capacity in percentage + * @return uint16_t the voltage in mV or the battery left capacity in % + */ +uint16_t watch_peripherals_get_battery_voltage(Battery_Unit_e unit); -uint8_t watch_peripherals_battery_voltage_to_level(uint16_t voltage); +/** + * @brief Converts the given battery voltage to the corresponding battery capacity for a single cell LiPo only (4.2V to 3.2V) + * + * @param voltage_in_mV the voltage to convert in mV + * @return uint8_t the corresponding capacity in % + */ +uint8_t battery_voltage_to_percentage(uint16_t voltage_in_mV); +/** + * @brief Triggers the vibration motor to vibrate at the provided strength for the set duration in ms. + * + * @param strength the vibration strength from 0 to 255 + * @param durationMs the vibration duration in ms + */ void watch_peripherals_vibrate(uint8_t strength, uint32_t durationMs); #endif //WATCH_PERIPHERALS_H \ No newline at end of file diff --git a/src/W800 SDK v1.00.08/app/gfx/gfx_task.c b/src/W800 SDK v1.00.08/app/gfx/gfx_task.c index 50c53b5..b894852 100644 --- a/src/W800 SDK v1.00.08/app/gfx/gfx_task.c +++ b/src/W800 SDK v1.00.08/app/gfx/gfx_task.c @@ -148,6 +148,9 @@ void gfx_task(void *param) lv_scr_load(watchFace.display); + /* Let's init the watch peripherals driver (vibration motor + battery voltage sense) */ + watch_peripherals_init(27); + /* Let's init the I2C interface */ i2c_init(I2C_SDA, I2C_SCL, 100000); @@ -251,8 +254,6 @@ void gfx_task(void *param) else APP_LOG_INFO("BMA456 set pin conf failed"); - watch_peripherals_init(27); - /* Once we are done with the initializing steps we don't forget to turn the backlight on ! */ setBrightness(persistency_get_settings()->display.brightness); @@ -260,6 +261,7 @@ void gfx_task(void *param) extern LCDConfig_t LCDConfig; float temperature = 0; float pressure = 0; + uint16_t battery_voltage = 0; for(;;) { @@ -296,10 +298,11 @@ void gfx_task(void *param) { pressure = BMP280_get_pressure(&temperature); BMP280_trigger_measurement(); - APP_LOG_DEBUG("GFX thread, temp : %0.2f °C, press : %0.2f hPa, bat : %u mV", + APP_LOG_DEBUG("GFX thread, temp : %0.2f °C, press : %0.2f hPa, bat : %u mV <-> %u %%", temperature, pressure/100, - watch_peripherals_get_battery_voltage()); + battery_voltage = watch_peripherals_get_battery_voltage(Battery_Unit_mV), + battery_voltage_to_percentage(battery_voltage)); aliveCounter = 0; } @@ -312,7 +315,10 @@ void gfx_task(void *param) //lcd_on(&LCDConfig, false); lcd_sleep(&LCDConfig, true); QMC5883L_set_power_mode(Standby); - CST816D_set_power_mode(); + if(CST816D_set_power_mode()) + APP_LOG_DEBUG("CST816D Sleep cmd ok"); + else + APP_LOG_DEBUG("CST816D Sleep cmd fail"); // Let's sleep tls_pmu_sleep_start(); // On wake up, we force the watch face to sync up with the rtc /!\ RTC update delay WTF ? @@ -334,8 +340,7 @@ void gfx_task(void *param) { tls_sys_clk_set(CPU_CLK_40M); APP_LOG_DEBUG("CPU 40Mhz"); - } - + } } else { @@ -344,7 +349,6 @@ void gfx_task(void *param) tls_sys_clk_set(CPU_CLK_160M); APP_LOG_DEBUG("CPU 160Mhz"); } - } } } \ No newline at end of file diff --git a/src/W800 SDK v1.00.08/include/wm_type_def.h b/src/W800 SDK v1.00.08/include/wm_type_def.h index 7a125a1..2313422 100644 --- a/src/W800 SDK v1.00.08/include/wm_type_def.h +++ b/src/W800 SDK v1.00.08/include/wm_type_def.h @@ -115,6 +115,11 @@ typedef unsigned long ULONG; #endif typedef unsigned char u8_t; +#ifdef int8_t +#undef int8_t +#endif +typedef signed char int8_t; + #ifdef uint8_t #undef uint8_t #endif