Skip to content

Commit

Permalink
Encoder for T330 collar (#322)
Browse files Browse the repository at this point in the history
  • Loading branch information
Puff-Machine authored Nov 25, 2024
1 parent bd9be52 commit 645fe33
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
12 changes: 12 additions & 0 deletions include/radio/rmt/T330Encoder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include "ShockerCommandType.h"

#include <esp32-hal-rmt.h>

#include <cstdint>
#include <vector>

namespace OpenShock::Rmt::T330Encoder {
std::vector<rmt_data_t> GetSequence(uint16_t transmitterId, OpenShock::ShockerCommandType type, uint8_t intensity);
}
50 changes: 50 additions & 0 deletions src/radio/rmt/T330Encoder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "radio/rmt/T330Encoder.h"

#include "radio/rmt/internal/Shared.h"

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};
const rmt_data_t kRmtPostamble = {220, 1, 135, 0};

using namespace OpenShock;

std::vector<rmt_data_t> Rmt::T330Encoder::GetSequence(uint16_t transmitterId, ShockerCommandType type, uint8_t intensity)
{
// Intensity must be between 0 and 100
intensity = std::min(intensity, static_cast<uint8_t>(100));

uint8_t typeVal = 0;
switch (type) {
case ShockerCommandType::Shock:
typeVal = 0b01100001;
break;
case ShockerCommandType::Vibrate:
typeVal = 0b01110010;
break;
case ShockerCommandType::Sound:
typeVal = 0b10000100;
intensity = 0; // The remote always sends 0, I don't know what happens if you send something else.
break;
default:
return {}; // Invalid type
}

uint8_t channelId = 0; // CH1 is 0b0000 and CH2 is 0b1110 on my remote but other values probably work.

// Payload layout: [channelId:4][typeU:4][transmitterId:16][intensity:8][typeL:4][channelId:4]
uint64_t data = (static_cast<uint64_t>(channelId & 0xF) << 36) | (static_cast<uint64_t>(typeVal & 0xF0) << 28) | (static_cast<uint64_t>(transmitterId) << 16) | (static_cast<uint64_t>(intensity) << 8) | (static_cast<uint64_t>(typeVal & 0xF) << 4) | static_cast<uint64_t>(channelId & 0xF);

// Shift the data left by 1 bit to append a zero
data <<= 1;

std::vector<rmt_data_t> pulses;
pulses.reserve(43);

// Generate the sequence
pulses.push_back(kRmtPreamble);
Internal::EncodeBits<41>(pulses, data, kRmtOne, kRmtZero);
pulses.push_back(kRmtPostamble);

return pulses;
}

1 comment on commit 645fe33

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cpp-Linter Report ⚠️

Some files did not pass the configured checks!

clang-format (v18.1.8) reports: 1 file(s) not formatted
  • src/radio/rmt/T330Encoder.cpp

Have any feedback or feature suggestions? Share it here.

Please sign in to comment.