From 7f9cc51b050e1034b573e37484f7afe29c370d81 Mon Sep 17 00:00:00 2001 From: Jean-François Milants Date: Sun, 6 Jun 2021 15:56:03 +0200 Subject: Initialize SystemTask, DisplayApp and HeartRateTask as global static variable instead of variables on the heap. We don't need them on the heap as we know their size at build time, it'll reduce memory fragmentation and it'll make memory analysis easier. --- src/systemtask/SystemTask.h | 53 ++++++++++++++++++--------------------------- 1 file changed, 21 insertions(+), 32 deletions(-) (limited to 'src/systemtask/SystemTask.h') diff --git a/src/systemtask/SystemTask.h b/src/systemtask/SystemTask.h index ea41a69d..fa6a949c 100644 --- a/src/systemtask/SystemTask.h +++ b/src/systemtask/SystemTask.h @@ -6,7 +6,6 @@ #include #include #include -#include #include #include #include @@ -27,6 +26,7 @@ #endif #include "drivers/Watchdog.h" +#include "Messages.h" namespace Pinetime { namespace Drivers { @@ -40,27 +40,6 @@ namespace Pinetime { namespace System { class SystemTask { public: - enum class Messages { - GoToSleep, - GoToRunning, - TouchWakeUp, - OnNewTime, - OnNewNotification, - OnTimerDone, - OnNewCall, - BleConnected, - UpdateTimeOut, - BleFirmwareUpdateStarted, - BleFirmwareUpdateFinished, - OnTouchEvent, - OnButtonEvent, - OnDisplayTaskSleeping, - EnableSleeping, - DisableSleeping, - OnNewDay, - OnChargingEvent - }; - SystemTask(Drivers::SpiMaster& spi, Drivers::St7789& lcd, Pinetime::Drivers::SpiNorFlash& spiNorFlash, @@ -69,10 +48,18 @@ namespace Pinetime { Components::LittleVgl& lvgl, Controllers::Battery& batteryController, Controllers::Ble& bleController, + Controllers::DateTime& dateTimeController, + Controllers::TimerController& timerController, + Drivers::Watchdog& watchdog, + Pinetime::Controllers::NotificationManager& notificationManager, Pinetime::Controllers::MotorController& motorController, Pinetime::Drivers::Hrs3300& heartRateSensor, + Pinetime::Controllers::MotionController& motionController, Pinetime::Drivers::Bma421& motionSensor, - Controllers::Settings& settingsController); + Controllers::Settings& settingsController, + Pinetime::Controllers::HeartRateController& heartRateController, + Pinetime::Applications::DisplayApp& displayApp, + Pinetime::Applications::HeartRateTask& heartRateApp); void Start(); void PushMessage(Messages msg); @@ -96,27 +83,29 @@ namespace Pinetime { Pinetime::Drivers::Cst816S& touchPanel; Pinetime::Components::LittleVgl& lvgl; Pinetime::Controllers::Battery& batteryController; - std::unique_ptr displayApp; - Pinetime::Controllers::HeartRateController heartRateController; - std::unique_ptr heartRateApp; + Pinetime::Controllers::Ble& bleController; - Pinetime::Controllers::DateTime dateTimeController; - Pinetime::Controllers::TimerController timerController; + Pinetime::Controllers::DateTime& dateTimeController; + Pinetime::Controllers::TimerController& timerController; QueueHandle_t systemTasksMsgQueue; std::atomic isSleeping {false}; std::atomic isGoingToSleep {false}; std::atomic isWakingUp {false}; - Pinetime::Drivers::Watchdog watchdog; - Pinetime::Drivers::WatchdogView watchdogView; - Pinetime::Controllers::NotificationManager notificationManager; + Pinetime::Drivers::Watchdog& watchdog; + Pinetime::Controllers::NotificationManager& notificationManager; Pinetime::Controllers::MotorController& motorController; Pinetime::Drivers::Hrs3300& heartRateSensor; Pinetime::Drivers::Bma421& motionSensor; Pinetime::Controllers::Settings& settingsController; + Pinetime::Controllers::HeartRateController& heartRateController; Pinetime::Controllers::NimbleController nimbleController; Controllers::BrightnessController brightnessController; - Pinetime::Controllers::MotionController motionController; + Pinetime::Controllers::MotionController& motionController; + + Pinetime::Applications::DisplayApp& displayApp; + Pinetime::Applications::HeartRateTask& heartRateApp; + static constexpr uint8_t pinSpiSck = 2; static constexpr uint8_t pinSpiMosi = 3; -- cgit v1.2.3 From 0045fb16b66b008c13888312b92688dcb7535a0c Mon Sep 17 00:00:00 2001 From: Neil O'Fix <69880003+nlfx@users.noreply.github.com> Date: Sat, 26 Jun 2021 18:53:32 +0000 Subject: SPI flash sleep if bootloader >= 1.0.0 (#322) * Retrieve and display bootloader version - Display bootloader version on System Info screen - Enable SPI flash sleep mode if bootloader version >= 1.0.0 * Wait for SPI flash to wakeup before starting OTA DFU --- src/BootloaderVersion.cpp | 30 ++++++++++++++++++++---------- src/BootloaderVersion.h | 15 ++++++++++----- src/components/ble/DfuService.cpp | 5 +++++ src/displayapp/screens/SystemInfo.cpp | 11 +++++++---- src/main.cpp | 4 ++++ src/systemtask/SystemTask.h | 4 ++++ 6 files changed, 50 insertions(+), 19 deletions(-) (limited to 'src/systemtask/SystemTask.h') diff --git a/src/BootloaderVersion.cpp b/src/BootloaderVersion.cpp index 5eba7a1d..07a1da4e 100644 --- a/src/BootloaderVersion.cpp +++ b/src/BootloaderVersion.cpp @@ -1,26 +1,36 @@ #include +#include #include "BootloaderVersion.h" using namespace Pinetime; -// NOTE : current bootloader does not export its version to the application firmware. +// NOTE : version < 1.0.0 of bootloader does not export its version to the application firmware. -uint32_t BootloaderVersion::Major() { - return 0; +uint32_t BootloaderVersion::version = 0; +char BootloaderVersion::versionString[BootloaderVersion::VERSION_STR_LEN] = "0.0.0"; + +const uint32_t BootloaderVersion::Major() { + return (BootloaderVersion::version >> 16u) & 0xff; } -uint32_t BootloaderVersion::Minor() { - return 0; +const uint32_t BootloaderVersion::Minor() { + return (BootloaderVersion::version >> 8u) & 0xff; } -uint32_t BootloaderVersion::Patch() { - return 0; +const uint32_t BootloaderVersion::Patch() { + return BootloaderVersion::version & 0xff; } const char* BootloaderVersion::VersionString() { - return "0.0.0"; + return BootloaderVersion::versionString; +} + +const bool BootloaderVersion::IsValid() { + return BootloaderVersion::version >= 0x00010000; } -bool BootloaderVersion::IsValid() { - return false; +void BootloaderVersion::SetVersion(uint32_t v) { + BootloaderVersion::version = v; + snprintf(BootloaderVersion::versionString, BootloaderVersion::VERSION_STR_LEN, "%ld.%ld.%ld", + BootloaderVersion::Major(), BootloaderVersion::Minor(), BootloaderVersion::Patch()); } diff --git a/src/BootloaderVersion.h b/src/BootloaderVersion.h index c1ede0f5..f8127414 100644 --- a/src/BootloaderVersion.h +++ b/src/BootloaderVersion.h @@ -3,10 +3,15 @@ namespace Pinetime { class BootloaderVersion { public: - static uint32_t Major(); - static uint32_t Minor(); - static uint32_t Patch(); + static const uint32_t Major(); + static const uint32_t Minor(); + static const uint32_t Patch(); static const char* VersionString(); - static bool IsValid(); + static const bool IsValid(); + static void SetVersion(uint32_t v); + private: + static uint32_t version; + static constexpr size_t VERSION_STR_LEN = 12; + static char versionString[VERSION_STR_LEN]; }; -} \ No newline at end of file +} diff --git a/src/components/ble/DfuService.cpp b/src/components/ble/DfuService.cpp index cec194cc..e6bcea81 100644 --- a/src/components/ble/DfuService.cpp +++ b/src/components/ble/DfuService.cpp @@ -121,6 +121,11 @@ int DfuService::WritePacketHandler(uint16_t connectionHandle, os_mbuf* om) { NRF_LOG_INFO( "[DFU] -> Start data received : SD size : %d, BT size : %d, app size : %d", softdeviceSize, bootloaderSize, applicationSize); + // wait until SystemTask has finished waking up all devices + while (systemTask.IsSleeping()) { + vTaskDelay(50); // 50ms + } + dfuImage.Erase(); uint8_t data[] {16, 1, 1}; diff --git a/src/displayapp/screens/SystemInfo.cpp b/src/displayapp/screens/SystemInfo.cpp index 1cf895c7..60e53ad6 100644 --- a/src/displayapp/screens/SystemInfo.cpp +++ b/src/displayapp/screens/SystemInfo.cpp @@ -3,6 +3,7 @@ #include "../DisplayApp.h" #include "Label.h" #include "Version.h" +#include "BootloaderVersion.h" #include "components/battery/BatteryController.h" #include "components/ble/BleController.h" #include "components/brightness/BrightnessController.h" @@ -83,17 +84,19 @@ std::unique_ptr SystemInfo::CreateScreen1() { lv_label_set_recolor(label, true); lv_label_set_text_fmt(label, "#FFFF00 InfiniTime#\n\n" - "#444444 Version# %ld.%ld.%ld\n\n" - "#444444 Short Ref# %s\n\n" + "#444444 Version# %ld.%ld.%ld\n" + "#444444 Short Ref# %s\n" "#444444 Build date#\n" "%s\n" - "%s\n", + "%s\n\n" + "#444444 Bootloader# %s", Version::Major(), Version::Minor(), Version::Patch(), Version::GitCommitHash(), __DATE__, - __TIME__); + __TIME__, + BootloaderVersion::VersionString()); lv_label_set_align(label, LV_LABEL_ALIGN_CENTER); lv_obj_align(label, lv_scr_act(), LV_ALIGN_CENTER, 0, 0); return std::make_unique(0, 5, app, label); diff --git a/src/main.cpp b/src/main.cpp index 4c2c5de8..5832a78f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -28,6 +28,7 @@ #include #include +#include "BootloaderVersion.h" #include "components/battery/BatteryController.h" #include "components/ble/BleController.h" #include "components/ble/NotificationManager.h" @@ -306,6 +307,9 @@ int main(void) { debounceTimer = xTimerCreate("debounceTimer", 200, pdFALSE, (void*) 0, DebounceTimerCallback); debounceChargeTimer = xTimerCreate("debounceTimerCharge", 200, pdFALSE, (void*) 0, DebounceTimerChargeCallback); + // retrieve version stored by bootloader + Pinetime::BootloaderVersion::SetVersion(NRF_TIMER2->CC[0]); + lvgl.Init(); systemTask.Start(); diff --git a/src/systemtask/SystemTask.h b/src/systemtask/SystemTask.h index fa6a949c..f563640c 100644 --- a/src/systemtask/SystemTask.h +++ b/src/systemtask/SystemTask.h @@ -73,6 +73,10 @@ namespace Pinetime { return nimbleController; }; + bool IsSleeping() const { + return isSleeping; + } + private: TaskHandle_t taskHandle; -- cgit v1.2.3 From 084123b752c74f7b8a6da23cb9bce0ac4efa3715 Mon Sep 17 00:00:00 2001 From: "joaquim.org" Date: Sun, 11 Jul 2021 14:06:06 +0100 Subject: Using littlefs (#438) * add submodule littlefs * base fs * Save settings using littlefs * Small fixes and suggestions from PR * More small fixes from PR suggestions * Code clean up * Change SpiNorFlash functions to be private in FS --- .gitmodules | 3 + src/CMakeLists.txt | 29 +++++- src/components/fs/FS.cpp | 197 +++++++++++++++++++++++++++++++++++ src/components/fs/FS.h | 71 +++++++++++++ src/components/settings/Settings.cpp | 100 ++++-------------- src/components/settings/Settings.h | 58 +++++------ src/libs/littlefs | 1 + src/libs/lv_conf.h | 4 +- src/main.cpp | 13 ++- src/systemtask/SystemTask.cpp | 11 +- src/systemtask/SystemTask.h | 12 ++- 11 files changed, 369 insertions(+), 130 deletions(-) create mode 100644 src/components/fs/FS.cpp create mode 100644 src/components/fs/FS.h create mode 160000 src/libs/littlefs (limited to 'src/systemtask/SystemTask.h') diff --git a/.gitmodules b/.gitmodules index 6f6d0e10..815fc022 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "src/libs/lvgl"] path = src/libs/lvgl url = https://github.com/joaquimorg/lvgl.git +[submodule "src/libs/littlefs"] + path = src/libs/littlefs + url = https://github.com/littlefs-project/littlefs.git diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index aca86543..40e1f2a5 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -166,6 +166,13 @@ set(NIMBLE_SRC libs/mynewt-nimble/nimble/host/util/src/addr.c ) +set(LITTLEFS_SRC + libs/littlefs/lfs_util.h + libs/littlefs/lfs.h + libs/littlefs/lfs_util.c + libs/littlefs/lfs.c + ) + set(LVGL_SRC libs/lv_conf.h libs/lvgl/lvgl.h @@ -465,6 +472,7 @@ list(APPEND SOURCE_FILES components/motor/MotorController.cpp components/settings/Settings.cpp components/timer/TimerController.cpp + components/fs/FS.cpp drivers/Cst816s.cpp FreeRTOS/port.c FreeRTOS/port_cmsis_systick.c @@ -543,6 +551,7 @@ list(APPEND RECOVERY_SOURCE_FILES components/heartrate/Biquad.cpp components/heartrate/Ptagc.cpp components/motor/MotorController.cpp + components/fs/FS.cpp ) list(APPEND RECOVERYLOADER_SOURCE_FILES @@ -801,13 +810,25 @@ target_compile_options(lvgl PRIVATE $<$: -MP -MD -x assembler-with-cpp> ) +# LITTLEFS_SRC +add_library(littlefs STATIC ${LITTLEFS_SRC}) +target_include_directories(littlefs SYSTEM PUBLIC . ../) +target_include_directories(littlefs SYSTEM PUBLIC ${INCLUDES_FROM_LIBS}) +target_compile_options(littlefs PRIVATE + $<$,$>: ${COMMON_FLAGS} -Wno-unused-function -Og -g3> + $<$,$>: ${COMMON_FLAGS} -Wno-unused-function -Os> + $<$,$>: ${COMMON_FLAGS} -Wno-unused-function -Og -g3 -fno-rtti> + $<$,$>: ${COMMON_FLAGS} -Wno-unused-function -Os -fno-rtti> + $<$: -MP -MD -x assembler-with-cpp> + ) + # Build autonomous binary (without support for bootloader) set(EXECUTABLE_NAME "pinetime-app") set(EXECUTABLE_FILE_NAME ${EXECUTABLE_NAME}-${pinetime_VERSION_MAJOR}.${pinetime_VERSION_MINOR}.${pinetime_VERSION_PATCH}) set(NRF5_LINKER_SCRIPT "${CMAKE_SOURCE_DIR}/gcc_nrf52.ld") add_executable(${EXECUTABLE_NAME} ${SOURCE_FILES}) set_target_properties(${EXECUTABLE_NAME} PROPERTIES OUTPUT_NAME ${EXECUTABLE_FILE_NAME}) -target_link_libraries(${EXECUTABLE_NAME} nimble nrf-sdk lvgl) +target_link_libraries(${EXECUTABLE_NAME} nimble nrf-sdk lvgl littlefs) target_compile_options(${EXECUTABLE_NAME} PUBLIC $<$,$>: ${COMMON_FLAGS} -Og -g3> $<$,$>: ${COMMON_FLAGS} -Os> @@ -836,7 +857,7 @@ set(IMAGE_MCUBOOT_FILE_NAME ${EXECUTABLE_MCUBOOT_NAME}-image-${pinetime_VERSION_ set(DFU_MCUBOOT_FILE_NAME ${EXECUTABLE_MCUBOOT_NAME}-dfu-${pinetime_VERSION_MAJOR}.${pinetime_VERSION_MINOR}.${pinetime_VERSION_PATCH}.zip) set(NRF5_LINKER_SCRIPT_MCUBOOT "${CMAKE_SOURCE_DIR}/gcc_nrf52-mcuboot.ld") add_executable(${EXECUTABLE_MCUBOOT_NAME} ${SOURCE_FILES}) -target_link_libraries(${EXECUTABLE_MCUBOOT_NAME} nimble nrf-sdk lvgl) +target_link_libraries(${EXECUTABLE_MCUBOOT_NAME} nimble nrf-sdk lvgl littlefs) set_target_properties(${EXECUTABLE_MCUBOOT_NAME} PROPERTIES OUTPUT_NAME ${EXECUTABLE_MCUBOOT_FILE_NAME}) target_compile_options(${EXECUTABLE_MCUBOOT_NAME} PUBLIC $<$,$>: ${COMMON_FLAGS} -Og -g3> @@ -872,7 +893,7 @@ endif() set(EXECUTABLE_RECOVERY_NAME "pinetime-recovery") set(EXECUTABLE_RECOVERY_FILE_NAME ${EXECUTABLE_RECOVERY_NAME}-${pinetime_VERSION_MAJOR}.${pinetime_VERSION_MINOR}.${pinetime_VERSION_PATCH}) add_executable(${EXECUTABLE_RECOVERY_NAME} ${RECOVERY_SOURCE_FILES}) -target_link_libraries(${EXECUTABLE_RECOVERY_NAME} nimble nrf-sdk) +target_link_libraries(${EXECUTABLE_RECOVERY_NAME} nimble nrf-sdk littlefs) set_target_properties(${EXECUTABLE_RECOVERY_NAME} PROPERTIES OUTPUT_NAME ${EXECUTABLE_RECOVERY_FILE_NAME}) target_compile_definitions(${EXECUTABLE_RECOVERY_NAME} PUBLIC "PINETIME_IS_RECOVERY") target_compile_options(${EXECUTABLE_RECOVERY_NAME} PUBLIC @@ -902,7 +923,7 @@ set(EXECUTABLE_RECOVERY_MCUBOOT_FILE_NAME ${EXECUTABLE_RECOVERY_MCUBOOT_NAME}-${ set(IMAGE_RECOVERY_MCUBOOT_FILE_NAME ${EXECUTABLE_RECOVERY_MCUBOOT_NAME}-image-${pinetime_VERSION_MAJOR}.${pinetime_VERSION_MINOR}.${pinetime_VERSION_PATCH}.bin) set(DFU_RECOVERY_MCUBOOT_FILE_NAME ${EXECUTABLE_RECOVERY_MCUBOOT_NAME}-dfu-${pinetime_VERSION_MAJOR}.${pinetime_VERSION_MINOR}.${pinetime_VERSION_PATCH}.zip) add_executable(${EXECUTABLE_RECOVERY_MCUBOOT_NAME} ${RECOVERY_SOURCE_FILES}) -target_link_libraries(${EXECUTABLE_RECOVERY_MCUBOOT_NAME} nimble nrf-sdk) +target_link_libraries(${EXECUTABLE_RECOVERY_MCUBOOT_NAME} nimble nrf-sdk littlefs) set_target_properties(${EXECUTABLE_RECOVERY_MCUBOOT_NAME} PROPERTIES OUTPUT_NAME ${EXECUTABLE_RECOVERY_MCUBOOT_FILE_NAME}) target_compile_definitions(${EXECUTABLE_RECOVERY_MCUBOOT_NAME} PUBLIC "PINETIME_IS_RECOVERY") target_compile_options(${EXECUTABLE_RECOVERY_MCUBOOT_NAME} PUBLIC diff --git a/src/components/fs/FS.cpp b/src/components/fs/FS.cpp new file mode 100644 index 00000000..857e6bf9 --- /dev/null +++ b/src/components/fs/FS.cpp @@ -0,0 +1,197 @@ +#include "FS.h" +#include +#include +#include + +using namespace Pinetime::Controllers; + +FS::FS(Pinetime::Drivers::SpiNorFlash& driver) : + flashDriver{ driver }, + lfsConfig{ + .context = this, + .read = SectorRead, + .prog = SectorProg, + .erase = SectorErase, + .sync = SectorSync, + + .read_size = 16, + .prog_size = 8, + .block_size = blockSize, + .block_count = size / blockSize, + .block_cycles = 1000u, + + .cache_size = 16, + .lookahead_size = 16, + + .name_max = 50, + .attr_max = 50, + } +{ } + + +void FS::Init() { + + // try mount + int err = lfs_mount(&lfs, &lfsConfig); + + // reformat if we can't mount the filesystem + // this should only happen on the first boot + if (err != LFS_ERR_OK) { + lfs_format(&lfs, &lfsConfig); + err = lfs_mount(&lfs, &lfsConfig); + if (err != LFS_ERR_OK) { + return; + } + } + +#ifndef PINETIME_IS_RECOVERY + VerifyResource(); + LVGLFileSystemInit(); +#endif + +} + +void FS::VerifyResource() { + // validate the resource metadata + resourcesValid = true; +} + +int FS::FileOpen(lfs_file_t* file_p, const char* fileName, const int flags) { + return lfs_file_open(&lfs, file_p, fileName, flags); +} + +int FS::FileClose(lfs_file_t* file_p) { + return lfs_file_close(&lfs, file_p); +} + +int FS::FileRead(lfs_file_t* file_p, uint8_t* buff, uint32_t size) { + return lfs_file_read(&lfs, file_p, buff, size); +} + +int FS::FileWrite(lfs_file_t* file_p, const uint8_t* buff, uint32_t size) { + return lfs_file_write(&lfs, file_p, buff, size); +} + +int FS::FileSeek(lfs_file_t* file_p, uint32_t pos) { + return lfs_file_seek(&lfs, file_p, pos, LFS_SEEK_SET); +} + +int FS::FileDelete(const char* fileName) { + return lfs_remove(&lfs, fileName); +} + + +int FS::DirCreate(const char* path) { + return lfs_mkdir(&lfs, path); +} + +// Delete directory and all files inside +int FS::DirDelete(const char* path) { + + lfs_dir_t lfs_dir; + lfs_info entryInfo; + + int err; + err = lfs_dir_open(&lfs, &lfs_dir, path); + if (err) { + return err; + } + while (lfs_dir_read(&lfs, &lfs_dir, &entryInfo)) { + lfs_remove(&lfs, entryInfo.name); + } + lfs_dir_close(&lfs, &lfs_dir); + return LFS_ERR_OK; +} + +/* + + ----------- Interface between littlefs and SpiNorFlash ----------- + +*/ +int FS::SectorSync(const struct lfs_config* c) { + return 0; +} + +int FS::SectorErase(const struct lfs_config* c, lfs_block_t block) { + Pinetime::Controllers::FS& lfs = *(static_cast(c->context)); + const size_t address = startAddress + (block * blockSize); + lfs.flashDriver.SectorErase(address); + return lfs.flashDriver.EraseFailed() ? -1 : 0; +} + +int FS::SectorProg(const struct lfs_config* c, lfs_block_t block, lfs_off_t off, const void* buffer, lfs_size_t size) { + Pinetime::Controllers::FS& lfs = *(static_cast(c->context)); + const size_t address = startAddress + (block * blockSize) + off; + lfs.flashDriver.Write(address, (uint8_t*) buffer, size); + return lfs.flashDriver.ProgramFailed() ? -1 : 0; +} + +int FS::SectorRead(const struct lfs_config* c, lfs_block_t block, lfs_off_t off, void* buffer, lfs_size_t size) { + Pinetime::Controllers::FS& lfs = *(static_cast(c->context)); + const size_t address = startAddress + (block * blockSize) + off; + lfs.flashDriver.Read(address, static_cast(buffer), size); + return 0; +} + +/* + + ----------- LVGL filesystem integration ----------- + +*/ + +namespace { + lv_fs_res_t lvglOpen(lv_fs_drv_t* drv, void* file_p, const char* path, lv_fs_mode_t mode) { + + lfs_file_t* file = static_cast(file_p); + FS* filesys = static_cast(drv->user_data); + filesys->FileOpen(file, path, LFS_O_RDONLY); + + if (file->type == 0) { + return LV_FS_RES_FS_ERR; + } + else { + return LV_FS_RES_OK; + } + } + + lv_fs_res_t lvglClose(lv_fs_drv_t* drv, void* file_p) { + FS* filesys = static_cast(drv->user_data); + lfs_file_t* file = static_cast(file_p); + filesys->FileClose(file); + + return LV_FS_RES_OK; + } + + lv_fs_res_t lvglRead(lv_fs_drv_t* drv, void* file_p, void* buf, uint32_t btr, uint32_t* br) { + FS* filesys = static_cast(drv->user_data); + lfs_file_t* file = static_cast(file_p); + filesys->FileRead(file, static_cast(buf), btr); + *br = btr; + return LV_FS_RES_OK; + } + + lv_fs_res_t lvglSeek(lv_fs_drv_t* drv, void* file_p, uint32_t pos) { + FS* filesys = static_cast(drv->user_data); + lfs_file_t* file = static_cast(file_p); + filesys->FileSeek(file, pos); + return LV_FS_RES_OK; + } +} + +void FS::LVGLFileSystemInit() { + + lv_fs_drv_t fs_drv; + lv_fs_drv_init(&fs_drv); + + fs_drv.file_size = sizeof(lfs_file_t); + fs_drv.letter = 'F'; + fs_drv.open_cb = lvglOpen; + fs_drv.close_cb = lvglClose; + fs_drv.read_cb = lvglRead; + fs_drv.seek_cb = lvglSeek; + + fs_drv.user_data = this; + + lv_fs_drv_register(&fs_drv); + +} \ No newline at end of file diff --git a/src/components/fs/FS.h b/src/components/fs/FS.h new file mode 100644 index 00000000..1f2eb7e0 --- /dev/null +++ b/src/components/fs/FS.h @@ -0,0 +1,71 @@ +#pragma once + +#include +#include "drivers/SpiNorFlash.h" +#include + +namespace Pinetime { + namespace Controllers { + class FS { + public: + FS(Pinetime::Drivers::SpiNorFlash&); + + void Init(); + void LVGLFileSystemInit(); + + int FileOpen(lfs_file_t* file_p, const char* fileName, const int flags); + int FileClose(lfs_file_t* file_p); + int FileRead(lfs_file_t* file_p, uint8_t* buff, uint32_t size); + int FileWrite(lfs_file_t* file_p, const uint8_t* buff, uint32_t size); + int FileSeek(lfs_file_t* file_p, uint32_t pos); + + int FileDelete(const char* fileName); + + int DirCreate(const char* path); + int DirDelete(const char* path); + + void VerifyResource(); + + private: + + Pinetime::Drivers::SpiNorFlash& flashDriver; + + /* + * External Flash MAP (4 MBytes) + * + * 0x000000 +---------------------------------------+ + * | Bootloader Assets | + * | 256 KBytes | + * | | + * 0x040000 +---------------------------------------+ + * | OTA | + * | 464 KBytes | + * | | + * | | + * | | + * 0x0B4000 +---------------------------------------+ + * | File System | + * | | + * | | + * | | + * | | + * 0x400000 +---------------------------------------+ + * + */ + static constexpr size_t startAddress = 0x0B4000; + static constexpr size_t size = 0x3C0000; + static constexpr size_t blockSize = 4096; + + bool resourcesValid = false; + const struct lfs_config lfsConfig; + + lfs_t lfs; + + static int SectorSync(const struct lfs_config* c); + static int SectorErase(const struct lfs_config* c, lfs_block_t block); + static int SectorProg(const struct lfs_config* c, lfs_block_t block, lfs_off_t off, const void* buffer, lfs_size_t size); + static int SectorRead(const struct lfs_config* c, lfs_block_t block, lfs_off_t off, void* buffer, lfs_size_t size); + + }; + } +} diff --git a/src/components/settings/Settings.cpp b/src/components/settings/Settings.cpp index 071940b8..37c09f91 100644 --- a/src/components/settings/Settings.cpp +++ b/src/components/settings/Settings.cpp @@ -4,108 +4,44 @@ using namespace Pinetime::Controllers; -struct SettingsHeader { - uint8_t isActive; // 0xF1 = Block is active, 0xF0 = Block is inactive - uint16_t version; // Current version, to verify if the saved data is for the current Version -}; - -#define HEADER_SIZE sizeof(SettingsHeader) - -Settings::Settings(Pinetime::Drivers::SpiNorFlash& spiNorFlash) : spiNorFlash {spiNorFlash} { +Settings::Settings(Pinetime::Controllers::FS& fs) : fs {fs} { } void Settings::Init() { // Load default settings from Flash - LoadSettingsFromFlash(); + LoadSettingsFromFile(); } void Settings::SaveSettings() { // verify if is necessary to save if (settingsChanged) { - SaveSettingsToFlash(); + SaveSettingsToFile(); } settingsChanged = false; } -bool Settings::FindHeader() { - SettingsHeader settingsHeader; - uint8_t bufferHead[sizeof(settingsHeader)]; - - for (uint8_t block = 0; block < 10; block++) { +void Settings::LoadSettingsFromFile() { + SettingsData bufferSettings; + lfs_file_t settingsFile; - spiNorFlash.Read(settingsBaseAddr + (block * 0x1000), bufferHead, sizeof(settingsHeader)); - std::memcpy(&settingsHeader, bufferHead, sizeof(settingsHeader)); - if (settingsHeader.isActive == 0xF1 && settingsHeader.version == settingsVersion) { - settingsFlashBlock = block; - return true; - } + if ( fs.FileOpen(&settingsFile, "/settings.dat", LFS_O_RDWR | LFS_O_CREAT) != LFS_ERR_OK) { + return; } - return false; -} - -void Settings::ReadSettingsData() { - uint8_t bufferSettings[sizeof(settings)]; - spiNorFlash.Read(settingsBaseAddr + (settingsFlashBlock * 0x1000) + HEADER_SIZE, bufferSettings, sizeof(settings)); - std::memcpy(&settings, bufferSettings, sizeof(settings)); -} - -void Settings::EraseBlock() { - - spiNorFlash.SectorErase(settingsBaseAddr + (settingsFlashBlock * 0x1000)); -} - -void Settings::SetHeader(bool state) { - SettingsHeader settingsHeader; - uint8_t bufferHead[sizeof(settingsHeader)]; - settingsHeader.isActive = state ? 0xF1 : 0xF0; - settingsHeader.version = settingsVersion; - - std::memcpy(bufferHead, &settingsHeader, sizeof(settingsHeader)); - spiNorFlash.Write(settingsBaseAddr + (settingsFlashBlock * 0x1000), bufferHead, sizeof(settingsHeader)); -} - -void Settings::SaveSettingsData() { - uint8_t bufferSettings[sizeof(settings)]; - std::memcpy(bufferSettings, &settings, sizeof(settings)); - spiNorFlash.Write(settingsBaseAddr + (settingsFlashBlock * 0x1000) + HEADER_SIZE, bufferSettings, sizeof(settings)); -} - -void Settings::LoadSettingsFromFlash() { - - if (settingsFlashBlock == 99) { - // Find current Block, if can't find use default settings and set block to 0 ans save ! - if (FindHeader()) { - ReadSettingsData(); - } else { - SaveSettingsToFlash(); - } - } else { - // Read Settings from flash... - // never used :) - ReadSettingsData(); + fs.FileRead(&settingsFile, reinterpret_cast(&bufferSettings), sizeof(settings)); + fs.FileClose(&settingsFile); + if ( bufferSettings.version == settingsVersion ) { + settings = bufferSettings; } } -void Settings::SaveSettingsToFlash() { - - // calculate where to save... - // mark current to inactive - // erase the new location and save - // set settingsFlashBlock - - // if first time hever, only saves to block 0 and set settingsFlashBlock +void Settings::SaveSettingsToFile() { + lfs_file_t settingsFile; - if (settingsFlashBlock != 99) { - SetHeader(false); + if ( fs.FileOpen(&settingsFile, "/settings.dat", LFS_O_RDWR | LFS_O_CREAT) != LFS_ERR_OK) { + return; } - - settingsFlashBlock++; - if (settingsFlashBlock > 9) - settingsFlashBlock = 0; - - EraseBlock(); - SetHeader(true); - SaveSettingsData(); + fs.FileWrite(&settingsFile, reinterpret_cast(&settings), sizeof(settings)); + fs.FileClose(&settingsFile); } diff --git a/src/components/settings/Settings.h b/src/components/settings/Settings.h index 4409425b..577455eb 100644 --- a/src/components/settings/Settings.h +++ b/src/components/settings/Settings.h @@ -2,25 +2,26 @@ #include #include "components/datetime/DateTimeController.h" #include "components/brightness/BrightnessController.h" -#include "drivers/SpiNorFlash.h" +#include "components/fs/FS.h" #include "drivers/Cst816s.h" namespace Pinetime { namespace Controllers { class Settings { public: - enum class ClockType { H24, H12 }; - enum class Vibration { ON, OFF }; - enum class WakeUpMode { None, SingleTap, DoubleTap, RaiseWrist }; + enum class ClockType : uint8_t { H24, H12 }; + enum class Vibration : uint8_t { ON, OFF }; + enum class WakeUpMode : uint8_t { None, SingleTap, DoubleTap, RaiseWrist }; - Settings(Pinetime::Drivers::SpiNorFlash& spiNorFlash); + Settings(Pinetime::Controllers::FS& fs); void Init(); void SaveSettings(); void SetClockFace(uint8_t face) { - if (face != settings.clockFace) + if (face != settings.clockFace) { settingsChanged = true; + } settings.clockFace = face; }; uint8_t GetClockFace() const { @@ -42,8 +43,9 @@ namespace Pinetime { }; void SetClockType(ClockType clocktype) { - if (clocktype != settings.clockType) + if (clocktype != settings.clockType) { settingsChanged = true; + } settings.clockType = clocktype; }; ClockType GetClockType() const { @@ -51,8 +53,9 @@ namespace Pinetime { }; void SetVibrationStatus(Vibration status) { - if (status != settings.vibrationStatus) + if (status != settings.vibrationStatus) { settingsChanged = true; + } settings.vibrationStatus = status; }; Vibration GetVibrationStatus() const { @@ -60,8 +63,9 @@ namespace Pinetime { }; void SetScreenTimeOut(uint32_t timeout) { - if (timeout != settings.screenTimeOut) + if (timeout != settings.screenTimeOut) { settingsChanged = true; + } settings.screenTimeOut = timeout; }; uint32_t GetScreenTimeOut() const { @@ -69,8 +73,9 @@ namespace Pinetime { }; void setWakeUpMode(WakeUpMode wakeUp) { - if (wakeUp != settings.wakeUpMode) + if (wakeUp != settings.wakeUpMode) { settingsChanged = true; + } settings.wakeUpMode = wakeUp; }; WakeUpMode getWakeUpMode() const { @@ -78,8 +83,9 @@ namespace Pinetime { }; void SetBrightness(Controllers::BrightnessController::Levels level) { - if (level != settings.brightLevel) + if (level != settings.brightLevel) { settingsChanged = true; + } settings.brightLevel = level; }; Controllers::BrightnessController::Levels GetBrightness() const { @@ -87,25 +93,29 @@ namespace Pinetime { }; void SetStepsGoal( uint32_t goal ) { - if ( goal != settings.stepsGoal ) + if ( goal != settings.stepsGoal ) { settingsChanged = true; + } settings.stepsGoal = goal; }; uint32_t GetStepsGoal() const { return settings.stepsGoal; }; private: - Pinetime::Drivers::SpiNorFlash& spiNorFlash; + Pinetime::Controllers::FS& fs; + + static constexpr uint32_t settingsVersion = 0x0001; struct SettingsData { + uint32_t version = settingsVersion; + uint32_t stepsGoal = 10000; + uint32_t screenTimeOut = 15000; + ClockType clockType = ClockType::H24; Vibration vibrationStatus = Vibration::ON; uint8_t clockFace = 0; - uint32_t stepsGoal = 10000; - uint32_t screenTimeOut = 15000; - WakeUpMode wakeUpMode = WakeUpMode::None; Controllers::BrightnessController::Levels brightLevel = Controllers::BrightnessController::Levels::Medium; @@ -117,20 +127,8 @@ namespace Pinetime { uint8_t appMenu = 0; uint8_t settingsMenu = 0; - // There are 10 blocks of reserved flash to save settings - // to minimize wear, the recording is done in a rotating way by the 10 blocks - uint8_t settingsFlashBlock = 99; // default to indicate it needs to find the active block - - static constexpr uint32_t settingsBaseAddr = 0x3F6000; // Flash Settings Location - static constexpr uint16_t settingsVersion = 0x0100; // Flash Settings Version - - bool FindHeader(); - void ReadSettingsData(); - void EraseBlock(); - void SetHeader(bool state); - void SaveSettingsData(); - void LoadSettingsFromFlash(); - void SaveSettingsToFlash(); + void LoadSettingsFromFile(); + void SaveSettingsToFile(); }; } } \ No newline at end of file diff --git a/src/libs/littlefs b/src/libs/littlefs new file mode 160000 index 00000000..ead50807 --- /dev/null +++ b/src/libs/littlefs @@ -0,0 +1 @@ +Subproject commit ead50807f1ca3fdf2da00b77a0ce02651ded2d13 diff --git a/src/libs/lv_conf.h b/src/libs/lv_conf.h index a03a4833..e85eeb1f 100644 --- a/src/libs/lv_conf.h +++ b/src/libs/lv_conf.h @@ -204,7 +204,7 @@ e.g. "stm32f769xx.h" or "stm32f429xx.h" */ /* 1: Enable file system (might be required for images */ // TODO: Enable FS -#define LV_USE_FILESYSTEM 0 +#define LV_USE_FILESYSTEM 1 #if LV_USE_FILESYSTEM /*Declare the type of the user data of file system drivers (can be e.g. `void *`, `int`, `struct`)*/ typedef void * lv_fs_drv_user_data_t; @@ -236,7 +236,7 @@ typedef void * lv_fs_drv_user_data_t; * With complex image decoders (e.g. PNG or JPG) caching can save the continuous open/decode of images. * However the opened images might consume additional RAM. * LV_IMG_CACHE_DEF_SIZE must be >= 1 */ -#define LV_IMG_CACHE_DEF_SIZE 1 +#define LV_IMG_CACHE_DEF_SIZE 6 /*Declare the type of the user data of image decoder (can be e.g. `void *`, `int`, `struct`)*/ typedef void* lv_img_decoder_user_data_t; diff --git a/src/main.cpp b/src/main.cpp index 5832a78f..1b7015aa 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -35,6 +35,7 @@ #include "components/motor/MotorController.h" #include "components/datetime/DateTimeController.h" #include "components/heartrate/HeartRateController.h" +#include "components/fs/FS.h" #include "drivers/Spi.h" #include "drivers/SpiMaster.h" #include "drivers/SpiNorFlash.h" @@ -108,10 +109,6 @@ void ble_manager_set_ble_disconnection_callback(void (*disconnection)()); static constexpr uint8_t pinTouchIrq = 28; static constexpr uint8_t pinPowerPresentIrq = 19; -Pinetime::Controllers::Settings settingsController {spiNorFlash}; - -Pinetime::Controllers::MotorController motorController {settingsController}; - Pinetime::Controllers::HeartRateController heartRateController; Pinetime::Applications::HeartRateTask heartRateApp(heartRateSensor, heartRateController); @@ -122,6 +119,11 @@ Pinetime::Controllers::NotificationManager notificationManager; Pinetime::Controllers::MotionController motionController; Pinetime::Controllers::TimerController timerController; +Pinetime::Controllers::FS fs {spiNorFlash}; +Pinetime::Controllers::Settings settingsController {fs}; +Pinetime::Controllers::MotorController motorController {settingsController}; + + Pinetime::Applications::DisplayApp displayApp(lcd, lvgl, touchPanel, @@ -155,7 +157,8 @@ Pinetime::System::SystemTask systemTask(spi, settingsController, heartRateController, displayApp, - heartRateApp); + heartRateApp, + fs); void nrfx_gpiote_evt_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action) { if (pin == pinTouchIrq) { diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index 13755f71..17e78230 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -59,7 +59,8 @@ SystemTask::SystemTask(Drivers::SpiMaster& spi, Controllers::Settings& settingsController, Pinetime::Controllers::HeartRateController& heartRateController, Pinetime::Applications::DisplayApp& displayApp, - Pinetime::Applications::HeartRateTask& heartRateApp) + Pinetime::Applications::HeartRateTask& heartRateApp, + Pinetime::Controllers::FS& fs) : spi {spi}, lcd {lcd}, spiNorFlash {spiNorFlash}, @@ -77,10 +78,11 @@ SystemTask::SystemTask(Drivers::SpiMaster& spi, motionSensor {motionSensor}, settingsController {settingsController}, heartRateController{heartRateController}, - nimbleController(*this, bleController, dateTimeController, notificationManager, batteryController, spiNorFlash, heartRateController), motionController{motionController}, displayApp{displayApp}, - heartRateApp(heartRateApp) { + heartRateApp(heartRateApp), + fs{fs}, + nimbleController(*this, bleController, dateTimeController, notificationManager, batteryController, spiNorFlash, heartRateController) { } @@ -107,6 +109,9 @@ void SystemTask::Work() { spi.Init(); spiNorFlash.Init(); spiNorFlash.Wakeup(); + + fs.Init(); + nimbleController.Init(); nimbleController.StartAdvertising(); brightnessController.Init(); diff --git a/src/systemtask/SystemTask.h b/src/systemtask/SystemTask.h index f563640c..bfb97264 100644 --- a/src/systemtask/SystemTask.h +++ b/src/systemtask/SystemTask.h @@ -16,13 +16,15 @@ #include "components/ble/NotificationManager.h" #include "components/motor/MotorController.h" #include "components/timer/TimerController.h" +#include "components/fs/FS.h" + #ifdef PINETIME_IS_RECOVERY #include "displayapp/DisplayAppRecovery.h" #include "displayapp/DummyLittleVgl.h" #else #include "components/settings/Settings.h" #include "displayapp/DisplayApp.h" - #include "displayapp/LittleVgl.h" + #include "displayapp/LittleVgl.h" #endif #include "drivers/Watchdog.h" @@ -59,7 +61,8 @@ namespace Pinetime { Controllers::Settings& settingsController, Pinetime::Controllers::HeartRateController& heartRateController, Pinetime::Applications::DisplayApp& displayApp, - Pinetime::Applications::HeartRateTask& heartRateApp); + Pinetime::Applications::HeartRateTask& heartRateApp, + Pinetime::Controllers::FS& fs); void Start(); void PushMessage(Messages msg); @@ -103,13 +106,14 @@ namespace Pinetime { Pinetime::Drivers::Bma421& motionSensor; Pinetime::Controllers::Settings& settingsController; Pinetime::Controllers::HeartRateController& heartRateController; - Pinetime::Controllers::NimbleController nimbleController; + Controllers::BrightnessController brightnessController; Pinetime::Controllers::MotionController& motionController; Pinetime::Applications::DisplayApp& displayApp; Pinetime::Applications::HeartRateTask& heartRateApp; - + Pinetime::Controllers::FS& fs; + Pinetime::Controllers::NimbleController nimbleController; static constexpr uint8_t pinSpiSck = 2; static constexpr uint8_t pinSpiMosi = 3; -- cgit v1.2.3 From e21f6a7f414d1f832e8fddfeaab3b9de05aa3459 Mon Sep 17 00:00:00 2001 From: Jean-François Milants Date: Sun, 11 Jul 2021 16:55:06 +0200 Subject: Notify battery level every 10 minutes when connected to a BLE host. Refactor battery percent : only use uint8_t to store the battery % remaining. --- src/components/battery/BatteryController.cpp | 13 ++--- src/components/battery/BatteryController.h | 18 ++++--- src/components/ble/BatteryInformationService.cpp | 8 ++- src/components/ble/BatteryInformationService.h | 2 +- src/components/ble/NimbleController.cpp | 6 +++ src/components/ble/NimbleController.h | 3 +- src/displayapp/screens/BatteryIcon.cpp | 3 +- src/displayapp/screens/BatteryIcon.h | 2 +- src/displayapp/screens/BatteryInfo.cpp | 66 ++++++++++-------------- src/displayapp/screens/BatteryInfo.h | 2 +- src/displayapp/screens/PineTimeStyle.h | 2 +- src/displayapp/screens/SystemInfo.cpp | 2 +- src/displayapp/screens/WatchFaceAnalog.cpp | 1 - src/displayapp/screens/WatchFaceAnalog.h | 2 +- src/displayapp/screens/WatchFaceDigital.h | 2 +- src/systemtask/SystemTask.cpp | 5 ++ src/systemtask/SystemTask.h | 2 + 17 files changed, 71 insertions(+), 68 deletions(-) (limited to 'src/systemtask/SystemTask.h') diff --git a/src/components/battery/BatteryController.cpp b/src/components/battery/BatteryController.cpp index 76ad8cb3..fa476ea3 100644 --- a/src/components/battery/BatteryController.cpp +++ b/src/components/battery/BatteryController.cpp @@ -1,9 +1,7 @@ #include "BatteryController.h" #include #include -#include #include -#include using namespace Pinetime::Controllers; @@ -18,7 +16,6 @@ void Battery::Init() { } void Battery::Update() { - isCharging = !nrf_gpio_pin_read(chargingPin); isPowerPresent = !nrf_gpio_pin_read(powerPresentPin); @@ -33,13 +30,13 @@ void Battery::Update() { nrfx_saadc_sample(); } -void Battery::adcCallbackStatic(nrfx_saadc_evt_t const* event) { +void Battery::AdcCallbackStatic(nrfx_saadc_evt_t const* event) { instance->SaadcEventHandler(event); } void Battery::SaadcInit() { nrfx_saadc_config_t adcConfig = NRFX_SAADC_DEFAULT_CONFIG; - APP_ERROR_CHECK(nrfx_saadc_init(&adcConfig, adcCallbackStatic)); + APP_ERROR_CHECK(nrfx_saadc_init(&adcConfig, AdcCallbackStatic)); nrf_saadc_channel_config_t adcChannelConfig = {.resistor_p = NRF_SAADC_RESISTOR_DISABLED, .resistor_n = NRF_SAADC_RESISTOR_DISABLED, @@ -55,7 +52,6 @@ void Battery::SaadcInit() { } void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) { - const uint16_t battery_max = 4180; // maximum voltage of battery ( max charging voltage is 4.21 ) const uint16_t battery_min = 3200; // minimum voltage of battery before shutdown ( depends on the battery ) @@ -69,13 +65,10 @@ void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) { // reference_voltage is 0.6V // p_event->data.done.p_buffer[0] = (adc_voltage / reference_voltage) * 1024 voltage = p_event->data.done.p_buffer[0] * 6000 / 1024; - percentRemaining = (voltage - battery_min) * 100 / (battery_max - battery_min); - percentRemaining = std::max(percentRemaining, 0); percentRemaining = std::min(percentRemaining, 100); - - percentRemainingBuffer.insert(percentRemaining); + percentRemainingBuffer.Insert(percentRemaining); samples++; if (samples > percentRemainingSamples) { diff --git a/src/components/battery/BatteryController.h b/src/components/battery/BatteryController.h index 26e24938..1333ad0e 100644 --- a/src/components/battery/BatteryController.h +++ b/src/components/battery/BatteryController.h @@ -19,7 +19,7 @@ namespace Pinetime { insert member function overwrites the next data to the current HEAD and moves the HEAD to the newly inserted value. */ - void insert(const int num) { + void Insert(const uint8_t num) { head %= cap; arr[head++] = num; if (sz != cap) { @@ -27,13 +27,13 @@ namespace Pinetime { } } - int GetAverage() const { + uint8_t GetAverage() const { int sum = std::accumulate(arr.begin(), arr.end(), 0); - return (sum / sz); + return static_cast(sum / sz); } private: - std::array arr; /**< internal array used to store the values*/ + std::array arr; /**< internal array used to store the values*/ uint8_t sz; /**< The current size of the array.*/ uint8_t cap; /**< Total capacity of the CircBuffer.*/ uint8_t head; /**< The current head of the CircBuffer*/ @@ -46,8 +46,11 @@ namespace Pinetime { void Init(); void Update(); - int PercentRemaining() const { - return percentRemainingBuffer.GetAverage(); + uint8_t PercentRemaining() const { + auto avg = percentRemainingBuffer.GetAverage(); + avg = std::min(avg, static_cast(100)); + avg = std::max(avg, static_cast(0)); + return avg; } uint16_t Voltage() const { @@ -57,6 +60,7 @@ namespace Pinetime { bool IsCharging() const { return isCharging; } + bool IsPowerPresent() const { return isPowerPresent; } @@ -80,7 +84,7 @@ namespace Pinetime { void SaadcInit(); void SaadcEventHandler(nrfx_saadc_evt_t const* p_event); - static void adcCallbackStatic(nrfx_saadc_evt_t const* event); + static void AdcCallbackStatic(nrfx_saadc_evt_t const* event); bool isReading = false; uint8_t samples = 0; diff --git a/src/components/ble/BatteryInformationService.cpp b/src/components/ble/BatteryInformationService.cpp index 10a78d67..7f176904 100644 --- a/src/components/ble/BatteryInformationService.cpp +++ b/src/components/ble/BatteryInformationService.cpp @@ -17,7 +17,7 @@ BatteryInformationService::BatteryInformationService(Controllers::Battery& batte characteristicDefinition {{.uuid = (ble_uuid_t*) &batteryLevelUuid, .access_cb = BatteryInformationServiceCallback, .arg = this, - .flags = BLE_GATT_CHR_F_READ, + .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY, .val_handle = &batteryLevelHandle}, {0}}, serviceDefinition { @@ -48,4 +48,8 @@ int BatteryInformationService::OnBatteryServiceRequested(uint16_t connectionHand return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; } return 0; -} \ No newline at end of file +} +void BatteryInformationService::NotifyBatteryLevel(uint16_t connectionHandle, uint8_t level) { + auto* om = ble_hs_mbuf_from_flat(&level, 1); + ble_gattc_notify_custom(connectionHandle, batteryLevelHandle, om); +} diff --git a/src/components/ble/BatteryInformationService.h b/src/components/ble/BatteryInformationService.h index 7d060909..1303fd6c 100644 --- a/src/components/ble/BatteryInformationService.h +++ b/src/components/ble/BatteryInformationService.h @@ -17,7 +17,7 @@ namespace Pinetime { void Init(); int OnBatteryServiceRequested(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt* context); - + void NotifyBatteryLevel(uint16_t connectionHandle, uint8_t level); private: Controllers::Battery& batteryController; static constexpr uint16_t batteryInformationServiceId {0x180F}; diff --git a/src/components/ble/NimbleController.cpp b/src/components/ble/NimbleController.cpp index 2c1d0f99..5eb227bf 100644 --- a/src/components/ble/NimbleController.cpp +++ b/src/components/ble/NimbleController.cpp @@ -235,3 +235,9 @@ void NimbleController::StartDiscovery() { uint16_t NimbleController::connHandle() { return connectionHandle; } + +void NimbleController::NotifyBatteryLevel(uint8_t level) { + if(connectionHandle != BLE_HS_CONN_HANDLE_NONE) { + batteryInformationService.NotifyBatteryLevel(connectionHandle, level); + } +} diff --git a/src/components/ble/NimbleController.h b/src/components/ble/NimbleController.h index 5dd01e42..0cfe983c 100644 --- a/src/components/ble/NimbleController.h +++ b/src/components/ble/NimbleController.h @@ -70,6 +70,7 @@ namespace Pinetime { }; uint16_t connHandle(); + void NotifyBatteryLevel(uint8_t level); private: static constexpr const char* deviceName = "InfiniTime"; @@ -92,7 +93,7 @@ namespace Pinetime { HeartRateService heartRateService; uint8_t addrType; // 1 = Random, 0 = PUBLIC - uint16_t connectionHandle = 0; + uint16_t connectionHandle = BLE_HS_CONN_HANDLE_NONE; ble_uuid128_t dfuServiceUuid { .u {.type = BLE_UUID_TYPE_128}, diff --git a/src/displayapp/screens/BatteryIcon.cpp b/src/displayapp/screens/BatteryIcon.cpp index 6b54a305..bb6626a5 100644 --- a/src/displayapp/screens/BatteryIcon.cpp +++ b/src/displayapp/screens/BatteryIcon.cpp @@ -1,9 +1,10 @@ +#include #include "BatteryIcon.h" #include "Symbols.h" using namespace Pinetime::Applications::Screens; -const char* BatteryIcon::GetBatteryIcon(int batteryPercent) { +const char* BatteryIcon::GetBatteryIcon(uint8_t batteryPercent) { if (batteryPercent > 90) return Symbols::batteryFull; if (batteryPercent > 75) diff --git a/src/displayapp/screens/BatteryIcon.h b/src/displayapp/screens/BatteryIcon.h index 9c192ff7..b370b331 100644 --- a/src/displayapp/screens/BatteryIcon.h +++ b/src/displayapp/screens/BatteryIcon.h @@ -6,7 +6,7 @@ namespace Pinetime { class BatteryIcon { public: static const char* GetUnknownIcon(); - static const char* GetBatteryIcon(int batteryPercent); + static const char* GetBatteryIcon(uint8_t batteryPercent); static const char* GetPlugIcon(bool isCharging); }; } diff --git a/src/displayapp/screens/BatteryInfo.cpp b/src/displayapp/screens/BatteryInfo.cpp index 87c8b4bb..2af024e7 100644 --- a/src/displayapp/screens/BatteryInfo.cpp +++ b/src/displayapp/screens/BatteryInfo.cpp @@ -38,11 +38,7 @@ BatteryInfo::BatteryInfo(Pinetime::Applications::DisplayApp* app, Pinetime::Cont percent = lv_label_create(lv_scr_act(), nullptr); lv_obj_set_style_local_text_font(percent, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_76); - if (batteryPercent >= 0) { - lv_label_set_text_fmt(percent, "%02i%%", batteryPercent); - } else { - lv_label_set_text(percent, "--%"); - } + lv_label_set_text_fmt(percent, "%02i%%", batteryPercent); lv_label_set_align(percent, LV_LABEL_ALIGN_LEFT); lv_obj_align(percent, nullptr, LV_ALIGN_CENTER, 0, -60); @@ -72,24 +68,22 @@ BatteryInfo::~BatteryInfo() { void BatteryInfo::UpdateAnim() { batteryPercent = batteryController.PercentRemaining(); - if (batteryPercent >= 0) { - if (batteryController.IsCharging() and batteryPercent < 100) { - animation += 1; - if (animation >= 100) { - animation = 0; - } - - } else { - if (animation > batteryPercent) { - animation--; - } - if (animation < batteryPercent) { - animation++; - } + if (batteryController.IsCharging() and batteryPercent < 100) { + animation += 1; + if (animation >= 100) { + animation = 0; } - lv_bar_set_value(charging_bar, animation, LV_ANIM_OFF); + } else { + if (animation > batteryPercent) { + animation--; + } + if (animation < batteryPercent) { + animation++; + } } + + lv_bar_set_value(charging_bar, animation, LV_ANIM_OFF); } void BatteryInfo::UpdateScreen() { @@ -99,28 +93,22 @@ void BatteryInfo::UpdateScreen() { batteryPercent = batteryController.PercentRemaining(); batteryVoltage = batteryController.Voltage(); - if (batteryPercent >= 0) { - if (batteryController.IsCharging() and batteryPercent < 100) { - lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC, LV_STATE_DEFAULT, LV_COLOR_RED); - lv_label_set_text_static(status, "Battery charging"); - } else if (batteryPercent == 100) { - lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC, LV_STATE_DEFAULT, LV_COLOR_BLUE); - lv_label_set_text_static(status, "Battery is fully charged"); - } else if (batteryPercent < 10) { - lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC, LV_STATE_DEFAULT, LV_COLOR_YELLOW); - lv_label_set_text_static(status, "Battery is low"); - } else { - lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC, LV_STATE_DEFAULT, LV_COLOR_GREEN); - lv_label_set_text_static(status, "Battery discharging"); - } - - lv_label_set_text_fmt(percent, "%02i%%", batteryPercent); - + if (batteryController.IsCharging() and batteryPercent < 100) { + lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC, LV_STATE_DEFAULT, LV_COLOR_RED); + lv_label_set_text_static(status, "Battery charging"); + } else if (batteryPercent == 100) { + lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC, LV_STATE_DEFAULT, LV_COLOR_BLUE); + lv_label_set_text_static(status, "Battery is fully charged"); + } else if (batteryPercent < 10) { + lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC, LV_STATE_DEFAULT, LV_COLOR_YELLOW); + lv_label_set_text_static(status, "Battery is low"); } else { - lv_label_set_text_static(status, "Reading Battery status"); - lv_label_set_text(percent, "--%"); + lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC, LV_STATE_DEFAULT, LV_COLOR_GREEN); + lv_label_set_text_static(status, "Battery discharging"); } + lv_label_set_text_fmt(percent, "%02i%%", batteryPercent); + lv_obj_align(status, charging_bar, LV_ALIGN_OUT_BOTTOM_MID, 0, 20); lv_label_set_text_fmt(voltage, "%1i.%02i volts", batteryVoltage / 1000, batteryVoltage % 1000 / 10); } diff --git a/src/displayapp/screens/BatteryInfo.h b/src/displayapp/screens/BatteryInfo.h index 346dc571..da85c6dd 100644 --- a/src/displayapp/screens/BatteryInfo.h +++ b/src/displayapp/screens/BatteryInfo.h @@ -36,7 +36,7 @@ namespace Pinetime { lv_task_t* taskAnim; int8_t animation = 0; - int8_t batteryPercent = -1; + uint8_t batteryPercent = 0; uint16_t batteryVoltage = 0; }; } diff --git a/src/displayapp/screens/PineTimeStyle.h b/src/displayapp/screens/PineTimeStyle.h index 70794cc5..3b4ded1e 100644 --- a/src/displayapp/screens/PineTimeStyle.h +++ b/src/displayapp/screens/PineTimeStyle.h @@ -42,7 +42,7 @@ namespace Pinetime { Pinetime::Controllers::DateTime::Days currentDayOfWeek = Pinetime::Controllers::DateTime::Days::Unknown; uint8_t currentDay = 0; - DirtyValue batteryPercentRemaining {}; + DirtyValue batteryPercentRemaining {}; DirtyValue bleState {}; DirtyValue> currentDateTime {}; DirtyValue motionSensorOk {}; diff --git a/src/displayapp/screens/SystemInfo.cpp b/src/displayapp/screens/SystemInfo.cpp index 5ae3a595..f5bf0cc9 100644 --- a/src/displayapp/screens/SystemInfo.cpp +++ b/src/displayapp/screens/SystemInfo.cpp @@ -103,7 +103,7 @@ std::unique_ptr SystemInfo::CreateScreen1() { } std::unique_ptr SystemInfo::CreateScreen2() { - auto batteryPercent = static_cast(batteryController.PercentRemaining()); + auto batteryPercent = batteryController.PercentRemaining(); auto resetReason = [this]() { switch (watchdog.ResetReason()) { case Drivers::Watchdog::ResetReasons::Watchdog: diff --git a/src/displayapp/screens/WatchFaceAnalog.cpp b/src/displayapp/screens/WatchFaceAnalog.cpp index 0051408c..b0eb65bc 100644 --- a/src/displayapp/screens/WatchFaceAnalog.cpp +++ b/src/displayapp/screens/WatchFaceAnalog.cpp @@ -165,7 +165,6 @@ void WatchFaceAnalog::UpdateClock() { } bool WatchFaceAnalog::Refresh() { - batteryPercentRemaining = batteryController.PercentRemaining(); if (batteryPercentRemaining.IsUpdated()) { auto batteryPercent = batteryPercentRemaining.Get(); diff --git a/src/displayapp/screens/WatchFaceAnalog.h b/src/displayapp/screens/WatchFaceAnalog.h index 96225558..ac7f0ac5 100644 --- a/src/displayapp/screens/WatchFaceAnalog.h +++ b/src/displayapp/screens/WatchFaceAnalog.h @@ -48,7 +48,7 @@ namespace Pinetime { Pinetime::Controllers::DateTime::Days currentDayOfWeek = Pinetime::Controllers::DateTime::Days::Unknown; uint8_t currentDay = 0; - DirtyValue batteryPercentRemaining {0}; + DirtyValue batteryPercentRemaining {0}; DirtyValue> currentDateTime; DirtyValue notificationState {false}; diff --git a/src/displayapp/screens/WatchFaceDigital.h b/src/displayapp/screens/WatchFaceDigital.h index 246efc95..76c8d3dc 100644 --- a/src/displayapp/screens/WatchFaceDigital.h +++ b/src/displayapp/screens/WatchFaceDigital.h @@ -45,7 +45,7 @@ namespace Pinetime { Pinetime::Controllers::DateTime::Days currentDayOfWeek = Pinetime::Controllers::DateTime::Days::Unknown; uint8_t currentDay = 0; - DirtyValue batteryPercentRemaining {}; + DirtyValue batteryPercentRemaining {}; DirtyValue bleState {}; DirtyValue> currentDateTime {}; DirtyValue motionSensorOk {}; diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index 17e78230..eb29638a 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -330,6 +330,11 @@ void SystemTask::Work() { } } + if (xTaskGetTickCount() - batteryNotificationTick > batteryNotificationPeriod) { + nimbleController.NotifyBatteryLevel(batteryController.PercentRemaining()); + batteryNotificationTick = xTaskGetTickCount(); + } + monitor.Process(); uint32_t systick_counter = nrf_rtc_counter_get(portNRF_RTC_REG); dateTimeController.UpdateTime(systick_counter); diff --git a/src/systemtask/SystemTask.h b/src/systemtask/SystemTask.h index bfb97264..f8cf6370 100644 --- a/src/systemtask/SystemTask.h +++ b/src/systemtask/SystemTask.h @@ -135,6 +135,8 @@ namespace Pinetime { void GoToRunning(); void UpdateMotion(); bool stepCounterMustBeReset = false; + static constexpr TickType_t batteryNotificationPeriod = 1000 * 60 * 10; // 1 tick ~= 1ms. 1ms * 60 * 10 = 10 minutes + TickType_t batteryNotificationTick = 0; #if configUSE_TRACE_FACILITY == 1 SystemMonitor monitor; -- cgit v1.2.3