unleashed-firmware/applications/main/subghz/views/subghz_frequency_analyzer.c

462 lines
16 KiB
C
Raw Normal View History

#include "subghz_frequency_analyzer.h"
#include "../subghz_i.h"
#include <math.h>
#include <furi.h>
#include <furi_hal.h>
#include <input/input.h>
#include <gui/elements.h>
#include <notification/notification_messages.h>
#include "../helpers/subghz_frequency_analyzer_worker.h"
#include "../helpers/subghz_frequency_analyzer_log_item_array.h"
#include <assets_icons.h>
#define LOG_FREQUENCY_MAX_ITEMS 60 // uint8_t (limited by 'seq' of SubGhzFrequencyAnalyzerLogItem)
#define RSSI_OFFSET 74
#define RSSI_MAX 53 // 127 - RSSI_OFFSET
#define SNPRINTF_FREQUENCY(buff, freq) \
snprintf(buff, sizeof(buff), "%03ld.%03ld", freq / 1000000 % 1000, freq / 1000 % 1000);
typedef enum {
SubGhzFrequencyAnalyzerStatusIDLE,
} SubGhzFrequencyAnalyzerStatus;
typedef enum {
SubGhzFrequencyAnalyzerFragmentBottomTypeMain,
SubGhzFrequencyAnalyzerFragmentBottomTypeLog,
} SubGhzFrequencyAnalyzerFragmentBottomType;
struct SubGhzFrequencyAnalyzer {
View* view;
SubGhzFrequencyAnalyzerWorker* worker;
SubGhzFrequencyAnalyzerCallback callback;
void* context;
bool locked;
uint32_t last_frequency;
};
typedef struct {
uint32_t frequency;
uint8_t rssi;
uint32_t history_frequency[3];
bool signal;
SubGhzFrequencyAnalyzerLogItemArray_t log_frequency;
SubGhzFrequencyAnalyzerFragmentBottomType fragment_bottom_type;
SubGhzFrequencyAnalyzerLogOrderBy log_frequency_order_by;
uint8_t log_frequency_scroll_offset;
} SubGhzFrequencyAnalyzerModel;
static inline uint8_t rssi_sanitize(float rssi) {
return (rssi * -1.0f) - RSSI_OFFSET;
}
void subghz_frequency_analyzer_set_callback(
SubGhzFrequencyAnalyzer* subghz_frequency_analyzer,
SubGhzFrequencyAnalyzerCallback callback,
void* context) {
furi_assert(subghz_frequency_analyzer);
furi_assert(callback);
subghz_frequency_analyzer->callback = callback;
subghz_frequency_analyzer->context = context;
}
void subghz_frequency_analyzer_draw_rssi(Canvas* canvas, uint8_t rssi, uint8_t x, uint8_t y) {
uint8_t column_number = 0;
if(rssi) {
rssi = rssi / 3;
for(uint8_t i = 1; i < rssi; i++) {
if(i > 20) break;
if(i % 4) {
column_number++;
canvas_draw_box(canvas, x + 2 * i, y - column_number, 2, 4 + column_number);
}
}
}
}
static void subghz_frequency_analyzer_log_frequency_draw(
Canvas* canvas,
SubGhzFrequencyAnalyzerModel* model) {
char buffer[64];
const uint8_t offset_x = 0;
const uint8_t offset_y = 43;
canvas_set_font(canvas, FontKeyboard);
const size_t items_count = SubGhzFrequencyAnalyzerLogItemArray_size(model->log_frequency);
if(items_count == 0) {
canvas_draw_rframe(canvas, offset_x + 27u, offset_y - 3u, 73u, 16u, 5u);
canvas_draw_str_aligned(
canvas, offset_x + 64u, offset_y + 8u, AlignCenter, AlignBottom, "No records");
return;
} else if(items_count > 3) {
elements_scrollbar_pos(
canvas,
offset_x + 127,
offset_y - 8,
29,
model->log_frequency_scroll_offset,
items_count - 2);
}
SubGhzFrequencyAnalyzerLogItem_t* log_frequency_item;
for(uint8_t i = 0; i < 3; ++i) {
const uint8_t item_pos = model->log_frequency_scroll_offset + i;
if(item_pos >= items_count) {
break;
}
log_frequency_item =
SubGhzFrequencyAnalyzerLogItemArray_get(model->log_frequency, item_pos);
// Frequency
SNPRINTF_FREQUENCY(buffer, (*log_frequency_item)->frequency)
canvas_draw_str(canvas, offset_x, offset_y + i * 10, buffer);
// Count
snprintf(buffer, sizeof(buffer), "%3d", (*log_frequency_item)->count);
canvas_draw_str(canvas, offset_x + 48, offset_y + i * 10, buffer);
// Max RSSI
subghz_frequency_analyzer_draw_rssi(
canvas, (*log_frequency_item)->rssi_max, offset_x + 69, (offset_y + i * 10));
}
canvas_set_font(canvas, FontSecondary);
}
static void subghz_frequency_analyzer_history_frequency_draw(
Canvas* canvas,
SubGhzFrequencyAnalyzerModel* model) {
char buffer[64];
uint8_t x = 66;
uint8_t y = 43;
canvas_set_font(canvas, FontKeyboard);
for(uint8_t i = 0; i < 3; i++) {
if(model->history_frequency[i]) {
SNPRINTF_FREQUENCY(buffer, model->history_frequency[i])
canvas_draw_str(canvas, x, y + i * 10, buffer);
} else {
canvas_draw_str(canvas, x, y + i * 10, "---.---");
}
canvas_draw_str(canvas, x + 44, y + i * 10, "MHz");
}
canvas_set_font(canvas, FontSecondary);
}
void subghz_frequency_analyzer_draw(Canvas* canvas, SubGhzFrequencyAnalyzerModel* model) {
furi_assert(canvas);
furi_assert(model);
char buffer[64];
canvas_set_color(canvas, ColorBlack);
canvas_set_font(canvas, FontSecondary);
if(model->fragment_bottom_type == SubGhzFrequencyAnalyzerFragmentBottomTypeLog) {
const size_t items_count = SubGhzFrequencyAnalyzerLogItemArray_size(model->log_frequency);
const char* log_order_by_name =
subghz_frequency_analyzer_log_get_order_name(model->log_frequency_order_by);
if(items_count < LOG_FREQUENCY_MAX_ITEMS) {
snprintf(buffer, sizeof(buffer), "Frequency Analyzer [%s]", log_order_by_name);
canvas_draw_str_aligned(canvas, 64, 8, AlignCenter, AlignBottom, buffer);
} else {
snprintf(buffer, sizeof(buffer), "The log is full! [%s]", log_order_by_name);
canvas_draw_str(canvas, 2, 8, buffer);
}
subghz_frequency_analyzer_log_frequency_draw(canvas, model);
} else {
canvas_draw_str(canvas, 20, 8, "Frequency Analyzer");
canvas_draw_str(canvas, 0, 64, "RSSI");
subghz_frequency_analyzer_draw_rssi(canvas, model->rssi, 20u, 64u);
subghz_frequency_analyzer_history_frequency_draw(canvas, model);
}
// Frequency
canvas_set_font(canvas, FontBigNumbers);
snprintf(
buffer,
sizeof(buffer),
"%03ld.%03ld",
model->frequency / 1000000 % 1000,
model->frequency / 1000 % 1000);
if(model->signal) {
canvas_draw_box(canvas, 4, 12, 121, 22);
canvas_set_color(canvas, ColorWhite);
}
canvas_draw_str(canvas, 8, 30, buffer);
canvas_draw_icon(canvas, 96, 19, &I_MHz_25x11);
}
static void subghz_frequency_analyzer_log_frequency_sort(SubGhzFrequencyAnalyzerModel* model) {
furi_assert(model);
M_LET((cmp, model->log_frequency_order_by), SubGhzFrequencyAnalyzerLogItemArray_compare_by_t)
SubGhzFrequencyAnalyzerLogItemArray_sort_fo(
model->log_frequency, SubGhzFrequencyAnalyzerLogItemArray_compare_by_as_interface(cmp));
}
bool subghz_frequency_analyzer_input(InputEvent* event, void* context) {
furi_assert(context);
SubGhzFrequencyAnalyzer* instance = context;
if(event->key == InputKeyBack) {
return false;
}
if((event->type == InputTypeShort) &&
((event->key == InputKeyLeft) || (event->key == InputKeyRight))) {
with_view_model(
instance->view,
SubGhzFrequencyAnalyzerModel * model,
{
if(event->key == InputKeyLeft) {
if(model->fragment_bottom_type == 0) {
model->fragment_bottom_type = SubGhzFrequencyAnalyzerFragmentBottomTypeLog;
} else {
--model->fragment_bottom_type;
}
} else if(event->key == InputKeyRight) {
if(model->fragment_bottom_type ==
SubGhzFrequencyAnalyzerFragmentBottomTypeLog) {
model->fragment_bottom_type = 0;
} else {
++model->fragment_bottom_type;
}
}
},
true);
} else if((event->type == InputTypeShort) && (event->key == InputKeyOk)) {
with_view_model(
instance->view,
SubGhzFrequencyAnalyzerModel * model,
{
if(model->fragment_bottom_type == SubGhzFrequencyAnalyzerFragmentBottomTypeLog) {
++model->log_frequency_order_by;
if(model->log_frequency_order_by >
SubGhzFrequencyAnalyzerLogOrderByFrequencyAsc) {
model->log_frequency_order_by = 0;
}
subghz_frequency_analyzer_log_frequency_sort(model);
}
},
true);
} else if((event->type == InputTypeShort) || (event->type == InputTypeRepeat)) {
with_view_model(
instance->view,
SubGhzFrequencyAnalyzerModel * model,
{
if(model->fragment_bottom_type == SubGhzFrequencyAnalyzerFragmentBottomTypeLog) {
if(event->key == InputKeyUp) {
if(model->log_frequency_scroll_offset > 0) {
--model->log_frequency_scroll_offset;
}
} else if(event->key == InputKeyDown) {
const size_t items_count =
SubGhzFrequencyAnalyzerLogItemArray_size(model->log_frequency);
if((model->log_frequency_scroll_offset + 3u) < items_count) {
++model->log_frequency_scroll_offset;
}
}
}
},
true);
}
return true;
}
static void subghz_frequency_analyzer_log_frequency_search_it(
SubGhzFrequencyAnalyzerLogItemArray_it_t* itref,
SubGhzFrequencyAnalyzerLogItemArray_t* log_frequency,
uint32_t frequency) {
furi_assert(log_frequency);
SubGhzFrequencyAnalyzerLogItemArray_it(*itref, *log_frequency);
SubGhzFrequencyAnalyzerLogItem_t* item;
while(!SubGhzFrequencyAnalyzerLogItemArray_end_p(*itref)) {
item = SubGhzFrequencyAnalyzerLogItemArray_ref(*itref);
if((*item)->frequency == frequency) {
break;
}
SubGhzFrequencyAnalyzerLogItemArray_next(*itref);
}
}
static bool subghz_frequency_analyzer_log_frequency_insert(SubGhzFrequencyAnalyzerModel* model) {
furi_assert(model);
const size_t items_count = SubGhzFrequencyAnalyzerLogItemArray_size(model->log_frequency);
if(items_count < LOG_FREQUENCY_MAX_ITEMS) {
SubGhzFrequencyAnalyzerLogItem_t* item =
SubGhzFrequencyAnalyzerLogItemArray_push_new(model->log_frequency);
if(item == NULL) {
return false;
}
(*item)->frequency = model->frequency;
(*item)->count = 1u;
(*item)->rssi_max = model->rssi;
(*item)->seq = items_count;
return true;
}
return false;
}
static void subghz_frequency_analyzer_log_frequency_update(
SubGhzFrequencyAnalyzerModel* model,
bool need_insert) {
furi_assert(model);
if(!model->frequency) {
return;
}
SubGhzFrequencyAnalyzerLogItemArray_it_t it;
subghz_frequency_analyzer_log_frequency_search_it(
&it, &model->log_frequency, model->frequency);
if(!SubGhzFrequencyAnalyzerLogItemArray_end_p(it)) {
SubGhzFrequencyAnalyzerLogItem_t* item = SubGhzFrequencyAnalyzerLogItemArray_ref(it);
if((*item)->rssi_max < model->rssi) {
(*item)->rssi_max = model->rssi;
}
if(need_insert && (*item)->count < UINT8_MAX) {
++(*item)->count;
subghz_frequency_analyzer_log_frequency_sort(model);
}
} else if(need_insert) {
if(subghz_frequency_analyzer_log_frequency_insert(model)) {
subghz_frequency_analyzer_log_frequency_sort(model);
}
}
}
void subghz_frequency_analyzer_pair_callback(
void* context,
uint32_t frequency,
float rssi,
bool signal) {
SubGhzFrequencyAnalyzer* instance = context;
if((rssi == 0.f) && (instance->locked)) {
if(instance->callback) {
instance->callback(SubGhzCustomEventSceneAnalyzerUnlock, instance->context);
}
instance->last_frequency = 0;
//update history
with_view_model(
instance->view,
SubGhzFrequencyAnalyzerModel * model,
{
model->history_frequency[2] = model->history_frequency[1];
model->history_frequency[1] = model->history_frequency[0];
model->history_frequency[0] = model->frequency;
},
false);
} else if((rssi != 0.f) && (!instance->locked)) {
if(instance->callback) {
instance->callback(SubGhzCustomEventSceneAnalyzerLock, instance->context);
}
}
instance->locked = (rssi != 0.f);
with_view_model(
instance->view,
SubGhzFrequencyAnalyzerModel * model,
{
model->rssi = rssi_sanitize(rssi);
model->frequency = frequency;
model->signal = signal;
if(frequency) {
subghz_frequency_analyzer_log_frequency_update(
model, frequency != instance->last_frequency);
instance->last_frequency = frequency;
}
},
true);
}
void subghz_frequency_analyzer_enter(void* context) {
furi_assert(context);
SubGhzFrequencyAnalyzer* instance = context;
//Start worker
instance->worker = subghz_frequency_analyzer_worker_alloc(instance->context);
subghz_frequency_analyzer_worker_set_pair_callback(
instance->worker,
(SubGhzFrequencyAnalyzerWorkerPairCallback)subghz_frequency_analyzer_pair_callback,
instance);
subghz_frequency_analyzer_worker_start(instance->worker);
with_view_model(
instance->view,
SubGhzFrequencyAnalyzerModel * model,
{
model->rssi = 0u;
model->frequency = 0;
model->fragment_bottom_type = SubGhzFrequencyAnalyzerFragmentBottomTypeMain;
model->log_frequency_order_by = SubGhzFrequencyAnalyzerLogOrderBySeqDesc;
model->log_frequency_scroll_offset = 0u;
model->history_frequency[0] = model->history_frequency[1] =
model->history_frequency[2] = 0u;
SubGhzFrequencyAnalyzerLogItemArray_init(model->log_frequency);
},
true);
}
void subghz_frequency_analyzer_exit(void* context) {
furi_assert(context);
SubGhzFrequencyAnalyzer* instance = context;
//Stop worker
if(subghz_frequency_analyzer_worker_is_running(instance->worker)) {
subghz_frequency_analyzer_worker_stop(instance->worker);
}
subghz_frequency_analyzer_worker_free(instance->worker);
with_view_model(
instance->view,
SubGhzFrequencyAnalyzerModel * model,
{
model->rssi = 0u;
model->frequency = 0;
model->fragment_bottom_type = SubGhzFrequencyAnalyzerFragmentBottomTypeMain;
model->log_frequency_order_by = SubGhzFrequencyAnalyzerLogOrderBySeqDesc;
model->log_frequency_scroll_offset = 0u;
model->history_frequency[0] = model->history_frequency[1] =
model->history_frequency[2] = 0u;
SubGhzFrequencyAnalyzerLogItemArray_clear(model->log_frequency);
},
true);
}
SubGhzFrequencyAnalyzer* subghz_frequency_analyzer_alloc() {
SubGhzFrequencyAnalyzer* instance = malloc(sizeof(SubGhzFrequencyAnalyzer));
// View allocation and configuration
instance->last_frequency = 0;
instance->view = view_alloc();
view_allocate_model(
instance->view, ViewModelTypeLocking, sizeof(SubGhzFrequencyAnalyzerModel));
view_set_context(instance->view, instance);
view_set_draw_callback(instance->view, (ViewDrawCallback)subghz_frequency_analyzer_draw);
view_set_input_callback(instance->view, subghz_frequency_analyzer_input);
view_set_enter_callback(instance->view, subghz_frequency_analyzer_enter);
view_set_exit_callback(instance->view, subghz_frequency_analyzer_exit);
with_view_model(
instance->view, SubGhzFrequencyAnalyzerModel * model, { model->rssi = 0; }, true);
return instance;
}
void subghz_frequency_analyzer_free(SubGhzFrequencyAnalyzer* instance) {
furi_assert(instance);
view_free(instance->view);
free(instance);
}
View* subghz_frequency_analyzer_get_view(SubGhzFrequencyAnalyzer* instance) {
furi_assert(instance);
return instance->view;
[FL-2274] Inventing streams and moving FFF to them (#981) * Streams: string stream * String stream: updated insert/delete api * Streams: generic stream interface and string stream implementation * Streams: helpers for insert and delete_and_insert * FFF: now compatible with streams * MinUnit: introduced tests with arguments * FFF: stream access violation * Streams: copy data between streams * Streams: file stream * FFF: documentation * FFStream: documentation * FFF: alloc as file * MinUnit: support for nested tests * Streams: changed delete_and_insert, now it returns success flag. Added ability dump stream inner parameters and data to cout. * FFF: simplified file open function * Streams: unit tests * FFF: tests * Streams: declare cache_size constant as define, to allow variable modified arrays * FFF: lib moved to a separate folder * iButton: new FFF * RFID: new FFF * Animations: new FFF * IR: new FFF * NFC: new FFF * Flipper file format: delete lib * U2F: new FFF * Subghz: new FFF and streams * Streams: read line * Streams: split * FuriCore: implement memset with extra asserts * FuriCore: implement extra heap asserts without inventing memset * Scene manager: protected access to the scene id stack with a size check * NFC worker: dirty fix for issue where hal_nfc was busy on app start * Furi: update allocator to erase memory on allocation. Replace furi_alloc with malloc. * FuriCore: cleanup memmgr code. * Furi HAL: furi_hal_init is split into critical and non-critical parts. The critical part is currently clock and console. * Memmgr: added ability to track allocations and deallocations through console. * FFStream: some speedup * Streams, FF: minor fixes * Tests: restore * File stream: a slightly more thread-safe version of file_stream_delete_and_insert Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2022-02-18 19:53:46 +00:00
}