From 4793d97531135d273ee192079ed4d206cbc5a553 Mon Sep 17 00:00:00 2001 From: hhvrc Date: Mon, 20 Jan 2025 21:20:13 +0100 Subject: [PATCH] Decrease allocations for RF encoding + transmitting --- include/ShockerCommandType.h | 2 +- include/radio/rmt/CaiXianlinEncoder.h | 6 +- include/radio/rmt/MainEncoder.h | 63 ++++++++++++- include/radio/rmt/Petrainer998DREncoder.h | 6 +- include/radio/rmt/PetrainerEncoder.h | 6 +- include/radio/rmt/RmtSequence.h | 58 ------------ include/radio/rmt/T330Encoder.h | 6 +- src/radio/RFTransmitter.cpp | 109 +++++++++------------- src/radio/rmt/CaiXianlinEncoder.cpp | 19 ++-- src/radio/rmt/MainEncoder.cpp | 42 +++++++-- src/radio/rmt/Petrainer998DREncoder.cpp | 19 ++-- src/radio/rmt/PetrainerEncoder.cpp | 19 ++-- src/radio/rmt/T330Encoder.cpp | 19 ++-- 13 files changed, 186 insertions(+), 188 deletions(-) delete mode 100644 include/radio/rmt/RmtSequence.h diff --git a/include/ShockerCommandType.h b/include/ShockerCommandType.h index 5adb91a4..a140d576 100644 --- a/include/ShockerCommandType.h +++ b/include/ShockerCommandType.h @@ -4,7 +4,7 @@ #include namespace OpenShock { - enum class ShockerCommandType { + enum class ShockerCommandType : uint8_t { Stop, Shock, Vibrate, diff --git a/include/radio/rmt/CaiXianlinEncoder.h b/include/radio/rmt/CaiXianlinEncoder.h index 9d4f53d6..36ef4da6 100644 --- a/include/radio/rmt/CaiXianlinEncoder.h +++ b/include/radio/rmt/CaiXianlinEncoder.h @@ -1,10 +1,12 @@ #pragma once -#include "RmtSequence.h" #include "ShockerCommandType.h" +#include + #include namespace OpenShock::Rmt::CaiXianlinEncoder { - RmtSequence GetSequence(uint16_t shockerId, uint8_t channelId, OpenShock::ShockerCommandType type, uint8_t intensity); + size_t GetBufferSize(); + bool FillBuffer(rmt_data_t* data, uint16_t shockerId, uint8_t channelId, ShockerCommandType type, uint8_t intensity); } diff --git a/include/radio/rmt/MainEncoder.h b/include/radio/rmt/MainEncoder.h index 22f33cc5..5c1201f8 100644 --- a/include/radio/rmt/MainEncoder.h +++ b/include/radio/rmt/MainEncoder.h @@ -1,11 +1,70 @@ #pragma once -#include "RmtSequence.h" +#include "Common.h" #include "ShockerCommandType.h" #include "ShockerModelType.h" +#include + #include namespace OpenShock::Rmt { - RmtSequence GetSequence(ShockerModelType model, uint16_t shockerId, OpenShock::ShockerCommandType type, uint8_t intensity); + class MainEncoder { + DISABLE_COPY(MainEncoder); + + public: + MainEncoder() + : m_data(nullptr) + , m_size(0) + , m_shockerModel() + , m_shockerId(0) + { + } + MainEncoder(ShockerModelType shockerModel, uint16_t shockerId); + MainEncoder(MainEncoder&& other) noexcept + : m_data(other.m_data) + , m_size(other.m_size) + , m_shockerModel(other.m_shockerModel) + , m_shockerId(other.m_shockerId) + { + other.m_data = nullptr; + other.m_size = 0; + other.m_shockerId = 0; + } + ~MainEncoder() { free(m_data); } + + inline bool is_valid() const noexcept { return m_data != nullptr && m_size > 0; } + + inline ShockerModelType shockerModel() const noexcept { return m_shockerModel; } + inline uint16_t shockerId() const noexcept { return m_shockerId; } + + inline rmt_data_t* payload() noexcept { return m_data; } + inline const rmt_data_t* payload() const noexcept { return m_data; } + inline rmt_data_t* terminator() noexcept { return m_data + m_size; } + inline const rmt_data_t* terminator() const noexcept { return m_data + m_size; } + inline size_t size() const noexcept { return m_size; } + + bool fillSequence(ShockerCommandType commandType, uint8_t intensity); + + MainEncoder& operator=(MainEncoder&& other) + { + if (this == &other) return *this; + + free(m_data); + + m_data = other.m_data; + m_size = other.m_size; + + other.m_data = nullptr; + other.m_size = 0; + + return *this; + } + + private: + rmt_data_t* m_data; + size_t m_size; + ShockerModelType m_shockerModel; + uint16_t m_shockerId; + }; } // namespace OpenShock::Rmt diff --git a/include/radio/rmt/Petrainer998DREncoder.h b/include/radio/rmt/Petrainer998DREncoder.h index 5c3f8ad8..717966fd 100644 --- a/include/radio/rmt/Petrainer998DREncoder.h +++ b/include/radio/rmt/Petrainer998DREncoder.h @@ -1,10 +1,12 @@ #pragma once -#include "RmtSequence.h" #include "ShockerCommandType.h" +#include + #include namespace OpenShock::Rmt::Petrainer998DREncoder { - RmtSequence GetSequence(uint16_t shockerId, OpenShock::ShockerCommandType type, uint8_t intensity); + size_t GetBufferSize(); + bool FillBuffer(rmt_data_t* data, uint16_t shockerId, ShockerCommandType type, uint8_t intensity); } diff --git a/include/radio/rmt/PetrainerEncoder.h b/include/radio/rmt/PetrainerEncoder.h index 1766cfd0..e55b8275 100644 --- a/include/radio/rmt/PetrainerEncoder.h +++ b/include/radio/rmt/PetrainerEncoder.h @@ -1,10 +1,12 @@ #pragma once -#include "RmtSequence.h" #include "ShockerCommandType.h" +#include + #include namespace OpenShock::Rmt::PetrainerEncoder { - RmtSequence GetSequence(uint16_t shockerId, OpenShock::ShockerCommandType type, uint8_t intensity); + size_t GetBufferSize(); + bool FillBuffer(rmt_data_t* data, uint16_t shockerId, ShockerCommandType type, uint8_t intensity); } diff --git a/include/radio/rmt/RmtSequence.h b/include/radio/rmt/RmtSequence.h deleted file mode 100644 index 94f5e14f..00000000 --- a/include/radio/rmt/RmtSequence.h +++ /dev/null @@ -1,58 +0,0 @@ -#pragma once - -#include "Common.h" - -#include - -#include - -namespace OpenShock::Rmt { - class RmtSequence { - DISABLE_COPY(RmtSequence); - - public: - RmtSequence() - : m_data(nullptr) - , m_size(0) - { - } - RmtSequence(size_t size) - : m_data(reinterpret_cast(malloc(size * 2 * sizeof(rmt_data_t)))) - , m_size(size) - { - } - RmtSequence(RmtSequence&& other) noexcept - : m_data(other.m_data) - , m_size(other.m_size) - { - other.m_data = nullptr; - other.m_size = 0; - } - ~RmtSequence() { free(m_data); } - - constexpr bool is_valid() const noexcept { return m_data != nullptr && m_size != 0; } - - constexpr rmt_data_t* payload() noexcept { return m_data; } - constexpr rmt_data_t* terminator() noexcept { return m_data + m_size; } - constexpr size_t size() const noexcept { return m_size; } - - RmtSequence& operator=(RmtSequence&& other) - { - if (this == &other) return *this; - - free(m_data); - - m_data = other.m_data; - m_size = other.m_size; - - other.m_data = nullptr; - other.m_size = 0; - - return *this; - } - - private: - rmt_data_t* m_data; - size_t m_size; - }; -} // namespace OpenShock::Rmt diff --git a/include/radio/rmt/T330Encoder.h b/include/radio/rmt/T330Encoder.h index b81321b4..87368778 100644 --- a/include/radio/rmt/T330Encoder.h +++ b/include/radio/rmt/T330Encoder.h @@ -1,10 +1,12 @@ #pragma once -#include "RmtSequence.h" #include "ShockerCommandType.h" +#include + #include namespace OpenShock::Rmt::T330Encoder { - RmtSequence GetSequence(uint16_t shockerId, OpenShock::ShockerCommandType type, uint8_t intensity); + size_t GetBufferSize(); + bool FillBuffer(rmt_data_t* data, uint16_t shockerId, ShockerCommandType type, uint8_t intensity); } diff --git a/src/radio/RFTransmitter.cpp b/src/radio/RFTransmitter.cpp index a153779f..8cbc6431 100644 --- a/src/radio/RFTransmitter.cpp +++ b/src/radio/RFTransmitter.cpp @@ -7,7 +7,6 @@ const char* const TAG = "RFTransmitter"; #include "estop/EStopManager.h" #include "Logging.h" #include "radio/rmt/MainEncoder.h" -#include "radio/rmt/RmtSequence.h" #include "Time.h" #include "util/FnProxy.h" #include "util/TaskUtils.h" @@ -19,14 +18,22 @@ const BaseType_t RFTRANSMITTER_TASK_PRIORITY = 1; const uint32_t RFTRANSMITTER_TASK_STACK_SIZE = 4096; // PROFILED: 1.4KB stack usage const float RFTRANSMITTER_TICKRATE_NS = 1000; const int64_t TRANSMIT_END_DURATION = 300; +const int64_t SEQUENCE_TIME_TO_LIVE = 1000; using namespace OpenShock; struct command_t { int64_t until; - Rmt::RmtSequence sequence; + ShockerModelType model; + ShockerCommandType type; uint16_t shockerId; - bool overwrite; + uint8_t intensity; + bool overwrite : 1; + bool destroy : 1; +}; +struct sequence_t { + int64_t until; + Rmt::MainEncoder encoder; }; RFTransmitter::RFTransmitter(gpio_num_t gpioPin) @@ -47,7 +54,7 @@ RFTransmitter::RFTransmitter(gpio_num_t gpioPin) float realTick = rmtSetTick(m_rmtHandle, RFTRANSMITTER_TICKRATE_NS); OS_LOGD(TAG, "[pin-%hhi] real tick set to: %fns", m_txPin, realTick); - m_queueHandle = xQueueCreate(RFTRANSMITTER_QUEUE_SIZE, sizeof(command_t*)); + m_queueHandle = xQueueCreate(RFTRANSMITTER_QUEUE_SIZE, sizeof(command_t)); if (m_queueHandle == nullptr) { OS_LOGE(TAG, "[pin-%hhi] Failed to create queue", m_txPin); destroy(); @@ -76,24 +83,11 @@ bool RFTransmitter::SendCommand(ShockerModelType model, uint16_t shockerId, Shoc return false; } - auto sequence = Rmt::GetSequence(model, shockerId, type, intensity); - if (!sequence.is_valid()) { - OS_LOGE(TAG, "[pin-%hhi] Failed to create sequence for command, refusing to send", m_txPin); - return false; - } - - command_t* cmd = new command_t {.until = OpenShock::millis() + durationMs, .sequence = std::move(sequence), .shockerId = shockerId, .overwrite = overwriteExisting}; - - // We will use nullptr commands to end the task, if we got a nullptr here, we are out of memory... :( - if (cmd == nullptr) { - OS_LOGE(TAG, "[pin-%hhi] Failed to allocate command", m_txPin); - return false; - } + command_t cmd = command_t {.until = OpenShock::millis() + durationMs, .model = model, .type = type, .shockerId = shockerId, .intensity = intensity, .overwrite = overwriteExisting, .destroy = false}; // Add the command to the queue, wait max 10 ms (Adjust this) if (xQueueSend(m_queueHandle, &cmd, pdMS_TO_TICKS(10)) != pdTRUE) { OS_LOGE(TAG, "[pin-%hhi] Failed to send command to queue", m_txPin); - delete cmd; return false; } @@ -108,9 +102,8 @@ void RFTransmitter::ClearPendingCommands() OS_LOGI(TAG, "[pin-%hhi] Clearing pending commands", m_txPin); - command_t* command; + command_t command; while (xQueueReceive(m_queueHandle, &command, 0) == pdPASS) { - delete command; } } @@ -120,7 +113,7 @@ void RFTransmitter::destroy() OS_LOGD(TAG, "[pin-%hhi] Stopping task", m_txPin); // Wait for the task to stop - command_t* cmd = nullptr; + command_t cmd {.destroy = true}; while (eTaskGetState(m_taskHandle) != eDeleted) { vTaskDelay(pdMS_TO_TICKS(10)); @@ -149,85 +142,67 @@ void RFTransmitter::TransmitTask() { OS_LOGD(TAG, "[pin-%hhi] RMT loop running on core %d", m_txPin, xPortGetCoreID()); - std::vector commands; + std::vector sequences; while (true) { // Receive commands - command_t* cmd = nullptr; - while (xQueueReceive(m_queueHandle, &cmd, commands.empty() ? portMAX_DELAY : 0) == pdTRUE) { - if (cmd == nullptr) { - OS_LOGD(TAG, "[pin-%hhi] Received nullptr (stop command), cleaning up...", m_txPin); - - for (auto it = commands.begin(); it != commands.end(); ++it) { - delete *it; - } - - OS_LOGD(TAG, "[pin-%hhi] Cleanup done, stopping task", m_txPin); - + command_t cmd; + while (xQueueReceive(m_queueHandle, &cmd, sequences.empty() ? portMAX_DELAY : 0) == pdTRUE) { + // Destroy task if we receive destroy command + if (cmd.destroy) { + sequences.clear(); vTaskDelete(nullptr); return; } - // Replace the command if it already exists - for (auto it = commands.begin(); it != commands.end(); ++it) { - const command_t* existingCmd = *it; - - if (existingCmd->shockerId == cmd->shockerId) { - // Only replace the command if it should be overwritten - if (existingCmd->overwrite) { - delete *it; - *it = cmd; - } else { - delete cmd; - } - - cmd = nullptr; - - break; + if (cmd.overwrite) { + // Replace the sequence if it already exists + auto it = std::find_if(sequences.begin(), sequences.end(), [&cmd](const sequence_t& seq) { return seq.encoder.shockerId() == cmd.shockerId; }); + if (it != sequences.end()) { + it->encoder.fillSequence(cmd.type, cmd.intensity); + continue; } } - // If the command was not replaced, add it to the queue - if (cmd != nullptr) { - commands.push_back(cmd); + Rmt::MainEncoder encoder(cmd.model, cmd.shockerId); + if (encoder.is_valid()) { + encoder.fillSequence(cmd.type, cmd.intensity); + + // If the command was not replaced, add it to the queue + sequences.push_back(sequence_t {.until = cmd.until, .encoder = std::move(encoder)}); } } if (OpenShock::EStopManager::IsEStopped()) { int64_t whenEStoppedTime = EStopManager::LastEStopped(); - for (auto it = commands.begin(); it != commands.end(); ++it) { - cmd = *it; - - cmd->until = whenEStoppedTime; + for (auto seq = sequences.begin(); seq != sequences.end(); ++seq) { + seq->until = whenEStoppedTime; } } // Send queued commands - for (auto it = commands.begin(); it != commands.end();) { - cmd = *it; - - bool expired = cmd->until < OpenShock::millis(); + for (auto seq = sequences.begin(); seq != sequences.end();) { + bool expired = seq->until < OpenShock::millis(); // Remove expired commands, else send the command. // After sending/receiving a command, move to the next one. if (expired) { // Send the zero sequence to stop the shocker - rmtWriteBlocking(m_rmtHandle, cmd->sequence.terminator(), cmd->sequence.size()); + rmtWriteBlocking(m_rmtHandle, seq->encoder.terminator(), seq->encoder.size()); - if (cmd->until + TRANSMIT_END_DURATION < OpenShock::millis()) { + if (seq->until + TRANSMIT_END_DURATION < OpenShock::millis()) { // Remove the command and move to the next one - it = commands.erase(it); - delete cmd; + seq = sequences.erase(seq); } else { // Move to the next command - ++it; + ++seq; } } else { // Send the command - rmtWriteBlocking(m_rmtHandle, cmd->sequence.payload(), cmd->sequence.size()); + rmtWriteBlocking(m_rmtHandle, seq->encoder.payload(), seq->encoder.size()); // Move to the next command - ++it; + ++seq; } } } diff --git a/src/radio/rmt/CaiXianlinEncoder.cpp b/src/radio/rmt/CaiXianlinEncoder.cpp index 1b5dd969..419d8b71 100644 --- a/src/radio/rmt/CaiXianlinEncoder.cpp +++ b/src/radio/rmt/CaiXianlinEncoder.cpp @@ -4,6 +4,8 @@ #include "Checksum.h" +#include + // This is the encoder for the CaiXianlin shocker. // // It is based on the following documentation: @@ -15,7 +17,12 @@ const rmt_data_t kRmtZero = {250, 1, 750, 0}; using namespace OpenShock; -static bool fillSequence(rmt_data_t* sequence, uint16_t shockerId, uint8_t channelId, ShockerCommandType type, uint8_t intensity) +size_t Rmt::CaiXianlinEncoder::GetBufferSize() +{ + return 44; +} + +bool Rmt::CaiXianlinEncoder::FillBuffer(rmt_data_t* sequence, uint16_t shockerId, uint8_t channelId, ShockerCommandType type, uint8_t intensity) { // Intensity must be between 0 and 99 intensity = std::min(intensity, static_cast(99)); @@ -54,13 +61,3 @@ static bool fillSequence(rmt_data_t* sequence, uint16_t shockerId, uint8_t chann return true; } - -Rmt::RmtSequence Rmt::CaiXianlinEncoder::GetSequence(uint16_t shockerId, uint8_t channelId, ShockerCommandType type, uint8_t intensity) -{ - Rmt::RmtSequence sequence(44); - - if (!fillSequence(sequence.payload(), shockerId, channelId, type, intensity)) return {}; - if (!fillSequence(sequence.terminator(), shockerId, channelId, ShockerCommandType::Vibrate, 0)) return {}; - - return sequence; -} diff --git a/src/radio/rmt/MainEncoder.cpp b/src/radio/rmt/MainEncoder.cpp index ee4895be..79cafd50 100644 --- a/src/radio/rmt/MainEncoder.cpp +++ b/src/radio/rmt/MainEncoder.cpp @@ -6,20 +6,46 @@ const char* const TAG = "RmtMainEncoder"; #include "radio/rmt/CaiXianlinEncoder.h" #include "radio/rmt/Petrainer998DREncoder.h" #include "radio/rmt/PetrainerEncoder.h" +#include "radio/rmt/T330Encoder.h" using namespace OpenShock; -Rmt::RmtSequence Rmt::GetSequence(ShockerModelType model, uint16_t shockerId, ShockerCommandType type, uint8_t intensity) +static size_t getSequenceBufferSize(ShockerModelType shockerModelType) { - switch (model) { - case ShockerModelType::Petrainer: - return Rmt::PetrainerEncoder::GetSequence(shockerId, type, intensity); + switch (shockerModelType) { + case ShockerModelType::CaiXianlin: + return Rmt::CaiXianlinEncoder::GetBufferSize(); case ShockerModelType::Petrainer998DR: - return Rmt::Petrainer998DREncoder::GetSequence(shockerId, type, intensity); + return Rmt::Petrainer998DREncoder::GetBufferSize(); + case ShockerModelType::Petrainer: + return Rmt::PetrainerEncoder::GetBufferSize(); + default: + return 0; + } +} + +Rmt::MainEncoder::MainEncoder(ShockerModelType shockerModel, uint16_t shockerId) + : m_data(nullptr) + , m_size(getSequenceBufferSize(shockerModel)) + , m_shockerModel(shockerModel) + , m_shockerId(shockerId) +{ + if (m_size > 0) { + m_data = reinterpret_cast(malloc(m_size * 2 * sizeof(rmt_data_t))); + } +} + +bool Rmt::MainEncoder::fillSequence(ShockerCommandType commandType, uint8_t intensity) +{ + switch (m_shockerModel) { case ShockerModelType::CaiXianlin: - return Rmt::CaiXianlinEncoder::GetSequence(shockerId, 0, type, intensity); + return Rmt::CaiXianlinEncoder::FillBuffer(m_data, m_shockerId, 0, commandType, intensity) && Rmt::CaiXianlinEncoder::FillBuffer(m_data + m_size, m_shockerId, 0, ShockerCommandType::Vibrate, 0); + case ShockerModelType::Petrainer: + return Rmt::PetrainerEncoder::FillBuffer(m_data, m_shockerId, commandType, intensity) && Rmt::PetrainerEncoder::FillBuffer(m_data + m_size, m_shockerId, ShockerCommandType::Vibrate, 0); + case ShockerModelType::Petrainer998DR: + return Rmt::Petrainer998DREncoder::FillBuffer(m_data, m_shockerId, commandType, intensity) && Rmt::Petrainer998DREncoder::FillBuffer(m_data + m_size, m_shockerId, ShockerCommandType::Vibrate, 0); default: - OS_LOGE(TAG, "Unknown shocker model: %u", model); - return {}; + OS_LOGE(TAG, "Unknown shocker model: %u", m_shockerModel); + return false; } } diff --git a/src/radio/rmt/Petrainer998DREncoder.cpp b/src/radio/rmt/Petrainer998DREncoder.cpp index 8e830788..2572bb8b 100644 --- a/src/radio/rmt/Petrainer998DREncoder.cpp +++ b/src/radio/rmt/Petrainer998DREncoder.cpp @@ -4,6 +4,8 @@ #include "Checksum.h" +#include + const rmt_data_t kRmtPreamble = {1500, 1, 750, 0}; const rmt_data_t kRmtOne = {750, 1, 250, 0}; const rmt_data_t kRmtZero = {250, 1, 750, 0}; @@ -11,7 +13,12 @@ const rmt_data_t kRmtPostamble = {250, 1, 3750, 0}; // Some subvariants expect using namespace OpenShock; -static bool fillSequence(rmt_data_t* sequence, uint16_t shockerId, ShockerCommandType type, uint8_t intensity) +size_t Rmt::Petrainer998DREncoder::GetBufferSize() +{ + return 42; +} + +bool Rmt::Petrainer998DREncoder::FillBuffer(rmt_data_t* sequence, uint16_t shockerId, ShockerCommandType type, uint8_t intensity) { // Intensity must be between 0 and 100 intensity = std::min(intensity, static_cast(100)); @@ -52,13 +59,3 @@ static bool fillSequence(rmt_data_t* sequence, uint16_t shockerId, ShockerComman return true; } - -Rmt::RmtSequence Rmt::Petrainer998DREncoder::GetSequence(uint16_t shockerId, ShockerCommandType type, uint8_t intensity) -{ - Rmt::RmtSequence sequence(42); - - if (!fillSequence(sequence.payload(), shockerId, type, intensity)) return {}; - if (!fillSequence(sequence.terminator(), shockerId, ShockerCommandType::Vibrate, 0)) return {}; - - return sequence; -} diff --git a/src/radio/rmt/PetrainerEncoder.cpp b/src/radio/rmt/PetrainerEncoder.cpp index 85e7572c..64afa549 100644 --- a/src/radio/rmt/PetrainerEncoder.cpp +++ b/src/radio/rmt/PetrainerEncoder.cpp @@ -2,6 +2,8 @@ #include "radio/rmt/internal/Shared.h" +#include + const rmt_data_t kRmtPreamble = {750, 1, 750, 0}; const rmt_data_t kRmtOne = {200, 1, 1500, 0}; const rmt_data_t kRmtZero = {200, 1, 750, 0}; @@ -9,7 +11,12 @@ const rmt_data_t kRmtPostamble = {200, 1, 7000, 0}; using namespace OpenShock; -static bool fillSequence(rmt_data_t* sequence, uint16_t shockerId, ShockerCommandType type, uint8_t intensity) +size_t Rmt::PetrainerEncoder::GetBufferSize() +{ + return 42; +} + +bool Rmt::PetrainerEncoder::FillBuffer(rmt_data_t* sequence, uint16_t shockerId, ShockerCommandType type, uint8_t intensity) { // Intensity must be between 0 and 100 intensity = std::min(intensity, static_cast(100)); @@ -45,13 +52,3 @@ static bool fillSequence(rmt_data_t* sequence, uint16_t shockerId, ShockerComman return true; } - -Rmt::RmtSequence Rmt::PetrainerEncoder::GetSequence(uint16_t shockerId, ShockerCommandType type, uint8_t intensity) -{ - Rmt::RmtSequence sequence(42); - - if (!fillSequence(sequence.payload(), shockerId, type, intensity)) return {}; - if (!fillSequence(sequence.terminator(), shockerId, ShockerCommandType::Vibrate, 0)) return {}; - - return sequence; -} diff --git a/src/radio/rmt/T330Encoder.cpp b/src/radio/rmt/T330Encoder.cpp index 1a7b4b4c..e361e73e 100644 --- a/src/radio/rmt/T330Encoder.cpp +++ b/src/radio/rmt/T330Encoder.cpp @@ -2,6 +2,8 @@ #include "radio/rmt/internal/Shared.h" +#include + const rmt_data_t kRmtPreamble = {960, 1, 790, 0}; const rmt_data_t kRmtOne = {220, 1, 980, 0}; const rmt_data_t kRmtZero = {220, 1, 580, 0}; @@ -9,7 +11,12 @@ const rmt_data_t kRmtPostamble = {220, 1, 135, 0}; using namespace OpenShock; -static bool fillSequence(rmt_data_t* sequence, uint16_t shockerId, ShockerCommandType type, uint8_t intensity) +size_t Rmt::T330Encoder::GetBufferSize() +{ + return 43; +} + +bool Rmt::T330Encoder::FillBuffer(rmt_data_t* sequence, uint16_t shockerId, ShockerCommandType type, uint8_t intensity) { // Intensity must be between 0 and 100 intensity = std::min(intensity, static_cast(100)); @@ -46,13 +53,3 @@ static bool fillSequence(rmt_data_t* sequence, uint16_t shockerId, ShockerComman return true; } - -Rmt::RmtSequence Rmt::T330Encoder::GetSequence(uint16_t shockerId, ShockerCommandType type, uint8_t intensity) -{ - Rmt::RmtSequence sequence(43); - - if (!fillSequence(sequence.payload(), shockerId, type, intensity)) return {}; - if (!fillSequence(sequence.terminator(), shockerId, ShockerCommandType::Vibrate, 0)) return {}; - - return sequence; -}