From 1729acd9b72369af45ee5ed45a8d29642f3df367 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Tue, 6 Jun 2017 09:11:22 +0200 Subject: wip o Dropping Samsung-specific code, will be replaces by a small script. o Starting on some unit test code. --- include/script_decoder.h | 84 ++++++++++++++++++++++++++++++------------------ 1 file changed, 53 insertions(+), 31 deletions(-) (limited to 'include/script_decoder.h') 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; } }; -- cgit v1.2.3