From 940cd3459f95073e60c61b37519486f569aac840 Mon Sep 17 00:00:00 2001 From: mark9064 <30447455+mark9064@users.noreply.github.com> Date: Mon, 1 Apr 2024 17:14:33 +0100 Subject: [PATCH] Use functional abstraction for hooks --- src/drivers/Spi.cpp | 2 +- src/drivers/Spi.h | 3 ++- src/drivers/SpiMaster.cpp | 2 +- src/drivers/SpiMaster.h | 5 +++-- src/drivers/St7789.cpp | 41 ++++++++++++++++++++++----------------- src/drivers/St7789.h | 11 +++++++---- src/main.cpp | 2 +- src/recoveryLoader.cpp | 2 +- 8 files changed, 39 insertions(+), 29 deletions(-) diff --git a/src/drivers/Spi.cpp b/src/drivers/Spi.cpp index e0b716fa..df018775 100644 --- a/src/drivers/Spi.cpp +++ b/src/drivers/Spi.cpp @@ -9,7 +9,7 @@ Spi::Spi(SpiMaster& spiMaster, uint8_t pinCsn) : spiMaster {spiMaster}, pinCsn { nrf_gpio_pin_set(pinCsn); } -bool Spi::Write(const uint8_t* data, size_t size, void (*TransactionHook)(bool)) { +bool Spi::Write(const uint8_t* data, size_t size, std::function TransactionHook) { return spiMaster.Write(pinCsn, data, size, TransactionHook); } diff --git a/src/drivers/Spi.h b/src/drivers/Spi.h index 55eef05c..4f48fafe 100644 --- a/src/drivers/Spi.h +++ b/src/drivers/Spi.h @@ -1,6 +1,7 @@ #pragma once #include #include +#include #include "drivers/SpiMaster.h" namespace Pinetime { @@ -14,7 +15,7 @@ namespace Pinetime { Spi& operator=(Spi&&) = delete; bool Init(); - bool Write(const uint8_t* data, size_t size, void (*TransactionHook)(bool)); + bool Write(const uint8_t* data, size_t size, std::function TransactionHook); bool Read(uint8_t* cmd, size_t cmdSize, uint8_t* data, size_t dataSize); bool WriteCmdAndBuffer(const uint8_t* cmd, size_t cmdSize, const uint8_t* data, size_t dataSize); void Sleep(); diff --git a/src/drivers/SpiMaster.cpp b/src/drivers/SpiMaster.cpp index f878c7d5..5bfbf7b4 100644 --- a/src/drivers/SpiMaster.cpp +++ b/src/drivers/SpiMaster.cpp @@ -170,7 +170,7 @@ void SpiMaster::PrepareRx(const uint32_t bufferAddress, const size_t size) { spiBaseAddress->EVENTS_END = 0; } -bool SpiMaster::Write(uint8_t pinCsn, const uint8_t* data, size_t size, void (*TransactionHook)(bool)) { +bool SpiMaster::Write(uint8_t pinCsn, const uint8_t* data, size_t size, std::function TransactionHook) { if (data == nullptr) return false; auto ok = xSemaphoreTake(mutex, portMAX_DELAY); diff --git a/src/drivers/SpiMaster.h b/src/drivers/SpiMaster.h index 131965e1..21980f4f 100644 --- a/src/drivers/SpiMaster.h +++ b/src/drivers/SpiMaster.h @@ -1,6 +1,7 @@ #pragma once #include #include +#include #include #include @@ -31,7 +32,7 @@ namespace Pinetime { SpiMaster& operator=(SpiMaster&&) = delete; bool Init(); - bool Write(uint8_t pinCsn, const uint8_t* data, size_t size, void (*TransactionHook)(bool)); + bool Write(uint8_t pinCsn, const uint8_t* data, size_t size, std::function TransactionHook); bool Read(uint8_t pinCsn, uint8_t* cmd, size_t cmdSize, uint8_t* data, size_t dataSize); bool WriteCmdAndBuffer(uint8_t pinCsn, const uint8_t* cmd, size_t cmdSize, const uint8_t* data, size_t dataSize); @@ -50,7 +51,7 @@ namespace Pinetime { NRF_SPIM_Type* spiBaseAddress; uint8_t pinCsn; - void (*TransactionHook)(bool); + std::function TransactionHook; SpiMaster::SpiModule spi; SpiMaster::Parameters params; diff --git a/src/drivers/St7789.cpp b/src/drivers/St7789.cpp index 78ad3a66..6e5d13b1 100644 --- a/src/drivers/St7789.cpp +++ b/src/drivers/St7789.cpp @@ -3,17 +3,16 @@ #include #include #include "drivers/Spi.h" -#include "drivers/PinMap.h" using namespace Pinetime::Drivers; -St7789::St7789(Spi& spi) : spi {spi} { +St7789::St7789(Spi& spi, uint8_t pinDataCommand, uint8_t pinReset) : spi {spi}, pinDataCommand {pinDataCommand}, pinReset {pinReset} { } void St7789::Init() { - nrf_gpio_cfg_output(PinMap::LcdDataCommand); - nrf_gpio_cfg_output(PinMap::LcdReset); - nrf_gpio_pin_set(PinMap::LcdReset); + nrf_gpio_cfg_output(pinDataCommand); + nrf_gpio_cfg_output(pinReset); + nrf_gpio_pin_set(pinReset); HardwareReset(); SoftwareReset(); SleepOut(); @@ -32,25 +31,29 @@ void St7789::Init() { void St7789::EnableDataMode(bool isStart) { if (isStart) { - nrf_gpio_pin_set(PinMap::LcdDataCommand); + nrf_gpio_pin_set(pinDataCommand); } } void St7789::EnableCommandMode(bool isStart) { if (isStart) { - nrf_gpio_pin_clear(PinMap::LcdDataCommand); + nrf_gpio_pin_clear(pinDataCommand); } } -void St7789::WriteCommand(uint8_t cmd) { - WriteSpi(&cmd, 1, EnableCommandMode); -} - void St7789::WriteData(uint8_t data) { - WriteSpi(&data, 1, EnableDataMode); + WriteSpi(&data, 1, [this](bool isStart) { + EnableDataMode(isStart); + }); } -void St7789::WriteSpi(const uint8_t* data, size_t size, void (*TransactionHook)(bool)) { +void St7789::WriteCommand(uint8_t cmd) { + WriteSpi(&cmd, 1, [this](bool isStart) { + EnableCommandMode(isStart); + }); +} + +void St7789::WriteSpi(const uint8_t* data, size_t size, std::function TransactionHook) { spi.Write(data, size, TransactionHook); } @@ -135,7 +138,9 @@ void St7789::SetAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) { void St7789::WriteToRam(const uint8_t* data, size_t size) { WriteCommand(static_cast(Commands::WriteToRam)); - WriteSpi(data, size, EnableDataMode); + WriteSpi(data, size, [this](bool isStart) { + EnableDataMode(isStart); + }); } void St7789::SetVdv() { @@ -166,19 +171,19 @@ void St7789::DrawBuffer(uint16_t x, uint16_t y, uint16_t width, uint16_t height, } void St7789::HardwareReset() { - nrf_gpio_pin_clear(PinMap::LcdReset); + nrf_gpio_pin_clear(pinReset); nrf_delay_ms(10); - nrf_gpio_pin_set(PinMap::LcdReset); + nrf_gpio_pin_set(pinReset); } void St7789::Sleep() { SleepIn(); - nrf_gpio_cfg_default(PinMap::LcdDataCommand); + nrf_gpio_cfg_default(pinDataCommand); NRF_LOG_INFO("[LCD] Sleep"); } void St7789::Wakeup() { - nrf_gpio_cfg_output(PinMap::LcdDataCommand); + nrf_gpio_cfg_output(pinDataCommand); SleepOut(); VerticalScrollStartAddress(verticalScrollingStartAddress); DisplayOn(); diff --git a/src/drivers/St7789.h b/src/drivers/St7789.h index 339776ae..0c73f77e 100644 --- a/src/drivers/St7789.h +++ b/src/drivers/St7789.h @@ -1,6 +1,7 @@ #pragma once #include #include +#include namespace Pinetime { namespace Drivers { @@ -8,7 +9,7 @@ namespace Pinetime { class St7789 { public: - explicit St7789(Spi& spi); + explicit St7789(Spi& spi, uint8_t pinDataCommand, uint8_t pinReset); St7789(const St7789&) = delete; St7789& operator=(const St7789&) = delete; St7789(St7789&&) = delete; @@ -26,6 +27,8 @@ namespace Pinetime { private: Spi& spi; + uint8_t pinDataCommand; + uint8_t pinReset; uint8_t verticalScrollingStartAddress = 0; void HardwareReset(); @@ -43,9 +46,9 @@ namespace Pinetime { void SetAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1); void SetVdv(); void WriteCommand(uint8_t cmd); - void WriteSpi(const uint8_t* data, size_t size, void (*TransactionHook)(bool)); - static void EnableDataMode(bool isStart); - static void EnableCommandMode(bool isStart); + void WriteSpi(const uint8_t* data, size_t size, std::function TransactionHook); + void EnableDataMode(bool isStart); + void EnableCommandMode(bool isStart); enum class Commands : uint8_t { SoftwareReset = 0x01, diff --git a/src/main.cpp b/src/main.cpp index 723c2e63..ee6a6d3d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -68,7 +68,7 @@ Pinetime::Drivers::SpiMaster spi {Pinetime::Drivers::SpiMaster::SpiModule::SPI0, Pinetime::PinMap::SpiMiso}}; Pinetime::Drivers::Spi lcdSpi {spi, Pinetime::PinMap::SpiLcdCsn}; -Pinetime::Drivers::St7789 lcd {lcdSpi}; +Pinetime::Drivers::St7789 lcd {lcdSpi, Pinetime::PinMap::LcdDataCommand, Pinetime::PinMap::LcdReset}; Pinetime::Drivers::Spi flashSpi {spi, Pinetime::PinMap::SpiFlashCsn}; Pinetime::Drivers::SpiNorFlash spiNorFlash {flashSpi}; diff --git a/src/recoveryLoader.cpp b/src/recoveryLoader.cpp index 55f85123..fc9ab76c 100644 --- a/src/recoveryLoader.cpp +++ b/src/recoveryLoader.cpp @@ -45,7 +45,7 @@ Pinetime::Drivers::Spi flashSpi {spi, Pinetime::PinMap::SpiFlashCsn}; Pinetime::Drivers::SpiNorFlash spiNorFlash {flashSpi}; Pinetime::Drivers::Spi lcdSpi {spi, Pinetime::PinMap::SpiLcdCsn}; -Pinetime::Drivers::St7789 lcd {lcdSpi}; +Pinetime::Drivers::St7789 lcd {lcdSpi, Pinetime::PinMap::LcdDataCommand, Pinetime::PinMap::LcdReset}; Pinetime::Controllers::BrightnessController brightnessController;