InfiniTime.git

commit 940cd3459f95073e60c61b37519486f569aac840

Author: mark9064 <30447455+mark9064@users.noreply.github.com>

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 | 39 ++++++++++++++++++++++-----------------
 src/drivers/St7789.h | 11 +++++++----
 src/main.cpp | 2 +-
 src/recoveryLoader.cpp | 2 +-


diff --git a/src/drivers/Spi.cpp b/src/drivers/Spi.cpp
index e0b716faa1e5092fe03f271481e630a9e90677fe..df0187756af8b47afca89438d4742822c70a0160 100644
--- a/src/drivers/Spi.cpp
+++ b/src/drivers/Spi.cpp
@@ -9,7 +9,7 @@   nrf_gpio_cfg_output(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<void(bool)> TransactionHook) {
   return spiMaster.Write(pinCsn, data, size, TransactionHook);
 }
 




diff --git a/src/drivers/Spi.h b/src/drivers/Spi.h
index 55eef05cabb9765d4a275346571e3aae1afd5f55..4f48fafe491ce2f76a4ef9ff15163add392c6f33 100644
--- a/src/drivers/Spi.h
+++ b/src/drivers/Spi.h
@@ -1,6 +1,7 @@
 #pragma once
 #include <cstdint>
 #include <cstddef>
+#include <functional>
 #include "drivers/SpiMaster.h"
 
 namespace Pinetime {
@@ -14,7 +15,7 @@       Spi(Spi&&) = delete;
       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<void(bool)> 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 f878c7d5d6006d9ba8826590f021fdac25c451ee..5bfbf7b4698c8134efd476761890c4ea93b6c47c 100644
--- a/src/drivers/SpiMaster.cpp
+++ b/src/drivers/SpiMaster.cpp
@@ -170,7 +170,7 @@   spiBaseAddress->RXD.LIST = 0;
   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<void(bool)> 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 131965e1ce5c218db905a882d0982caddc97d8a2..21980f4fc6a3e8433696ee815cda3fa996dd2dc3 100644
--- a/src/drivers/SpiMaster.h
+++ b/src/drivers/SpiMaster.h
@@ -1,6 +1,7 @@
 #pragma once
 #include <cstddef>
 #include <cstdint>
+#include <functional>
 
 #include <FreeRTOS.h>
 #include <semphr.h>
@@ -31,7 +32,7 @@       SpiMaster(SpiMaster&&) = delete;
       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<void(bool)> 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 @@       void PrepareRx(const volatile uint32_t bufferAddress, const volatile size_t size);
 
       NRF_SPIM_Type* spiBaseAddress;
       uint8_t pinCsn;
-      void (*TransactionHook)(bool);
+      std::function<void(bool)> TransactionHook;
 
       SpiMaster::SpiModule spi;
       SpiMaster::Parameters params;




diff --git a/src/drivers/St7789.cpp b/src/drivers/St7789.cpp
index 78ad3a66bb6c8234a60549f6710bd83e094be1b6..6e5d13b1d4c53159ff09133454aa1a09960521a1 100644
--- a/src/drivers/St7789.cpp
+++ b/src/drivers/St7789.cpp
@@ -3,17 +3,16 @@ #include 
 #include <libraries/delay/nrf_delay.h>
 #include <nrfx_log.h>
 #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::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, [this](bool isStart) {
+    EnableDataMode(isStart);
+  });
 }
 
-void St7789::WriteData(uint8_t data) {
-  WriteSpi(&data, 1, EnableDataMode);
+void St7789::WriteCommand(uint8_t cmd) {
+  WriteSpi(&cmd, 1, [this](bool isStart) {
+    EnableCommandMode(isStart);
+  });
 }
 
-void St7789::WriteSpi(const uint8_t* data, size_t size, void (*TransactionHook)(bool)) {
+void St7789::WriteSpi(const uint8_t* data, size_t size, std::function<void(bool)> TransactionHook) {
   spi.Write(data, size, TransactionHook);
 }
 
@@ -135,7 +138,9 @@ }
 
 void St7789::WriteToRam(const uint8_t* data, size_t size) {
   WriteCommand(static_cast<uint8_t>(Commands::WriteToRam));
-  WriteSpi(data, size, EnableDataMode);
+  WriteSpi(data, size, [this](bool isStart) {
+    EnableDataMode(isStart);
+  });
 }
 
 void St7789::SetVdv() {
@@ -166,19 +171,19 @@   WriteToRam(data, size);
 }
 
 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 339776aee6d4bd14d8a80f26770e00894efe60ce..0c73f77eba4c25ecca8cc832cbcbded34bade8f2 100644
--- a/src/drivers/St7789.h
+++ b/src/drivers/St7789.h
@@ -1,6 +1,7 @@
 #pragma once
 #include <cstddef>
 #include <cstdint>
+#include <functional>
 
 namespace Pinetime {
   namespace Drivers {
@@ -8,7 +9,7 @@     class Spi;
 
     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 @@       void Wakeup();
 
     private:
       Spi& spi;
+      uint8_t pinDataCommand;
+      uint8_t pinReset;
       uint8_t verticalScrollingStartAddress = 0;
 
       void HardwareReset();
@@ -43,9 +46,9 @@
       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<void(bool)> 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 723c2e63e812c4def647117ff9e7685270af4f5d..ee6a6d3de5c600b8bab5ba846a30893d2edd0e0c 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -68,7 +68,7 @@                                    Pinetime::PinMap::SpiMosi,
                                    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 55f85123bcdabf453a38a06b02fd75982f4ac7d3..fc9ab76cff69389ebe3fb7002751730bf15f5b14 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;