From 24e6a2f8abab219868cb2eb6d02b3d83504c223c Mon Sep 17 00:00:00 2001 From: mark9064 <30447455+mark9064@users.noreply.github.com> Date: Thu, 11 Apr 2024 00:03:36 +0100 Subject: [PATCH] Avoid storing lambda --- src/drivers/Spi.cpp | 4 ++-- src/drivers/Spi.h | 2 +- src/drivers/SpiMaster.cpp | 16 +++------------- src/drivers/SpiMaster.h | 3 +-- src/drivers/St7789.cpp | 16 ++++++---------- src/drivers/St7789.h | 2 +- 6 files changed, 14 insertions(+), 29 deletions(-) diff --git a/src/drivers/Spi.cpp b/src/drivers/Spi.cpp index df018775..a03ea3c0 100644 --- a/src/drivers/Spi.cpp +++ b/src/drivers/Spi.cpp @@ -9,8 +9,8 @@ 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, std::function TransactionHook) { - return spiMaster.Write(pinCsn, data, size, TransactionHook); +bool Spi::Write(const uint8_t* data, size_t size, const std::function& transactionHook) { + return spiMaster.Write(pinCsn, data, size, transactionHook); } bool Spi::Read(uint8_t* cmd, size_t cmdSize, uint8_t* data, size_t dataSize) { diff --git a/src/drivers/Spi.h b/src/drivers/Spi.h index 4f48fafe..e30620cc 100644 --- a/src/drivers/Spi.h +++ b/src/drivers/Spi.h @@ -15,7 +15,7 @@ namespace Pinetime { Spi& operator=(Spi&&) = delete; bool Init(); - bool Write(const uint8_t* data, size_t size, std::function TransactionHook); + bool Write(const uint8_t* data, size_t size, const 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 5bfbf7b4..eec62cb7 100644 --- a/src/drivers/SpiMaster.cpp +++ b/src/drivers/SpiMaster.cpp @@ -137,9 +137,6 @@ void SpiMaster::OnEndEvent() { spiBaseAddress->TASKS_START = 1; } else { nrf_gpio_pin_set(this->pinCsn); - if (this->TransactionHook != nullptr) { - this->TransactionHook(false); - } currentBufferAddr = 0; BaseType_t xHigherPriorityTaskWoken = pdFALSE; xSemaphoreGiveFromISR(mutex, &xHigherPriorityTaskWoken); @@ -170,13 +167,12 @@ 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, std::function TransactionHook) { +bool SpiMaster::Write(uint8_t pinCsn, const uint8_t* data, size_t size, const std::function& transactionHook) { if (data == nullptr) return false; auto ok = xSemaphoreTake(mutex, portMAX_DELAY); ASSERT(ok == true); - this->TransactionHook = TransactionHook; this->pinCsn = pinCsn; if (size == 1) { @@ -185,8 +181,8 @@ bool SpiMaster::Write(uint8_t pinCsn, const uint8_t* data, size_t size, std::fun DisableWorkaroundForFtpan58(spiBaseAddress, 0, 0); } - if (this->TransactionHook != nullptr) { - this->TransactionHook(true); + if (transactionHook != nullptr) { + transactionHook(); } nrf_gpio_pin_clear(this->pinCsn); @@ -203,9 +199,6 @@ bool SpiMaster::Write(uint8_t pinCsn, const uint8_t* data, size_t size, std::fun while (spiBaseAddress->EVENTS_END == 0) ; nrf_gpio_pin_set(this->pinCsn); - if (this->TransactionHook != nullptr) { - this->TransactionHook(false); - } currentBufferAddr = 0; DisableWorkaroundForFtpan58(spiBaseAddress, 0, 0); @@ -219,7 +212,6 @@ bool SpiMaster::Write(uint8_t pinCsn, const uint8_t* data, size_t size, std::fun bool SpiMaster::Read(uint8_t pinCsn, uint8_t* cmd, size_t cmdSize, uint8_t* data, size_t dataSize) { xSemaphoreTake(mutex, portMAX_DELAY); - this->TransactionHook = nullptr; this->pinCsn = pinCsn; DisableWorkaroundForFtpan58(spiBaseAddress, 0, 0); spiBaseAddress->INTENCLR = (1 << 6); @@ -267,8 +259,6 @@ void SpiMaster::Wakeup() { bool SpiMaster::WriteCmdAndBuffer(uint8_t pinCsn, const uint8_t* cmd, size_t cmdSize, const uint8_t* data, size_t dataSize) { xSemaphoreTake(mutex, portMAX_DELAY); - this->TransactionHook = nullptr; - this->pinCsn = pinCsn; DisableWorkaroundForFtpan58(spiBaseAddress, 0, 0); spiBaseAddress->INTENCLR = (1 << 6); diff --git a/src/drivers/SpiMaster.h b/src/drivers/SpiMaster.h index 21980f4f..2f39a1b2 100644 --- a/src/drivers/SpiMaster.h +++ b/src/drivers/SpiMaster.h @@ -32,7 +32,7 @@ namespace Pinetime { SpiMaster& operator=(SpiMaster&&) = delete; bool Init(); - bool Write(uint8_t pinCsn, const uint8_t* data, size_t size, std::function TransactionHook); + bool Write(uint8_t pinCsn, const uint8_t* data, size_t size, const 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); @@ -51,7 +51,6 @@ namespace Pinetime { NRF_SPIM_Type* spiBaseAddress; uint8_t pinCsn; - std::function TransactionHook; SpiMaster::SpiModule spi; SpiMaster::Parameters params; diff --git a/src/drivers/St7789.cpp b/src/drivers/St7789.cpp index e42592e6..e933c374 100644 --- a/src/drivers/St7789.cpp +++ b/src/drivers/St7789.cpp @@ -34,10 +34,8 @@ void St7789::WriteData(uint8_t data) { } void St7789::WriteData(const uint8_t* data, size_t size) { - WriteSpi(data, size, [pinDataCommand = pinDataCommand](bool isStart) { - if (isStart) { - nrf_gpio_pin_set(pinDataCommand); - } + WriteSpi(data, size, [pinDataCommand = pinDataCommand]() { + nrf_gpio_pin_set(pinDataCommand); }); } @@ -46,15 +44,13 @@ void St7789::WriteCommand(uint8_t data) { } void St7789::WriteCommand(const uint8_t* data, size_t size) { - WriteSpi(data, size, [pinDataCommand = pinDataCommand](bool isStart) { - if (isStart) { - nrf_gpio_pin_clear(pinDataCommand); - } + WriteSpi(data, size, [pinDataCommand = pinDataCommand]() { + nrf_gpio_pin_clear(pinDataCommand); }); } -void St7789::WriteSpi(const uint8_t* data, size_t size, std::function TransactionHook) { - spi.Write(data, size, TransactionHook); +void St7789::WriteSpi(const uint8_t* data, size_t size, const std::function& transactionHook) { + spi.Write(data, size, transactionHook); } void St7789::SoftwareReset() { diff --git a/src/drivers/St7789.h b/src/drivers/St7789.h index 5eb60cfd..f49ed511 100644 --- a/src/drivers/St7789.h +++ b/src/drivers/St7789.h @@ -47,7 +47,7 @@ namespace Pinetime { void SetVdv(); void WriteCommand(uint8_t cmd); void WriteCommand(const uint8_t* data, size_t size); - void WriteSpi(const uint8_t* data, size_t size, std::function TransactionHook); + void WriteSpi(const uint8_t* data, size_t size, const std::function& transactionHook); enum class Commands : uint8_t { SoftwareReset = 0x01,