diff options
author | あく <alleteam@gmail.com> | 2021-06-30 00:19:20 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-30 00:19:20 +0300 |
commit | e8211226f3a4f7609143ef02d6ef684b9fb41f00 (patch) | |
tree | 039c38f63468a9db444598304604bea1f4263279 /applications/subghz/views | |
parent | dce3665f63be6ce77691c00fe8e7ca59d3ecddf9 (diff) |
[FL-1496] SubGhz: Library, Cli, Application (#543)
* ApiHal: set frequency and path in one go. Drivers: proper frequency registers calculation for CC1101. Update subghz cli to match new api.
* SubGhz: preparation for parsers porting, tim2 sharing
* ApiHal: add interrupts API, move all TIM2 related things there.
* SubGhz: refactor protocol lib and add keeloq.
* SubGhz: proper init_set for keeloq manafacture key
* SubGhz: port more protocols to lib
* SubGhz: load keeloq manufacture keys from sd card (if any).
* SubGhz: format output from protocols.
* SubGhz: use default frequency in subghz_rx cli command.
* SubGhz: keeloq key types
* Fix compillation error when internal storage disabled
* SubGhz: minor cleanup
* SubGhz: properly handle timeout and reset signal in subghz_rx
* SubGhz: Worker, Capture View. Furi: emulate thread join.
* SubGhz: free strings on keeloq key load end
* SubGhz: update protocols reporting API, app refactoring and capture view, update API HAL usage.
* SubGhz: update dump formatting
* ApiHal: backport subghz preset to F5
* ApiHal: backport subghz frequency range to F5
Diffstat (limited to 'applications/subghz/views')
-rw-r--r-- | applications/subghz/views/subghz_capture.c | 188 | ||||
-rw-r--r-- | applications/subghz/views/subghz_capture.h | 11 | ||||
-rw-r--r-- | applications/subghz/views/subghz_static.c | 186 | ||||
-rw-r--r-- | applications/subghz/views/subghz_static.h | 11 | ||||
-rw-r--r-- | applications/subghz/views/subghz_test_basic.c | 201 | ||||
-rw-r--r-- | applications/subghz/views/subghz_test_basic.h | 11 | ||||
-rw-r--r-- | applications/subghz/views/subghz_test_packet.c | 208 | ||||
-rw-r--r-- | applications/subghz/views/subghz_test_packet.h | 11 |
8 files changed, 827 insertions, 0 deletions
diff --git a/applications/subghz/views/subghz_capture.c b/applications/subghz/views/subghz_capture.c new file mode 100644 index 00000000..075468a5 --- /dev/null +++ b/applications/subghz/views/subghz_capture.c @@ -0,0 +1,188 @@ +#include "subghz_capture.h" +#include "../subghz_i.h" + +#include <math.h> +#include <furi.h> +#include <api-hal.h> +#include <input/input.h> +#include <gui/elements.h> +#include <notification/notification-messages.h> + +#include <fl_subghz/subghz_worker.h> +#include <fl_subghz/protocols/subghz_protocol.h> + +struct SubghzCapture { + View* view; + SubGhzWorker* worker; + SubGhzProtocol* protocol; +}; + +typedef struct { + uint8_t frequency; + uint32_t real_frequency; + uint32_t counter; + string_t text; +} SubghzCaptureModel; + +static const char subghz_symbols[] = {'-', '\\', '|', '/'}; + +void subghz_capture_draw(Canvas* canvas, SubghzCaptureModel* model) { + char buffer[64]; + + canvas_set_color(canvas, ColorBlack); + canvas_set_font(canvas, FontPrimary); + + snprintf( + buffer, + sizeof(buffer), + "Capture: %03ld.%03ldMHz %c", + model->real_frequency / 1000000 % 1000, + model->real_frequency / 1000 % 1000, + subghz_symbols[model->counter % 4]); + canvas_draw_str(canvas, 2, 12, buffer); + + canvas_set_font(canvas, FontSecondary); + elements_multiline_text(canvas, 0, 24, string_get_cstr(model->text)); +} + +bool subghz_capture_input(InputEvent* event, void* context) { + furi_assert(context); + SubghzCapture* subghz_capture = context; + + if(event->key == InputKeyBack) { + return false; + } + + with_view_model( + subghz_capture->view, (SubghzCaptureModel * model) { + bool reconfigure = false; + if(event->type == InputTypeShort) { + if(event->key == InputKeyLeft) { + if(model->frequency > 0) model->frequency--; + reconfigure = true; + } else if(event->key == InputKeyRight) { + if(model->frequency < subghz_frequencies_count - 1) model->frequency++; + reconfigure = true; + } + } + + if(reconfigure) { + api_hal_subghz_idle(); + model->real_frequency = + api_hal_subghz_set_frequency_and_path(subghz_frequencies[model->frequency]); + api_hal_subghz_rx(); + } + + return reconfigure; + }); + + return true; +} + +void subghz_capture_text_callback(string_t text, void* context) { + furi_assert(context); + SubghzCapture* subghz_capture = context; + + with_view_model( + subghz_capture->view, (SubghzCaptureModel * model) { + model->counter++; + string_set(model->text, text); + return true; + }); +} + +void subghz_capture_enter(void* context) { + furi_assert(context); + SubghzCapture* subghz_capture = context; + + api_hal_subghz_reset(); + api_hal_subghz_idle(); + api_hal_subghz_load_preset(ApiHalSubGhzPresetMP); + + with_view_model( + subghz_capture->view, (SubghzCaptureModel * model) { + model->frequency = subghz_frequencies_433_92; + model->real_frequency = + api_hal_subghz_set_frequency_and_path(subghz_frequencies[model->frequency]); + return true; + }); + + hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo, GpioSpeedLow); + + api_hal_subghz_set_capture_callback(subghz_worker_rx_callback, subghz_capture->worker); + api_hal_subghz_enable_capture(); + + subghz_worker_start(subghz_capture->worker); + + api_hal_subghz_flush_rx(); + api_hal_subghz_rx(); +} + +void subghz_capture_exit(void* context) { + furi_assert(context); + SubghzCapture* subghz_capture = context; + + subghz_worker_stop(subghz_capture->worker); + + api_hal_subghz_disable_capture(); + api_hal_subghz_init(); +} + +uint32_t subghz_capture_back(void* context) { + return SubGhzViewMenu; +} + +SubghzCapture* subghz_capture_alloc() { + SubghzCapture* subghz_capture = furi_alloc(sizeof(SubghzCapture)); + + // View allocation and configuration + subghz_capture->view = view_alloc(); + view_allocate_model(subghz_capture->view, ViewModelTypeLocking, sizeof(SubghzCaptureModel)); + view_set_context(subghz_capture->view, subghz_capture); + view_set_draw_callback(subghz_capture->view, (ViewDrawCallback)subghz_capture_draw); + view_set_input_callback(subghz_capture->view, subghz_capture_input); + view_set_enter_callback(subghz_capture->view, subghz_capture_enter); + view_set_exit_callback(subghz_capture->view, subghz_capture_exit); + view_set_previous_callback(subghz_capture->view, subghz_capture_back); + + with_view_model( + subghz_capture->view, (SubghzCaptureModel * model) { + string_init(model->text); + return true; + }); + + subghz_capture->worker = subghz_worker_alloc(); + subghz_capture->protocol = subghz_protocol_alloc(); + + subghz_worker_set_overrun_callback( + subghz_capture->worker, (SubGhzWorkerOverrunCallback)subghz_protocol_reset); + subghz_worker_set_pair_callback( + subghz_capture->worker, (SubGhzWorkerPairCallback)subghz_protocol_parse); + subghz_worker_set_context(subghz_capture->worker, subghz_capture->protocol); + + subghz_protocol_load_keeloq_file(subghz_capture->protocol, "/assets/subghz/keeloq_mfcodes"); + subghz_protocol_enable_dump( + subghz_capture->protocol, subghz_capture_text_callback, subghz_capture); + + return subghz_capture; +} + +void subghz_capture_free(SubghzCapture* subghz_capture) { + furi_assert(subghz_capture); + + subghz_protocol_free(subghz_capture->protocol); + subghz_worker_free(subghz_capture->worker); + + with_view_model( + subghz_capture->view, (SubghzCaptureModel * model) { + string_clear(model->text); + return true; + }); + view_free(subghz_capture->view); + free(subghz_capture); +} + +View* subghz_capture_get_view(SubghzCapture* subghz_capture) { + furi_assert(subghz_capture); + return subghz_capture->view; +} diff --git a/applications/subghz/views/subghz_capture.h b/applications/subghz/views/subghz_capture.h new file mode 100644 index 00000000..84159cf5 --- /dev/null +++ b/applications/subghz/views/subghz_capture.h @@ -0,0 +1,11 @@ +#pragma once + +#include <gui/view.h> + +typedef struct SubghzCapture SubghzCapture; + +SubghzCapture* subghz_capture_alloc(); + +void subghz_capture_free(SubghzCapture* subghz_capture); + +View* subghz_capture_get_view(SubghzCapture* subghz_capture); diff --git a/applications/subghz/views/subghz_static.c b/applications/subghz/views/subghz_static.c new file mode 100644 index 00000000..18da91f6 --- /dev/null +++ b/applications/subghz/views/subghz_static.c @@ -0,0 +1,186 @@ +#include "subghz_static.h" +#include "../subghz_i.h" + +#include <math.h> +#include <furi.h> +#include <api-hal.h> +#include <input/input.h> +#include <notification/notification-messages.h> + +static const uint8_t subghz_static_keys[][4] = { + {0x74, 0xBA, 0xDE}, + {0x74, 0xBA, 0xDD}, + {0x74, 0xBA, 0xDB}, + {0xE3, 0x4A, 0x4E}, +}; + +#define SUBGHZ_PT_ONE 376 +#define SUBGHZ_PT_ZERO (SUBGHZ_PT_ONE * 3) +#define SUBGHZ_PT_GUARD 10600 + +struct SubghzStatic { + View* view; +}; + +typedef enum { + SubghzStaticStatusRx, + SubghzStaticStatusTx, +} SubghzStaticStatus; + +typedef struct { + uint8_t frequency; + uint32_t real_frequency; + uint8_t button; +} SubghzStaticModel; + +void subghz_static_draw(Canvas* canvas, SubghzStaticModel* model) { + char buffer[64]; + + canvas_set_color(canvas, ColorBlack); + canvas_set_font(canvas, FontPrimary); + canvas_draw_str(canvas, 2, 12, "CC1101 Static"); + + 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, 2, 24, buffer); + snprintf(buffer, sizeof(buffer), "Key: %d", model->button); + canvas_draw_str(canvas, 2, 36, buffer); +} + +bool subghz_static_input(InputEvent* event, void* context) { + furi_assert(context); + SubghzStatic* subghz_static = context; + + if(event->key == InputKeyBack) { + return false; + } + + with_view_model( + subghz_static->view, (SubghzStaticModel * model) { + bool reconfigure = false; + if(event->type == InputTypeShort) { + if(event->key == InputKeyLeft) { + if(model->frequency > 0) model->frequency--; + reconfigure = true; + } else if(event->key == InputKeyRight) { + if(model->frequency < subghz_frequencies_count - 1) model->frequency++; + reconfigure = true; + } else if(event->key == InputKeyDown) { + if(model->button > 0) model->button--; + } else if(event->key == InputKeyUp) { + if(model->button < 3) model->button++; + } + } + + if(reconfigure) { + api_hal_subghz_idle(); + model->real_frequency = + api_hal_subghz_set_frequency_and_path(subghz_frequencies[model->frequency]); + api_hal_subghz_tx(); + } + + if(event->key == InputKeyOk) { + if(event->type == InputTypePress) { + const uint8_t* key = subghz_static_keys[model->button]; + + NotificationApp* notification = furi_record_open("notification"); + notification_message_block(notification, &sequence_set_red_255); + __disable_irq(); + for(uint8_t r = 0; r < 20; r++) { + //Payload + for(uint8_t i = 0; i < 24; i++) { + uint8_t byte = i / 8; + uint8_t bit = i % 8; + bool value = (key[byte] >> (7 - bit)) & 1; + // Payload send + hal_gpio_write(&gpio_cc1101_g0, false); + delay_us(value ? SUBGHZ_PT_ONE : SUBGHZ_PT_ZERO); + hal_gpio_write(&gpio_cc1101_g0, true); + delay_us(value ? SUBGHZ_PT_ZERO : SUBGHZ_PT_ONE); + } + // Last bit + hal_gpio_write(&gpio_cc1101_g0, false); + delay_us(SUBGHZ_PT_ONE); + hal_gpio_write(&gpio_cc1101_g0, true); + // Guard time + delay_us(10600); + } + __enable_irq(); + notification_message(notification, &sequence_reset_red); + furi_record_close("notification"); + } + } + + return true; + }); + + return true; +} + +void subghz_static_enter(void* context) { + furi_assert(context); + SubghzStatic* subghz_static = context; + + api_hal_subghz_reset(); + api_hal_subghz_load_preset(ApiHalSubGhzPresetOokAsync); + + hal_gpio_init(&gpio_cc1101_g0, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow); + hal_gpio_write(&gpio_cc1101_g0, true); + + with_view_model( + subghz_static->view, (SubghzStaticModel * model) { + model->frequency = subghz_frequencies_433_92; + model->real_frequency = + api_hal_subghz_set_frequency_and_path(subghz_frequencies[model->frequency]); + model->button = 0; + return true; + }); + + api_hal_subghz_tx(); +} + +void subghz_static_exit(void* context) { + furi_assert(context); + // SubghzStatic* subghz_static = context; + + // Reinitialize IC to default state + api_hal_subghz_init(); +} + +uint32_t subghz_static_back(void* context) { + return SubGhzViewMenu; +} + +SubghzStatic* subghz_static_alloc() { + SubghzStatic* subghz_static = furi_alloc(sizeof(SubghzStatic)); + + // View allocation and configuration + subghz_static->view = view_alloc(); + view_allocate_model(subghz_static->view, ViewModelTypeLockFree, sizeof(SubghzStaticModel)); + view_set_context(subghz_static->view, subghz_static); + view_set_draw_callback(subghz_static->view, (ViewDrawCallback)subghz_static_draw); + view_set_input_callback(subghz_static->view, subghz_static_input); + view_set_enter_callback(subghz_static->view, subghz_static_enter); + view_set_exit_callback(subghz_static->view, subghz_static_exit); + view_set_previous_callback(subghz_static->view, subghz_static_back); + + return subghz_static; +} + +void subghz_static_free(SubghzStatic* subghz_static) { + furi_assert(subghz_static); + view_free(subghz_static->view); + free(subghz_static); +} + +View* subghz_static_get_view(SubghzStatic* subghz_static) { + furi_assert(subghz_static); + return subghz_static->view; +} diff --git a/applications/subghz/views/subghz_static.h b/applications/subghz/views/subghz_static.h new file mode 100644 index 00000000..f80f8735 --- /dev/null +++ b/applications/subghz/views/subghz_static.h @@ -0,0 +1,11 @@ +#pragma once + +#include <gui/view.h> + +typedef struct SubghzStatic SubghzStatic; + +SubghzStatic* subghz_static_alloc(); + +void subghz_static_free(SubghzStatic* subghz_static); + +View* subghz_static_get_view(SubghzStatic* subghz_static); diff --git a/applications/subghz/views/subghz_test_basic.c b/applications/subghz/views/subghz_test_basic.c new file mode 100644 index 00000000..26c74e3c --- /dev/null +++ b/applications/subghz/views/subghz_test_basic.c @@ -0,0 +1,201 @@ +#include "subghz_test_basic.h" +#include "../subghz_i.h" + +#include <math.h> +#include <furi.h> +#include <api-hal.h> +#include <input/input.h> + +struct SubghzTestBasic { + View* view; + osTimerId timer; +}; + +typedef enum { + SubghzTestBasicModelStatusRx, + SubghzTestBasicModelStatusTx, +} SubghzTestBasicModelStatus; + +typedef struct { + uint8_t frequency; + uint32_t real_frequency; + ApiHalSubGhzPath path; + float rssi; + SubghzTestBasicModelStatus status; +} SubghzTestBasicModel; + +void subghz_test_basic_draw(Canvas* canvas, SubghzTestBasicModel* model) { + char buffer[64]; + + canvas_set_color(canvas, ColorBlack); + canvas_set_font(canvas, FontPrimary); + canvas_draw_str(canvas, 2, 12, "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, 2, 24, buffer); + // Path + char* path_name = "Unknown"; + if(model->path == ApiHalSubGhzPathIsolate) { + path_name = "isolate"; + } else if(model->path == ApiHalSubGhzPath433) { + path_name = "433MHz"; + } else if(model->path == ApiHalSubGhzPath315) { + path_name = "315MHz"; + } else if(model->path == ApiHalSubGhzPath868) { + path_name = "868MHz"; + } + snprintf(buffer, sizeof(buffer), "Path: %d - %s", model->path, path_name); + canvas_draw_str(canvas, 2, 36, buffer); + if(model->status == SubghzTestBasicModelStatusRx) { + snprintf( + buffer, + sizeof(buffer), + "RSSI: %ld.%ld dBm", + (int32_t)(model->rssi), + (int32_t)fabs(model->rssi * 10) % 10); + canvas_draw_str(canvas, 2, 48, buffer); + } else { + canvas_draw_str(canvas, 2, 48, "TX"); + } +} + +bool subghz_test_basic_input(InputEvent* event, void* context) { + furi_assert(context); + SubghzTestBasic* subghz_test_basic = context; + + if(event->key == InputKeyBack) { + return false; + } + + with_view_model( + subghz_test_basic->view, (SubghzTestBasicModel * model) { + osTimerStop(subghz_test_basic->timer); + api_hal_subghz_idle(); + + if(event->type == InputTypeShort) { + 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 < ApiHalSubGhzPath868) model->path++; + } else if(event->key == InputKeyOk) { + if(model->status == SubghzTestBasicModelStatusTx) { + model->status = SubghzTestBasicModelStatusRx; + } else { + model->status = SubghzTestBasicModelStatusTx; + } + } + + model->real_frequency = + api_hal_subghz_set_frequency(subghz_frequencies[model->frequency]); + api_hal_subghz_set_path(model->path); + } + + if(model->status == SubghzTestBasicModelStatusRx) { + hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo, GpioSpeedLow); + api_hal_subghz_rx(); + osTimerStart(subghz_test_basic->timer, 1024 / 4); + } else { + hal_gpio_init(&gpio_cc1101_g0, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow); + hal_gpio_write(&gpio_cc1101_g0, false); + api_hal_subghz_tx(); + } + + return true; + }); + + return true; +} + +void subghz_test_basic_enter(void* context) { + furi_assert(context); + SubghzTestBasic* subghz_test_basic = context; + + api_hal_subghz_reset(); + api_hal_subghz_load_preset(ApiHalSubGhzPresetOokAsync); + + hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo, GpioSpeedLow); + + with_view_model( + subghz_test_basic->view, (SubghzTestBasicModel * model) { + model->frequency = subghz_frequencies_433_92; // 433 + model->real_frequency = + api_hal_subghz_set_frequency(subghz_frequencies[model->frequency]); + model->path = ApiHalSubGhzPathIsolate; // isolate + model->rssi = 0.0f; + model->status = SubghzTestBasicModelStatusRx; + return true; + }); + + api_hal_subghz_rx(); + + osTimerStart(subghz_test_basic->timer, 1024 / 4); +} + +void subghz_test_basic_exit(void* context) { + furi_assert(context); + SubghzTestBasic* subghz_test_basic = context; + + osTimerStop(subghz_test_basic->timer); + + // Reinitialize IC to default state + api_hal_subghz_init(); +} + +void subghz_test_basic_rssi_timer_callback(void* context) { + furi_assert(context); + SubghzTestBasic* subghz_test_basic = context; + + with_view_model( + subghz_test_basic->view, (SubghzTestBasicModel * model) { + model->rssi = api_hal_subghz_get_rssi(); + return true; + }); +} + +uint32_t subghz_test_basic_back(void* context) { + return SubGhzViewMenu; +} + +SubghzTestBasic* subghz_test_basic_alloc() { + SubghzTestBasic* subghz_test_basic = furi_alloc(sizeof(SubghzTestBasic)); + + // View allocation and configuration + subghz_test_basic->view = view_alloc(); + view_allocate_model( + subghz_test_basic->view, ViewModelTypeLockFree, sizeof(SubghzTestBasicModel)); + view_set_context(subghz_test_basic->view, subghz_test_basic); + view_set_draw_callback(subghz_test_basic->view, (ViewDrawCallback)subghz_test_basic_draw); + view_set_input_callback(subghz_test_basic->view, subghz_test_basic_input); + view_set_enter_callback(subghz_test_basic->view, subghz_test_basic_enter); + view_set_exit_callback(subghz_test_basic->view, subghz_test_basic_exit); + view_set_previous_callback(subghz_test_basic->view, subghz_test_basic_back); + + subghz_test_basic->timer = osTimerNew( + subghz_test_basic_rssi_timer_callback, osTimerPeriodic, subghz_test_basic, NULL); + + return subghz_test_basic; +} + +void subghz_test_basic_free(SubghzTestBasic* subghz_test_basic) { + furi_assert(subghz_test_basic); + osTimerDelete(subghz_test_basic->timer); + view_free(subghz_test_basic->view); + free(subghz_test_basic); +} + +View* subghz_test_basic_get_view(SubghzTestBasic* subghz_test_basic) { + furi_assert(subghz_test_basic); + return subghz_test_basic->view; +} diff --git a/applications/subghz/views/subghz_test_basic.h b/applications/subghz/views/subghz_test_basic.h new file mode 100644 index 00000000..5229c7c6 --- /dev/null +++ b/applications/subghz/views/subghz_test_basic.h @@ -0,0 +1,11 @@ +#pragma once + +#include <gui/view.h> + +typedef struct SubghzTestBasic SubghzTestBasic; + +SubghzTestBasic* subghz_test_basic_alloc(); + +void subghz_test_basic_free(SubghzTestBasic* subghz_test_basic); + +View* subghz_test_basic_get_view(SubghzTestBasic* subghz_test_basic); diff --git a/applications/subghz/views/subghz_test_packet.c b/applications/subghz/views/subghz_test_packet.c new file mode 100644 index 00000000..28c35c72 --- /dev/null +++ b/applications/subghz/views/subghz_test_packet.c @@ -0,0 +1,208 @@ +#include "subghz_test_packet.h" +#include "../subghz_i.h" + +#include <math.h> +#include <furi.h> +#include <api-hal.h> +#include <input/input.h> + +static const uint8_t subghz_test_packet_data[] = { + 0x30, // 48bytes to transmit + 'F', 'L', 'I', 'P', 'P', 'E', 'R', ' ', 'T', 'E', 'S', 'T', ' ', 'P', 'A', 'C', + 'K', 'E', 'T', ' ', 'D', 'A', 'T', 'A', ' ', 'A', 'N', 'D', ' ', 'P', 'A', 'D', + 'F', 'L', 'I', 'P', 'P', 'E', 'R', ' ', 'T', 'E', 'S', 'T', ' ', 'P', 'A', 'C', + +}; + +struct SubghzTestPacket { + View* view; + osTimerId timer; +}; + +typedef enum { + SubghzTestPacketModelStatusRx, + SubghzTestPacketModelStatusTx, +} SubghzTestPacketModelStatus; + +typedef struct { + uint8_t frequency; + uint32_t real_frequency; + ApiHalSubGhzPath path; + float rssi; + SubghzTestPacketModelStatus status; +} SubghzTestPacketModel; + +void subghz_test_packet_draw(Canvas* canvas, SubghzTestPacketModel* model) { + char buffer[64]; + + canvas_set_color(canvas, ColorBlack); + canvas_set_font(canvas, FontPrimary); + canvas_draw_str(canvas, 2, 12, "CC1101 Packet 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, 2, 24, buffer); + // Path + char* path_name = "Unknown"; + if(model->path == ApiHalSubGhzPathIsolate) { + path_name = "isolate"; + } else if(model->path == ApiHalSubGhzPath433) { + path_name = "433MHz"; + } else if(model->path == ApiHalSubGhzPath315) { + path_name = "315MHz"; + } else if(model->path == ApiHalSubGhzPath868) { + path_name = "868MHz"; + } + snprintf(buffer, sizeof(buffer), "Path: %d - %s", model->path, path_name); + canvas_draw_str(canvas, 2, 36, buffer); + if(model->status == SubghzTestPacketModelStatusRx) { + snprintf( + buffer, + sizeof(buffer), + "RSSI: %ld.%ld dBm", + (int32_t)(model->rssi), + (int32_t)fabs(model->rssi * 10) % 10); + canvas_draw_str(canvas, 2, 48, buffer); + } else { + canvas_draw_str(canvas, 2, 48, "TX"); + } +} + +bool subghz_test_packet_input(InputEvent* event, void* context) { + furi_assert(context); + SubghzTestPacket* subghz_test_packet = context; + + if(event->key == InputKeyBack) { + return false; + } + + with_view_model( + subghz_test_packet->view, (SubghzTestPacketModel * model) { + osTimerStop(subghz_test_packet->timer); + api_hal_subghz_idle(); + + if(event->type == InputTypeShort) { + 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 < ApiHalSubGhzPath868) model->path++; + } else if(event->key == InputKeyOk) { + if(model->status == SubghzTestPacketModelStatusTx) { + model->status = SubghzTestPacketModelStatusRx; + } else { + model->status = SubghzTestPacketModelStatusTx; + } + } + + model->real_frequency = + api_hal_subghz_set_frequency(subghz_frequencies[model->frequency]); + api_hal_subghz_set_path(model->path); + } + + if(model->status == SubghzTestPacketModelStatusRx) { + api_hal_subghz_rx(); + osTimerStart(subghz_test_packet->timer, 1024 / 4); + } else { + api_hal_subghz_write_packet( + subghz_test_packet_data, sizeof(subghz_test_packet_data)); + api_hal_subghz_tx(); + } + + return true; + }); + + return true; +} + +void subghz_test_packet_enter(void* context) { + furi_assert(context); + SubghzTestPacket* subghz_test_packet = context; + + api_hal_subghz_reset(); + api_hal_subghz_load_preset(ApiHalSubGhzPreset2FskPacket); + + hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo, GpioSpeedLow); + + with_view_model( + subghz_test_packet->view, (SubghzTestPacketModel * model) { + model->frequency = subghz_frequencies_433_92; + model->real_frequency = + api_hal_subghz_set_frequency(subghz_frequencies[model->frequency]); + model->path = ApiHalSubGhzPathIsolate; // isolate + model->rssi = 0.0f; + model->status = SubghzTestPacketModelStatusRx; + return true; + }); + + api_hal_subghz_rx(); + + osTimerStart(subghz_test_packet->timer, 1024 / 4); +} + +void subghz_test_packet_exit(void* context) { + furi_assert(context); + SubghzTestPacket* subghz_test_packet = context; + + osTimerStop(subghz_test_packet->timer); + + // Reinitialize IC to default state + api_hal_subghz_init(); +} + +void subghz_test_packet_rssi_timer_callback(void* context) { + furi_assert(context); + SubghzTestPacket* subghz_test_packet = context; + + with_view_model( + subghz_test_packet->view, (SubghzTestPacketModel * model) { + model->rssi = api_hal_subghz_get_rssi(); + return true; + }); +} + +uint32_t subghz_test_packet_back(void* context) { + return SubGhzViewMenu; +} + +SubghzTestPacket* subghz_test_packet_alloc() { + SubghzTestPacket* subghz_test_packet = furi_alloc(sizeof(SubghzTestPacket)); + + // View allocation and configuration + subghz_test_packet->view = view_alloc(); + view_allocate_model( + subghz_test_packet->view, ViewModelTypeLockFree, sizeof(SubghzTestPacketModel)); + view_set_context(subghz_test_packet->view, subghz_test_packet); + view_set_draw_callback(subghz_test_packet->view, (ViewDrawCallback)subghz_test_packet_draw); + view_set_input_callback(subghz_test_packet->view, subghz_test_packet_input); + view_set_enter_callback(subghz_test_packet->view, subghz_test_packet_enter); + view_set_exit_callback(subghz_test_packet->view, subghz_test_packet_exit); + view_set_previous_callback(subghz_test_packet->view, subghz_test_packet_back); + + subghz_test_packet->timer = osTimerNew( + subghz_test_packet_rssi_timer_callback, osTimerPeriodic, subghz_test_packet, NULL); + + return subghz_test_packet; +} + +void subghz_test_packet_free(SubghzTestPacket* subghz_test_packet) { + furi_assert(subghz_test_packet); + osTimerDelete(subghz_test_packet->timer); + view_free(subghz_test_packet->view); + free(subghz_test_packet); +} + +View* subghz_test_packet_get_view(SubghzTestPacket* subghz_test_packet) { + furi_assert(subghz_test_packet); + return subghz_test_packet->view; +} diff --git a/applications/subghz/views/subghz_test_packet.h b/applications/subghz/views/subghz_test_packet.h new file mode 100644 index 00000000..00fae10e --- /dev/null +++ b/applications/subghz/views/subghz_test_packet.h @@ -0,0 +1,11 @@ +#pragma once + +#include <gui/view.h> + +typedef struct SubghzTestPacket SubghzTestPacket; + +SubghzTestPacket* subghz_test_packet_alloc(); + +void subghz_test_packet_free(SubghzTestPacket* subghz_test_packet); + +View* subghz_test_packet_get_view(SubghzTestPacket* subghz_test_packet); |