summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2017-06-04 10:00:57 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2017-06-04 10:00:57 +0200
commita484b095fb03313f5f192b23c90d04972ca3b957 (patch)
tree3f965869157a7901bc9c92e0dc0727e9ef8d5552 /src
parent0bc2a81c0aab3c89b534415d6f07d07e392260ce (diff)
downloadradio-controller-a484b095fb03313f5f192b23c90d04972ca3b957.tar.gz
radio-controller-a484b095fb03313f5f192b23c90d04972ca3b957.tar.bz2
radio-controller-a484b095fb03313f5f192b23c90d04972ca3b957.tar.xz
radio-controller-a484b095fb03313f5f192b23c90d04972ca3b957.zip
o Getting closer to a working decoder.
Diffstat (limited to 'src')
-rw-r--r--src/radio-controller.cpp134
1 files changed, 92 insertions, 42 deletions
diff --git a/src/radio-controller.cpp b/src/radio-controller.cpp
index 8b350d2..d31f5b7 100644
--- a/src/radio-controller.cpp
+++ b/src/radio-controller.cpp
@@ -5,9 +5,13 @@
#include "mcu/arm/semihosting.h"
#include "mcu/stm32cube/uart.h"
#include "mcu/stm32cube/debug.h"
+#include "mcu/util.h"
#include "misc.h"
+#include "decoder.h"
+#include "samsung_decoder.h"
using mcu::arm::mutex;
+using namespace radio_controller;
extern IWDG_HandleTypeDef hiwdg;
extern TIM_HandleTypeDef htim1;
@@ -17,9 +21,36 @@ extern UART_HandleTypeDef huart2;
mcu::stm32cube::uart::uart_port uart2(&huart2);
mcu::stm32cube::debug::dbg<100> dbg(uart2);
-struct sample {
- uint16_t period_us;
- uint16_t pulse_us;
+template<typename T>
+class buffer_iterator : public iterator<T> {
+ const T *values_;
+ const int size_;
+ int idx_;
+
+public:
+ buffer_iterator(const T *values, int size) : values_(values), size_(size), idx_(-1)
+ {};
+
+ __noinline
+ bool next() override
+ {
+ if (has_next()) {
+ idx_++;
+ return true;
+ }
+ return false;
+ }
+
+ inline
+ bool has_next() const override
+ {
+ return idx_ < size_ - 1;
+ };
+
+ const T &value() const override
+ {
+ return values_[idx_];
+ }
};
template<unsigned int BufferSize, typename T>
@@ -28,6 +59,7 @@ class buffer {
volatile unsigned int size_;
+ uint32_t start_;
bool first_;
int part_;
@@ -37,6 +69,7 @@ public:
size_ = 0;
first_ = true;
part_ = 1;
+ start_ = 0;
}
bool check_first_part()
@@ -74,12 +107,22 @@ public:
}
__always_inline
+ uint32_t start() const
+ {
+ return start_;
+ }
+
+ __always_inline
void append(T sample)
{
if (size_ == BufferSize) {
return;
}
+ if (size_ == 0) {
+ start_ = HAL_GetTick();
+ }
+
samples[size_++] = sample;
}
@@ -88,6 +131,11 @@ public:
{
return samples[i];
}
+
+ buffer_iterator<T> iterator()
+ {
+ return buffer_iterator<T>(this->samples, this->size_);
+ }
};
template<size_t BufferSize>
@@ -96,7 +144,7 @@ using sample_buffer = buffer<BufferSize, sample>;
sample_buffer<10> radio_buffer;
mutex radio_buffer_lock;
-sample_buffer<30> ir_buffer;
+sample_buffer<100> ir_buffer;
buffer<100, uint16_t> ir_level_buffer;
mutex ir_buffer_lock;
@@ -137,8 +185,6 @@ void main_post_init()
static uint32_t tick_next = 0;
-//static bool seen_high = false;
-
void main_loop()
{
auto now = HAL_GetTick();
@@ -150,7 +196,6 @@ void main_loop()
// CDC_Transmit_FS(const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(str)), 5);
tick_next += 1000;
-// seen_high = false;
}
if (radio_buffer.is_full()) {
@@ -174,27 +219,20 @@ void main_loop()
radio_buffer_lock.unlock();
}
- /*
- if (ir_buffer.is_full()) {
+ if (ir_buffer.is_full() || (!ir_buffer.is_empty() && now > ir_buffer.start() + 100)) {
ir_buffer_lock.lock();
- hal_ok(HAL_TIM_IC_Stop_IT(&htim2, TIM_CHANNEL_1));
- hal_ok(HAL_TIM_IC_Stop_IT(&htim2, TIM_CHANNEL_2));
- dbg.println("IR");
- for (unsigned int i = 0; i < ir_buffer.size(); i++) {
- sample s = ir_buffer.at(i);
- auto pulse = s.pulse_us;
- auto period = s.period_us;
- dbg.println("% 5d us % 5d us, %.02d%%", period, pulse, int(pulse / double(period) * 100));
- }
- dbg.println();
+ HAL_NVIC_DisableIRQ(TIM2_IRQn);
+
+ samsung_decoder d;
+ auto it = ir_buffer.iterator();
+ d.decode(&it);
ir_buffer.reset();
- hal_ok(HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_1));
- hal_ok(HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_2));
ir_buffer_lock.unlock();
+
+ HAL_NVIC_EnableIRQ(TIM2_IRQn);
}
- */
if (ir_level_buffer.is_full()) {
ir_buffer_lock.lock();
@@ -213,50 +251,61 @@ void main_loop()
HAL_NVIC_EnableIRQ(TIM2_IRQn);
}
-// seen_high |= HAL_GPIO_ReadPin(RADIO_RX_GPIO_Port, RADIO_RX_Pin);
-
- HAL_IWDG_Refresh(&hiwdg);
+ __HAL_IWDG_RELOAD_COUNTER(&hiwdg);
}
-//static uint16_t start = 0;
-
-uint16_t ir_value1, ir_value2;
-
static
-void ir_rx(TIM_HandleTypeDef *htim)
+void ir_rx_level(TIM_HandleTypeDef *htim)
{
+ static uint16_t ir_value1, ir_value2;
+
if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1) {
+ ir_value1 = static_cast<uint16_t>(htim->Instance->CCR1);
debug_pin(2);
- }
- if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2) {
+ } else if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2) {
+ ir_value2 = static_cast<uint16_t>(htim->Instance->CCR2);
debug_pin(3);
}
+ if (!ir_level_buffer.check_first()) {
+ if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1) {
+ ir_level_buffer.append(values::to_us<uint16_t>(ir_value1 - ir_value2));
+ } else if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2) {
+ ir_level_buffer.append(values::to_us<uint16_t>(ir_value2));
+ }
+ }
+}
+
+static
+void ir_rx(TIM_HandleTypeDef *htim)
+{
+ static uint16_t ir_value1, ir_value2;
+
if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1) {
ir_value1 = static_cast<uint16_t>(htim->Instance->CCR1);
+ debug_pin(2);
} else if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2) {
ir_value2 = static_cast<uint16_t>(htim->Instance->CCR2);
+ debug_pin(3);
}
- if (ir_level_buffer.check_first()) {
+ if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1 && ir_buffer.check_first()) {
+// debug_pin(10);
return;
}
if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1) {
- ir_level_buffer.append(values::to_us<uint16_t>(ir_value1 - ir_value2));
- } else if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2) {
- ir_level_buffer.append(values::to_us<uint16_t>(ir_value2));
- }
-
- /*
- if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1 && !ir_buffer.check_first()) {
if (ir_buffer_lock.try_lock()) {
- ir_buffer.append({values::to_us<uint16_t>(ir_value1), values::to_us<uint16_t>(ir_value2)});
+ auto period = values::to_us<uint16_t>(ir_value1);
+ auto pulse = values::to_us<uint16_t>(ir_value2);
+ if (ir_buffer.size() == 0) {
+ debug_pin(5);
+ }
+ ir_buffer.append({.period_us=period, .pulse_us=pulse});
ir_buffer_lock.unlock();
}
}
- */
}
static
@@ -302,5 +351,6 @@ void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
}
if (htim == &htim2) {
ir_rx(htim);
+// ir_rx_level(htim);
}
}