[FL-1505] Add RAW format (#576)
* Add RAW format * F5 stubs for build to pass * Fix saving decoded signal error * Irda: set ISR before starting timer, remove explicit NVIC configuration Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
77
lib/irda/worker/irda_transmit.c
Normal file
77
lib/irda/worker/irda_transmit.c
Normal file
@@ -0,0 +1,77 @@
|
||||
#include "irda.h"
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <furi.h>
|
||||
#include <api-hal-irda.h>
|
||||
#include <api-hal-delay.h>
|
||||
|
||||
#define IRDA_SET_TX_COMMON(d, l) irda_set_tx((d), (l), IRDA_COMMON_DUTY_CYCLE, IRDA_COMMON_CARRIER_FREQUENCY)
|
||||
|
||||
static void irda_set_tx(uint32_t duration, bool level, float duty_cycle, float frequency) {
|
||||
if (level) {
|
||||
api_hal_irda_pwm_set(duty_cycle, frequency);
|
||||
delay_us(duration);
|
||||
} else {
|
||||
api_hal_irda_pwm_stop();
|
||||
delay_us(duration);
|
||||
}
|
||||
}
|
||||
|
||||
void irda_send_raw_ext(const uint32_t timings[], uint32_t timings_cnt, bool start_from_mark, float duty_cycle, float frequency) {
|
||||
__disable_irq();
|
||||
for (uint32_t i = 0; i < timings_cnt; ++i) {
|
||||
irda_set_tx(timings[i], (i % 2) ^ start_from_mark, duty_cycle, frequency);
|
||||
}
|
||||
IRDA_SET_TX_COMMON(0, false);
|
||||
__enable_irq();
|
||||
}
|
||||
|
||||
void irda_send_raw(const uint32_t timings[], uint32_t timings_cnt, bool start_from_mark) {
|
||||
__disable_irq();
|
||||
for (uint32_t i = 0; i < timings_cnt; ++i) {
|
||||
IRDA_SET_TX_COMMON(timings[i], (i % 2) ^ start_from_mark);
|
||||
}
|
||||
IRDA_SET_TX_COMMON(0, false);
|
||||
__enable_irq();
|
||||
}
|
||||
|
||||
void irda_send(const IrdaMessage* message, int times) {
|
||||
furi_assert(message);
|
||||
furi_assert(irda_is_protocol_valid(message->protocol));
|
||||
|
||||
IrdaStatus status;
|
||||
uint32_t duration = 0;
|
||||
bool level = false;
|
||||
IrdaEncoderHandler* handler = irda_alloc_encoder();
|
||||
irda_reset_encoder(handler, message);
|
||||
|
||||
/* Hotfix: first timings is space timing, so make delay instead of locking
|
||||
* whole system for that long. Replace when async timing lib will be ready.
|
||||
* This timing doesn't have to be precise.
|
||||
*/
|
||||
status = irda_encode(handler, &duration, &level);
|
||||
furi_assert(status != IrdaStatusError);
|
||||
furi_assert(level == false);
|
||||
delay_us(duration);
|
||||
|
||||
__disable_irq();
|
||||
|
||||
while (times) {
|
||||
status = irda_encode(handler, &duration, &level);
|
||||
if (status != IrdaStatusError) {
|
||||
IRDA_SET_TX_COMMON(duration, level);
|
||||
} else {
|
||||
furi_assert(0);
|
||||
break;
|
||||
}
|
||||
if (status == IrdaStatusDone)
|
||||
--times;
|
||||
}
|
||||
|
||||
IRDA_SET_TX_COMMON(0, false);
|
||||
__enable_irq();
|
||||
|
||||
irda_free_encoder(handler);
|
||||
}
|
||||
|
41
lib/irda/worker/irda_transmit.h
Normal file
41
lib/irda/worker/irda_transmit.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#include <api-hal-irda.h>
|
||||
#include <irda.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Send message over IRDA.
|
||||
*
|
||||
* \param[in] message - message to send.
|
||||
* \param[in] times - number of times message should be sent.
|
||||
*/
|
||||
void irda_send(const IrdaMessage* message, int times);
|
||||
|
||||
/**
|
||||
* Send raw data through infrared port.
|
||||
*
|
||||
* \param[in] timings - array of timings to send.
|
||||
* \param[in] timings_cnt - timings array size.
|
||||
* \param[in] start_from_mark - true if timings starts from mark,
|
||||
* otherwise from space
|
||||
*/
|
||||
void irda_send_raw(const uint32_t timings[], uint32_t timings_cnt, bool start_from_mark);
|
||||
|
||||
/**
|
||||
* Send raw data through infrared port, with additional settings.
|
||||
*
|
||||
* \param[in] timings - array of timings to send.
|
||||
* \param[in] timings_cnt - timings array size.
|
||||
* \param[in] start_from_mark - true if timings starts from mark,
|
||||
* otherwise from space
|
||||
* \param[in] duty_cycle - duty cycle to generate on PWM
|
||||
* \param[in] frequency - frequency to generate on PWM
|
||||
*/
|
||||
void irda_send_raw_ext(const uint32_t timings[], uint32_t timings_cnt, bool start_from_mark, float duty_cycle, float frequency);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
237
lib/irda/worker/irda_worker.c
Normal file
237
lib/irda/worker/irda_worker.c
Normal file
@@ -0,0 +1,237 @@
|
||||
#include "irda_worker.h"
|
||||
#include <irda.h>
|
||||
#include <api-hal-irda.h>
|
||||
#include <limits.h>
|
||||
#include <stdint.h>
|
||||
#include <stream_buffer.h>
|
||||
#include <furi.h>
|
||||
#include <notification/notification-messages.h>
|
||||
|
||||
#define MAX_TIMINGS_AMOUNT 500
|
||||
#define IRDA_WORKER_RX_TIMEOUT 150 // ms
|
||||
#define IRDA_WORKER_RX_RECEIVED 0x01
|
||||
#define IRDA_WORKER_RX_TIMEOUT_RECEIVED 0x02
|
||||
#define IRDA_WORKER_OVERRUN 0x04
|
||||
#define IRDA_WORKER_EXIT 0x08
|
||||
|
||||
struct IrdaWorkerSignal {
|
||||
bool decoded;
|
||||
size_t timings_cnt;
|
||||
union {
|
||||
IrdaMessage message;
|
||||
uint32_t timings[MAX_TIMINGS_AMOUNT];
|
||||
} data;
|
||||
};
|
||||
|
||||
struct IrdaWorker {
|
||||
FuriThread* thread;
|
||||
IrdaDecoderHandler* irda_decoder;
|
||||
StreamBufferHandle_t stream;
|
||||
|
||||
TaskHandle_t worker_handle;
|
||||
IrdaWorkerSignal signal;
|
||||
|
||||
IrdaWorkerReceivedSignalCallback received_signal_callback;
|
||||
void* context;
|
||||
bool blink_enable;
|
||||
bool overrun;
|
||||
NotificationApp* notification;
|
||||
};
|
||||
|
||||
static void irda_worker_rx_timeout_callback(void* context) {
|
||||
IrdaWorker* instance = context;
|
||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||
xTaskNotifyFromISR(instance->worker_handle, IRDA_WORKER_RX_TIMEOUT_RECEIVED, eSetBits, &xHigherPriorityTaskWoken);
|
||||
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
||||
}
|
||||
|
||||
static void irda_worker_rx_callback(void* context, bool level, uint32_t duration) {
|
||||
IrdaWorker* instance = context;
|
||||
|
||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||
LevelDuration level_duration = level_duration_make(level, duration);
|
||||
|
||||
size_t ret =
|
||||
xStreamBufferSendFromISR(instance->stream, &level_duration, sizeof(LevelDuration), &xHigherPriorityTaskWoken);
|
||||
uint32_t notify_value = (ret == sizeof(LevelDuration)) ? IRDA_WORKER_RX_RECEIVED : IRDA_WORKER_OVERRUN;
|
||||
xTaskNotifyFromISR(instance->worker_handle, notify_value, eSetBits, &xHigherPriorityTaskWoken);
|
||||
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
||||
}
|
||||
|
||||
static void irda_worker_process_timeout(IrdaWorker* instance) {
|
||||
if (instance->signal.timings_cnt < 2)
|
||||
return;
|
||||
|
||||
instance->signal.decoded = false;
|
||||
if (instance->received_signal_callback)
|
||||
instance->received_signal_callback(instance->context, &instance->signal);
|
||||
}
|
||||
|
||||
static void irda_worker_process_timings(IrdaWorker* instance, uint32_t duration, bool level) {
|
||||
const IrdaMessage* message_decoded = irda_decode(instance->irda_decoder, level, duration);
|
||||
if (message_decoded) {
|
||||
instance->signal.data.message = *message_decoded;
|
||||
instance->signal.timings_cnt = 0;
|
||||
instance->signal.decoded = true;
|
||||
if (instance->received_signal_callback)
|
||||
instance->received_signal_callback(instance->context, &instance->signal);
|
||||
} else {
|
||||
/* Skip first timing if it's starts from Space */
|
||||
if ((instance->signal.timings_cnt == 0) && !level) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (instance->signal.timings_cnt < MAX_TIMINGS_AMOUNT) {
|
||||
instance->signal.data.timings[instance->signal.timings_cnt] = duration;
|
||||
++instance->signal.timings_cnt;
|
||||
} else {
|
||||
xTaskNotify(instance->worker_handle, IRDA_WORKER_OVERRUN, eSetBits);
|
||||
instance->overrun = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int32_t irda_worker_thread_callback(void* context) {
|
||||
IrdaWorker* instance = context;
|
||||
uint32_t notify_value = 0;
|
||||
LevelDuration level_duration;
|
||||
TickType_t last_blink_time = 0;
|
||||
|
||||
while(1) {
|
||||
BaseType_t result;
|
||||
result = xTaskNotifyWait(pdFALSE, ULONG_MAX, ¬ify_value, 1000);
|
||||
if (result != pdPASS)
|
||||
continue;
|
||||
|
||||
if (notify_value & IRDA_WORKER_RX_RECEIVED) {
|
||||
if (!instance->overrun && instance->blink_enable && ((xTaskGetTickCount() - last_blink_time) > 80)) {
|
||||
last_blink_time = xTaskGetTickCount();
|
||||
notification_message(instance->notification, &sequence_blink_blue_10);
|
||||
}
|
||||
if (instance->signal.timings_cnt == 0)
|
||||
notification_message(instance->notification, &sequence_display_on);
|
||||
while (sizeof(LevelDuration) == xStreamBufferReceive(instance->stream, &level_duration, sizeof(LevelDuration), 0)) {
|
||||
if (!instance->overrun) {
|
||||
bool level = level_duration_get_level(level_duration);
|
||||
uint32_t duration = level_duration_get_duration(level_duration);
|
||||
irda_worker_process_timings(instance, duration, level);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (notify_value & IRDA_WORKER_OVERRUN) {
|
||||
printf("#");
|
||||
irda_reset_decoder(instance->irda_decoder);
|
||||
instance->signal.timings_cnt = 0;
|
||||
if (instance->blink_enable)
|
||||
notification_message(instance->notification, &sequence_set_red_255);
|
||||
}
|
||||
if (notify_value & IRDA_WORKER_RX_TIMEOUT_RECEIVED) {
|
||||
if (instance->overrun) {
|
||||
printf("\nOVERRUN, max samples: %d\n", MAX_TIMINGS_AMOUNT);
|
||||
instance->overrun = false;
|
||||
if (instance->blink_enable)
|
||||
notification_message(instance->notification, &sequence_reset_red);
|
||||
} else {
|
||||
irda_worker_process_timeout(instance);
|
||||
}
|
||||
instance->signal.timings_cnt = 0;
|
||||
}
|
||||
if (notify_value & IRDA_WORKER_EXIT)
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void irda_worker_set_received_signal_callback(IrdaWorker* instance, IrdaWorkerReceivedSignalCallback callback) {
|
||||
furi_assert(instance);
|
||||
instance->received_signal_callback = callback;
|
||||
}
|
||||
|
||||
IrdaWorker* irda_worker_alloc() {
|
||||
IrdaWorker* instance = furi_alloc(sizeof(IrdaWorker));
|
||||
|
||||
instance->thread = furi_thread_alloc();
|
||||
furi_thread_set_name(instance->thread, "irda_worker");
|
||||
furi_thread_set_stack_size(instance->thread, 2048);
|
||||
furi_thread_set_context(instance->thread, instance);
|
||||
furi_thread_set_callback(instance->thread, irda_worker_thread_callback);
|
||||
|
||||
instance->stream = xStreamBufferCreate(sizeof(LevelDuration) * 512, sizeof(LevelDuration));
|
||||
|
||||
instance->irda_decoder = irda_alloc_decoder();
|
||||
instance->blink_enable = false;
|
||||
instance->notification = furi_record_open("notification");
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
void irda_worker_free(IrdaWorker* instance) {
|
||||
furi_assert(instance);
|
||||
furi_assert(!instance->worker_handle);
|
||||
|
||||
furi_record_close("notification");
|
||||
irda_free_decoder(instance->irda_decoder);
|
||||
vStreamBufferDelete(instance->stream);
|
||||
furi_thread_free(instance->thread);
|
||||
|
||||
free(instance);
|
||||
}
|
||||
|
||||
void irda_worker_set_context(IrdaWorker* instance, void* context) {
|
||||
furi_assert(instance);
|
||||
instance->context = context;
|
||||
}
|
||||
|
||||
void irda_worker_start(IrdaWorker* instance) {
|
||||
furi_assert(instance);
|
||||
furi_assert(!instance->worker_handle);
|
||||
|
||||
furi_thread_start(instance->thread);
|
||||
|
||||
instance->worker_handle = furi_thread_get_thread_id(instance->thread);
|
||||
api_hal_irda_rx_irq_init();
|
||||
api_hal_irda_rx_timeout_irq_init(IRDA_WORKER_RX_TIMEOUT);
|
||||
api_hal_irda_rx_irq_set_callback(irda_worker_rx_callback, instance);
|
||||
api_hal_irda_rx_timeout_irq_set_callback(irda_worker_rx_timeout_callback, instance);
|
||||
}
|
||||
|
||||
void irda_worker_stop(IrdaWorker* instance) {
|
||||
furi_assert(instance);
|
||||
furi_assert(instance->worker_handle);
|
||||
|
||||
api_hal_irda_rx_timeout_irq_set_callback(NULL, NULL);
|
||||
api_hal_irda_rx_irq_set_callback(NULL, NULL);
|
||||
api_hal_irda_rx_irq_deinit();
|
||||
|
||||
xTaskNotify(instance->worker_handle, IRDA_WORKER_EXIT, eSetBits);
|
||||
|
||||
instance->worker_handle = NULL;
|
||||
|
||||
furi_thread_join(instance->thread);
|
||||
}
|
||||
|
||||
bool irda_worker_signal_is_decoded(const IrdaWorkerSignal* signal) {
|
||||
furi_assert(signal);
|
||||
return signal->decoded;
|
||||
}
|
||||
|
||||
void irda_worker_get_raw_signal(const IrdaWorkerSignal* signal, const uint32_t** timings, size_t* timings_cnt) {
|
||||
furi_assert(signal);
|
||||
furi_assert(timings);
|
||||
furi_assert(timings_cnt);
|
||||
|
||||
*timings = signal->data.timings;
|
||||
*timings_cnt = signal->timings_cnt;
|
||||
}
|
||||
|
||||
const IrdaMessage* irda_worker_get_decoded_message(const IrdaWorkerSignal* signal) {
|
||||
furi_assert(signal);
|
||||
return &signal->data.message;
|
||||
}
|
||||
|
||||
void irda_worker_enable_blink_on_receiving(IrdaWorker* instance, bool enable) {
|
||||
furi_assert(instance);
|
||||
instance->blink_enable = enable;
|
||||
}
|
||||
|
91
lib/irda/worker/irda_worker.h
Normal file
91
lib/irda/worker/irda_worker.h
Normal file
@@ -0,0 +1,91 @@
|
||||
#pragma once
|
||||
|
||||
#include <irda.h>
|
||||
#include <api-hal.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/** Interface struct of irda worker */
|
||||
typedef struct IrdaWorker IrdaWorker;
|
||||
/** Interface struct of received signal */
|
||||
typedef struct IrdaWorkerSignal IrdaWorkerSignal;
|
||||
|
||||
/** Callback type to call by IrdaWorker thread when new signal is received */
|
||||
typedef void (*IrdaWorkerReceivedSignalCallback)(void* context, IrdaWorkerSignal* received_signal);
|
||||
|
||||
/** Allocate IrdaWorker
|
||||
*
|
||||
* @return just created instance of IrdaWorker
|
||||
*/
|
||||
IrdaWorker* irda_worker_alloc();
|
||||
|
||||
/** Free IrdaWorker
|
||||
*
|
||||
* @param[in] instance - IrdaWorker instance
|
||||
*/
|
||||
void irda_worker_free(IrdaWorker* instance);
|
||||
|
||||
/** Received data callback IrdaWorker
|
||||
*
|
||||
* @param[in] instance - IrdaWorker instance
|
||||
* @param[in] callback - IrdaWorkerReceivedSignalCallback callback
|
||||
*/
|
||||
void irda_worker_set_received_signal_callback(IrdaWorker* instance, IrdaWorkerReceivedSignalCallback callback);
|
||||
|
||||
/** Context callback IrdaWorker
|
||||
*
|
||||
* @param[in] instance - IrdaWorker instance
|
||||
* @param[in] context - context to pass to callbacks
|
||||
*/
|
||||
void irda_worker_set_context(IrdaWorker* instance, void* context);
|
||||
|
||||
/** Start IrdaWorker thread, initialise api-hal, prepare all work.
|
||||
*
|
||||
* @param[in] instance - IrdaWorker instance
|
||||
*/
|
||||
void irda_worker_start(IrdaWorker* instance);
|
||||
|
||||
/** Stop IrdaWorker thread, deinitialize api-hal.
|
||||
*
|
||||
* @param[in] instance - IrdaWorker instance
|
||||
*/
|
||||
void irda_worker_stop(IrdaWorker* instance);
|
||||
|
||||
/** Clarify is received signal either decoded or raw
|
||||
*
|
||||
* @param[in] signal - received signal
|
||||
* @return true if signal is decoded, false if signal is raw
|
||||
*/
|
||||
bool irda_worker_signal_is_decoded(const IrdaWorkerSignal* signal);
|
||||
|
||||
/** Acquire raw signal from interface struct 'IrdaWorkerSignal'.
|
||||
* First, you have to ensure that signal is raw.
|
||||
*
|
||||
* @param[in] signal - received signal
|
||||
* @param[out] timings - pointer to array of timings
|
||||
* @param[out] timings_cnt - pointer to amount of timings
|
||||
*/
|
||||
void irda_worker_get_raw_signal(const IrdaWorkerSignal* signal, const uint32_t** timings, size_t* timings_cnt);
|
||||
|
||||
/** Acquire decoded message from interface struct 'IrdaWorkerSignal'.
|
||||
* First, you have to ensure that signal is decoded.
|
||||
*
|
||||
* @param[in] signal - received signal
|
||||
* @return decoded irda message
|
||||
*/
|
||||
const IrdaMessage* irda_worker_get_decoded_message(const IrdaWorkerSignal* signal);
|
||||
|
||||
/** Enable blinking on receiving any signal on IR port.
|
||||
*
|
||||
* @param[in] instance - instance of IrdaWorker
|
||||
* @param[in] enable - true if you want to enable blinking
|
||||
* false otherwise
|
||||
*/
|
||||
void irda_worker_enable_blink_on_receiving(IrdaWorker* instance, bool enable);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user