From f07ffab4c1fa876e8da9a1bcc895ecf0dfa75acf Mon Sep 17 00:00:00 2001 From: JF Date: Sun, 23 Feb 2020 16:14:03 +0100 Subject: [PATCH] Re-enable BLE, BLE status on display and battery level on display. --- doc/SPI-LCD-driver.md | 46 ++++++++++++++++++++++++++++++ src/BLE/BleManager.h | 1 + src/CMakeLists.txt | 4 +-- src/DisplayApp/DisplayApp.cpp | 15 ++++------ src/DisplayApp/DisplayApp.h | 1 - src/DisplayApp/Screens/Clock.cpp | 24 +++++++++------- src/DisplayApp/Screens/Clock.h | 24 +++++++++------- src/DisplayApp/Screens/Message.cpp | 11 +++++-- src/DisplayApp/Screens/Message.h | 9 +++--- src/DisplayApp/Screens/Screen.h | 6 +--- src/DisplayApp/Screens/Tile.cpp | 38 +++++++++++++++--------- src/DisplayApp/Screens/Tile.h | 7 +++-- src/SystemTask/SystemTask.cpp | 6 ++-- src/main.cpp | 9 +----- 14 files changed, 129 insertions(+), 72 deletions(-) create mode 100644 doc/SPI-LCD-driver.md diff --git a/doc/SPI-LCD-driver.md b/doc/SPI-LCD-driver.md new file mode 100644 index 00000000..3c33c258 --- /dev/null +++ b/doc/SPI-LCD-driver.md @@ -0,0 +1,46 @@ +# The SPI LCD driver +## Introduction +The LCD controller that drive the display of the Pinetime is the Sitronix ST7789V. This controller is easy to integrate with an MCU thanks to its SPI interface, and has some interesting features like: +- an on-chip display data RAM that can store the whole framebuffer +- partial screen update +- hardware assisted vertical scrolling +- interrupt pin, allowing to drive the display with DMA and IRQ +- ... + +When you want to write a device driver for a specific component, its datasheet is your holy bible. This document contains a lot of information about the chip, its specification, characteristics, features and functionalities. +Luckily for us, the datasheet of the ST7789 is great! It contains everything we need to write a nice driver for our beloved Pinetime. + +In this document, I'll try to explain the process I've followed to write a device driver for the LCD. There were multiple iterations: +- First, I tried to find the correct initialization sequence so that the controller is configured correctly according to the hardware configuration; +- Then, I tried to display some pixels on the screen; +- Next, I wanted to display squares, colors and text; +- Following, there was a need to make that faster and faster again; +- And finally, I wanted to draw beautiful and useful UIs + +I'll describe all these steps in the following chapters. + +## The datasheet +As I said in the introduction, the datasheet will be your bedside book during your journey as a device driver designer. You'll read it from the beginning to the end once, twice, maybe ten times. Then, each time you'll want to do something new, you'll reopen the file and search for that specific paragraph or diagram than explains how the controller works so that you can figure out how to use it. + +The schematic of your board (the Pinetime schematics in this case) will also be very important, as you'll need to know how the LCD controller is physically connected to the MCU. + +How to read the datasheet? I recommand to read it from the beginning to the end (no joke) at least once. You certainly do not need to read everything in details, but it's good to know what information is available and where in the document. It'll be very useful during the developpment phase. +You'll want to read some part with more attention : +- Data color coding in 4-Line Serial Interface : how to send the pixel to be display to the controller +- Display Data Ram : how is the memory organised +- Power On/Off sequence +- System function commands : all the commands you can send to the controller. + +## One Pixel at a time + + +## Bulk transfert + +## DMA + +## IRQ + +## Bare metal integration +Integration customisée dans la lib GFX que j'ai écrite + +## Integration with LittleVGL \ No newline at end of file diff --git a/src/BLE/BleManager.h b/src/BLE/BleManager.h index 13b12a62..68fdff9a 100644 --- a/src/BLE/BleManager.h +++ b/src/BLE/BleManager.h @@ -1,4 +1,5 @@ #pragma once +#include #ifdef __cplusplus extern "C" { diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index efbb3861..3fb4dd6e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -158,7 +158,7 @@ list(APPEND SOURCE_FILES DisplayApp/DisplayApp.cpp DisplayApp/Screens/Screen.cpp DisplayApp/Screens/Clock.cpp -# DisplayApp/Screens/Message.cpp + DisplayApp/Screens/Message.cpp DisplayApp/Screens/Tile.cpp # DisplayApp/Screens/Tab.cpp main.cpp @@ -189,7 +189,7 @@ set(INCLUDE_FILES DisplayApp/DisplayApp.h DisplayApp/Screens/Screen.h DisplayApp/Screens/Clock.h -# DisplayApp/Screens/Message.h + DisplayApp/Screens/Message.h DisplayApp/Screens/Tile.h # DisplayApp/Screens/Tab.h drivers/St7789.h diff --git a/src/DisplayApp/DisplayApp.cpp b/src/DisplayApp/DisplayApp.cpp index d70726dd..1a794e04 100644 --- a/src/DisplayApp/DisplayApp.cpp +++ b/src/DisplayApp/DisplayApp.cpp @@ -4,17 +4,14 @@ #include #include #include -#include -#include "Components/Gfx/Gfx.h" #include #include #include -#include #include #include #include +#include #include "../SystemTask/SystemTask.h" -//#include using namespace Pinetime::Applications; @@ -31,7 +28,7 @@ DisplayApp::DisplayApp(Pinetime::Drivers::St7789& lcd, batteryController{batteryController}, bleController{bleController}, dateTimeController{dateTimeController}, - currentScreen{new Screens::Clock(this, dateTimeController) }, + currentScreen{new Screens::Clock(this, dateTimeController, batteryController, bleController) }, systemTask{systemTask} { msgQueue = xQueueCreate(queueSize, itemSize); } @@ -126,13 +123,13 @@ void DisplayApp::Refresh() { void DisplayApp::RunningState() { // clockScreen.SetCurrentDateTime(dateTimeController.CurrentDateTime()); - if(!currentScreen->Refresh(true)) { + if(!currentScreen->Refresh()) { currentScreen.reset(nullptr); switch(nextApp) { case Apps::None: case Apps::Launcher: currentScreen.reset(new Screens::Tile(this)); break; - case Apps::Clock: currentScreen.reset(new Screens::Clock(this, dateTimeController)); break; -// case Apps::Test: currentScreen.reset(new Screens::Message(this)); break; + case Apps::Clock: currentScreen.reset(new Screens::Clock(this, dateTimeController, batteryController, bleController)); break; + case Apps::Test: currentScreen.reset(new Screens::Message(this)); break; } nextApp = Apps::None; } @@ -158,7 +155,7 @@ void DisplayApp::OnTouchEvent() { // auto info = touchPanel.GetTouchInfo(); // // if(info.isTouch) { -// gfx.FillRectangle(info.x-10, info.y-10, 20,20, pointColor); +// lcd.DrawPixel(info.x, info.y, pointColor); // pointColor+=10; // } } diff --git a/src/DisplayApp/DisplayApp.h b/src/DisplayApp/DisplayApp.h index 656dd4ed..cb5e9f3b 100644 --- a/src/DisplayApp/DisplayApp.h +++ b/src/DisplayApp/DisplayApp.h @@ -14,7 +14,6 @@ #include "LittleVgl.h" #include #include -//#include namespace Pinetime { diff --git a/src/DisplayApp/Screens/Clock.cpp b/src/DisplayApp/Screens/Clock.cpp index 3b849150..f0bd8338 100644 --- a/src/DisplayApp/Screens/Clock.cpp +++ b/src/DisplayApp/Screens/Clock.cpp @@ -15,7 +15,11 @@ static void event_handler(lv_obj_t * obj, lv_event_t event) { screen->OnObjectEvent(obj, event); } -Clock::Clock(DisplayApp* app, Controllers::DateTime& dateTimeController) : Screen(app), currentDateTime{{}}, version {{}}, dateTimeController{dateTimeController} { +Clock::Clock(DisplayApp* app, + Controllers::DateTime& dateTimeController, + Controllers::Battery& batteryController, + Controllers::Ble& bleController) : Screen(app), currentDateTime{{}}, version {{}}, + dateTimeController{dateTimeController}, batteryController{batteryController}, bleController{bleController} { displayedChar[0] = 0; displayedChar[1] = 0; displayedChar[2] = 0; @@ -65,12 +69,9 @@ Clock::~Clock() { lv_obj_clean(lv_scr_act()); } -bool Clock::Refresh(bool fullRefresh) { - if(fullRefresh) { - auto currentDateTime = dateTimeController.CurrentDateTime(); - } - - if (fullRefresh || batteryPercentRemaining.IsUpdated()) { +bool Clock::Refresh() { + batteryPercentRemaining = batteryController.PercentRemaining(); + if (batteryPercentRemaining.IsUpdated()) { char batteryChar[11]; auto newBatteryValue = batteryPercentRemaining.Get(); newBatteryValue = (newBatteryValue > 100) ? 100 : newBatteryValue; @@ -80,8 +81,9 @@ bool Clock::Refresh(bool fullRefresh) { lv_label_set_text(label_battery, batteryChar); } - if (fullRefresh || bleState.IsUpdated()) { - if(bleState.Get() == BleConnectionStates::Connected) { + bleState = bleController.IsConnected(); + if (bleState.IsUpdated()) { + if(bleState.Get() == true) { lv_obj_set_hidden(label_ble, false); lv_label_set_text(label_ble, "BLE"); } else { @@ -91,7 +93,7 @@ bool Clock::Refresh(bool fullRefresh) { currentDateTime = dateTimeController.CurrentDateTime(); - if(fullRefresh || currentDateTime.IsUpdated()) { + if(currentDateTime.IsUpdated()) { auto newDateTime = currentDateTime.Get(); auto dp = date::floor(newDateTime); @@ -138,7 +140,7 @@ bool Clock::Refresh(bool fullRefresh) { } } - if(fullRefresh || version.IsUpdated()) { + if(version.IsUpdated()) { auto dummy = version.Get(); char versionStr[20]; sprintf(versionStr, "VERSION: %d.%d.%d", Version::Major(), Version::Minor(), Version::Patch()); diff --git a/src/DisplayApp/Screens/Clock.h b/src/DisplayApp/Screens/Clock.h index a358e41b..d6e44fda 100644 --- a/src/DisplayApp/Screens/Clock.h +++ b/src/DisplayApp/Screens/Clock.h @@ -7,6 +7,8 @@ #include #include #include +#include +#include #include "../Fonts/lcdfont14.h" #include "../Fonts/lcdfont70.h" #include "../../Version.h" @@ -24,8 +26,10 @@ namespace Pinetime { T& Get() { this->isUpdated = false; return value; } DirtyValue& operator=(const T& other) { - this->value = other; - this->isUpdated = true; + if (this->value != other) { + this->value = other; + this->isUpdated = true; + } return *this; } private: @@ -34,17 +38,15 @@ namespace Pinetime { }; class Clock : public Screen{ public: - enum class BleConnectionStates{ NotConnected, Connected}; - Clock(DisplayApp* app, Controllers::DateTime& dateTimeController); + Clock(DisplayApp* app, + Controllers::DateTime& dateTimeController, + Controllers::Battery& batteryController, + Controllers::Ble& bleController); ~Clock() override; - bool Refresh(bool fullRefresh) override; + bool Refresh() override; bool OnButtonPushed() override; - void SetBatteryPercentRemaining(uint8_t percent) { batteryPercentRemaining = percent; } - void SetBleConnectionState(BleConnectionStates state) { bleState = state; } - void SetCurrentDateTime(const std::chrono::time_point& tp) { currentDateTime = tp;} - void OnObjectEvent(lv_obj_t *pObj, lv_event_t i); private: static const char* MonthToString(Pinetime::Controllers::DateTime::Months month); @@ -60,7 +62,7 @@ namespace Pinetime { uint8_t currentDay = 0; DirtyValue batteryPercentRemaining {0}; - DirtyValue bleState {BleConnectionStates::NotConnected}; + DirtyValue bleState {false}; DirtyValue> currentDateTime; DirtyValue version; @@ -75,6 +77,8 @@ namespace Pinetime { lv_obj_t* backgroundLabel; Controllers::DateTime& dateTimeController; + Controllers::Battery& batteryController; + Controllers::Ble& bleController; bool running = true; diff --git a/src/DisplayApp/Screens/Message.cpp b/src/DisplayApp/Screens/Message.cpp index c9e0938f..c8a4ea1f 100644 --- a/src/DisplayApp/Screens/Message.cpp +++ b/src/DisplayApp/Screens/Message.cpp @@ -19,7 +19,7 @@ static void event_handler(lv_obj_t * obj, lv_event_t event) { screen->OnObjectEvent(obj, event); } -Message::Message(DisplayApp* app, Pinetime::Components::Gfx &gfx) : Screen(app, gfx) { +Message::Message(DisplayApp* app) : Screen(app) { backgroundLabel = lv_label_create(lv_scr_act(), NULL); backgroundLabel->user_data = this; @@ -55,11 +55,13 @@ Message::~Message() { lv_obj_clean(lv_scr_act()); } -void Message::Refresh(bool fullRefresh) { +bool Message::Refresh() { if(previousClickCount != clickCount) { lv_label_set_text_fmt(labelClick, "%d", clickCount); previousClickCount = clickCount; } + + return running; } void Message::OnObjectEvent(lv_obj_t *obj, lv_event_t event) { @@ -79,3 +81,8 @@ void Message::OnObjectEvent(lv_obj_t *obj, lv_event_t event) { NRF_LOG_INFO("Toggled"); } } + +bool Message::OnButtonPushed() { + running = false; + return true; +} diff --git a/src/DisplayApp/Screens/Message.h b/src/DisplayApp/Screens/Message.h index 2f1da942..4e87bba4 100644 --- a/src/DisplayApp/Screens/Message.h +++ b/src/DisplayApp/Screens/Message.h @@ -15,15 +15,13 @@ namespace Pinetime { namespace Screens { class Message : public Screen{ public: - explicit Message(DisplayApp* app, Components::Gfx& gfx); + explicit Message(DisplayApp* app); ~Message() override; - void Refresh(bool fullRefresh) override; + bool Refresh() override; + bool OnButtonPushed(); void OnObjectEvent(lv_obj_t* obj, lv_event_t event); - void OnButtonPushed() override { nextScreen = Screen::NextScreen::Menu; } private: - const FONT_INFO largeFont {lCD_70ptFontInfo.height, lCD_70ptFontInfo.startChar, lCD_70ptFontInfo.endChar, lCD_70ptFontInfo.spacePixels, lCD_70ptFontInfo.charInfo, lCD_70ptFontInfo.data}; - const FONT_INFO smallFont {lCD_14ptFontInfo.height, lCD_14ptFontInfo.startChar, lCD_14ptFontInfo.endChar, lCD_14ptFontInfo.spacePixels, lCD_14ptFontInfo.charInfo, lCD_14ptFontInfo.data}; lv_style_t* labelStyle; lv_obj_t * label; @@ -33,6 +31,7 @@ namespace Pinetime { uint32_t clickCount = 0 ; uint32_t previousClickCount = 0; + bool running = true; }; } } diff --git a/src/DisplayApp/Screens/Screen.h b/src/DisplayApp/Screens/Screen.h index 625daada..6cbd41ad 100644 --- a/src/DisplayApp/Screens/Screen.h +++ b/src/DisplayApp/Screens/Screen.h @@ -1,20 +1,16 @@ #pragma once -#include - namespace Pinetime { namespace Applications { class DisplayApp; namespace Screens { class Screen { public: - enum class NextScreen {None, Clock, Menu, App}; - Screen(DisplayApp* app) : app{app} {} virtual ~Screen() = default; // Return false if the app can be closed, true if it must continue to run - virtual bool Refresh(bool fullRefresh) = 0; + virtual bool Refresh() = 0; // Return false if the button hasn't been handled by the app, true if it has been handled virtual bool OnButtonPushed() { return false; } diff --git a/src/DisplayApp/Screens/Tile.cpp b/src/DisplayApp/Screens/Tile.cpp index c9e33544..6ee677dc 100644 --- a/src/DisplayApp/Screens/Tile.cpp +++ b/src/DisplayApp/Screens/Tile.cpp @@ -1,14 +1,8 @@ -#include -#include -#include -#include #include #include #include -#include #include "Tile.h" #include -#include using namespace Pinetime::Applications::Screens; @@ -17,7 +11,9 @@ extern lv_font_t jetbrains_mono_bold_20; static void event_handler(lv_obj_t * obj, lv_event_t event) { Tile* screen = static_cast(obj->user_data); - screen->OnObjectEvent(obj, event); + uint32_t* eventDataPtr = (uint32_t*) lv_event_get_data(); + uint32_t eventData = *eventDataPtr; + screen->OnObjectEvent(obj, event, eventData); } static const char * btnm_map1[] = {"App1", "App2", "App3", "\n", "App4", "App5", "App11", ""}; @@ -104,19 +100,28 @@ Tile::~Tile() { lv_obj_clean(lv_scr_act()); } -bool Tile::Refresh(bool fullRefresh) { +bool Tile::Refresh() { return running; } -void Tile::OnObjectEvent(lv_obj_t *obj, lv_event_t event) { +void Tile::OnObjectEvent(lv_obj_t *obj, lv_event_t event, uint32_t buttonId) { auto* tile = static_cast(obj->user_data); - if(event == LV_EVENT_CLICKED) { + if(event == LV_EVENT_VALUE_CHANGED) { + switch(buttonId) { + case 0: + case 1: + case 2: + tile->StartClockApp(); + break; + case 3: + case 4: + case 5: + tile->StartTestApp(); - tile->StartApp(); + break; + } clickCount++; } - else if(event == LV_EVENT_VALUE_CHANGED) { - } } bool Tile::OnButtonPushed() { @@ -125,7 +130,12 @@ bool Tile::OnButtonPushed() { return true; } -void Tile::StartApp() { +void Tile::StartClockApp() { app->StartApp(DisplayApp::Apps::Clock); running = false; } + +void Tile::StartTestApp() { + app->StartApp(DisplayApp::Apps::Test); + running = false; +} diff --git a/src/DisplayApp/Screens/Tile.h b/src/DisplayApp/Screens/Tile.h index 03cfb6d2..630fc666 100644 --- a/src/DisplayApp/Screens/Tile.h +++ b/src/DisplayApp/Screens/Tile.h @@ -18,10 +18,10 @@ namespace Pinetime { explicit Tile(DisplayApp* app); ~Tile() override; - bool Refresh(bool fullRefresh) override; + bool Refresh() override; bool OnButtonPushed() override; - void OnObjectEvent(lv_obj_t* obj, lv_event_t event); + void OnObjectEvent(lv_obj_t* obj, lv_event_t event, uint32_t buttonId); private: @@ -50,7 +50,8 @@ namespace Pinetime { uint32_t clickCount = 0 ; uint32_t previousClickCount = 0; - void StartApp(); + void StartClockApp(); + void StartTestApp(); bool running = true; }; } diff --git a/src/SystemTask/SystemTask.cpp b/src/SystemTask/SystemTask.cpp index 642e36a2..91822fa2 100644 --- a/src/SystemTask/SystemTask.cpp +++ b/src/SystemTask/SystemTask.cpp @@ -3,6 +3,8 @@ #include #include #include +#include +#include #include "SystemTask.h" #include "../main.h" using namespace Pinetime::System; @@ -16,7 +18,7 @@ SystemTask::SystemTask(Pinetime::Drivers::SpiMaster &spi, Pinetime::Drivers::St7 } void SystemTask::Start() { - if (pdPASS != xTaskCreate(SystemTask::Process, "MAIN", 256, this, 0, &taskHandle)) + if (pdPASS != xTaskCreate(SystemTask::Process, "MAIN", 350, this, 0, &taskHandle)) APP_ERROR_HANDLER(NRF_ERROR_NO_MEM); } @@ -29,7 +31,7 @@ void SystemTask::Process(void *instance) { void SystemTask::Work() { APP_GPIOTE_INIT(2); bool erase_bonds=false; -// nrf_sdh_freertos_init(ble_manager_start_advertising, &erase_bonds); + nrf_sdh_freertos_init(ble_manager_start_advertising, &erase_bonds); spi.Init(); lcd.Init(); diff --git a/src/main.cpp b/src/main.cpp index 08b15f62..6a271a49 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,25 +1,19 @@ #include #include -#include #include #include #include #include #include -#include #include #include -#include #include #include #include "BLE/BleManager.h" #include "Components/Battery/BatteryController.h" #include "Components/Ble/BleController.h" -#include "../drivers/Cst816s.h" #include #include - -#include #include #include @@ -134,12 +128,11 @@ int main(void) { systemTask.reset(new Pinetime::System::SystemTask(*spi, *lcd, *touchPanel, *lvgl, batteryController, bleController, dateTimeController)); systemTask->Start(); -/* ble_manager_init(); ble_manager_set_new_time_callback(OnNewTime); ble_manager_set_ble_connection_callback(OnBleConnection); ble_manager_set_ble_disconnection_callback(OnBleDisconnection); -*/ + vTaskStartScheduler(); for (;;) {