Skip to content

Commit

Permalink
New frequency analyzer flipperdevices#1557 (By ClusterM)] UPDATED
Browse files Browse the repository at this point in the history
  • Loading branch information
RogueMaster committed Aug 11, 2022
1 parent c3348cf commit 35f7906
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 57 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
- Known Issues:`FAP Loader` & `Chess`
- Added furi_message_queue_free(event_queue); to a bunch of plugins, thanks [Unleashed/xMasterX](https://github.com/Eng1n33r/flipperzero-firmware)
- Fix qFlipper and other apps causes flipper crash while trying to interact with files - see commit from [Unleashed/xMasterX](https://github.com/Eng1n33r/flipperzero-firmware)
- update universal ir audio & tv assets from [Unleashed/xMasterX](https://github.com/Eng1n33r/flipperzero-firmware)
- Update universal ir audio & tv assets from [Unleashed/xMasterX](https://github.com/Eng1n33r/flipperzero-firmware)
- Updated [New frequency analyzer #1557 (By ClusterM)](https://github.com/flipperdevices/flipperzero-firmware/pull/1557)

<table width="100%" border="0" cellspacing="0">
<tr> <td colspan=2> <h3>This software is for experimental purposes only and is not meant for any illegal activity/purposes. We do not condone illegal activity and strongly encourage keeping transmissions to legal/valid uses allowed by law.</h3> </td> </tr>
Expand Down
18 changes: 15 additions & 3 deletions applications/subghz/helpers/subghz_frequency_analyzer_worker.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct SubGhzFrequencyAnalyzerWorker {
SubGhzSetting* setting;

float filVal;
float trigger_level;

SubGhzFrequencyAnalyzerWorkerPairCallback pair_callback;
void* context;
Expand Down Expand Up @@ -154,7 +155,7 @@ static int32_t subghz_frequency_analyzer_worker_thread(void* context) {
(double)rssi_min);

// Second stage: fine scan
if(frequency_rssi.rssi_coarse > SUBGHZ_FREQUENCY_ANALYZER_THRESHOLD) {
if(frequency_rssi.rssi_coarse > instance->trigger_level) {
furi_hal_subghz_idle();
subghz_frequency_analyzer_worker_load_registers(subghz_preset_ook_58khz);
//for example -0.3 ... 433.92 ... +0.3 step 20KHz
Expand Down Expand Up @@ -189,7 +190,7 @@ static int32_t subghz_frequency_analyzer_worker_thread(void* context) {
}

// Deliver results fine
if(frequency_rssi.rssi_fine > SUBGHZ_FREQUENCY_ANALYZER_THRESHOLD) {
if(frequency_rssi.rssi_fine > instance->trigger_level) {
FURI_LOG_D(
TAG, "=:%u:%f", frequency_rssi.frequency_fine, (double)frequency_rssi.rssi_fine);

Expand All @@ -205,7 +206,7 @@ static int32_t subghz_frequency_analyzer_worker_thread(void* context) {
instance->context, frequency_rssi.frequency_fine, frequency_rssi.rssi_fine);
}
} else if( // Deliver results coarse
(frequency_rssi.rssi_coarse > SUBGHZ_FREQUENCY_ANALYZER_THRESHOLD) &&
(frequency_rssi.rssi_coarse > instance->trigger_level) &&
(instance->sample_hold_counter < 10)) {
FURI_LOG_D(
TAG,
Expand Down Expand Up @@ -255,6 +256,7 @@ SubGhzFrequencyAnalyzerWorker* subghz_frequency_analyzer_worker_alloc(void* cont

SubGhz* subghz = context;
instance->setting = subghz->setting;
instance->trigger_level = SUBGHZ_FREQUENCY_ANALYZER_THRESHOLD;
return instance;
}

Expand Down Expand Up @@ -297,3 +299,13 @@ bool subghz_frequency_analyzer_worker_is_running(SubGhzFrequencyAnalyzerWorker*
furi_assert(instance);
return instance->worker_running;
}

void subghz_frequency_analyzer_worker_set_trigger_level(
SubGhzFrequencyAnalyzerWorker* instance,
float value) {
instance->trigger_level = value;
}

float subghz_frequency_analyzer_worker_get_trigger_level(SubGhzFrequencyAnalyzerWorker* instance) {
return instance->trigger_level;
}
16 changes: 16 additions & 0 deletions applications/subghz/helpers/subghz_frequency_analyzer_worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,19 @@ void subghz_frequency_analyzer_worker_stop(SubGhzFrequencyAnalyzerWorker* instan
* @return bool - true if running
*/
bool subghz_frequency_analyzer_worker_is_running(SubGhzFrequencyAnalyzerWorker* instance);

/** Set RSSI trigger level
*
* @param instance SubGhzFrequencyAnalyzerWorker instance
* @param value RSSI level
*/
void subghz_frequency_analyzer_worker_set_trigger_level(
SubGhzFrequencyAnalyzerWorker* instance,
float value);

/** Get RSSI trigger level
*
* @param instance SubGhzFrequencyAnalyzerWorker instance
* @return RSSI trigger level
*/
float subghz_frequency_analyzer_worker_get_trigger_level(SubGhzFrequencyAnalyzerWorker* instance);
93 changes: 40 additions & 53 deletions applications/subghz/views/subghz_frequency_analyzer.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@

#define TAG "frequency_analyzer"

#define RSSI_MIN -101
#define RSSI_MIN -97
#define RSSI_MAX -60
#define RSSI_SCALE 2
#define TRIGGER_STEP 1
#define TRIGGER_MIN RSSI_MIN + RSSI_SCALE * 2

static const NotificationSequence sequence_hw_blink = {
&message_blink_start_10,
Expand All @@ -43,8 +42,7 @@ struct SubGhzFrequencyAnalyzer {
bool locked;
float rssi_last;
uint32_t frequency_last;
float trigger;
bool triggered;
uint32_t frequency_last_vis;
NotificationApp* notifications;
};

Expand Down Expand Up @@ -75,11 +73,11 @@ void subghz_frequency_analyzer_draw_rssi(
uint8_t y) {
// Current RSSI
if(rssi) {
if(rssi > RSSI_MAX) rssi = RSSI_MAX;
rssi = (rssi - RSSI_MIN) / RSSI_SCALE;
if(rssi > 20) rssi = 20;
uint8_t column_number = 0;
for(size_t i = 1; i < (uint8_t)rssi; i++) {
if(i % 4) {
for(size_t i = 0; i <= (uint8_t)rssi; i++) {
if((i + 1) % 4) {
column_number++;
canvas_draw_box(canvas, x + 2 * i, y - column_number, 2, 4 + column_number);
}
Expand All @@ -88,26 +86,21 @@ void subghz_frequency_analyzer_draw_rssi(

// Last RSSI
if(rssi_last) {
int max_x = (int)((rssi_last - RSSI_MIN) / RSSI_SCALE - 1) * 2;
if(rssi_last > RSSI_MAX) rssi_last = RSSI_MAX;
int max_x = (int)((rssi_last - RSSI_MIN) / RSSI_SCALE) * 2;
//if(!(max_x % 8)) max_x -= 2;
int max_h = (int)((rssi_last - RSSI_MIN) / RSSI_SCALE - 1) + 4;
int max_h = (int)((rssi_last - RSSI_MIN) / RSSI_SCALE) + 4;
max_h -= (max_h / 4) + 3;
if(max_x > 38) max_h = 38;
if(max_h > 19) max_h = 19;
if(max_x >= 0 && max_h > 0) {
canvas_draw_line(canvas, x + max_x + 1, y - max_h, x + max_x + 1, y + 3);
}
canvas_draw_line(canvas, x + max_x + 1, y - max_h, x + max_x + 1, y + 3);
}

// Trigger cursor
if(trigger >= RSSI_MIN + RSSI_SCALE * 2) {
trigger = (trigger - RSSI_MIN) / RSSI_SCALE;
uint8_t tr_x = x + 2 * trigger - 2;
canvas_draw_dot(canvas, tr_x, y + 4);
canvas_draw_line(canvas, tr_x - 1, y + 5, tr_x + 1, y + 5);
}
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);

canvas_draw_line(canvas, x + 2, y + 3, x + 39, y + 3);
canvas_draw_line(canvas, x, y + 3, x + (RSSI_MAX - RSSI_MIN) * 2 / RSSI_SCALE, y + 3);
}

void subghz_frequency_analyzer_draw(Canvas* canvas, SubGhzFrequencyAnalyzerModel* model) {
Expand All @@ -121,7 +114,7 @@ void subghz_frequency_analyzer_draw(Canvas* canvas, SubGhzFrequencyAnalyzerModel
// RSSI
canvas_draw_str(canvas, 33, 62, "RSSI");
subghz_frequency_analyzer_draw_rssi(
canvas, model->rssi, model->rssi_last, model->trigger, 55, 58);
canvas, model->rssi, model->rssi_last, model->trigger, 57, 58);

// Frequency
canvas_set_font(canvas, FontBigNumbers);
Expand Down Expand Up @@ -164,24 +157,20 @@ bool subghz_frequency_analyzer_input(InputEvent* event, void* context) {
if(((event->type == InputTypePress) || (event->type == InputTypeRepeat)) &&
((event->key == InputKeyLeft) || (event->key == InputKeyRight))) {
// Trigger setup
float trigger_level = subghz_frequency_analyzer_worker_get_trigger_level(instance->worker);
switch(event->key) {
case InputKeyLeft:
instance->trigger -= TRIGGER_STEP;
if(instance->trigger < RSSI_MIN + RSSI_SCALE * 2) instance->trigger = TRIGGER_MIN;
trigger_level -= TRIGGER_STEP;
if(trigger_level < RSSI_MIN) trigger_level = RSSI_MIN;
break;
default:
case InputKeyRight:
if(instance->trigger < RSSI_MIN + RSSI_SCALE * 2)
instance->trigger = TRIGGER_MIN;
else
instance->trigger += TRIGGER_STEP;
if(instance->trigger > RSSI_MAX) instance->trigger = RSSI_MAX;
trigger_level += TRIGGER_STEP;
if(trigger_level > RSSI_MAX) trigger_level = RSSI_MAX;
break;
}
if(instance->trigger > RSSI_MIN)
FURI_LOG_I(TAG, "trigger = %.1f", (double)instance->trigger);
else
FURI_LOG_I(TAG, "trigger disabled");
subghz_frequency_analyzer_worker_set_trigger_level(instance->worker, trigger_level);
FURI_LOG_I(TAG, "trigger = %.1f", (double)trigger_level);
need_redraw = true;
}

Expand All @@ -191,7 +180,8 @@ bool subghz_frequency_analyzer_input(InputEvent* event, void* context) {
instance->view, (SubGhzFrequencyAnalyzerModel * model) {
model->rssi_last = instance->rssi_last;
model->frequency_last = instance->frequency_last;
model->trigger = instance->trigger;
model->trigger =
subghz_frequency_analyzer_worker_get_trigger_level(instance->worker);
return true;
});
}
Expand All @@ -217,27 +207,24 @@ void subghz_frequency_analyzer_pair_callback(void* context, uint32_t frequency,

if((rssi == 0.f) && (instance->locked)) {
notification_message(instance->notifications, &sequence_hw_blink);
instance->triggered = false;
instance->frequency_last_vis = instance->frequency_last;
}

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
if((instance->trigger <= RSSI_MIN + RSSI_SCALE * 2) || (rssi >= instance->trigger)) {
if(!instance->triggered) {
// Triggered!
instance->triggered = true;
instance->rssi_last = rssi;
notification_message(instance->notifications, &sequence_hw_blink_stop);
notification_message(instance->notifications, &sequence_success);
FURI_LOG_D(TAG, "triggered");
}
// Update values
if(rssi > instance->rssi_last) instance->rssi_last = rssi;
if(!instance->locked) {
// Triggered!
instance->rssi_last = rssi;
notification_message(instance->notifications, &sequence_hw_blink_stop);
notification_message(instance->notifications, &sequence_success);
FURI_LOG_D(TAG, "triggered");
}
// Update values
if(rssi >= instance->rssi_last) {
instance->rssi_last = rssi;
instance->frequency_last = frequency;
} else {
instance->triggered = false;
}
}

Expand All @@ -247,8 +234,8 @@ void subghz_frequency_analyzer_pair_callback(void* context, uint32_t frequency,
model->rssi = rssi;
model->rssi_last = instance->rssi_last;
model->frequency = frequency;
model->frequency_last = instance->frequency_last;
model->trigger = instance->trigger;
model->frequency_last = instance->frequency_last_vis;
model->trigger = subghz_frequency_analyzer_worker_get_trigger_level(instance->worker);
return true;
});
}
Expand All @@ -273,16 +260,16 @@ void subghz_frequency_analyzer_enter(void* context) {

instance->rssi_last = 0;
instance->frequency_last = 0;
instance->trigger = TRIGGER_MIN;
instance->triggered = false;
instance->frequency_last_vis = 0;
subghz_frequency_analyzer_worker_set_trigger_level(instance->worker, RSSI_MIN);

with_view_model(
instance->view, (SubGhzFrequencyAnalyzerModel * model) {
model->rssi = 0;
model->rssi_last = 0;
model->frequency = 0;
model->frequency_last = 0;
model->trigger = instance->trigger;
model->trigger = RSSI_MIN;
return true;
});
}
Expand Down

0 comments on commit 35f7906

Please sign in to comment.