Skip to content

Commit

Permalink
Decrease allocations for RF encoding + transmitting
Browse files Browse the repository at this point in the history
  • Loading branch information
hhvrc committed Jan 20, 2025
1 parent 53ea3f8 commit 4793d97
Show file tree
Hide file tree
Showing 13 changed files with 186 additions and 188 deletions.
2 changes: 1 addition & 1 deletion include/ShockerCommandType.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <cstring>

namespace OpenShock {
enum class ShockerCommandType {
enum class ShockerCommandType : uint8_t {
Stop,
Shock,
Vibrate,
Expand Down
6 changes: 4 additions & 2 deletions include/radio/rmt/CaiXianlinEncoder.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#pragma once

#include "RmtSequence.h"
#include "ShockerCommandType.h"

#include <esp32-hal-rmt.h>

#include <cstdint>

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);
}
63 changes: 61 additions & 2 deletions include/radio/rmt/MainEncoder.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,70 @@
#pragma once

#include "RmtSequence.h"
#include "Common.h"
#include "ShockerCommandType.h"
#include "ShockerModelType.h"

#include <esp32-hal-rmt.h>

#include <cstdint>

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
6 changes: 4 additions & 2 deletions include/radio/rmt/Petrainer998DREncoder.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#pragma once

#include "RmtSequence.h"
#include "ShockerCommandType.h"

#include <esp32-hal-rmt.h>

#include <cstdint>

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);
}
6 changes: 4 additions & 2 deletions include/radio/rmt/PetrainerEncoder.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#pragma once

#include "RmtSequence.h"
#include "ShockerCommandType.h"

#include <esp32-hal-rmt.h>

#include <cstdint>

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);
}
58 changes: 0 additions & 58 deletions include/radio/rmt/RmtSequence.h

This file was deleted.

6 changes: 4 additions & 2 deletions include/radio/rmt/T330Encoder.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#pragma once

#include "RmtSequence.h"
#include "ShockerCommandType.h"

#include <esp32-hal-rmt.h>

#include <cstdint>

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

0 comments on commit 4793d97

Please sign in to comment.