2021-10-10 14:35:10 +00:00
|
|
|
#include "subghz_frequency_analyzer.h"
|
|
|
|
#include "../subghz_i.h"
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <furi.h>
|
2022-01-05 16:10:18 +00:00
|
|
|
#include <furi_hal.h>
|
2021-10-10 14:35:10 +00:00
|
|
|
#include <input/input.h>
|
2022-01-05 16:10:18 +00:00
|
|
|
#include <notification/notification_messages.h>
|
2021-10-10 14:35:10 +00:00
|
|
|
#include "../helpers/subghz_frequency_analyzer_worker.h"
|
|
|
|
|
|
|
|
#include <assets_icons.h>
|
|
|
|
|
|
|
|
typedef enum {
|
2022-03-03 09:48:56 +00:00
|
|
|
SubGhzFrequencyAnalyzerStatusIDLE,
|
|
|
|
} SubGhzFrequencyAnalyzerStatus;
|
2021-10-10 14:35:10 +00:00
|
|
|
|
2022-03-03 09:48:56 +00:00
|
|
|
struct SubGhzFrequencyAnalyzer {
|
2021-10-10 14:35:10 +00:00
|
|
|
View* view;
|
|
|
|
SubGhzFrequencyAnalyzerWorker* worker;
|
2022-03-03 09:48:56 +00:00
|
|
|
SubGhzFrequencyAnalyzerCallback callback;
|
2021-10-10 14:35:10 +00:00
|
|
|
void* context;
|
2022-04-28 17:20:59 +00:00
|
|
|
bool locked;
|
2021-10-10 14:35:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint32_t frequency;
|
|
|
|
float rssi;
|
2022-03-03 09:48:56 +00:00
|
|
|
} SubGhzFrequencyAnalyzerModel;
|
2021-10-10 14:35:10 +00:00
|
|
|
|
|
|
|
void subghz_frequency_analyzer_set_callback(
|
2022-03-03 09:48:56 +00:00
|
|
|
SubGhzFrequencyAnalyzer* subghz_frequency_analyzer,
|
|
|
|
SubGhzFrequencyAnalyzerCallback callback,
|
2021-10-10 14:35:10 +00:00
|
|
|
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, float rssi) {
|
|
|
|
uint8_t x = 48;
|
|
|
|
uint8_t y = 56;
|
|
|
|
uint8_t column_number = 0;
|
|
|
|
if(rssi) {
|
|
|
|
rssi = (rssi + 90) / 3;
|
|
|
|
for(size_t i = 1; i < (uint8_t)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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-03 09:48:56 +00:00
|
|
|
void subghz_frequency_analyzer_draw(Canvas* canvas, SubGhzFrequencyAnalyzerModel* model) {
|
2021-10-10 14:35:10 +00:00
|
|
|
char buffer[64];
|
|
|
|
|
|
|
|
canvas_set_color(canvas, ColorBlack);
|
|
|
|
canvas_set_font(canvas, FontSecondary);
|
|
|
|
canvas_draw_str(canvas, 20, 8, "Frequency Analyzer");
|
|
|
|
|
|
|
|
canvas_draw_str(canvas, 28, 60, "RSSI");
|
|
|
|
subghz_frequency_analyzer_draw_rssi(canvas, model->rssi);
|
|
|
|
|
|
|
|
//Frequency
|
|
|
|
canvas_set_font(canvas, FontBigNumbers);
|
|
|
|
snprintf(
|
|
|
|
buffer,
|
|
|
|
sizeof(buffer),
|
|
|
|
"%03ld.%03ld",
|
|
|
|
model->frequency / 1000000 % 1000,
|
|
|
|
model->frequency / 1000 % 1000);
|
|
|
|
canvas_draw_str(canvas, 8, 35, buffer);
|
|
|
|
canvas_draw_icon(canvas, 96, 24, &I_MHz_25x11);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool subghz_frequency_analyzer_input(InputEvent* event, void* context) {
|
|
|
|
furi_assert(context);
|
|
|
|
|
|
|
|
if(event->key == InputKeyBack) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void subghz_frequency_analyzer_pair_callback(void* context, uint32_t frequency, float rssi) {
|
2022-03-03 09:48:56 +00:00
|
|
|
SubGhzFrequencyAnalyzer* instance = context;
|
2022-04-28 17:20:59 +00:00
|
|
|
if((rssi == 0.f) && (instance->locked)) {
|
|
|
|
if(instance->callback) {
|
|
|
|
instance->callback(SubGhzCustomEventSceneAnalyzerUnlock, instance->context);
|
|
|
|
}
|
|
|
|
} else if((rssi != 0.f) && (!instance->locked)) {
|
|
|
|
if(instance->callback) {
|
|
|
|
instance->callback(SubGhzCustomEventSceneAnalyzerLock, instance->context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
instance->locked = (rssi != 0.f);
|
2021-10-10 14:35:10 +00:00
|
|
|
with_view_model(
|
2022-03-03 09:48:56 +00:00
|
|
|
instance->view, (SubGhzFrequencyAnalyzerModel * model) {
|
2021-10-10 14:35:10 +00:00
|
|
|
model->rssi = rssi;
|
|
|
|
model->frequency = frequency;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void subghz_frequency_analyzer_enter(void* context) {
|
|
|
|
furi_assert(context);
|
2022-03-03 09:48:56 +00:00
|
|
|
SubGhzFrequencyAnalyzer* instance = context;
|
2021-10-10 14:35:10 +00:00
|
|
|
|
|
|
|
//Start worker
|
|
|
|
instance->worker = subghz_frequency_analyzer_worker_alloc();
|
|
|
|
|
|
|
|
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(
|
2022-03-03 09:48:56 +00:00
|
|
|
instance->view, (SubGhzFrequencyAnalyzerModel * model) {
|
2021-10-10 14:35:10 +00:00
|
|
|
model->rssi = 0;
|
|
|
|
model->frequency = 0;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void subghz_frequency_analyzer_exit(void* context) {
|
|
|
|
furi_assert(context);
|
2022-03-03 09:48:56 +00:00
|
|
|
SubGhzFrequencyAnalyzer* instance = context;
|
2021-10-10 14:35:10 +00:00
|
|
|
|
|
|
|
//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(
|
2022-03-03 09:48:56 +00:00
|
|
|
instance->view, (SubGhzFrequencyAnalyzerModel * model) {
|
2021-10-10 14:35:10 +00:00
|
|
|
model->rssi = 0;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-03 09:48:56 +00:00
|
|
|
SubGhzFrequencyAnalyzer* subghz_frequency_analyzer_alloc() {
|
|
|
|
SubGhzFrequencyAnalyzer* instance = malloc(sizeof(SubGhzFrequencyAnalyzer));
|
2021-10-10 14:35:10 +00:00
|
|
|
|
|
|
|
// View allocation and configuration
|
|
|
|
instance->view = view_alloc();
|
|
|
|
view_allocate_model(
|
2022-03-03 09:48:56 +00:00
|
|
|
instance->view, ViewModelTypeLocking, sizeof(SubGhzFrequencyAnalyzerModel));
|
2021-10-10 14:35:10 +00:00
|
|
|
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(
|
2022-03-03 09:48:56 +00:00
|
|
|
instance->view, (SubGhzFrequencyAnalyzerModel * model) {
|
2021-10-10 14:35:10 +00:00
|
|
|
model->rssi = 0;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2022-03-03 09:48:56 +00:00
|
|
|
void subghz_frequency_analyzer_free(SubGhzFrequencyAnalyzer* instance) {
|
2021-10-10 14:35:10 +00:00
|
|
|
furi_assert(instance);
|
|
|
|
|
|
|
|
view_free(instance->view);
|
|
|
|
free(instance);
|
|
|
|
}
|
|
|
|
|
2022-03-03 09:48:56 +00:00
|
|
|
View* subghz_frequency_analyzer_get_view(SubGhzFrequencyAnalyzer* instance) {
|
2021-10-10 14:35:10 +00:00
|
|
|
furi_assert(instance);
|
|
|
|
return instance->view;
|
2022-02-18 19:53:46 +00:00
|
|
|
}
|