diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2017-06-06 09:11:22 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2017-06-06 09:11:22 +0200 |
commit | 1729acd9b72369af45ee5ed45a8d29642f3df367 (patch) | |
tree | 652f3f679b27287c9be6d3ea0676188f87fa5f33 /include | |
parent | 4ee01dab6105e346b731a13321a3e6a5d111d3e4 (diff) | |
download | radio-controller-1729acd9b72369af45ee5ed45a8d29642f3df367.tar.gz radio-controller-1729acd9b72369af45ee5ed45a8d29642f3df367.tar.bz2 radio-controller-1729acd9b72369af45ee5ed45a8d29642f3df367.tar.xz radio-controller-1729acd9b72369af45ee5ed45a8d29642f3df367.zip |
wip
o Dropping Samsung-specific code, will be replaces by a small script.
o Starting on some unit test code.
Diffstat (limited to 'include')
-rw-r--r-- | include/decoder.h | 13 | ||||
-rw-r--r-- | include/samsung_decoder.h | 93 | ||||
-rw-r--r-- | include/script_decoder.h | 84 |
3 files changed, 62 insertions, 128 deletions
diff --git a/include/decoder.h b/include/decoder.h index 4ae723c..a9b4217 100644 --- a/include/decoder.h +++ b/include/decoder.h @@ -109,17 +109,22 @@ class decoding_result { public: decoding_state state; bit_string data; + int field1; + int field2; + int field3; - decoding_result(decoding_state state) : state(state), data() + decoding_result() : state(decoding_state::OK), data(), field1(0), field2(0), field3(0) {} - decoding_result(decoding_state state, bit_string data) : state(state), data(data) - {} + void fail() + { + state = decoding_state::FAIL; + } }; class decoder { public: - virtual decoding_result decode(sample_iterator *it) = 0; + virtual decoding_result decode(sample_iterator &it) = 0; }; } // namespace radio_controller diff --git a/include/samsung_decoder.h b/include/samsung_decoder.h deleted file mode 100644 index 3e55a94..0000000 --- a/include/samsung_decoder.h +++ /dev/null @@ -1,93 +0,0 @@ -#pragma - -#include <cstdint> -#include <cstdio> -#include "decoder.h" - -namespace radio_controller { - -class samsung_decoder : public decoder { - - inline - bool between(uint16_t smallest, uint16_t biggest, uint16_t value) - { - return smallest <= value && value <= biggest; - } - - template<uint16_t PeriodTime, uint16_t PulseTime> - bool check_pulse(sample s) - { -// printf("between(%d, %d, %d) && between(%d, %d, %d)\n", -// int(PeriodTime * 0.8), int(PeriodTime * 1.2), s.period_us, -// int(PulseTime * 0.8), int(PulseTime * 1.2), s.pulse_us); - - return between(uint16_t(PeriodTime * 0.8), uint16_t(PeriodTime * 1.2), s.period_us) && - between(uint16_t(PulseTime * 0.8), uint16_t(PulseTime * 1.2), s.pulse_us); - } - - inline - bool start_bit(sample s) - { - return check_pulse<9000, 4500>(s); - } - - inline - bool one_bit(sample s) - { - return check_pulse<2250, 560>(s); - } - - inline - bool zero_bit(sample s) - { - return check_pulse<1125, 560>(s); - } - - void dump_values(sample_iterator *it) - { - while (it->next()) { - auto s = it->value(); - auto pct = int(s.pulse_us / double(s.period_us) * 100); - auto type = start_bit(s) ? "start" : one_bit(s) ? "1" : zero_bit(s) ? "0" : "?"; - printf("% 5d us % 5d us, %s\n", s.period_us, s.pulse_us, type); - } - } - -public: - - decoding_result decode(sample_iterator *it) override - { - return decoding_result{decoding_state::FAIL}; -// printf("Samsung, size=%d\n", it->size()); -// -// dump_values(it); -// it->reset(); -// -// sample s{}; -// bit_string data; -// -// if (!it->next()) { -// return {decoding_state::TOO_SHORT}; -// } -// s = it->value(); -// if (!start_bit(s)) { -// return {decoding_state::BAD_START}; -// } -// -// for (int i = 0; i < 32 && it->next(); i++) { -// s = it->value(); -// -// if (one_bit(s)) { -// data.append(true); -// } else if (zero_bit(s)) { -// data.append(false); -// } else { -// return {decoding_state::SHORT_BODY}; -// } -// } -// -// return {decoding_state::OK, data}; - } -}; - -} // namespace radio_controller diff --git a/include/script_decoder.h b/include/script_decoder.h index c3ad281..f60a3a4 100644 --- a/include/script_decoder.h +++ b/include/script_decoder.h @@ -6,21 +6,14 @@ namespace radio_controller { -enum class token_type { - START, - DATA, - SPACE, -}; - enum class instruction_return { CONTINUE, FAIL, - END, }; class instruction { public: - virtual instruction_return execute(sample s, bit_string &data) const = 0; + virtual instruction_return execute(sample_iterator &it, decoding_result &res) const = 0; }; class start : public instruction { @@ -31,13 +24,49 @@ public: start(uint16_t period_us, uint16_t pulse_us) noexcept: period_us_(period_us), pulse_us_(pulse_us) {} - instruction_return execute(sample s, bit_string &data) const override + instruction_return execute(sample_iterator &it, decoding_result &res) const override { + auto s = it.value(); return decoder_ns::check_pulse(s, period_us_, pulse_us_) ? instruction_return::CONTINUE : instruction_return::FAIL; } }; +class nop : public instruction { +public: + instruction_return execute(sample_iterator &it, decoding_result &res) const override + { + return instruction_return::CONTINUE; + } +}; + +class set_field : public instruction { + const int field_no; + const int bit_offset; + const int bit_count; + +public: + set_field(const int field_no, const int bit_offset, const int bit_count) noexcept : + field_no(field_no), bit_offset(bit_offset), bit_count(bit_count) + {} + + instruction_return execute(sample_iterator &it, decoding_result &res) const override + { + auto value = int(res.data.extract_bits(bit_offset, bit_count)); + + if (field_no == 1) { + res.field1 = value; + } else if (field_no == 2) { + res.field2 = value; + } else if (field_no == 3) { + res.field3 = value; + } else { + return instruction_return::FAIL; + } + return instruction_return::CONTINUE; + } +}; + class data : public instruction { const uint16_t one_period_us_; const uint16_t one_pulse_us_; @@ -50,15 +79,16 @@ public: zero_period_us_(zero_period_us), zero_pulse_us_(zero_pulse_us) {} - instruction_return execute(sample s, bit_string &data) const override + instruction_return execute(sample_iterator &it, decoding_result &res) const override { + auto s = it.value(); if (decoder_ns::check_pulse(s, one_period_us_, one_pulse_us_)) { - data.append(true); + res.data.append(true); return instruction_return::CONTINUE; } if (decoder_ns::check_pulse(s, zero_period_us_, zero_pulse_us_)) { - data.append(false); + res.data.append(false); return instruction_return::CONTINUE; } @@ -66,14 +96,6 @@ public: } }; -class end : public instruction { -public: - instruction_return execute(sample s, bit_string &data) const override - { - return instruction_return::END; - } -}; - class script_decoder : public decoder { const instruction *const *const instructions_; @@ -83,27 +105,27 @@ public: script_decoder(const instruction *const *const instructions, int count) : instructions_(instructions), count_(count) {} - decoding_result decode(sample_iterator *it) override + decoding_result decode(sample_iterator &it) override { - printf("Script, size=%d\n", it->size()); - bit_string data; + printf("Script, size=%d\n", it.size()); - for (int i = 0; i < count_ && it->next(); i++) { - sample s = it->value(); -// printf("script: period=%d, pulse=%d\n", s.period_us, s.pulse_us); + decoding_result res; + + for (int i = 0; i < count_ && it.next(); i++) { + sample s = it.value(); auto &inst = instructions_[i]; - switch (inst->execute(s, data)) { + + switch (inst->execute(it, res)) { case instruction_return::CONTINUE: break; - case instruction_return::END: - return decoding_result{decoding_state::OK, data}; case instruction_return::FAIL: - return decoding_result{decoding_state::FAIL}; + res.fail(); + return res; } } - return {decoding_state::OK, data}; + return res; } }; |