[FL-1652, FL-1554] IRDA: Continuous transmitting (#636)
* [FL-1652] IRDA: Continuous transmitting * continuous encoding and sending signals by pressing button on menu * fast buttons scrolling in remote menu * bruteforce: stop reading file if progress == 100% * IRDA: .hpp -> .h * [FL-1554] IRDA: xTaskNotify -> osEventsFlagSet * IRDA: some stability fixes * Irda: minor cleanup, api-hal to furi-hal rename. Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
@@ -34,6 +34,8 @@ typedef struct {
|
||||
IrdaEncoders encoder;
|
||||
uint8_t address_length;
|
||||
uint8_t command_length;
|
||||
uint32_t frequency;
|
||||
float duty_cycle;
|
||||
} IrdaProtocolImplementation;
|
||||
|
||||
struct IrdaEncoderHandler {
|
||||
@@ -58,6 +60,8 @@ static const IrdaProtocolImplementation irda_protocols[] = {
|
||||
.free = irda_encoder_nec_free},
|
||||
.address_length = 2,
|
||||
.command_length = 2,
|
||||
.frequency = IRDA_COMMON_CARRIER_FREQUENCY,
|
||||
.duty_cycle = IRDA_COMMON_DUTY_CYCLE,
|
||||
},
|
||||
// #1 - have to be after NEC
|
||||
{ .protocol = IrdaProtocolNECext,
|
||||
@@ -74,6 +78,8 @@ static const IrdaProtocolImplementation irda_protocols[] = {
|
||||
.free = irda_encoder_nec_free},
|
||||
.address_length = 4,
|
||||
.command_length = 2,
|
||||
.frequency = IRDA_COMMON_CARRIER_FREQUENCY,
|
||||
.duty_cycle = IRDA_COMMON_DUTY_CYCLE,
|
||||
},
|
||||
// #2
|
||||
{ .protocol = IrdaProtocolSamsung32,
|
||||
@@ -90,6 +96,8 @@ static const IrdaProtocolImplementation irda_protocols[] = {
|
||||
.free = irda_encoder_samsung32_free},
|
||||
.address_length = 2,
|
||||
.command_length = 2,
|
||||
.frequency = IRDA_COMMON_CARRIER_FREQUENCY,
|
||||
.duty_cycle = IRDA_COMMON_DUTY_CYCLE,
|
||||
},
|
||||
// #3
|
||||
{ .protocol = IrdaProtocolRC6,
|
||||
@@ -106,6 +114,8 @@ static const IrdaProtocolImplementation irda_protocols[] = {
|
||||
.free = irda_encoder_rc6_free},
|
||||
.address_length = 2,
|
||||
.command_length = 2,
|
||||
.frequency = IRDA_COMMON_CARRIER_FREQUENCY,
|
||||
.duty_cycle = IRDA_COMMON_DUTY_CYCLE,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -222,10 +232,12 @@ IrdaProtocol irda_get_protocol_by_name(const char* protocol_name) {
|
||||
if (!strcmp(irda_protocols[i].name, protocol_name))
|
||||
return i;
|
||||
}
|
||||
furi_assert(0);
|
||||
return IrdaProtocolUnknown;
|
||||
}
|
||||
|
||||
const char* irda_get_protocol_name(IrdaProtocol protocol) {
|
||||
furi_assert(irda_is_protocol_valid(protocol));
|
||||
if (irda_is_protocol_valid(protocol))
|
||||
return irda_protocols[protocol].name;
|
||||
else
|
||||
@@ -233,6 +245,7 @@ const char* irda_get_protocol_name(IrdaProtocol protocol) {
|
||||
}
|
||||
|
||||
uint8_t irda_get_protocol_address_length(IrdaProtocol protocol) {
|
||||
furi_assert(irda_is_protocol_valid(protocol));
|
||||
if (irda_is_protocol_valid(protocol))
|
||||
return irda_protocols[protocol].address_length;
|
||||
else
|
||||
@@ -240,9 +253,26 @@ uint8_t irda_get_protocol_address_length(IrdaProtocol protocol) {
|
||||
}
|
||||
|
||||
uint8_t irda_get_protocol_command_length(IrdaProtocol protocol) {
|
||||
furi_assert(irda_is_protocol_valid(protocol));
|
||||
if (irda_is_protocol_valid(protocol))
|
||||
return irda_protocols[protocol].command_length;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32_t irda_get_protocol_frequency(IrdaProtocol protocol) {
|
||||
furi_assert(irda_is_protocol_valid(protocol));
|
||||
if (irda_is_protocol_valid(protocol))
|
||||
return irda_protocols[protocol].frequency;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
float irda_get_protocol_duty_cycle(IrdaProtocol protocol) {
|
||||
furi_assert(irda_is_protocol_valid(protocol));
|
||||
if (irda_is_protocol_valid(protocol))
|
||||
return irda_protocols[protocol].duty_cycle;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@@ -10,6 +10,12 @@ extern "C" {
|
||||
#define IRDA_COMMON_CARRIER_FREQUENCY 38000
|
||||
#define IRDA_COMMON_DUTY_CYCLE 0.33
|
||||
|
||||
/* if we want to see splitted raw signals during brutforce,
|
||||
* we have to have RX raw timing delay less than TX */
|
||||
#define IRDA_RAW_RX_TIMING_DELAY_US 150000
|
||||
#define IRDA_RAW_TX_TIMING_DELAY_US 180000
|
||||
|
||||
|
||||
typedef struct IrdaDecoderHandler IrdaDecoderHandler;
|
||||
typedef struct IrdaEncoderHandler IrdaEncoderHandler;
|
||||
|
||||
@@ -150,6 +156,24 @@ IrdaStatus irda_encode(IrdaEncoderHandler* handler, uint32_t* duration, bool* le
|
||||
*/
|
||||
void irda_reset_encoder(IrdaEncoderHandler* handler, const IrdaMessage* message);
|
||||
|
||||
/**
|
||||
* Get PWM frequency value for selected protocol
|
||||
*
|
||||
* \param[in] protocol - protocol to get from PWM frequency
|
||||
*
|
||||
* \return frequency
|
||||
*/
|
||||
uint32_t irda_get_protocol_frequency(IrdaProtocol protocol);
|
||||
|
||||
/**
|
||||
* Get PWM duty cycle value for selected protocol
|
||||
*
|
||||
* \param[in] protocol - protocol to get from PWM duty cycle
|
||||
*
|
||||
* \return duty cycle
|
||||
*/
|
||||
float irda_get_protocol_duty_cycle(IrdaProtocol protocol);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
@@ -137,7 +137,8 @@ extern const IrdaCommonProtocolSpec protocol_samsung32;
|
||||
#define IRDA_RC6_BIT 444 // half of time-quant for 1 bit
|
||||
#define IRDA_RC6_PREAMBLE_TOLERANCE 0.07 // percents
|
||||
#define IRDA_RC6_BIT_TOLERANCE 120 // us
|
||||
#define IRDA_RC6_SILENCE 2700
|
||||
/* protocol allows 2700 silence, but it is hard to send 1 message without repeat */
|
||||
#define IRDA_RC6_SILENCE (2700 * 10)
|
||||
|
||||
void* irda_decoder_rc6_alloc(void);
|
||||
void irda_decoder_rc6_reset(void* decoder);
|
||||
|
@@ -23,7 +23,7 @@ FuriHalIrdaTxGetDataState irda_get_raw_data_callback (void* context, uint32_t* d
|
||||
if (irda_tx_raw_add_silence && (irda_tx_raw_timings_index == 0)) {
|
||||
irda_tx_raw_add_silence = false;
|
||||
*level = false;
|
||||
*duration = 180000; // 180 ms delay between raw packets
|
||||
*duration = IRDA_RAW_TX_TIMING_DELAY_US;
|
||||
} else {
|
||||
*level = irda_tx_raw_start_from_mark ^ (irda_tx_raw_timings_index % 2);
|
||||
*duration = timings[irda_tx_raw_timings_index++];
|
||||
|
@@ -1,20 +1,45 @@
|
||||
#include "furi/check.h"
|
||||
#include "furi/common_defines.h"
|
||||
#include "sys/_stdint.h"
|
||||
#include "irda_worker.h"
|
||||
#include <irda.h>
|
||||
#include <furi-hal-irda.h>
|
||||
#include <limits.h>
|
||||
#include <stdint.h>
|
||||
#include <stream_buffer.h>
|
||||
#include <furi.h>
|
||||
#include <notification/notification-messages.h>
|
||||
#include <stream_buffer.h>
|
||||
|
||||
#define IRDA_WORKER_RX_TIMEOUT IRDA_RAW_RX_TIMING_DELAY_US
|
||||
|
||||
#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
|
||||
#define IRDA_WORKER_TX_FILL_BUFFER 0x10
|
||||
#define IRDA_WORKER_TX_MESSAGE_SENT 0x20
|
||||
|
||||
struct IrdaWorkerSignal {
|
||||
#define IRDA_WORKER_ALL_RX_EVENTS (IRDA_WORKER_RX_RECEIVED \
|
||||
| IRDA_WORKER_RX_TIMEOUT_RECEIVED \
|
||||
| IRDA_WORKER_OVERRUN \
|
||||
| IRDA_WORKER_EXIT)
|
||||
|
||||
#define IRDA_WORKER_ALL_TX_EVENTS (IRDA_WORKER_TX_FILL_BUFFER \
|
||||
| IRDA_WORKER_TX_MESSAGE_SENT \
|
||||
| IRDA_WORKER_EXIT)
|
||||
|
||||
#define IRDA_WORKER_ALL_EVENTS (IRDA_WORKER_ALL_RX_EVENTS | IRDA_WORKER_ALL_TX_EVENTS)
|
||||
|
||||
typedef enum {
|
||||
IrdaWorkerStateIdle,
|
||||
IrdaWorkerStateRunRx,
|
||||
IrdaWorkerStateRunTx,
|
||||
IrdaWorkerStateWaitTxEnd,
|
||||
IrdaWorkerStateStopTx,
|
||||
IrdaWorkerStateStartTx,
|
||||
} IrdaWorkerState;
|
||||
|
||||
struct IrdaWorkerSignal{
|
||||
bool decoded;
|
||||
size_t timings_cnt;
|
||||
union {
|
||||
@@ -25,37 +50,67 @@ struct IrdaWorkerSignal {
|
||||
|
||||
struct IrdaWorker {
|
||||
FuriThread* thread;
|
||||
IrdaDecoderHandler* irda_decoder;
|
||||
StreamBufferHandle_t stream;
|
||||
osEventFlagsId_t events;
|
||||
|
||||
TaskHandle_t worker_handle;
|
||||
IrdaWorkerSignal signal;
|
||||
|
||||
IrdaWorkerReceivedSignalCallback received_signal_callback;
|
||||
void* context;
|
||||
bool blink_enable;
|
||||
bool overrun;
|
||||
IrdaWorkerState state;
|
||||
IrdaEncoderHandler* irda_encoder;
|
||||
IrdaDecoderHandler* irda_decoder;
|
||||
NotificationApp* notification;
|
||||
bool blink_enable;
|
||||
|
||||
union {
|
||||
struct {
|
||||
IrdaWorkerGetSignalCallback get_signal_callback;
|
||||
IrdaWorkerMessageSentCallback message_sent_callback;
|
||||
void* get_signal_context;
|
||||
void* message_sent_context;
|
||||
uint32_t frequency;
|
||||
float duty_cycle;
|
||||
uint32_t tx_raw_cnt;
|
||||
bool need_reinitialization;
|
||||
bool steady_signal_sent;
|
||||
} tx;
|
||||
struct {
|
||||
IrdaWorkerReceivedSignalCallback received_signal_callback;
|
||||
void* received_signal_context;
|
||||
bool overrun;
|
||||
} rx;
|
||||
};
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
uint32_t duration;
|
||||
bool level;
|
||||
FuriHalIrdaTxGetDataState state;
|
||||
} IrdaWorkerTiming;
|
||||
|
||||
static int32_t irda_worker_tx_thread(void* context);
|
||||
static FuriHalIrdaTxGetDataState irda_worker_furi_hal_data_isr_callback(void* context, uint32_t* duration, bool* level);
|
||||
static void irda_worker_furi_hal_message_sent_isr_callback(void* context);
|
||||
|
||||
|
||||
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);
|
||||
uint32_t flags_set = osEventFlagsSet(instance->events, IRDA_WORKER_RX_TIMEOUT_RECEIVED);
|
||||
furi_check(flags_set & IRDA_WORKER_RX_TIMEOUT_RECEIVED);
|
||||
}
|
||||
|
||||
static void irda_worker_rx_callback(void* context, bool level, uint32_t duration) {
|
||||
IrdaWorker* instance = context;
|
||||
|
||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||
furi_assert(duration != 0);
|
||||
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);
|
||||
uint32_t events = (ret == sizeof(LevelDuration)) ? IRDA_WORKER_RX_RECEIVED : IRDA_WORKER_OVERRUN;
|
||||
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
||||
|
||||
uint32_t flags_set = osEventFlagsSet(instance->events, events);
|
||||
furi_check(flags_set & events);
|
||||
}
|
||||
|
||||
static void irda_worker_process_timeout(IrdaWorker* instance) {
|
||||
@@ -63,8 +118,8 @@ static void irda_worker_process_timeout(IrdaWorker* instance) {
|
||||
return;
|
||||
|
||||
instance->signal.decoded = false;
|
||||
if (instance->received_signal_callback)
|
||||
instance->received_signal_callback(instance->context, &instance->signal);
|
||||
if (instance->rx.received_signal_callback)
|
||||
instance->rx.received_signal_callback(instance->rx.received_signal_context, &instance->signal);
|
||||
}
|
||||
|
||||
static void irda_worker_process_timings(IrdaWorker* instance, uint32_t duration, bool level) {
|
||||
@@ -73,8 +128,8 @@ static void irda_worker_process_timings(IrdaWorker* instance, uint32_t duration,
|
||||
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);
|
||||
if (instance->rx.received_signal_callback)
|
||||
instance->rx.received_signal_callback(instance->rx.received_signal_context, &instance->signal);
|
||||
} else {
|
||||
/* Skip first timing if it's starts from Space */
|
||||
if ((instance->signal.timings_cnt == 0) && !level) {
|
||||
@@ -85,50 +140,49 @@ static void irda_worker_process_timings(IrdaWorker* instance, uint32_t duration,
|
||||
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;
|
||||
uint32_t flags_set = osEventFlagsSet(instance->events, IRDA_WORKER_OVERRUN);
|
||||
furi_check(flags_set & IRDA_WORKER_OVERRUN);
|
||||
instance->rx.overrun = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int32_t irda_worker_thread_callback(void* context) {
|
||||
IrdaWorker* instance = context;
|
||||
uint32_t notify_value = 0;
|
||||
static int32_t irda_worker_rx_thread(void* thread_context) {
|
||||
IrdaWorker* instance = thread_context;
|
||||
uint32_t events = 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;
|
||||
events = osEventFlagsWait(instance->events, IRDA_WORKER_ALL_RX_EVENTS, 0, osWaitForever);
|
||||
furi_check(events & IRDA_WORKER_ALL_RX_EVENTS); /* at least one caught */
|
||||
|
||||
if (notify_value & IRDA_WORKER_RX_RECEIVED) {
|
||||
if (!instance->overrun && instance->blink_enable && ((xTaskGetTickCount() - last_blink_time) > 80)) {
|
||||
if (events & IRDA_WORKER_RX_RECEIVED) {
|
||||
if (!instance->rx.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) {
|
||||
if (!instance->rx.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) {
|
||||
if (events & 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) {
|
||||
if (events & IRDA_WORKER_RX_TIMEOUT_RECEIVED) {
|
||||
if (instance->rx.overrun) {
|
||||
printf("\nOVERRUN, max samples: %d\n", MAX_TIMINGS_AMOUNT);
|
||||
instance->overrun = false;
|
||||
instance->rx.overrun = false;
|
||||
if (instance->blink_enable)
|
||||
notification_message(instance->notification, &sequence_reset_red);
|
||||
} else {
|
||||
@@ -136,16 +190,17 @@ static int32_t irda_worker_thread_callback(void* context) {
|
||||
}
|
||||
instance->signal.timings_cnt = 0;
|
||||
}
|
||||
if (notify_value & IRDA_WORKER_EXIT)
|
||||
if (events & IRDA_WORKER_EXIT)
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void irda_worker_set_received_signal_callback(IrdaWorker* instance, IrdaWorkerReceivedSignalCallback callback) {
|
||||
void irda_worker_rx_set_received_signal_callback(IrdaWorker* instance, IrdaWorkerReceivedSignalCallback callback, void* context) {
|
||||
furi_assert(instance);
|
||||
instance->received_signal_callback = callback;
|
||||
instance->rx.received_signal_callback = callback;
|
||||
instance->rx.received_signal_context = context;
|
||||
}
|
||||
|
||||
IrdaWorker* irda_worker_alloc() {
|
||||
@@ -155,60 +210,67 @@ IrdaWorker* irda_worker_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));
|
||||
|
||||
size_t buffer_size = MAX(sizeof(IrdaWorkerTiming) * MAX_TIMINGS_AMOUNT, sizeof(LevelDuration) * MAX_TIMINGS_AMOUNT);
|
||||
instance->stream = xStreamBufferCreate(buffer_size, sizeof(IrdaWorkerTiming));
|
||||
instance->irda_decoder = irda_alloc_decoder();
|
||||
instance->irda_encoder = irda_alloc_encoder();
|
||||
instance->blink_enable = false;
|
||||
instance->notification = furi_record_open("notification");
|
||||
instance->state = IrdaWorkerStateIdle;
|
||||
instance->events = osEventFlagsNew(NULL);
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
void irda_worker_free(IrdaWorker* instance) {
|
||||
furi_assert(instance);
|
||||
furi_assert(!instance->worker_handle);
|
||||
furi_assert(instance->state == IrdaWorkerStateIdle);
|
||||
|
||||
furi_record_close("notification");
|
||||
irda_free_decoder(instance->irda_decoder);
|
||||
irda_free_encoder(instance->irda_encoder);
|
||||
vStreamBufferDelete(instance->stream);
|
||||
furi_thread_free(instance->thread);
|
||||
osEventFlagsDelete(instance->events);
|
||||
|
||||
free(instance);
|
||||
}
|
||||
|
||||
void irda_worker_set_context(IrdaWorker* instance, void* context) {
|
||||
void irda_worker_rx_start(IrdaWorker* instance) {
|
||||
furi_assert(instance);
|
||||
instance->context = context;
|
||||
}
|
||||
furi_assert(instance->state == IrdaWorkerStateIdle);
|
||||
|
||||
void irda_worker_start(IrdaWorker* instance) {
|
||||
furi_assert(instance);
|
||||
furi_assert(!instance->worker_handle);
|
||||
xStreamBufferSetTriggerLevel(instance->stream, sizeof(LevelDuration));
|
||||
|
||||
osEventFlagsClear(instance->events, IRDA_WORKER_ALL_EVENTS);
|
||||
furi_thread_set_callback(instance->thread, irda_worker_rx_thread);
|
||||
furi_thread_start(instance->thread);
|
||||
|
||||
instance->worker_handle = furi_thread_get_thread_id(instance->thread);
|
||||
furi_hal_irda_async_rx_start();
|
||||
furi_hal_irda_async_rx_set_timeout(IRDA_WORKER_RX_TIMEOUT);
|
||||
furi_hal_irda_async_rx_set_capture_isr_callback(irda_worker_rx_callback, instance);
|
||||
furi_hal_irda_async_rx_set_timeout_isr_callback(irda_worker_rx_timeout_callback, instance);
|
||||
furi_hal_irda_async_rx_start();
|
||||
furi_hal_irda_async_rx_set_timeout(IRDA_WORKER_RX_TIMEOUT);
|
||||
|
||||
instance->state = IrdaWorkerStateRunRx;
|
||||
}
|
||||
|
||||
void irda_worker_stop(IrdaWorker* instance) {
|
||||
void irda_worker_rx_stop(IrdaWorker* instance) {
|
||||
furi_assert(instance);
|
||||
furi_assert(instance->worker_handle);
|
||||
furi_assert(instance->state == IrdaWorkerStateRunRx);
|
||||
|
||||
furi_hal_irda_async_rx_set_timeout_isr_callback(NULL, NULL);
|
||||
furi_hal_irda_async_rx_set_capture_isr_callback(NULL, NULL);
|
||||
furi_hal_irda_async_rx_stop();
|
||||
|
||||
xTaskNotify(instance->worker_handle, IRDA_WORKER_EXIT, eSetBits);
|
||||
|
||||
instance->worker_handle = NULL;
|
||||
|
||||
osEventFlagsSet(instance->events, IRDA_WORKER_EXIT);
|
||||
furi_thread_join(instance->thread);
|
||||
|
||||
BaseType_t xReturn = pdFAIL;
|
||||
xReturn = xStreamBufferReset(instance->stream);
|
||||
furi_assert(xReturn == pdPASS);
|
||||
instance->state = IrdaWorkerStateIdle;
|
||||
instance->state = IrdaWorkerStateIdle;
|
||||
}
|
||||
|
||||
bool irda_worker_signal_is_decoded(const IrdaWorkerSignal* signal) {
|
||||
@@ -225,13 +287,268 @@ void irda_worker_get_raw_signal(const IrdaWorkerSignal* signal, const uint32_t**
|
||||
*timings_cnt = signal->timings_cnt;
|
||||
}
|
||||
|
||||
const IrdaMessage* irda_worker_get_decoded_message(const IrdaWorkerSignal* signal) {
|
||||
const IrdaMessage* irda_worker_get_decoded_signal(const IrdaWorkerSignal* signal) {
|
||||
furi_assert(signal);
|
||||
return &signal->data.message;
|
||||
}
|
||||
|
||||
void irda_worker_enable_blink_on_receiving(IrdaWorker* instance, bool enable) {
|
||||
void irda_worker_rx_enable_blink_on_receiving(IrdaWorker* instance, bool enable) {
|
||||
furi_assert(instance);
|
||||
instance->blink_enable = enable;
|
||||
}
|
||||
|
||||
void irda_worker_tx_start(IrdaWorker* instance) {
|
||||
furi_assert(instance);
|
||||
furi_assert(instance->state == IrdaWorkerStateIdle);
|
||||
|
||||
// size have to be greater than api hal irda async tx buffer size
|
||||
xStreamBufferSetTriggerLevel(instance->stream, sizeof(IrdaWorkerTiming));
|
||||
|
||||
osEventFlagsClear(instance->events, IRDA_WORKER_ALL_EVENTS);
|
||||
furi_thread_set_callback(instance->thread, irda_worker_tx_thread);
|
||||
furi_thread_start(instance->thread);
|
||||
|
||||
instance->tx.steady_signal_sent = false;
|
||||
instance->tx.need_reinitialization = false;
|
||||
furi_hal_irda_async_tx_set_data_isr_callback(irda_worker_furi_hal_data_isr_callback, instance);
|
||||
furi_hal_irda_async_tx_set_signal_sent_isr_callback(irda_worker_furi_hal_message_sent_isr_callback, instance);
|
||||
|
||||
instance->state = IrdaWorkerStateStartTx;
|
||||
}
|
||||
|
||||
static void irda_worker_furi_hal_message_sent_isr_callback(void* context) {
|
||||
IrdaWorker* instance = context;
|
||||
uint32_t flags_set = osEventFlagsSet(instance->events, IRDA_WORKER_TX_MESSAGE_SENT);
|
||||
furi_check(flags_set & IRDA_WORKER_TX_MESSAGE_SENT);
|
||||
}
|
||||
|
||||
static FuriHalIrdaTxGetDataState irda_worker_furi_hal_data_isr_callback(void* context, uint32_t* duration, bool* level) {
|
||||
furi_assert(context);
|
||||
furi_assert(duration);
|
||||
furi_assert(level);
|
||||
|
||||
IrdaWorker* instance = context;
|
||||
IrdaWorkerTiming timing = {.state = FuriHalIrdaTxGetDataStateError} ;
|
||||
|
||||
if (sizeof(IrdaWorkerTiming) == xStreamBufferReceiveFromISR(instance->stream, &timing, sizeof(IrdaWorkerTiming), 0)) {
|
||||
*level = timing.level;
|
||||
*duration = timing.duration;
|
||||
furi_assert(timing.state != FuriHalIrdaTxGetDataStateError);
|
||||
} else {
|
||||
furi_assert(0);
|
||||
timing.state = FuriHalIrdaTxGetDataStateError;
|
||||
}
|
||||
|
||||
uint32_t flags_set = osEventFlagsSet(instance->events, IRDA_WORKER_TX_FILL_BUFFER);
|
||||
furi_check(flags_set & IRDA_WORKER_TX_FILL_BUFFER);
|
||||
|
||||
return timing.state;
|
||||
}
|
||||
|
||||
static bool irda_get_new_signal(IrdaWorker* instance) {
|
||||
bool new_signal_obtained = false;
|
||||
|
||||
IrdaWorkerGetSignalResponse response = instance->tx.get_signal_callback(instance->tx.get_signal_context, instance);
|
||||
if (response == IrdaWorkerGetSignalResponseNew) {
|
||||
uint32_t new_tx_frequency = 0;
|
||||
float new_tx_duty_cycle = 0;
|
||||
if (instance->signal.decoded) {
|
||||
new_tx_frequency = irda_get_protocol_frequency(instance->signal.data.message.protocol);
|
||||
new_tx_duty_cycle = irda_get_protocol_duty_cycle(instance->signal.data.message.protocol);
|
||||
} else {
|
||||
furi_assert(instance->signal.timings_cnt > 1);
|
||||
new_tx_frequency = IRDA_COMMON_CARRIER_FREQUENCY;
|
||||
new_tx_duty_cycle = IRDA_COMMON_DUTY_CYCLE;
|
||||
}
|
||||
|
||||
instance->tx.tx_raw_cnt = 0;
|
||||
instance->tx.need_reinitialization = (new_tx_frequency != instance->tx.frequency) || (new_tx_duty_cycle != instance->tx.duty_cycle);
|
||||
instance->tx.frequency = new_tx_frequency;
|
||||
instance->tx.duty_cycle = new_tx_duty_cycle;
|
||||
if (instance->signal.decoded) {
|
||||
irda_reset_encoder(instance->irda_encoder, &instance->signal.data.message);
|
||||
}
|
||||
new_signal_obtained = true;
|
||||
} else if (response == IrdaWorkerGetSignalResponseSame) {
|
||||
new_signal_obtained = true;
|
||||
/* no need to reinit */
|
||||
} else if (response == IrdaWorkerGetSignalResponseStop) {
|
||||
new_signal_obtained = false;
|
||||
} else {
|
||||
furi_assert(0);
|
||||
}
|
||||
|
||||
return new_signal_obtained;
|
||||
}
|
||||
|
||||
static bool irda_worker_tx_fill_buffer(IrdaWorker* instance) {
|
||||
bool new_data_available = true;
|
||||
IrdaWorkerTiming timing;
|
||||
IrdaStatus status = IrdaStatusError;
|
||||
|
||||
while(!xStreamBufferIsFull(instance->stream) && !instance->tx.need_reinitialization && new_data_available) {
|
||||
if (instance->signal.decoded) {
|
||||
status = irda_encode(instance->irda_encoder, &timing.duration, &timing.level);
|
||||
} else {
|
||||
timing.duration = instance->signal.data.timings[instance->tx.tx_raw_cnt];
|
||||
/* raw always starts from Mark, but we fulfill it with space delay at start */
|
||||
timing.level = (instance->tx.tx_raw_cnt % 2);
|
||||
++instance->tx.tx_raw_cnt;
|
||||
if (instance->tx.tx_raw_cnt >= instance->signal.timings_cnt) {
|
||||
instance->tx.tx_raw_cnt = 0;
|
||||
status = IrdaStatusDone;
|
||||
} else {
|
||||
status = IrdaStatusOk;
|
||||
}
|
||||
}
|
||||
|
||||
if (status == IrdaStatusError) {
|
||||
furi_assert(0);
|
||||
new_data_available = false;
|
||||
break;
|
||||
} else if (status == IrdaStatusOk) {
|
||||
timing.state = FuriHalIrdaTxGetDataStateOk;
|
||||
} else if (status == IrdaStatusDone) {
|
||||
timing.state = FuriHalIrdaTxGetDataStateDone;
|
||||
|
||||
new_data_available = irda_get_new_signal(instance);
|
||||
if (instance->tx.need_reinitialization || !new_data_available) {
|
||||
timing.state = FuriHalIrdaTxGetDataStateLastDone;
|
||||
}
|
||||
} else {
|
||||
furi_assert(0);
|
||||
}
|
||||
uint32_t written_size = xStreamBufferSend(instance->stream, &timing, sizeof(IrdaWorkerTiming), 0);
|
||||
furi_assert(sizeof(IrdaWorkerTiming) == written_size);
|
||||
}
|
||||
|
||||
return new_data_available;
|
||||
}
|
||||
|
||||
static int32_t irda_worker_tx_thread(void* thread_context) {
|
||||
IrdaWorker* instance = thread_context;
|
||||
furi_assert(instance->state == IrdaWorkerStateStartTx);
|
||||
furi_assert(thread_context);
|
||||
|
||||
uint32_t events = 0;
|
||||
bool new_data_available = true;
|
||||
bool exit = false;
|
||||
|
||||
exit = !irda_get_new_signal(instance);
|
||||
furi_assert(!exit);
|
||||
|
||||
while(!exit) {
|
||||
switch (instance->state) {
|
||||
case IrdaWorkerStateStartTx:
|
||||
instance->tx.need_reinitialization = false;
|
||||
new_data_available = irda_worker_tx_fill_buffer(instance);
|
||||
furi_hal_irda_async_tx_start(instance->tx.frequency, instance->tx.duty_cycle);
|
||||
|
||||
if (!new_data_available) {
|
||||
instance->state = IrdaWorkerStateStopTx;
|
||||
} else if (instance->tx.need_reinitialization) {
|
||||
instance->state = IrdaWorkerStateWaitTxEnd;
|
||||
} else {
|
||||
instance->state = IrdaWorkerStateRunTx;
|
||||
}
|
||||
|
||||
break;
|
||||
case IrdaWorkerStateStopTx:
|
||||
furi_hal_irda_async_tx_stop();
|
||||
exit = true;
|
||||
break;
|
||||
case IrdaWorkerStateWaitTxEnd:
|
||||
furi_hal_irda_async_tx_wait_termination();
|
||||
instance->state = IrdaWorkerStateStartTx;
|
||||
|
||||
events = osEventFlagsGet(instance->events);
|
||||
if(events & IRDA_WORKER_EXIT) {
|
||||
exit = true;
|
||||
break;
|
||||
}
|
||||
|
||||
break;
|
||||
case IrdaWorkerStateRunTx:
|
||||
events = osEventFlagsWait(instance->events, IRDA_WORKER_ALL_TX_EVENTS, 0, osWaitForever);
|
||||
furi_check(events & IRDA_WORKER_ALL_TX_EVENTS); /* at least one caught */
|
||||
|
||||
if (events & IRDA_WORKER_EXIT) {
|
||||
instance->state = IrdaWorkerStateStopTx;
|
||||
break;
|
||||
}
|
||||
|
||||
if (events & IRDA_WORKER_TX_FILL_BUFFER) {
|
||||
irda_worker_tx_fill_buffer(instance);
|
||||
|
||||
if (instance->tx.need_reinitialization) {
|
||||
instance->state = IrdaWorkerStateWaitTxEnd;
|
||||
}
|
||||
}
|
||||
|
||||
if (events & IRDA_WORKER_TX_MESSAGE_SENT) {
|
||||
if (instance->tx.message_sent_callback)
|
||||
instance->tx.message_sent_callback(instance->tx.message_sent_context);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
furi_assert(0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void irda_worker_tx_set_get_signal_callback(IrdaWorker* instance, IrdaWorkerGetSignalCallback callback, void* context) {
|
||||
furi_assert(instance);
|
||||
instance->tx.get_signal_callback = callback;
|
||||
instance->tx.get_signal_context = context;
|
||||
}
|
||||
|
||||
void irda_worker_tx_set_signal_sent_callback(IrdaWorker* instance, IrdaWorkerMessageSentCallback callback, void* context) {
|
||||
furi_assert(instance);
|
||||
instance->tx.message_sent_callback = callback;
|
||||
instance->tx.message_sent_context = context;
|
||||
}
|
||||
|
||||
void irda_worker_tx_stop(IrdaWorker* instance) {
|
||||
furi_assert(instance);
|
||||
furi_assert(instance->state != IrdaWorkerStateRunRx);
|
||||
|
||||
osEventFlagsSet(instance->events, IRDA_WORKER_EXIT);
|
||||
furi_thread_join(instance->thread);
|
||||
furi_hal_irda_async_tx_set_data_isr_callback(NULL, NULL);
|
||||
furi_hal_irda_async_tx_set_signal_sent_isr_callback(NULL, NULL);
|
||||
|
||||
instance->signal.timings_cnt = 0;
|
||||
BaseType_t xReturn = pdFAIL;
|
||||
xReturn = xStreamBufferReset(instance->stream);
|
||||
furi_assert(xReturn == pdPASS);
|
||||
instance->state = IrdaWorkerStateIdle;
|
||||
}
|
||||
|
||||
void irda_worker_set_decoded_signal(IrdaWorker* instance, const IrdaMessage* message) {
|
||||
furi_assert(instance);
|
||||
furi_assert(message);
|
||||
|
||||
instance->signal.decoded = true;
|
||||
instance->signal.data.message = *message;
|
||||
}
|
||||
|
||||
void irda_worker_set_raw_signal(IrdaWorker* instance, const uint32_t* timings, size_t timings_cnt) {
|
||||
furi_assert(instance);
|
||||
furi_assert(timings);
|
||||
furi_assert(timings_cnt > 2);
|
||||
|
||||
instance->signal.data.timings[0] = IRDA_RAW_TX_TIMING_DELAY_US;
|
||||
memcpy(&instance->signal.data.timings[1], timings, timings_cnt * sizeof(uint32_t));
|
||||
instance->signal.decoded = false;
|
||||
instance->signal.timings_cnt = timings_cnt + 1;
|
||||
}
|
||||
|
||||
IrdaWorkerGetSignalResponse irda_worker_tx_get_signal_steady_callback(void* context, IrdaWorker* instance) {
|
||||
IrdaWorkerGetSignalResponse response = instance->tx.steady_signal_sent ? IrdaWorkerGetSignalResponseSame : IrdaWorkerGetSignalResponseNew;
|
||||
instance->tx.steady_signal_sent = true;
|
||||
return response;
|
||||
}
|
||||
|
||||
|
@@ -7,11 +7,28 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define MAX_TIMINGS_AMOUNT 512
|
||||
|
||||
/** Interface struct of irda worker */
|
||||
typedef struct IrdaWorker IrdaWorker;
|
||||
/** Interface struct of received signal */
|
||||
typedef struct IrdaWorkerSignal IrdaWorkerSignal;
|
||||
|
||||
typedef enum {
|
||||
IrdaWorkerGetSignalResponseNew, /** Signal, provided by callback is new and encoder should be reseted */
|
||||
IrdaWorkerGetSignalResponseSame, /** Signal, provided by callback is same. No encoder resetting. */
|
||||
IrdaWorkerGetSignalResponseStop, /** No more signals available. */
|
||||
} IrdaWorkerGetSignalResponse;
|
||||
|
||||
/** Callback type for providing next signal to send. Should be used with
|
||||
* irda_worker_make_decoded_signal() or irda_worker_make_raw_signal()
|
||||
*/
|
||||
typedef IrdaWorkerGetSignalResponse (*IrdaWorkerGetSignalCallback)(void* context, IrdaWorker* instance);
|
||||
|
||||
/** Callback type for 'message is sent' event */
|
||||
typedef void (*IrdaWorkerMessageSentCallback)(void* context);
|
||||
|
||||
|
||||
/** Callback type to call by IrdaWorker thread when new signal is received */
|
||||
typedef void (*IrdaWorkerReceivedSignalCallback)(void* context, IrdaWorkerSignal* received_signal);
|
||||
|
||||
@@ -27,31 +44,33 @@ IrdaWorker* irda_worker_alloc();
|
||||
*/
|
||||
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 furi-hal, prepare all work.
|
||||
*
|
||||
* @param[in] instance - IrdaWorker instance
|
||||
*/
|
||||
void irda_worker_start(IrdaWorker* instance);
|
||||
void irda_worker_rx_start(IrdaWorker* instance);
|
||||
|
||||
/** Stop IrdaWorker thread, deinitialize furi-hal.
|
||||
*
|
||||
* @param[in] instance - IrdaWorker instance
|
||||
*/
|
||||
void irda_worker_stop(IrdaWorker* instance);
|
||||
void irda_worker_rx_stop(IrdaWorker* instance);
|
||||
|
||||
/** Set received data callback IrdaWorker
|
||||
*
|
||||
* @param[in] instance - IrdaWorker instance
|
||||
* @param[in] context - context to pass to callbacks
|
||||
* @param[in] callback - IrdaWorkerReceivedSignalCallback callback
|
||||
*/
|
||||
void irda_worker_rx_set_received_signal_callback(IrdaWorker* instance, IrdaWorkerReceivedSignalCallback callback, void* context);
|
||||
|
||||
/** 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_rx_enable_blink_on_receiving(IrdaWorker* instance, bool enable);
|
||||
|
||||
/** Clarify is received signal either decoded or raw
|
||||
*
|
||||
@@ -60,6 +79,48 @@ void irda_worker_stop(IrdaWorker* instance);
|
||||
*/
|
||||
bool irda_worker_signal_is_decoded(const IrdaWorkerSignal* signal);
|
||||
|
||||
/** Start transmitting signal. Callback IrdaWorkerGetSignalCallback should be
|
||||
* set before this function is called, as it calls for it to fill buffer before
|
||||
* starting transmission.
|
||||
*
|
||||
* @param[in] instance - IrdaWorker instance
|
||||
*/
|
||||
void irda_worker_tx_start(IrdaWorker* instance);
|
||||
|
||||
/** Stop transmitting signal. Waits for end of current signal and stops transmission.
|
||||
*
|
||||
* @param[in] instance - IrdaWorker instance
|
||||
*/
|
||||
void irda_worker_tx_stop(IrdaWorker* instance);
|
||||
|
||||
/** Set callback for providing next signal to send
|
||||
*
|
||||
* @param[in] instance - IrdaWorker instance
|
||||
* @param[in] context - context to pass to callbacks
|
||||
* @param[in] callback - IrdaWorkerGetSignalCallback callback
|
||||
*/
|
||||
void irda_worker_tx_set_get_signal_callback(IrdaWorker* instance, IrdaWorkerGetSignalCallback callback, void* context);
|
||||
|
||||
/** Set callback for end of signal transmitting
|
||||
*
|
||||
* @param[in] instance - IrdaWorker instance
|
||||
* @param[in] context - context to pass to callbacks
|
||||
* @param[in] callback - IrdaWorkerMessageSentCallback callback
|
||||
*/
|
||||
void irda_worker_tx_set_signal_sent_callback(IrdaWorker* instance, IrdaWorkerMessageSentCallback callback, void* context);
|
||||
|
||||
/** Callback to pass to irda_worker_tx_set_get_signal_callback() if signal
|
||||
* is steady and will not be changed between irda_worker start and stop.
|
||||
* Before starting transmission, desired steady signal must be set with
|
||||
* irda_worker_make_decoded_signal() or irda_worker_make_raw_signal().
|
||||
*
|
||||
* This function should not be implicitly called.
|
||||
*
|
||||
* @param[in] context - context
|
||||
* @param[out] instance - IrdaWorker instance
|
||||
*/
|
||||
IrdaWorkerGetSignalResponse irda_worker_tx_get_signal_steady_callback(void* context, IrdaWorker* instance);
|
||||
|
||||
/** Acquire raw signal from interface struct 'IrdaWorkerSignal'.
|
||||
* First, you have to ensure that signal is raw.
|
||||
*
|
||||
@@ -73,17 +134,24 @@ void irda_worker_get_raw_signal(const IrdaWorkerSignal* signal, const uint32_t**
|
||||
* First, you have to ensure that signal is decoded.
|
||||
*
|
||||
* @param[in] signal - received signal
|
||||
* @return decoded irda message
|
||||
* @return decoded IRDA message
|
||||
*/
|
||||
const IrdaMessage* irda_worker_get_decoded_message(const IrdaWorkerSignal* signal);
|
||||
const IrdaMessage* irda_worker_get_decoded_signal(const IrdaWorkerSignal* signal);
|
||||
|
||||
/** Enable blinking on receiving any signal on IR port.
|
||||
/** Set current decoded signal for IrdaWorker instance
|
||||
*
|
||||
* @param[in] instance - instance of IrdaWorker
|
||||
* @param[in] enable - true if you want to enable blinking
|
||||
* false otherwise
|
||||
* @param[out] instance - IrdaWorker instance
|
||||
* @param[in] message - decoded signal
|
||||
*/
|
||||
void irda_worker_enable_blink_on_receiving(IrdaWorker* instance, bool enable);
|
||||
void irda_worker_set_decoded_signal(IrdaWorker* instance, const IrdaMessage* message);
|
||||
|
||||
/** Set current raw signal for IrdaWorker instance
|
||||
*
|
||||
* @param[out] instance - IrdaWorker instance
|
||||
* @param[in] timings - array of raw timings
|
||||
* @param[in] timings_cnt - size of array of raw timings
|
||||
*/
|
||||
void irda_worker_set_raw_signal(IrdaWorker* instance, const uint32_t* timings, size_t timings_cnt);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
Reference in New Issue
Block a user