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);
}
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);
}

View file

@ -1,6 +1,7 @@
#pragma once
#include <cstdint>
#include <cstddef>
#include <functional>
#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<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();

View file

@ -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<void(bool)> TransactionHook) {
if (data == nullptr)
return false;
auto ok = xSemaphoreTake(mutex, portMAX_DELAY);

View file

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

View file

@ -3,17 +3,16 @@
#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::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<void(bool)> 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<uint8_t>(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();

View file

@ -1,6 +1,7 @@
#pragma once
#include <cstddef>
#include <cstdint>
#include <functional>
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<void(bool)> TransactionHook);
void EnableDataMode(bool isStart);
void EnableCommandMode(bool isStart);
enum class Commands : uint8_t {
SoftwareReset = 0x01,

View file

@ -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};

View file

@ -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;