From beb7a86af1e834df9866e22290fd8d41b0c0b17d Mon Sep 17 00:00:00 2001 From: anschrammh Date: Sun, 26 Mar 2023 23:22:45 +0200 Subject: [PATCH] Added a lot of settings handling callbacks, added the code to test the BMA456 step counter feature and it seems to work just fine --- src/W800_SDK_v1.00.10/app/gfx/gfx_task.c | 189 ++++++++++++++++++----- 1 file changed, 149 insertions(+), 40 deletions(-) diff --git a/src/W800_SDK_v1.00.10/app/gfx/gfx_task.c b/src/W800_SDK_v1.00.10/app/gfx/gfx_task.c index 336b316..c0e9bd2 100644 --- a/src/W800_SDK_v1.00.10/app/gfx/gfx_task.c +++ b/src/W800_SDK_v1.00.10/app/gfx/gfx_task.c @@ -17,6 +17,9 @@ #include "watch_peripherals.h" #include "watch_settings.h" +#include "ble_modem.h" +#include "ble_service.h" + static void date_time_cb(struct tm * const dateTime) { if(!dateTime)return; @@ -24,11 +27,7 @@ static void date_time_cb(struct tm * const dateTime) //APP_LOG_DEBUG("RTC time : %d:%d:%d", dateTime->tm_hour, dateTime->tm_min, dateTime->tm_sec); } -static uint8_t battery_percentage = 100; -static uint8_t battery_indicator_cb(void) -{ - return battery_percentage; -} +static uint8_t _battery_percentage = 100; WatchFace_t watchFace; MenuScreen_t menuScreen; @@ -39,7 +38,25 @@ struct bma4_dev bma; struct bma4_accel_config accel_conf; struct bma456w_wrist_wear_wakeup_params setting; struct bma4_int_pin_config pin_config; -uint16_t int_status; + +struct +{ + uint16_t int_status; + bool battery_controller_status; +} _interrupts_statuses = {.int_status = 0, .battery_controller_status = false}; + + +/* This call back is automatically called by the watch face when it wants to refresh the battery */ +static void battery_indicator_cb(uint8_t *levelInPercent, BatteryState_e *batteryState) +{ + *levelInPercent = _battery_percentage; + *batteryState = watch_peripherals_get_battery_controller_status(); +} + +static void battery_controller_status_on_change_cb(battery_controller_status_e old, battery_controller_status_e new) +{ + _interrupts_statuses.battery_controller_status = true; +} static void setGetBrightnessCb(uint8_t *brightness, SettingMode_e mode) { @@ -49,7 +66,10 @@ static void setGetBrightnessCb(uint8_t *brightness, SettingMode_e mode) } else { - persistency_get_settings()->display.display_brightness = *brightness; + // Updates the settings in the watch settings structure + watch_settings_display_set_brightness(*brightness); + + // Physically apply the setting to the hardware watch_peripherals_set_brightness(*brightness); } } @@ -102,7 +122,7 @@ static void setDisplayVibrationDuration(uint8_t *duration, SettingMode_e mode) } else { - persistency_get_settings()->display.display_vibrate_on_touch_duration = *duration; + watch_settings_display_set_vibrate_on_touch_duration(*duration); } } @@ -114,7 +134,7 @@ static void setDisplayVibrationStrength(uint8_t *strength, SettingMode_e mode) } else { - persistency_get_settings()->display.display_vibrate_on_touch_strength = *strength; + watch_settings_display_set_vibrate_on_touch_strength(*strength); } } @@ -126,7 +146,7 @@ static void setTimeoutCb(uint8_t *timeout, SettingMode_e mode) } else { - persistency_get_settings()->display.display_delay_before_sleep = *timeout; + watch_settings_display_set_delay_before_sleep(*timeout); } } @@ -138,7 +158,7 @@ static void setOrientationCb(uint8_t *orientation, SettingMode_e mode) } else { - persistency_get_settings()->display.display_orientation = *orientation; + watch_settings_display_set_orientation(*orientation); watch_peripherals_set_orientation(*orientation); // Forces to redraw the full screen to avoid strange artifacts lv_obj_invalidate(lv_scr_act()); @@ -153,10 +173,26 @@ static void setBLEEnabledCb(bool *enabled, SettingMode_e mode) } else { - persistency_get_settings()->connectivity.connectivity_ble_enabled = *enabled; + watch_settings_connectivity_set_ble_enabled(*enabled); + //Let's turn the BLE on or OFF here + if(*enabled) + { + if(!ble_modem_on(true)) + APP_LOG_ERROR("Failed to start BLE modem with service"); + else + watch_face_set_bluetooth_indicator(&watchFace, BLUETOOTH_STATE_ON); + } + else + { + if(!ble_modem_off()) + APP_LOG_ERROR("Failed to stop BLE modem with service"); + else + watch_face_set_bluetooth_indicator(&watchFace, BLUETOOTH_STATE_OFF); + } } } +/* This may never get implemented due to power consumption */ static void setWiFiEnabledCb(bool *enabled, SettingMode_e mode) { if(SETTING_MODE_GET == mode) @@ -165,7 +201,7 @@ static void setWiFiEnabledCb(bool *enabled, SettingMode_e mode) } else { - persistency_get_settings()->connectivity.connectivity_wifi_enabled = *enabled; + watch_settings_connectivity_set_wifi_enabled(*enabled); } } @@ -177,21 +213,46 @@ static void setLanguageCb(uint8_t *language, SettingMode_e mode) } else { - persistency_get_settings()->languageAndUI.language = *language; + watch_settings_language_and_UI_set_language(*language); } } +static void saveSettingsToFlash(void) +{ + /*if(!persistency_save_settings_to_flash()) + { + APP_LOG_ERROR("Failed to save watch settings to flash"); + }*/ +} + +static void performFactoryReset() +{ + // Reload factory settings + persistency_factory_reset(); + + if(!persistency_save_settings_to_flash()) + { + APP_LOG_ERROR("Failed to save factory reset settings to flash"); + return; + } + + // We do a hardware reset of the watch + tls_sys_reset(); +} + SettingsScreenAPIInterface_t settingsScreenAPIInterface = { - .setTimeSettingsCb = setTimeCb, - .setBrightnessSettingsCb = setGetBrightnessCb, - .setTimeoutSettingsCb = setTimeoutCb, - .setDisplayVibrationDurationSettingsCb = setDisplayVibrationDuration, - .setDisplayVibrationStrengthSettingsCb = setDisplayVibrationStrength, - .setOrientationSettingsCb = setOrientationCb, - .setBLEEnabledSettingsCb = setBLEEnabledCb, - .setWiFiEnabledSettingsCb = setWiFiEnabledCb, - .setLanguageSettingsCb = setLanguageCb, + .setTimeSettingsCb = &(setTimeCb), + .setBrightnessSettingsCb = &(setGetBrightnessCb), + .setTimeoutSettingsCb = &(setTimeoutCb), + .setDisplayVibrationDurationSettingsCb = &(setDisplayVibrationDuration), + .setDisplayVibrationStrengthSettingsCb = &(setDisplayVibrationStrength), + .setOrientationSettingsCb = &(setOrientationCb), + .setBLEEnabledSettingsCb = &(setBLEEnabledCb), + .setWiFiEnabledSettingsCb = &(setWiFiEnabledCb), + .setLanguageSettingsCb = &(setLanguageCb), + .saveSettingsCb = &(saveSettingsToFlash), + .factoryResetCb = &(performFactoryReset), }; static uint16_t angle_with_offset(uint16_t angle, uint16_t offset) @@ -223,6 +284,21 @@ static void delay_us(uint32_t period, void *intf_ptr) tls_os_time_delay(pdMS_TO_TICKS(period / 1000)); } +static void ble_service_state_change_cb(ble_service_state_e ble_service_state) +{ + switch(ble_service_state) + { + case BLE_SERVICE_MODE_CONNECTED: + watch_face_set_bluetooth_indicator(&watchFace, BLUETOOTH_STATE_CONNECTED); + break; + case BLE_SERVICE_MODE_ADVERTISING: + watch_face_set_bluetooth_indicator(&watchFace, BLUETOOTH_STATE_ON); + break; + default: + break; + } +} + static void scan_result_cb(void) { size_t buffer_size = sizeof(struct tls_scan_bss_format2_t) + sizeof(struct tls_bss_info_format2_t) * 10; @@ -262,11 +338,26 @@ static void scan_result_cb(void) void gfx_task(void *param) { - APP_LOG_TRACE("starting"); + APP_LOG_TRACE("GFX task starting"); /* Init and load watch settings using the persistency layer */ persistency_init(); + if(!persistency_load_settings_from_flash()) + { + APP_LOG_ERROR("Failed to retrieve watch settings from flash"); + } + + //persistency_debug(); + + /* Let's init the watch peripherals driver (vibration motor + battery voltage sense) */ + watch_peripherals_init(27); + watch_peripherals_register_battery_controller_status_change_cb(&(battery_controller_status_on_change_cb)); + + /* Make the first battery voltage reading here */ + uint16_t battery_voltage = watch_peripherals_get_battery_voltage(battery_unit_mv); + _battery_percentage = battery_voltage_to_percentage(battery_voltage); + /* Check whether the RTC is running or not, if not, then the board was reset So we start the RTC */ if(!tls_is_rtc_running()) @@ -299,7 +390,7 @@ void gfx_task(void *param) lv_scr_load(watchFace.display); /* Let's init the I2C interface */ - i2c_init(I2C_SDA, I2C_SCL, 100000); + i2c_init(I2C_SDA, I2C_SCL, I2C_CLOCK_SPEED); uint8_t aliveCounter = 0; @@ -374,7 +465,7 @@ void gfx_task(void *param) else APP_LOG_INFO("BMA456 accel en failed"); - bma456w_feature_enable(BMA456W_WRIST_WEAR_WAKEUP, 1, &bma); + bma456w_feature_enable(BMA456W_WRIST_WEAR_WAKEUP, BMA4_ENABLE, &bma); bma456w_get_wrist_wear_wakeup_param_config(&setting, &bma); @@ -401,19 +492,21 @@ void gfx_task(void *param) else APP_LOG_INFO("BMA456 set pin conf failed"); - /* Let's init the watch peripherals driver (vibration motor + battery voltage sense) */ - watch_peripherals_init(27); - /* Make the first battery voltage reading here */ - uint16_t battery_voltage = watch_peripherals_get_battery_voltage(battery_unit_mv); - battery_percentage = battery_voltage_to_percentage(battery_voltage); - watch_face_set_battery_indicator(&watchFace, battery_percentage); + /* Configure BMA's step counter */ + if(bma456w_feature_enable(BMA456W_STEP_CNTR, BMA4_ENABLE, &bma) == BMA4_OK) + APP_LOG_INFO("BMA456 step cnter feature enable ok"); + else + APP_LOG_INFO("BMA456 step cnter feature enable failed"); + + /* Configure and register BLE stack and services callbacks */ + ble_service_register_state_change_cb(&(ble_service_state_change_cb)); /* Once we are done with the initializing steps we don't forget to turn the backlight on ! */ watch_peripherals_set_brightness(persistency_get_settings()->display.display_brightness); /* Enable WiFi hotspot scanning for antenna performance test purposes */ - tls_wifi_scan_result_cb_register(&(scan_result_cb)); + //tls_wifi_scan_result_cb_register(&(scan_result_cb)); extern LCDConfig_t LCDConfig; float temperature = 0; @@ -441,27 +534,33 @@ void gfx_task(void *param) } - uint8_t rslt = bma456w_read_int_status(&int_status, &bma); + uint8_t rslt = bma456w_read_int_status(&_interrupts_statuses.int_status, &bma); if(rslt != BMA4_OK) - APP_LOG_DEBUG("Failed to read int status"); + APP_LOG_DEBUG("Failed to read int status"); - if((BMA4_OK == rslt) && (int_status & BMA456W_WRIST_WEAR_WAKEUP_INT)) + if((BMA4_OK == rslt) && (_interrupts_statuses.int_status & BMA456W_WRIST_WEAR_WAKEUP_INT)) { APP_LOG_DEBUG("Wrist tilt"); } if(++aliveCounter % 200 == 0) { + uint32_t steps = 0; + if(bma456w_step_counter_output(&steps, &bma) != BMA4_OK) + APP_LOG_DEBUG("Failed to read step counts"); + + watch_face_set_step_count(&watchFace, steps); + pressure = BMP280_get_pressure(&temperature); BMP280_trigger_measurement(); battery_voltage = watch_peripherals_get_battery_voltage(battery_unit_mv); - battery_percentage = battery_voltage_to_percentage(battery_voltage); - APP_LOG_DEBUG("GFX thread, temp : %0.2f °C, press : %0.2f hPa, bat(%d) : %u mV <-> %u %%", + _battery_percentage = battery_voltage_to_percentage(battery_voltage); + APP_LOG_DEBUG("GFX thread, temp : %0.2f °C, press : %0.2f hPa, battery(%s) : %u mV <-> %u %%", temperature, pressure/100, - watch_peripherals_get_battery_controller_status(), + battery_controller_status_2_str(watch_peripherals_get_battery_controller_status()), battery_voltage, - battery_percentage); + _battery_percentage); //APP_LOG_DEBUG("Scanning WiFi : %d", tls_wifi_scan()); @@ -512,5 +611,15 @@ void gfx_task(void *param) APP_LOG_DEBUG("CPU 160Mhz"); } } + + /* Handle any interrupts status */ + if(_interrupts_statuses.battery_controller_status) + { + _interrupts_statuses.battery_controller_status = false; + //Let's refresh the battery percentage as well: + battery_voltage = watch_peripherals_get_battery_voltage(battery_unit_mv); + _battery_percentage = battery_voltage_to_percentage(battery_voltage); + watch_face_set_battery_indicator(&watchFace, _battery_percentage, watch_peripherals_get_battery_controller_status()); + } } } \ No newline at end of file