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>
|
2022-08-05 09:53:27 +00:00
|
|
|
#include <gui/elements.h>
|
2021-10-10 14:35:10 +00:00
|
|
|
#include "../helpers/subghz_frequency_analyzer_worker.h"
|
|
|
|
|
|
|
|
#include <assets_icons.h>
|
|
|
|
|
2022-08-05 09:53:27 +00:00
|
|
|
#define TAG "frequency_analyzer"
|
|
|
|
|
2022-08-11 15:46:58 +00:00
|
|
|
#define RSSI_MIN -97
|
2022-08-05 09:53:27 +00:00
|
|
|
#define RSSI_MAX -60
|
|
|
|
#define RSSI_SCALE 2
|
|
|
|
#define TRIGGER_STEP 1
|
|
|
|
|
|
|
|
static const NotificationSequence sequence_hw_blink = {
|
|
|
|
&message_blink_start_10,
|
|
|
|
&message_blink_set_color_cyan,
|
|
|
|
&message_do_not_reset,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const NotificationSequence sequence_hw_blink_stop = {
|
|
|
|
&message_blink_stop,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
2021-10-10 14:35:10 +00:00
|
|
|
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;
|
2022-08-24 23:24:11 +00:00
|
|
|
uint8_t feedback_level; // 0 - no feedback, 1 - vibro only, 2 - vibro and sound
|
2022-08-05 09:53:27 +00:00
|
|
|
float rssi_last;
|
|
|
|
uint32_t frequency_last;
|
2022-08-11 15:46:58 +00:00
|
|
|
uint32_t frequency_last_vis;
|
2022-08-05 09:53:27 +00:00
|
|
|
NotificationApp* notifications;
|
2021-10-10 14:35:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint32_t frequency;
|
2022-08-05 09:53:27 +00:00
|
|
|
uint32_t frequency_last;
|
2021-10-10 14:35:10 +00:00
|
|
|
float rssi;
|
2022-08-05 09:53:27 +00:00
|
|
|
float rssi_last;
|
|
|
|
float trigger;
|
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;
|
|
|
|
}
|
|
|
|
|
2022-08-05 09:53:27 +00:00
|
|
|
void subghz_frequency_analyzer_draw_rssi(
|
|
|
|
Canvas* canvas,
|
|
|
|
float rssi,
|
|
|
|
float rssi_last,
|
|
|
|
float trigger,
|
|
|
|
uint8_t x,
|
|
|
|
uint8_t y) {
|
|
|
|
// Current RSSI
|
2021-10-10 14:35:10 +00:00
|
|
|
if(rssi) {
|
2022-08-11 15:46:58 +00:00
|
|
|
if(rssi > RSSI_MAX) rssi = RSSI_MAX;
|
2022-08-05 09:53:27 +00:00
|
|
|
rssi = (rssi - RSSI_MIN) / RSSI_SCALE;
|
|
|
|
uint8_t column_number = 0;
|
2022-08-11 15:46:58 +00:00
|
|
|
for(size_t i = 0; i <= (uint8_t)rssi; i++) {
|
|
|
|
if((i + 1) % 4) {
|
2021-10-10 14:35:10 +00:00
|
|
|
column_number++;
|
|
|
|
canvas_draw_box(canvas, x + 2 * i, y - column_number, 2, 4 + column_number);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-05 09:53:27 +00:00
|
|
|
|
|
|
|
// Last RSSI
|
|
|
|
if(rssi_last) {
|
2022-08-11 15:46:58 +00:00
|
|
|
if(rssi_last > RSSI_MAX) rssi_last = RSSI_MAX;
|
|
|
|
int max_x = (int)((rssi_last - RSSI_MIN) / RSSI_SCALE) * 2;
|
2022-08-05 09:53:27 +00:00
|
|
|
//if(!(max_x % 8)) max_x -= 2;
|
2022-08-11 15:46:58 +00:00
|
|
|
int max_h = (int)((rssi_last - RSSI_MIN) / RSSI_SCALE) + 4;
|
2022-08-05 09:53:27 +00:00
|
|
|
max_h -= (max_h / 4) + 3;
|
2022-08-11 15:46:58 +00:00
|
|
|
canvas_draw_line(canvas, x + max_x + 1, y - max_h, x + max_x + 1, y + 3);
|
2022-08-05 09:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Trigger cursor
|
2022-08-11 15:46:58 +00:00
|
|
|
trigger = (trigger - RSSI_MIN) / RSSI_SCALE;
|
|
|
|
uint8_t tr_x = x + 2 * trigger;
|
|
|
|
canvas_draw_dot(canvas, tr_x, y + 4);
|
|
|
|
canvas_draw_line(canvas, tr_x - 1, y + 5, tr_x + 1, y + 5);
|
2022-08-05 09:53:27 +00:00
|
|
|
|
2022-08-11 15:46:58 +00:00
|
|
|
canvas_draw_line(canvas, x, y + 3, x + (RSSI_MAX - RSSI_MIN) * 2 / RSSI_SCALE, y + 3);
|
2021-10-10 14:35:10 +00:00
|
|
|
}
|
|
|
|
|
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];
|
|
|
|
|
2022-08-05 09:53:27 +00:00
|
|
|
// Title
|
2021-10-10 14:35:10 +00:00
|
|
|
canvas_set_color(canvas, ColorBlack);
|
|
|
|
canvas_set_font(canvas, FontSecondary);
|
|
|
|
canvas_draw_str(canvas, 20, 8, "Frequency Analyzer");
|
|
|
|
|
2022-08-05 09:53:27 +00:00
|
|
|
// RSSI
|
|
|
|
canvas_draw_str(canvas, 33, 62, "RSSI");
|
|
|
|
subghz_frequency_analyzer_draw_rssi(
|
2022-08-11 15:46:58 +00:00
|
|
|
canvas, model->rssi, model->rssi_last, model->trigger, 57, 58);
|
2021-10-10 14:35:10 +00:00
|
|
|
|
2022-08-05 09:53:27 +00:00
|
|
|
// Frequency
|
2021-10-10 14:35:10 +00:00
|
|
|
canvas_set_font(canvas, FontBigNumbers);
|
|
|
|
snprintf(
|
|
|
|
buffer,
|
|
|
|
sizeof(buffer),
|
|
|
|
"%03ld.%03ld",
|
|
|
|
model->frequency / 1000000 % 1000,
|
|
|
|
model->frequency / 1000 % 1000);
|
2022-08-05 09:53:27 +00:00
|
|
|
canvas_draw_str(canvas, 8, 30, buffer);
|
|
|
|
canvas_draw_icon(canvas, 96, 19, &I_MHz_25x11);
|
|
|
|
|
|
|
|
// Last detected frequency
|
|
|
|
canvas_set_font(canvas, FontSecondary);
|
|
|
|
if(model->frequency_last) {
|
|
|
|
snprintf(
|
|
|
|
buffer,
|
|
|
|
sizeof(buffer),
|
|
|
|
"Last: %03ld.%03ld MHz",
|
|
|
|
model->frequency_last / 1000000 % 1000,
|
|
|
|
model->frequency_last / 1000 % 1000);
|
|
|
|
} else {
|
|
|
|
strcpy(buffer, "Last: ---.--- MHz");
|
|
|
|
}
|
|
|
|
canvas_draw_str(canvas, 9, 42, buffer);
|
|
|
|
|
|
|
|
// Buttons hint
|
|
|
|
elements_button_left(canvas, "T-");
|
|
|
|
elements_button_right(canvas, "T+");
|
2021-10-10 14:35:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool subghz_frequency_analyzer_input(InputEvent* event, void* context) {
|
|
|
|
furi_assert(context);
|
2022-08-05 09:53:27 +00:00
|
|
|
SubGhzFrequencyAnalyzer* instance = context;
|
|
|
|
|
|
|
|
bool need_redraw = false;
|
2021-10-10 14:35:10 +00:00
|
|
|
|
2022-08-05 09:53:27 +00:00
|
|
|
if(event->key == InputKeyBack) return false;
|
|
|
|
|
|
|
|
if(((event->type == InputTypePress) || (event->type == InputTypeRepeat)) &&
|
|
|
|
((event->key == InputKeyLeft) || (event->key == InputKeyRight))) {
|
|
|
|
// Trigger setup
|
2022-08-11 15:46:58 +00:00
|
|
|
float trigger_level = subghz_frequency_analyzer_worker_get_trigger_level(instance->worker);
|
2022-08-05 09:53:27 +00:00
|
|
|
switch(event->key) {
|
|
|
|
case InputKeyLeft:
|
2022-08-11 15:46:58 +00:00
|
|
|
trigger_level -= TRIGGER_STEP;
|
|
|
|
if(trigger_level < RSSI_MIN) trigger_level = RSSI_MIN;
|
2022-08-05 09:53:27 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
case InputKeyRight:
|
2022-08-11 15:46:58 +00:00
|
|
|
trigger_level += TRIGGER_STEP;
|
|
|
|
if(trigger_level > RSSI_MAX) trigger_level = RSSI_MAX;
|
2022-08-05 09:53:27 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-08-11 15:46:58 +00:00
|
|
|
subghz_frequency_analyzer_worker_set_trigger_level(instance->worker, trigger_level);
|
|
|
|
FURI_LOG_I(TAG, "trigger = %.1f", (double)trigger_level);
|
2022-08-05 09:53:27 +00:00
|
|
|
need_redraw = true;
|
|
|
|
}
|
|
|
|
|
2022-08-24 23:24:11 +00:00
|
|
|
if(event->type == InputTypePress && event->key == InputKeyUp) {
|
|
|
|
if(instance->feedback_level == 0) {
|
|
|
|
instance->feedback_level = 2;
|
|
|
|
} else {
|
|
|
|
instance->feedback_level--;
|
|
|
|
}
|
|
|
|
FURI_LOG_D(TAG, "feedback_level = %d", instance->feedback_level);
|
|
|
|
need_redraw = true;
|
|
|
|
}
|
|
|
|
|
2022-08-05 09:53:27 +00:00
|
|
|
if(need_redraw) {
|
|
|
|
SubGhzFrequencyAnalyzer* instance = context;
|
2022-08-24 23:24:11 +00:00
|
|
|
instance->view with_view_model(
|
2022-08-05 09:53:27 +00:00
|
|
|
instance->view, (SubGhzFrequencyAnalyzerModel * model) {
|
|
|
|
model->rssi_last = instance->rssi_last;
|
|
|
|
model->frequency_last = instance->frequency_last;
|
2022-08-11 15:46:58 +00:00
|
|
|
model->trigger =
|
|
|
|
subghz_frequency_analyzer_worker_get_trigger_level(instance->worker);
|
2022-08-05 09:53:27 +00:00
|
|
|
return true;
|
|
|
|
});
|
2021-10-10 14:35:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-08-05 09:53:27 +00:00
|
|
|
uint32_t round_int(uint32_t value, uint8_t n) {
|
|
|
|
// Round value
|
|
|
|
uint8_t on = n;
|
|
|
|
while(n--) {
|
|
|
|
uint8_t i = value % 10;
|
|
|
|
value /= 10;
|
|
|
|
if(i >= 5) value++;
|
|
|
|
}
|
|
|
|
while(on--) value *= 10;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2021-10-10 14:35:10 +00:00
|
|
|
void subghz_frequency_analyzer_pair_callback(void* context, uint32_t frequency, float rssi) {
|
2022-08-05 09:53:27 +00:00
|
|
|
furi_assert(context);
|
2022-03-03 09:48:56 +00:00
|
|
|
SubGhzFrequencyAnalyzer* instance = context;
|
2022-08-05 09:53:27 +00:00
|
|
|
|
2022-04-28 17:20:59 +00:00
|
|
|
if((rssi == 0.f) && (instance->locked)) {
|
2022-08-05 09:53:27 +00:00
|
|
|
notification_message(instance->notifications, &sequence_hw_blink);
|
2022-08-11 15:46:58 +00:00
|
|
|
instance->frequency_last_vis = instance->frequency_last;
|
2022-08-05 09:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if((rssi != 0.f) && (frequency != 0)) {
|
|
|
|
// Threre is some signal
|
|
|
|
FURI_LOG_I(TAG, "rssi = %.2f, frequency = %d Hz", (double)rssi, frequency);
|
|
|
|
frequency = round_int(frequency, 3); // Round 299999990Hz to 300000000Hz
|
2022-08-11 15:46:58 +00:00
|
|
|
if(!instance->locked) {
|
|
|
|
// Triggered!
|
|
|
|
instance->rssi_last = rssi;
|
|
|
|
notification_message(instance->notifications, &sequence_hw_blink_stop);
|
2022-08-24 23:24:11 +00:00
|
|
|
|
|
|
|
switch(instance->feedback_level) {
|
|
|
|
case 1: // 1 - only vibro
|
|
|
|
notification_message(instance->notifications, &sequence_single_vibro);
|
|
|
|
break;
|
|
|
|
case 2: // 2 - vibro and beep
|
|
|
|
notification_message(instance->notifications, &sequence_success);
|
|
|
|
break;
|
|
|
|
default: // 0 - no feedback
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-08-11 15:46:58 +00:00
|
|
|
FURI_LOG_D(TAG, "triggered");
|
|
|
|
}
|
|
|
|
// Update values
|
|
|
|
if(rssi >= instance->rssi_last) {
|
|
|
|
instance->rssi_last = rssi;
|
2022-08-05 09:53:27 +00:00
|
|
|
instance->frequency_last = frequency;
|
2022-04-28 17:20:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2022-08-05 09:53:27 +00:00
|
|
|
model->rssi_last = instance->rssi_last;
|
2021-10-10 14:35:10 +00:00
|
|
|
model->frequency = frequency;
|
2022-08-11 15:46:58 +00:00
|
|
|
model->frequency_last = instance->frequency_last_vis;
|
|
|
|
model->trigger = subghz_frequency_analyzer_worker_get_trigger_level(instance->worker);
|
2021-10-10 14:35:10 +00:00
|
|
|
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
|
|
|
|
2022-08-05 09:53:27 +00:00
|
|
|
// Notifications
|
|
|
|
instance->notifications = furi_record_open(RECORD_NOTIFICATION);
|
|
|
|
notification_message(instance->notifications, &sequence_hw_blink);
|
|
|
|
|
2021-10-10 14:35:10 +00:00
|
|
|
//Start worker
|
2022-06-01 13:07:53 +00:00
|
|
|
instance->worker = subghz_frequency_analyzer_worker_alloc(instance->context);
|
2021-10-10 14:35:10 +00:00
|
|
|
|
|
|
|
subghz_frequency_analyzer_worker_set_pair_callback(
|
|
|
|
instance->worker,
|
|
|
|
(SubGhzFrequencyAnalyzerWorkerPairCallback)subghz_frequency_analyzer_pair_callback,
|
|
|
|
instance);
|
|
|
|
|
|
|
|
subghz_frequency_analyzer_worker_start(instance->worker);
|
|
|
|
|
2022-08-05 09:53:27 +00:00
|
|
|
instance->rssi_last = 0;
|
|
|
|
instance->frequency_last = 0;
|
2022-08-11 15:46:58 +00:00
|
|
|
instance->frequency_last_vis = 0;
|
|
|
|
subghz_frequency_analyzer_worker_set_trigger_level(instance->worker, RSSI_MIN);
|
2022-08-05 09:53:27 +00:00
|
|
|
|
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 = 0;
|
2022-08-05 09:53:27 +00:00
|
|
|
model->rssi_last = 0;
|
2021-10-10 14:35:10 +00:00
|
|
|
model->frequency = 0;
|
2022-08-05 09:53:27 +00:00
|
|
|
model->frequency_last = 0;
|
2022-08-11 15:46:58 +00:00
|
|
|
model->trigger = RSSI_MIN;
|
2021-10-10 14:35:10 +00:00
|
|
|
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
|
|
|
|
2022-08-05 09:53:27 +00:00
|
|
|
// Stop blinking
|
|
|
|
notification_message(instance->notifications, &sequence_hw_blink_stop);
|
|
|
|
|
|
|
|
// Stop worker
|
2021-10-10 14:35:10 +00:00
|
|
|
if(subghz_frequency_analyzer_worker_is_running(instance->worker)) {
|
|
|
|
subghz_frequency_analyzer_worker_stop(instance->worker);
|
|
|
|
}
|
|
|
|
subghz_frequency_analyzer_worker_free(instance->worker);
|
|
|
|
|
2022-08-05 09:53:27 +00:00
|
|
|
furi_record_close(RECORD_NOTIFICATION);
|
2021-10-10 14:35:10 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 09:48:56 +00:00
|
|
|
SubGhzFrequencyAnalyzer* subghz_frequency_analyzer_alloc() {
|
|
|
|
SubGhzFrequencyAnalyzer* instance = malloc(sizeof(SubGhzFrequencyAnalyzer));
|
2022-08-05 09:53:27 +00:00
|
|
|
furi_assert(instance);
|
2021-10-10 14:35:10 +00:00
|
|
|
|
2022-08-24 23:24:11 +00:00
|
|
|
instance->feedback_level = 2;
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
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-08-11 15:46:58 +00:00
|
|
|
}
|