flipperzero-firmware/applications/subghz/views/subghz_test_carrier.c

212 lines
7.1 KiB
C
Raw Normal View History

2021-08-12 14:42:56 +00:00
#include "subghz_test_carrier.h"
#include "../subghz_i.h"
#include <math.h>
#include <furi.h>
2021-08-08 18:03:25 +00:00
#include <furi-hal.h>
#include <input/input.h>
2021-08-12 14:42:56 +00:00
struct SubghzTestCarrier {
View* view;
osTimerId timer;
SubghzTestCarrierCallback callback;
void* context;
};
typedef enum {
2021-08-12 14:42:56 +00:00
SubghzTestCarrierModelStatusRx,
SubghzTestCarrierModelStatusTx,
} SubghzTestCarrierModelStatus;
typedef struct {
uint8_t frequency;
uint32_t real_frequency;
2021-08-08 18:03:25 +00:00
FuriHalSubGhzPath path;
float rssi;
2021-08-12 14:42:56 +00:00
SubghzTestCarrierModelStatus status;
} SubghzTestCarrierModel;
void subghz_test_carrier_set_callback(
SubghzTestCarrier* subghz_test_carrier,
SubghzTestCarrierCallback callback,
void* context) {
furi_assert(subghz_test_carrier);
furi_assert(callback);
subghz_test_carrier->callback = callback;
subghz_test_carrier->context = context;
}
2021-08-12 14:42:56 +00:00
void subghz_test_carrier_draw(Canvas* canvas, SubghzTestCarrierModel* model) {
char buffer[64];
canvas_set_color(canvas, ColorBlack);
canvas_set_font(canvas, FontPrimary);
canvas_draw_str(canvas, 0, 8, "CC1101 Basic Test");
canvas_set_font(canvas, FontSecondary);
// Frequency
snprintf(
buffer,
sizeof(buffer),
"Freq: %03ld.%03ld.%03ld Hz",
model->real_frequency / 1000000 % 1000,
model->real_frequency / 1000 % 1000,
model->real_frequency % 1000);
canvas_draw_str(canvas, 0, 20, buffer);
// Path
char* path_name = "Unknown";
2021-08-08 18:03:25 +00:00
if(model->path == FuriHalSubGhzPathIsolate) {
path_name = "isolate";
2021-08-08 18:03:25 +00:00
} else if(model->path == FuriHalSubGhzPath433) {
path_name = "433MHz";
2021-08-08 18:03:25 +00:00
} else if(model->path == FuriHalSubGhzPath315) {
path_name = "315MHz";
2021-08-08 18:03:25 +00:00
} else if(model->path == FuriHalSubGhzPath868) {
path_name = "868MHz";
}
snprintf(buffer, sizeof(buffer), "Path: %d - %s", model->path, path_name);
canvas_draw_str(canvas, 0, 31, buffer);
2021-08-12 14:42:56 +00:00
if(model->status == SubghzTestCarrierModelStatusRx) {
snprintf(
buffer,
sizeof(buffer),
"RSSI: %ld.%ld dBm",
(int32_t)(model->rssi),
(int32_t)fabs(model->rssi * 10) % 10);
canvas_draw_str(canvas, 0, 42, buffer);
} else {
canvas_draw_str(canvas, 0, 42, "TX");
}
}
2021-08-12 14:42:56 +00:00
bool subghz_test_carrier_input(InputEvent* event, void* context) {
furi_assert(context);
2021-08-12 14:42:56 +00:00
SubghzTestCarrier* subghz_test_carrier = context;
if(event->key == InputKeyBack || event->type != InputTypeShort) {
return false;
}
with_view_model(
2021-08-12 14:42:56 +00:00
subghz_test_carrier->view, (SubghzTestCarrierModel * model) {
2021-08-08 18:03:25 +00:00
furi_hal_subghz_idle();
if(event->key == InputKeyLeft) {
if(model->frequency > 0) model->frequency--;
} else if(event->key == InputKeyRight) {
if(model->frequency < subghz_frequencies_count - 1) model->frequency++;
} else if(event->key == InputKeyDown) {
if(model->path > 0) model->path--;
} else if(event->key == InputKeyUp) {
if(model->path < FuriHalSubGhzPath868) model->path++;
} else if(event->key == InputKeyOk) {
if(model->status == SubghzTestCarrierModelStatusTx) {
model->status = SubghzTestCarrierModelStatusRx;
} else {
model->status = SubghzTestCarrierModelStatusTx;
}
}
model->real_frequency =
furi_hal_subghz_set_frequency(subghz_frequencies[model->frequency]);
furi_hal_subghz_set_path(model->path);
2021-08-12 14:42:56 +00:00
if(model->status == SubghzTestCarrierModelStatusRx) {
2021-05-08 19:24:05 +00:00
hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo, GpioSpeedLow);
2021-08-08 18:03:25 +00:00
furi_hal_subghz_rx();
} else {
2021-05-08 19:24:05 +00:00
hal_gpio_init(&gpio_cc1101_g0, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
hal_gpio_write(&gpio_cc1101_g0, true);
if(!furi_hal_subghz_tx()) {
hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo, GpioSpeedLow);
subghz_test_carrier->callback(
SubghzTestCarrierEventOnlyRx, subghz_test_carrier->context);
}
}
return true;
});
return true;
}
2021-08-12 14:42:56 +00:00
void subghz_test_carrier_enter(void* context) {
furi_assert(context);
2021-08-12 14:42:56 +00:00
SubghzTestCarrier* subghz_test_carrier = context;
2021-08-08 18:03:25 +00:00
furi_hal_subghz_reset();
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
furi_hal_subghz_load_preset(FuriHalSubGhzPresetOok650Async);
2021-05-08 19:24:05 +00:00
hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo, GpioSpeedLow);
with_view_model(
2021-08-12 14:42:56 +00:00
subghz_test_carrier->view, (SubghzTestCarrierModel * model) {
model->frequency = subghz_frequencies_433_92; // 433
model->real_frequency =
2021-08-08 18:03:25 +00:00
furi_hal_subghz_set_frequency(subghz_frequencies[model->frequency]);
model->path = FuriHalSubGhzPathIsolate; // isolate
model->rssi = 0.0f;
2021-08-12 14:42:56 +00:00
model->status = SubghzTestCarrierModelStatusRx;
return true;
});
2021-08-08 18:03:25 +00:00
furi_hal_subghz_rx();
2021-08-12 14:42:56 +00:00
osTimerStart(subghz_test_carrier->timer, 1024 / 4);
}
2021-08-12 14:42:56 +00:00
void subghz_test_carrier_exit(void* context) {
furi_assert(context);
2021-08-12 14:42:56 +00:00
SubghzTestCarrier* subghz_test_carrier = context;
2021-08-12 14:42:56 +00:00
osTimerStop(subghz_test_carrier->timer);
// Reinitialize IC to default state
2021-08-08 18:03:25 +00:00
furi_hal_subghz_sleep();
}
2021-08-12 14:42:56 +00:00
void subghz_test_carrier_rssi_timer_callback(void* context) {
furi_assert(context);
2021-08-12 14:42:56 +00:00
SubghzTestCarrier* subghz_test_carrier = context;
with_view_model(
2021-08-12 14:42:56 +00:00
subghz_test_carrier->view, (SubghzTestCarrierModel * model) {
if(model->status == SubghzTestCarrierModelStatusRx) {
model->rssi = furi_hal_subghz_get_rssi();
return true;
}
return false;
});
}
2021-08-12 14:42:56 +00:00
SubghzTestCarrier* subghz_test_carrier_alloc() {
SubghzTestCarrier* subghz_test_carrier = furi_alloc(sizeof(SubghzTestCarrier));
// View allocation and configuration
2021-08-12 14:42:56 +00:00
subghz_test_carrier->view = view_alloc();
view_allocate_model(
subghz_test_carrier->view, ViewModelTypeLocking, sizeof(SubghzTestCarrierModel));
2021-08-12 14:42:56 +00:00
view_set_context(subghz_test_carrier->view, subghz_test_carrier);
view_set_draw_callback(subghz_test_carrier->view, (ViewDrawCallback)subghz_test_carrier_draw);
view_set_input_callback(subghz_test_carrier->view, subghz_test_carrier_input);
view_set_enter_callback(subghz_test_carrier->view, subghz_test_carrier_enter);
view_set_exit_callback(subghz_test_carrier->view, subghz_test_carrier_exit);
subghz_test_carrier->timer = osTimerNew(
subghz_test_carrier_rssi_timer_callback, osTimerPeriodic, subghz_test_carrier, NULL);
return subghz_test_carrier;
}
2021-08-12 14:42:56 +00:00
void subghz_test_carrier_free(SubghzTestCarrier* subghz_test_carrier) {
furi_assert(subghz_test_carrier);
osTimerDelete(subghz_test_carrier->timer);
view_free(subghz_test_carrier->view);
free(subghz_test_carrier);
}
2021-08-12 14:42:56 +00:00
View* subghz_test_carrier_get_view(SubghzTestCarrier* subghz_test_carrier) {
furi_assert(subghz_test_carrier);
return subghz_test_carrier->view;
}