[FL-2230] SubGhz: protocol API refactoring (#969)
* SubGhz: protocols library refactoring * SubGhz: new architecture and refactoring * SubGhz: simplify protocol structure, remove unused types * SubGhz: rename Subghz to SubGhz * SubGhz: add environment concept Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com> Co-authored-by: DrZlo13 <who.just.the.doctor@gmail.com>
This commit is contained in:
@@ -7,26 +7,26 @@
|
||||
#include <furi_hal.h>
|
||||
#include <input/input.h>
|
||||
#include <toolbox/level_duration.h>
|
||||
#include <lib/subghz/protocols/subghz_protocol_princeton.h>
|
||||
#include <lib/subghz/protocols/princeton_for_testing.h>
|
||||
|
||||
#define SUBGHZ_TEST_PACKET_COUNT 500
|
||||
|
||||
struct SubghzTestPacket {
|
||||
struct SubGhzTestPacket {
|
||||
View* view;
|
||||
osTimerId_t timer;
|
||||
|
||||
SubGhzDecoderPrinceton* decoder;
|
||||
SubGhzEncoderPrinceton* encoder;
|
||||
volatile size_t packet_rx;
|
||||
SubghzTestPacketCallback callback;
|
||||
SubGhzTestPacketCallback callback;
|
||||
void* context;
|
||||
};
|
||||
|
||||
typedef enum {
|
||||
SubghzTestPacketModelStatusRx,
|
||||
SubghzTestPacketModelStatusOnlyRx,
|
||||
SubghzTestPacketModelStatusTx,
|
||||
} SubghzTestPacketModelStatus;
|
||||
SubGhzTestPacketModelStatusRx,
|
||||
SubGhzTestPacketModelStatusOnlyRx,
|
||||
SubGhzTestPacketModelStatusTx,
|
||||
} SubGhzTestPacketModelStatus;
|
||||
|
||||
typedef struct {
|
||||
uint8_t frequency;
|
||||
@@ -34,14 +34,14 @@ typedef struct {
|
||||
FuriHalSubGhzPath path;
|
||||
float rssi;
|
||||
size_t packets;
|
||||
SubghzTestPacketModelStatus status;
|
||||
} SubghzTestPacketModel;
|
||||
SubGhzTestPacketModelStatus status;
|
||||
} SubGhzTestPacketModel;
|
||||
|
||||
volatile bool subghz_test_packet_overrun = false;
|
||||
|
||||
void subghz_test_packet_set_callback(
|
||||
SubghzTestPacket* subghz_test_packet,
|
||||
SubghzTestPacketCallback callback,
|
||||
SubGhzTestPacket* subghz_test_packet,
|
||||
SubGhzTestPacketCallback callback,
|
||||
void* context) {
|
||||
furi_assert(subghz_test_packet);
|
||||
furi_assert(callback);
|
||||
@@ -51,34 +51,36 @@ void subghz_test_packet_set_callback(
|
||||
|
||||
static void subghz_test_packet_rx_callback(bool level, uint32_t duration, void* context) {
|
||||
furi_assert(context);
|
||||
SubghzTestPacket* instance = context;
|
||||
subghz_decoder_princeton_parse(instance->decoder, level, duration);
|
||||
SubGhzTestPacket* instance = context;
|
||||
subghz_decoder_princeton_for_testing_parse(instance->decoder, level, duration);
|
||||
}
|
||||
|
||||
static void subghz_test_packet_rx_pt_callback(SubGhzProtocolCommon* parser, void* context) {
|
||||
//todo
|
||||
static void subghz_test_packet_rx_pt_callback(SubGhzDecoderPrinceton* parser, void* context) {
|
||||
furi_assert(context);
|
||||
SubghzTestPacket* instance = context;
|
||||
SubGhzTestPacket* instance = context;
|
||||
instance->packet_rx++;
|
||||
}
|
||||
|
||||
static void subghz_test_packet_rssi_timer_callback(void* context) {
|
||||
furi_assert(context);
|
||||
SubghzTestPacket* instance = context;
|
||||
SubGhzTestPacket* instance = context;
|
||||
|
||||
with_view_model(
|
||||
instance->view, (SubghzTestPacketModel * model) {
|
||||
if(model->status == SubghzTestPacketModelStatusRx) {
|
||||
instance->view, (SubGhzTestPacketModel * model) {
|
||||
if(model->status == SubGhzTestPacketModelStatusRx) {
|
||||
model->rssi = furi_hal_subghz_get_rssi();
|
||||
model->packets = instance->packet_rx;
|
||||
} else if(model->status == SubghzTestPacketModelStatusTx) {
|
||||
model->packets = SUBGHZ_TEST_PACKET_COUNT -
|
||||
subghz_encoder_princeton_get_repeat_left(instance->encoder);
|
||||
} else if(model->status == SubGhzTestPacketModelStatusTx) {
|
||||
model->packets =
|
||||
SUBGHZ_TEST_PACKET_COUNT -
|
||||
subghz_encoder_princeton_for_testing_get_repeat_left(instance->encoder);
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
static void subghz_test_packet_draw(Canvas* canvas, SubghzTestPacketModel* model) {
|
||||
static void subghz_test_packet_draw(Canvas* canvas, SubGhzTestPacketModel* model) {
|
||||
char buffer[64];
|
||||
|
||||
canvas_set_color(canvas, ColorBlack);
|
||||
@@ -112,7 +114,7 @@ static void subghz_test_packet_draw(Canvas* canvas, SubghzTestPacketModel* model
|
||||
snprintf(buffer, sizeof(buffer), "Packets: %d", model->packets);
|
||||
canvas_draw_str(canvas, 0, 42, buffer);
|
||||
|
||||
if(model->status == SubghzTestPacketModelStatusRx) {
|
||||
if(model->status == SubGhzTestPacketModelStatusRx) {
|
||||
snprintf(
|
||||
buffer,
|
||||
sizeof(buffer),
|
||||
@@ -127,18 +129,18 @@ static void subghz_test_packet_draw(Canvas* canvas, SubghzTestPacketModel* model
|
||||
|
||||
static bool subghz_test_packet_input(InputEvent* event, void* context) {
|
||||
furi_assert(context);
|
||||
SubghzTestPacket* instance = context;
|
||||
SubGhzTestPacket* instance = context;
|
||||
|
||||
if(event->key == InputKeyBack || event->type != InputTypeShort) {
|
||||
return false;
|
||||
}
|
||||
|
||||
with_view_model(
|
||||
instance->view, (SubghzTestPacketModel * model) {
|
||||
if(model->status == SubghzTestPacketModelStatusRx) {
|
||||
instance->view, (SubGhzTestPacketModel * model) {
|
||||
if(model->status == SubGhzTestPacketModelStatusRx) {
|
||||
furi_hal_subghz_stop_async_rx();
|
||||
} else if(model->status == SubghzTestPacketModelStatusTx) {
|
||||
subghz_encoder_princeton_stop(instance->encoder, millis());
|
||||
} else if(model->status == SubGhzTestPacketModelStatusTx) {
|
||||
subghz_encoder_princeton_for_testing_stop(instance->encoder, millis());
|
||||
furi_hal_subghz_stop_async_tx();
|
||||
}
|
||||
|
||||
@@ -151,10 +153,10 @@ static bool subghz_test_packet_input(InputEvent* event, void* context) {
|
||||
} else if(event->key == InputKeyUp) {
|
||||
if(model->path < FuriHalSubGhzPath868) model->path++;
|
||||
} else if(event->key == InputKeyOk) {
|
||||
if(model->status == SubghzTestPacketModelStatusRx) {
|
||||
model->status = SubghzTestPacketModelStatusTx;
|
||||
if(model->status == SubGhzTestPacketModelStatusRx) {
|
||||
model->status = SubGhzTestPacketModelStatusTx;
|
||||
} else {
|
||||
model->status = SubghzTestPacketModelStatusRx;
|
||||
model->status = SubGhzTestPacketModelStatusRx;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -162,18 +164,18 @@ static bool subghz_test_packet_input(InputEvent* event, void* context) {
|
||||
furi_hal_subghz_set_frequency(subghz_frequencies_testing[model->frequency]);
|
||||
furi_hal_subghz_set_path(model->path);
|
||||
|
||||
if(model->status == SubghzTestPacketModelStatusRx) {
|
||||
if(model->status == SubGhzTestPacketModelStatusRx) {
|
||||
furi_hal_subghz_start_async_rx(subghz_test_packet_rx_callback, instance);
|
||||
} else {
|
||||
subghz_encoder_princeton_set(
|
||||
subghz_encoder_princeton_for_testing_set(
|
||||
instance->encoder,
|
||||
0x00AABBCC,
|
||||
SUBGHZ_TEST_PACKET_COUNT,
|
||||
subghz_frequencies_testing[model->frequency]);
|
||||
if(!furi_hal_subghz_start_async_tx(
|
||||
subghz_encoder_princeton_yield, instance->encoder)) {
|
||||
model->status = SubghzTestPacketModelStatusOnlyRx;
|
||||
instance->callback(SubghzTestPacketEventOnlyRx, instance->context);
|
||||
subghz_encoder_princeton_for_testing_yield, instance->encoder)) {
|
||||
model->status = SubGhzTestPacketModelStatusOnlyRx;
|
||||
instance->callback(SubGhzTestPacketEventOnlyRx, instance->context);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -185,19 +187,19 @@ static bool subghz_test_packet_input(InputEvent* event, void* context) {
|
||||
|
||||
void subghz_test_packet_enter(void* context) {
|
||||
furi_assert(context);
|
||||
SubghzTestPacket* instance = context;
|
||||
SubGhzTestPacket* instance = context;
|
||||
|
||||
furi_hal_subghz_reset();
|
||||
furi_hal_subghz_load_preset(FuriHalSubGhzPresetOok650Async);
|
||||
|
||||
with_view_model(
|
||||
instance->view, (SubghzTestPacketModel * model) {
|
||||
instance->view, (SubGhzTestPacketModel * model) {
|
||||
model->frequency = subghz_frequencies_433_92_testing;
|
||||
model->real_frequency =
|
||||
furi_hal_subghz_set_frequency(subghz_frequencies_testing[model->frequency]);
|
||||
model->path = FuriHalSubGhzPathIsolate; // isolate
|
||||
model->rssi = 0.0f;
|
||||
model->status = SubghzTestPacketModelStatusRx;
|
||||
model->status = SubGhzTestPacketModelStatusRx;
|
||||
return true;
|
||||
});
|
||||
|
||||
@@ -208,17 +210,17 @@ void subghz_test_packet_enter(void* context) {
|
||||
|
||||
void subghz_test_packet_exit(void* context) {
|
||||
furi_assert(context);
|
||||
SubghzTestPacket* instance = context;
|
||||
SubGhzTestPacket* instance = context;
|
||||
|
||||
osTimerStop(instance->timer);
|
||||
|
||||
// Reinitialize IC to default state
|
||||
with_view_model(
|
||||
instance->view, (SubghzTestPacketModel * model) {
|
||||
if(model->status == SubghzTestPacketModelStatusRx) {
|
||||
instance->view, (SubGhzTestPacketModel * model) {
|
||||
if(model->status == SubGhzTestPacketModelStatusRx) {
|
||||
furi_hal_subghz_stop_async_rx();
|
||||
} else if(model->status == SubghzTestPacketModelStatusTx) {
|
||||
subghz_encoder_princeton_stop(instance->encoder, millis());
|
||||
} else if(model->status == SubGhzTestPacketModelStatusTx) {
|
||||
subghz_encoder_princeton_for_testing_stop(instance->encoder, millis());
|
||||
furi_hal_subghz_stop_async_tx();
|
||||
}
|
||||
return true;
|
||||
@@ -226,12 +228,12 @@ void subghz_test_packet_exit(void* context) {
|
||||
furi_hal_subghz_sleep();
|
||||
}
|
||||
|
||||
SubghzTestPacket* subghz_test_packet_alloc() {
|
||||
SubghzTestPacket* instance = malloc(sizeof(SubghzTestPacket));
|
||||
SubGhzTestPacket* subghz_test_packet_alloc() {
|
||||
SubGhzTestPacket* instance = malloc(sizeof(SubGhzTestPacket));
|
||||
|
||||
// View allocation and configuration
|
||||
instance->view = view_alloc();
|
||||
view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(SubghzTestPacketModel));
|
||||
view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(SubGhzTestPacketModel));
|
||||
view_set_context(instance->view, instance);
|
||||
view_set_draw_callback(instance->view, (ViewDrawCallback)subghz_test_packet_draw);
|
||||
view_set_input_callback(instance->view, subghz_test_packet_input);
|
||||
@@ -241,26 +243,26 @@ SubghzTestPacket* subghz_test_packet_alloc() {
|
||||
instance->timer =
|
||||
osTimerNew(subghz_test_packet_rssi_timer_callback, osTimerPeriodic, instance, NULL);
|
||||
|
||||
instance->decoder = subghz_decoder_princeton_alloc();
|
||||
subghz_protocol_common_set_callback(
|
||||
(SubGhzProtocolCommon*)instance->decoder, subghz_test_packet_rx_pt_callback, instance);
|
||||
instance->encoder = subghz_encoder_princeton_alloc();
|
||||
instance->decoder = subghz_decoder_princeton_for_testing_alloc();
|
||||
subghz_decoder_princeton_for_testing_set_callback(
|
||||
instance->decoder, subghz_test_packet_rx_pt_callback, instance);
|
||||
instance->encoder = subghz_encoder_princeton_for_testing_alloc();
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
void subghz_test_packet_free(SubghzTestPacket* instance) {
|
||||
void subghz_test_packet_free(SubGhzTestPacket* instance) {
|
||||
furi_assert(instance);
|
||||
|
||||
subghz_decoder_princeton_free(instance->decoder);
|
||||
subghz_encoder_princeton_free(instance->encoder);
|
||||
subghz_decoder_princeton_for_testing_free(instance->decoder);
|
||||
subghz_encoder_princeton_for_testing_free(instance->encoder);
|
||||
|
||||
osTimerDelete(instance->timer);
|
||||
view_free(instance->view);
|
||||
free(instance);
|
||||
}
|
||||
|
||||
View* subghz_test_packet_get_view(SubghzTestPacket* instance) {
|
||||
View* subghz_test_packet_get_view(SubGhzTestPacket* instance) {
|
||||
furi_assert(instance);
|
||||
return instance->view;
|
||||
}
|
||||
|
Reference in New Issue
Block a user