From 094c977b8877d652f629260cc753aacc7000e328 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 4 Jun 2017 17:39:52 +0200 Subject: o Daewoo -> Samsung. --- CMakeLists.txt | 7 +++- include/daewoo_decoder.h | 91 ---------------------------------------------- include/decoder.h | 6 ++-- include/samsung_decoder.h | 92 +++++++++++++++++++++++++++++++++++++++++++++++ src/radio-controller.cpp | 7 ++-- 5 files changed, 106 insertions(+), 97 deletions(-) delete mode 100644 include/daewoo_decoder.h create mode 100644 include/samsung_decoder.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 307f18d..52c9c42 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,7 +9,12 @@ set(CMAKE_CXX_STANDARD 14) include(thirdparty/mcucpp/cmake/mcucpp.cmake) -add_executable(firmware src/radio-controller.cpp include/radio-controller.h include) +add_executable(firmware src/radio-controller.cpp + include/decoder.h + include/misc.h + include/radio-controller.h + include/samsung_decoder.h + include) target_compile_definitions(firmware PUBLIC MCUCMAKE_USING_STM32CUBE=1) target_include_directories(firmware PUBLIC include) target_link_libraries(firmware PUBLIC gcc) diff --git a/include/daewoo_decoder.h b/include/daewoo_decoder.h deleted file mode 100644 index b790a1a..0000000 --- a/include/daewoo_decoder.h +++ /dev/null @@ -1,91 +0,0 @@ -#pragma - -#include "decoder.h" - -namespace radio_controller { - -class daewoo_decoder : public decoder { - - inline - bool between(uint16_t smallest, uint16_t biggest, uint16_t value) - { - return smallest <= value && value <= biggest; - } - - template - 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); - } - - __noinline - 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: - - __noinline - decoding_result decode(sample_iterator *it) override - { - printf("Daewoo, 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/decoder.h b/include/decoder.h index 55e7d96..56ea647 100644 --- a/include/decoder.h +++ b/include/decoder.h @@ -1,5 +1,7 @@ #pragma once +#include + namespace radio_controller { struct sample { @@ -26,7 +28,8 @@ class bit_string final { int size_ = 0; public: - int size() const { + int size() const + { return size_; } @@ -42,7 +45,6 @@ public: } } - __noinline void append(bool value) { if (size_ < 32) { diff --git a/include/samsung_decoder.h b/include/samsung_decoder.h new file mode 100644 index 0000000..52978ec --- /dev/null +++ b/include/samsung_decoder.h @@ -0,0 +1,92 @@ +#pragma + +#include +#include +#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 + 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 + { + 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/src/radio-controller.cpp b/src/radio-controller.cpp index 8d3149c..2c038ed 100644 --- a/src/radio-controller.cpp +++ b/src/radio-controller.cpp @@ -1,3 +1,6 @@ +#include "decoder.h" +#include "samsung_decoder.h" + #include #include #include "radio-controller.h" @@ -7,8 +10,6 @@ #include "mcu/stm32cube/debug.h" #include "mcu/util.h" #include "misc.h" -#include "decoder.h" -#include "daewoo_decoder.h" using mcu::arm::mutex; using namespace radio_controller; @@ -233,7 +234,7 @@ void main_loop() HAL_NVIC_DisableIRQ(TIM2_IRQn); - daewoo_decoder d; + samsung_decoder d; auto it = ir_buffer.iterator(); auto result = d.decode(&it); -- cgit v1.2.3