Files
IR-protocol/Analyzer/raw/IR_Fox/tests/IrFoxDecoderTests.cpp

116 lines
3.0 KiB
C++

#include "IrFoxDecoder.h"
#include <cassert>
#include <cstdint>
#include <vector>
namespace {
uint8_t crc8(const uint8_t* data, uint8_t end, uint8_t poly)
{
uint8_t crc = 0xFF;
for (uint8_t i = 0; i < end; ++i)
{
crc ^= data[i];
for (uint8_t bit = 0; bit < 8; ++bit)
crc = (crc & 0x80U) ? static_cast<uint8_t>((crc << 1U) ^ poly) : static_cast<uint8_t>(crc << 1U);
}
return crc;
}
struct DecoderHarness
{
IrFoxDecoder decoder;
std::vector<IrFoxEmitPacket> packets;
std::vector<IrFoxEmitBit> events;
uint64_t phase = 0;
static constexpr uint32_t kFs = 1000000U;
DecoderHarness()
{
decoder.reset();
}
void edge(uint64_t sample, bool rising)
{
decoder.processEdge(sample, rising, kFs, [this](const IrFoxEmitBit& event) { events.push_back(event); },
[this](const IrFoxEmitPacket& packet) { packets.push_back(packet); });
}
void lockPreamble()
{
constexpr uint64_t first_rise = 40000;
constexpr uint64_t period = irfox::kBitTimeUs * 3U;
edge(first_rise, true);
edge(first_rise + 700U, false);
edge(first_rise + period, true);
edge(first_rise + period + 700U, false);
edge(first_rise + period * 2U, true);
phase = first_rise + period * 2U + period / 2U;
}
void emitCell(bool bit)
{
// The decoder calls a cell a one when the inactive (HIGH) interval is
// longer than the active (LOW) interval. The waveform is TSOP output.
const uint64_t high_us = bit ? 262U : 700U;
edge(phase + high_us, false);
phase += irfox::kBitTimeUs;
edge(phase, true);
}
void emitByte(uint8_t value, bool emit_sync)
{
for (uint8_t i = 0; i < 8; ++i)
emitCell((value & static_cast<uint8_t>(0x80U >> i)) != 0U);
if (emit_sync)
{
const bool sync = (value & 1U) == 0U;
for (uint8_t i = 0; i < irfox::kSyncBits; ++i)
emitCell(sync);
}
}
};
} // namespace
int main()
{
uint8_t packet[] = {0xE7, 0x00, 0x01, 0x00, 0x2A, 0x00, 0x00};
packet[5] = crc8(packet, 5, irfox::kPoly1);
packet[6] = crc8(packet, 6, irfox::kPoly2);
DecoderHarness valid;
valid.lockPreamble();
for (uint8_t i = 0; i < sizeof packet; ++i)
valid.emitByte(packet[i], i + 1U != sizeof packet);
assert(valid.packets.size() == 1U);
assert(valid.packets[0].crc_ok);
assert(valid.packets[0].pack_size == sizeof packet);
assert(valid.packets[0].start_sample == 40000);
assert(valid.packets[0].start_sample < valid.packets[0].end_sample);
bool saw_preamble = false;
std::vector<uint8_t> decoded_bytes;
for (const IrFoxEmitBit& event : valid.events)
{
if (event.frame_type == IRF_FT_PREAMBLE)
{
saw_preamble = true;
assert(event.start_sample == 40000);
}
if (event.frame_type == IRF_FT_DATA_BYTE)
decoded_bytes.push_back(static_cast<uint8_t>(event.bit_value));
}
assert(saw_preamble);
assert(decoded_bytes.size() == sizeof packet);
for (uint8_t i = 0; i < sizeof packet; ++i)
assert(decoded_bytes[i] == packet[i]);
DecoderHarness too_short;
too_short.lockPreamble();
too_short.emitByte(0xE1, false);
assert(too_short.packets.size() == 1U);
assert(!too_short.packets[0].crc_ok);
assert(too_short.packets[0].pack_size == 1U);
return 0;
}