Use functional abstraction for hooks

This commit is contained in:
mark9064 2024-04-01 17:14:33 +01:00 committed by JF
parent 869bec8f88
commit 940cd3459f
8 changed files with 39 additions and 29 deletions

View File

@ -9,7 +9,7 @@ Spi::Spi(SpiMaster& spiMaster, uint8_t pinCsn) : spiMaster {spiMaster}, pinCsn {
nrf_gpio_pin_set(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); return spiMaster.Write(pinCsn, data, size, TransactionHook);
} }

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <cstdint> #include <cstdint>
#include <cstddef> #include <cstddef>
#include <functional>
#include "drivers/SpiMaster.h" #include "drivers/SpiMaster.h"
namespace Pinetime { namespace Pinetime {
@ -14,7 +15,7 @@ namespace Pinetime {
Spi& operator=(Spi&&) = delete; Spi& operator=(Spi&&) = delete;
bool Init(); 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 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); bool WriteCmdAndBuffer(const uint8_t* cmd, size_t cmdSize, const uint8_t* data, size_t dataSize);
void Sleep(); void Sleep();

View File

@ -170,7 +170,7 @@ void SpiMaster::PrepareRx(const uint32_t bufferAddress, const size_t size) {
spiBaseAddress->EVENTS_END = 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) if (data == nullptr)
return false; return false;
auto ok = xSemaphoreTake(mutex, portMAX_DELAY); auto ok = xSemaphoreTake(mutex, portMAX_DELAY);

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include <functional>
#include <FreeRTOS.h> #include <FreeRTOS.h>
#include <semphr.h> #include <semphr.h>
@ -31,7 +32,7 @@ namespace Pinetime {
SpiMaster& operator=(SpiMaster&&) = delete; SpiMaster& operator=(SpiMaster&&) = delete;
bool Init(); 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 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); 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; NRF_SPIM_Type* spiBaseAddress;
uint8_t pinCsn; uint8_t pinCsn;
void (*TransactionHook)(bool); std::function<void(bool)> TransactionHook;
SpiMaster::SpiModule spi; SpiMaster::SpiModule spi;
SpiMaster::Parameters params; SpiMaster::Parameters params;

View File

@ -3,17 +3,16 @@
#include <libraries/delay/nrf_delay.h> #include <libraries/delay/nrf_delay.h>
#include <nrfx_log.h> #include <nrfx_log.h>
#include "drivers/Spi.h" #include "drivers/Spi.h"
#include "drivers/PinMap.h"
using namespace Pinetime::Drivers; 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() { void St7789::Init() {
nrf_gpio_cfg_output(PinMap::LcdDataCommand); nrf_gpio_cfg_output(pinDataCommand);
nrf_gpio_cfg_output(PinMap::LcdReset); nrf_gpio_cfg_output(pinReset);
nrf_gpio_pin_set(PinMap::LcdReset); nrf_gpio_pin_set(pinReset);
HardwareReset(); HardwareReset();
SoftwareReset(); SoftwareReset();
SleepOut(); SleepOut();
@ -32,25 +31,29 @@ void St7789::Init() {
void St7789::EnableDataMode(bool isStart) { void St7789::EnableDataMode(bool isStart) {
if (isStart) { if (isStart) {
nrf_gpio_pin_set(PinMap::LcdDataCommand); nrf_gpio_pin_set(pinDataCommand);
} }
} }
void St7789::EnableCommandMode(bool isStart) { void St7789::EnableCommandMode(bool isStart) {
if (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) { 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<void(bool)> TransactionHook) {
spi.Write(data, size, 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) { void St7789::WriteToRam(const uint8_t* data, size_t size) {
WriteCommand(static_cast<uint8_t>(Commands::WriteToRam)); WriteCommand(static_cast<uint8_t>(Commands::WriteToRam));
WriteSpi(data, size, EnableDataMode); WriteSpi(data, size, [this](bool isStart) {
EnableDataMode(isStart);
});
} }
void St7789::SetVdv() { 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() { void St7789::HardwareReset() {
nrf_gpio_pin_clear(PinMap::LcdReset); nrf_gpio_pin_clear(pinReset);
nrf_delay_ms(10); nrf_delay_ms(10);
nrf_gpio_pin_set(PinMap::LcdReset); nrf_gpio_pin_set(pinReset);
} }
void St7789::Sleep() { void St7789::Sleep() {
SleepIn(); SleepIn();
nrf_gpio_cfg_default(PinMap::LcdDataCommand); nrf_gpio_cfg_default(pinDataCommand);
NRF_LOG_INFO("[LCD] Sleep"); NRF_LOG_INFO("[LCD] Sleep");
} }
void St7789::Wakeup() { void St7789::Wakeup() {
nrf_gpio_cfg_output(PinMap::LcdDataCommand); nrf_gpio_cfg_output(pinDataCommand);
SleepOut(); SleepOut();
VerticalScrollStartAddress(verticalScrollingStartAddress); VerticalScrollStartAddress(verticalScrollingStartAddress);
DisplayOn(); DisplayOn();

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include <functional>
namespace Pinetime { namespace Pinetime {
namespace Drivers { namespace Drivers {
@ -8,7 +9,7 @@ namespace Pinetime {
class St7789 { class St7789 {
public: public:
explicit St7789(Spi& spi); explicit St7789(Spi& spi, uint8_t pinDataCommand, uint8_t pinReset);
St7789(const St7789&) = delete; St7789(const St7789&) = delete;
St7789& operator=(const St7789&) = delete; St7789& operator=(const St7789&) = delete;
St7789(St7789&&) = delete; St7789(St7789&&) = delete;
@ -26,6 +27,8 @@ namespace Pinetime {
private: private:
Spi& spi; Spi& spi;
uint8_t pinDataCommand;
uint8_t pinReset;
uint8_t verticalScrollingStartAddress = 0; uint8_t verticalScrollingStartAddress = 0;
void HardwareReset(); void HardwareReset();
@ -43,9 +46,9 @@ namespace Pinetime {
void SetAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1); void SetAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1);
void SetVdv(); void SetVdv();
void WriteCommand(uint8_t cmd); void WriteCommand(uint8_t cmd);
void WriteSpi(const uint8_t* data, size_t size, void (*TransactionHook)(bool)); void WriteSpi(const uint8_t* data, size_t size, std::function<void(bool)> TransactionHook);
static void EnableDataMode(bool isStart); void EnableDataMode(bool isStart);
static void EnableCommandMode(bool isStart); void EnableCommandMode(bool isStart);
enum class Commands : uint8_t { enum class Commands : uint8_t {
SoftwareReset = 0x01, SoftwareReset = 0x01,

View File

@ -68,7 +68,7 @@ Pinetime::Drivers::SpiMaster spi {Pinetime::Drivers::SpiMaster::SpiModule::SPI0,
Pinetime::PinMap::SpiMiso}}; Pinetime::PinMap::SpiMiso}};
Pinetime::Drivers::Spi lcdSpi {spi, Pinetime::PinMap::SpiLcdCsn}; 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::Spi flashSpi {spi, Pinetime::PinMap::SpiFlashCsn};
Pinetime::Drivers::SpiNorFlash spiNorFlash {flashSpi}; Pinetime::Drivers::SpiNorFlash spiNorFlash {flashSpi};

View File

@ -45,7 +45,7 @@ Pinetime::Drivers::Spi flashSpi {spi, Pinetime::PinMap::SpiFlashCsn};
Pinetime::Drivers::SpiNorFlash spiNorFlash {flashSpi}; Pinetime::Drivers::SpiNorFlash spiNorFlash {flashSpi};
Pinetime::Drivers::Spi lcdSpi {spi, Pinetime::PinMap::SpiLcdCsn}; 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; Pinetime::Controllers::BrightnessController brightnessController;