2021-06-29 21:19:20 +00:00
|
|
|
#include "subghz_worker.h"
|
|
|
|
|
|
|
|
#include <stream_buffer.h>
|
|
|
|
#include <furi.h>
|
|
|
|
|
2021-11-12 13:04:35 +00:00
|
|
|
#define TAG "SubGhzWorker"
|
|
|
|
|
2021-06-29 21:19:20 +00:00
|
|
|
struct SubGhzWorker {
|
|
|
|
FuriThread* thread;
|
|
|
|
StreamBufferHandle_t stream;
|
|
|
|
|
|
|
|
volatile bool running;
|
|
|
|
volatile bool overrun;
|
|
|
|
|
2021-09-28 00:05:40 +00:00
|
|
|
LevelDuration filter_level_duration;
|
|
|
|
bool filter_running;
|
|
|
|
uint16_t filter_duration;
|
|
|
|
|
2021-06-29 21:19:20 +00:00
|
|
|
SubGhzWorkerOverrunCallback overrun_callback;
|
|
|
|
SubGhzWorkerPairCallback pair_callback;
|
|
|
|
void* context;
|
|
|
|
};
|
|
|
|
|
2021-07-02 13:25:49 +00:00
|
|
|
/** Rx callback timer
|
|
|
|
*
|
|
|
|
* @param level received signal level
|
|
|
|
* @param duration received signal duration
|
|
|
|
* @param context
|
|
|
|
*/
|
2021-07-07 19:49:45 +00:00
|
|
|
void subghz_worker_rx_callback(bool level, uint32_t duration, void* context) {
|
2021-06-29 21:19:20 +00:00
|
|
|
SubGhzWorker* instance = context;
|
|
|
|
|
|
|
|
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
2021-07-07 19:49:45 +00:00
|
|
|
LevelDuration level_duration = level_duration_make(level, duration);
|
2021-06-29 21:19:20 +00:00
|
|
|
if(instance->overrun) {
|
|
|
|
instance->overrun = false;
|
2021-07-07 19:49:45 +00:00
|
|
|
level_duration = level_duration_reset();
|
2021-06-29 21:19:20 +00:00
|
|
|
}
|
2021-09-28 00:05:40 +00:00
|
|
|
size_t ret = xStreamBufferSendFromISR(
|
|
|
|
instance->stream, &level_duration, sizeof(LevelDuration), &xHigherPriorityTaskWoken);
|
2021-07-07 19:49:45 +00:00
|
|
|
if(sizeof(LevelDuration) != ret) instance->overrun = true;
|
2021-06-29 21:19:20 +00:00
|
|
|
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
|
|
|
}
|
|
|
|
|
2021-07-02 13:25:49 +00:00
|
|
|
/** Worker callback thread
|
|
|
|
*
|
|
|
|
* @param context
|
|
|
|
* @return exit code
|
|
|
|
*/
|
2021-06-29 21:19:20 +00:00
|
|
|
static int32_t subghz_worker_thread_callback(void* context) {
|
|
|
|
SubGhzWorker* instance = context;
|
|
|
|
|
2021-07-07 19:49:45 +00:00
|
|
|
LevelDuration level_duration;
|
2021-06-29 21:19:20 +00:00
|
|
|
while(instance->running) {
|
2021-09-28 00:05:40 +00:00
|
|
|
int ret =
|
|
|
|
xStreamBufferReceive(instance->stream, &level_duration, sizeof(LevelDuration), 10);
|
2021-07-07 19:49:45 +00:00
|
|
|
if(ret == sizeof(LevelDuration)) {
|
|
|
|
if(level_duration_is_reset(level_duration)) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Overrun buffer");;
|
2021-09-28 00:05:40 +00:00
|
|
|
if(instance->overrun_callback) instance->overrun_callback(instance->context);
|
2021-06-29 21:19:20 +00:00
|
|
|
} else {
|
2021-07-07 19:49:45 +00:00
|
|
|
bool level = level_duration_get_level(level_duration);
|
|
|
|
uint32_t duration = level_duration_get_duration(level_duration);
|
2021-09-28 00:05:40 +00:00
|
|
|
|
|
|
|
if(instance->filter_running) {
|
|
|
|
if((duration < instance->filter_duration) ||
|
|
|
|
(instance->filter_level_duration.level == level)) {
|
|
|
|
instance->filter_level_duration.duration += duration;
|
|
|
|
|
|
|
|
} else if(instance->filter_level_duration.level != level) {
|
|
|
|
if(instance->pair_callback)
|
|
|
|
instance->pair_callback(
|
|
|
|
instance->context,
|
|
|
|
instance->filter_level_duration.level,
|
|
|
|
instance->filter_level_duration.duration);
|
|
|
|
|
|
|
|
instance->filter_level_duration.duration = duration;
|
|
|
|
instance->filter_level_duration.level = level;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(instance->pair_callback)
|
|
|
|
instance->pair_callback(instance->context, level, duration);
|
|
|
|
}
|
2021-06-29 21:19:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
SubGhzWorker* subghz_worker_alloc() {
|
|
|
|
SubGhzWorker* instance = furi_alloc(sizeof(SubGhzWorker));
|
|
|
|
|
|
|
|
instance->thread = furi_thread_alloc();
|
2021-11-11 16:17:50 +00:00
|
|
|
furi_thread_set_name(instance->thread, "SubghzWorker");
|
2021-06-29 21:19:20 +00:00
|
|
|
furi_thread_set_stack_size(instance->thread, 2048);
|
|
|
|
furi_thread_set_context(instance->thread, instance);
|
|
|
|
furi_thread_set_callback(instance->thread, subghz_worker_thread_callback);
|
2021-09-28 00:05:40 +00:00
|
|
|
|
2021-11-11 12:49:19 +00:00
|
|
|
instance->stream = xStreamBufferCreate(sizeof(LevelDuration) * 2048, sizeof(LevelDuration));
|
2021-06-29 21:19:20 +00:00
|
|
|
|
2021-09-28 00:05:40 +00:00
|
|
|
//setting filter
|
|
|
|
instance->filter_running = true;
|
|
|
|
instance->filter_duration = 20;
|
|
|
|
|
2021-06-29 21:19:20 +00:00
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
void subghz_worker_free(SubGhzWorker* instance) {
|
|
|
|
furi_assert(instance);
|
|
|
|
|
|
|
|
vStreamBufferDelete(instance->stream);
|
|
|
|
furi_thread_free(instance->thread);
|
|
|
|
|
|
|
|
free(instance);
|
|
|
|
}
|
|
|
|
|
2021-09-28 00:05:40 +00:00
|
|
|
void subghz_worker_set_overrun_callback(
|
|
|
|
SubGhzWorker* instance,
|
|
|
|
SubGhzWorkerOverrunCallback callback) {
|
2021-06-29 21:19:20 +00:00
|
|
|
furi_assert(instance);
|
|
|
|
instance->overrun_callback = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
void subghz_worker_set_pair_callback(SubGhzWorker* instance, SubGhzWorkerPairCallback callback) {
|
|
|
|
furi_assert(instance);
|
|
|
|
instance->pair_callback = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
void subghz_worker_set_context(SubGhzWorker* instance, void* context) {
|
|
|
|
furi_assert(instance);
|
|
|
|
instance->context = context;
|
|
|
|
}
|
|
|
|
|
|
|
|
void subghz_worker_start(SubGhzWorker* instance) {
|
|
|
|
furi_assert(instance);
|
|
|
|
furi_assert(!instance->running);
|
|
|
|
|
|
|
|
instance->running = true;
|
|
|
|
|
|
|
|
furi_thread_start(instance->thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
void subghz_worker_stop(SubGhzWorker* instance) {
|
|
|
|
furi_assert(instance);
|
|
|
|
furi_assert(instance->running);
|
|
|
|
|
|
|
|
instance->running = false;
|
|
|
|
|
|
|
|
furi_thread_join(instance->thread);
|
|
|
|
}
|
Skorp subghz signal archive (#667)
* SubGhz: Add millis() furi, add subghz history struct
* SubGhz: Fix subghz history
* Gubghz: Fix code repeat history, add clean history
* SubGhz: reading and adding keys to history
* Gui: Renaming Sub 1-Ghz -> SubGhz
* Archive: Renaming Sub 1-Ghz -> SubGhz
* SubGhz: Add menu history, modified button for sending a signal, changed output of data about accepted protocol
* Archive: Fix name subghz
* SubGhz: Menu navigation
* Assets: Add assets/SubGHz/icon.png
* Assets: add new icons for subghz
* SubGhz: Fix name Add manually scene
* SubGhz: Fix load icon Read scene. rename encoder struct, rename protocol function load from file, add load raw data protocol, add info pleasant signals all protocol
* SubGhz: fix memory leak
* SubGhz: change of receiving frequency for read scene
* SubGhz: Add save/load frequency and preset, add automatic configuration of transmit/receive to the desired frequency and modulation, add button "save" config scene
* SubGhz: Fix frequency and preset, fix frequency add manualli scene, fix re-executing the parser
* Furi-hal-subghz: add 2-FSK config, fix ook config 650KHz BW Tx filter
* Fix formatting and release build
* SubGhz: Delete read scene
* SubGhz: Fix frequency add manualli scene, refactoring code
* SubGhz: 2 profiles for OOK, fix broken build.
* SubGhz: Add passing static codes from read scene, add notification read scene, refactoring code
* SubGhz: fix assert on worker double stop.
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2021-08-28 13:51:48 +00:00
|
|
|
|
|
|
|
bool subghz_worker_is_running(SubGhzWorker* instance) {
|
|
|
|
furi_assert(instance);
|
|
|
|
return instance->running;
|
|
|
|
}
|