|
@@ -1,258 +1,16 @@
|
|
|
/* Copyright (C) 2022-2023 Salvatore Sanfilippo -- All Rights Reserved
|
|
/* Copyright (C) 2022-2023 Salvatore Sanfilippo -- All Rights Reserved
|
|
|
* See the LICENSE file for information about the license. */
|
|
* See the LICENSE file for information about the license. */
|
|
|
|
|
|
|
|
-#include <furi.h>
|
|
|
|
|
-#include <furi_hal.h>
|
|
|
|
|
-#include <lib/flipper_format/flipper_format.h>
|
|
|
|
|
-#include <input/input.h>
|
|
|
|
|
-#include <gui/gui.h>
|
|
|
|
|
-#include <stdlib.h>
|
|
|
|
|
#include "app.h"
|
|
#include "app.h"
|
|
|
-#include "app_buffer.h"
|
|
|
|
|
|
|
|
|
|
RawSamplesBuffer *RawSamples, *DetectedSamples;
|
|
RawSamplesBuffer *RawSamples, *DetectedSamples;
|
|
|
extern const SubGhzProtocolRegistry protoview_protocol_registry;
|
|
extern const SubGhzProtocolRegistry protoview_protocol_registry;
|
|
|
|
|
|
|
|
-/* Render the received signal.
|
|
|
|
|
- *
|
|
|
|
|
- * The screen of the flipper is 128 x 64. Even using 4 pixels per line
|
|
|
|
|
- * (where low level signal is one pixel high, high level is 4 pixels
|
|
|
|
|
- * high) and 4 pixels of spacing between the different lines, we can
|
|
|
|
|
- * plot comfortably 8 lines.
|
|
|
|
|
- *
|
|
|
|
|
- * The 'idx' argument is the first sample to render in the circular
|
|
|
|
|
- * buffer. */
|
|
|
|
|
-void render_signal(ProtoViewApp *app, Canvas *const canvas, RawSamplesBuffer *buf, uint32_t idx) {
|
|
|
|
|
- canvas_set_color(canvas, ColorBlack);
|
|
|
|
|
-
|
|
|
|
|
- int rows = 8;
|
|
|
|
|
- uint32_t time_per_pixel = app->us_scale;
|
|
|
|
|
- uint32_t start_idx = idx;
|
|
|
|
|
- bool level = 0;
|
|
|
|
|
- uint32_t dur = 0, sample_num = 0;
|
|
|
|
|
- for (int row = 0; row < rows ; row++) {
|
|
|
|
|
- for (int x = 0; x < 128; x++) {
|
|
|
|
|
- int y = 3 + row*8;
|
|
|
|
|
- if (dur < time_per_pixel/2) {
|
|
|
|
|
- /* Get more data. */
|
|
|
|
|
- raw_samples_get(buf, idx++, &level, &dur);
|
|
|
|
|
- sample_num++;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- canvas_draw_line(canvas, x,y,x,y-(level*3));
|
|
|
|
|
-
|
|
|
|
|
- /* Write a small triangle under the last sample detected. */
|
|
|
|
|
- if (app->signal_bestlen != 0 &&
|
|
|
|
|
- sample_num+start_idx == app->signal_bestlen+1)
|
|
|
|
|
- {
|
|
|
|
|
- canvas_draw_dot(canvas,x,y+2);
|
|
|
|
|
- canvas_draw_dot(canvas,x-1,y+3);
|
|
|
|
|
- canvas_draw_dot(canvas,x,y+3);
|
|
|
|
|
- canvas_draw_dot(canvas,x+1,y+3);
|
|
|
|
|
- sample_num++; /* Make sure we don't mark the next, too. */
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /* Remove from the current level duration the time we
|
|
|
|
|
- * just plot. */
|
|
|
|
|
- if (dur > time_per_pixel)
|
|
|
|
|
- dur -= time_per_pixel;
|
|
|
|
|
- else
|
|
|
|
|
- dur = 0;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-/* Return the time difference between a and b, always >= 0 since
|
|
|
|
|
- * the absolute value is returned. */
|
|
|
|
|
-uint32_t duration_delta(uint32_t a, uint32_t b) {
|
|
|
|
|
- return a > b ? a - b : b - a;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-/* This function starts scanning samples at offset idx looking for the
|
|
|
|
|
- * longest run of pulses, either high or low, that are among 10%
|
|
|
|
|
- * of each other, for a maximum of three classes. The classes are
|
|
|
|
|
- * counted separtely for high and low signals (RF on / off) because
|
|
|
|
|
- * many devices tend to have different pulse lenghts depending on
|
|
|
|
|
- * the level of the pulse.
|
|
|
|
|
- *
|
|
|
|
|
- * For instance Oregon2 sensors, in the case of protocol 2.1 will send
|
|
|
|
|
- * pulses of ~400us (RF on) VS ~580us (RF off). */
|
|
|
|
|
-#define SEARCH_CLASSES 3
|
|
|
|
|
-uint32_t search_coherent_signal(RawSamplesBuffer *s, uint32_t idx) {
|
|
|
|
|
- struct {
|
|
|
|
|
- uint32_t dur[2]; /* dur[0] = low, dur[1] = high */
|
|
|
|
|
- uint32_t count[2]; /* Associated observed frequency. */
|
|
|
|
|
- } classes[SEARCH_CLASSES];
|
|
|
|
|
-
|
|
|
|
|
- memset(classes,0,sizeof(classes));
|
|
|
|
|
- uint32_t minlen = 40, maxlen = 4000; /* Depends on data rate, here we
|
|
|
|
|
- allow for high and low. */
|
|
|
|
|
- uint32_t len = 0; /* Observed len of coherent samples. */
|
|
|
|
|
- s->short_pulse_dur = 0;
|
|
|
|
|
- for (uint32_t j = idx; j < idx+500; j++) {
|
|
|
|
|
- bool level;
|
|
|
|
|
- uint32_t dur;
|
|
|
|
|
- raw_samples_get(s, j, &level, &dur);
|
|
|
|
|
- if (dur < minlen || dur > maxlen) break; /* return. */
|
|
|
|
|
-
|
|
|
|
|
- /* Let's see if it matches a class we already have or if we
|
|
|
|
|
- * can populate a new (yet empty) class. */
|
|
|
|
|
- uint32_t k;
|
|
|
|
|
- for (k = 0; k < SEARCH_CLASSES; k++) {
|
|
|
|
|
- if (classes[k].count[level] == 0) {
|
|
|
|
|
- classes[k].dur[level] = dur;
|
|
|
|
|
- classes[k].count[level] = 1;
|
|
|
|
|
- break; /* Sample accepted. */
|
|
|
|
|
- } else {
|
|
|
|
|
- uint32_t classavg = classes[k].dur[level];
|
|
|
|
|
- uint32_t count = classes[k].count[level];
|
|
|
|
|
- uint32_t delta = duration_delta(dur,classavg);
|
|
|
|
|
- if (delta < classavg/10) {
|
|
|
|
|
- /* It is useful to compute the average of the class
|
|
|
|
|
- * we are observing. We know how many samples we got so
|
|
|
|
|
- * far, so we can recompute the average easily.
|
|
|
|
|
- * By always having a better estimate of the pulse len
|
|
|
|
|
- * we can avoid missing next samples in case the first
|
|
|
|
|
- * observed samples are too off. */
|
|
|
|
|
- classavg = ((classavg * count) + dur) / (count+1);
|
|
|
|
|
- classes[k].dur[level] = classavg;
|
|
|
|
|
- classes[k].count[level]++;
|
|
|
|
|
- break; /* Sample accepted. */
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (k == SEARCH_CLASSES) break; /* No match, return. */
|
|
|
|
|
-
|
|
|
|
|
- /* If we are here, we accepted this sample. Try with the next
|
|
|
|
|
- * one. */
|
|
|
|
|
- len++;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /* Update the buffer setting the shortest pulse we found
|
|
|
|
|
- * among the three classes. This will be used when scaling
|
|
|
|
|
- * for visualization. */
|
|
|
|
|
- for (int j = 0; j < SEARCH_CLASSES; j++) {
|
|
|
|
|
- for (int level = 0; level < 2; level++) {
|
|
|
|
|
- if (classes[j].dur[level] == 0) continue;
|
|
|
|
|
- if (classes[j].count[level] < 3) continue;
|
|
|
|
|
- if (s->short_pulse_dur == 0 ||
|
|
|
|
|
- s->short_pulse_dur > classes[j].dur[level])
|
|
|
|
|
- {
|
|
|
|
|
- s->short_pulse_dur = classes[j].dur[level];
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- return len;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-/* Search the buffer with the stored signal (last N samples received)
|
|
|
|
|
- * in order to find a coherent signal. If a signal that does not appear to
|
|
|
|
|
- * be just noise is found, it is set in DetectedSamples global signal
|
|
|
|
|
- * buffer, that is what is rendered on the screen. */
|
|
|
|
|
-void scan_for_signal(ProtoViewApp *app) {
|
|
|
|
|
- /* We need to work on a copy: the RawSamples buffer is populated
|
|
|
|
|
- * by the background thread receiving data. */
|
|
|
|
|
- RawSamplesBuffer *copy = raw_samples_alloc();
|
|
|
|
|
- raw_samples_copy(copy,RawSamples);
|
|
|
|
|
-
|
|
|
|
|
- /* Try to seek on data that looks to have a regular high low high low
|
|
|
|
|
- * pattern. */
|
|
|
|
|
- uint32_t minlen = 13; /* Min run of coherent samples. Up to
|
|
|
|
|
- 12 samples it's very easy to mistake
|
|
|
|
|
- noise for signal. */
|
|
|
|
|
-
|
|
|
|
|
- uint32_t i = 0;
|
|
|
|
|
- while (i < copy->total-1) {
|
|
|
|
|
- uint32_t thislen = search_coherent_signal(copy,i);
|
|
|
|
|
- if (thislen > minlen && thislen > app->signal_bestlen) {
|
|
|
|
|
- app->signal_bestlen = thislen;
|
|
|
|
|
- raw_samples_copy(DetectedSamples,copy);
|
|
|
|
|
- DetectedSamples->idx = (DetectedSamples->idx+i)%
|
|
|
|
|
- DetectedSamples->total;
|
|
|
|
|
- FURI_LOG_E(TAG, "Displayed sample updated (%d samples)",
|
|
|
|
|
- (int)thislen);
|
|
|
|
|
- }
|
|
|
|
|
- i += thislen ? thislen : 1;
|
|
|
|
|
- }
|
|
|
|
|
- raw_samples_free(copy);
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
/* Draw some text with a border. If the outside color is black and the inside
|
|
/* Draw some text with a border. If the outside color is black and the inside
|
|
|
* color is white, it just writes the border of the text, but the function can
|
|
* color is white, it just writes the border of the text, but the function can
|
|
|
* also be used to write a bold variation of the font setting both the
|
|
* also be used to write a bold variation of the font setting both the
|
|
|
* colors to black, or alternatively to write a black text with a white
|
|
* colors to black, or alternatively to write a black text with a white
|
|
|
* border so that it is visible if there are black stuff on the background. */
|
|
* border so that it is visible if there are black stuff on the background. */
|
|
|
-void canvas_draw_str_with_border(Canvas* canvas, uint8_t x, uint8_t y, const char* str, Color text_color, Color border_color)
|
|
|
|
|
-{
|
|
|
|
|
- struct {
|
|
|
|
|
- uint8_t x; uint8_t y;
|
|
|
|
|
- } dir[8] = {
|
|
|
|
|
- {-1,-1},
|
|
|
|
|
- {0,-1},
|
|
|
|
|
- {1,-1},
|
|
|
|
|
- {1,0},
|
|
|
|
|
- {1,1},
|
|
|
|
|
- {0,1},
|
|
|
|
|
- {-1,1},
|
|
|
|
|
- {-1,0}
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
- /* Rotate in all the directions writing the same string to create a
|
|
|
|
|
- * border, then write the actual string in the other color in the
|
|
|
|
|
- * middle. */
|
|
|
|
|
- canvas_set_color(canvas, border_color);
|
|
|
|
|
- for (int j = 0; j < 8; j++)
|
|
|
|
|
- canvas_draw_str(canvas,x+dir[j].x,y+dir[j].y,str);
|
|
|
|
|
- canvas_set_color(canvas, text_color);
|
|
|
|
|
- canvas_draw_str(canvas,x,y,str);
|
|
|
|
|
- canvas_set_color(canvas, ColorBlack);
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-/* Raw pulses rendering. This is our default view. */
|
|
|
|
|
-void render_view_raw_pulses(Canvas *const canvas, ProtoViewApp *app) {
|
|
|
|
|
- /* Show signal. */
|
|
|
|
|
- render_signal(app, canvas, DetectedSamples, app->signal_offset);
|
|
|
|
|
-
|
|
|
|
|
- /* Show signal information. */
|
|
|
|
|
- char buf[64];
|
|
|
|
|
- snprintf(buf,sizeof(buf),"%luus",
|
|
|
|
|
- (unsigned long)DetectedSamples->short_pulse_dur);
|
|
|
|
|
- canvas_set_font(canvas, FontSecondary);
|
|
|
|
|
- canvas_draw_str_with_border(canvas, 97, 63, buf, ColorWhite, ColorBlack);
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-/* Renders a single view with frequency and modulation setting. However
|
|
|
|
|
- * this are logically two different views, and only one of the settings
|
|
|
|
|
- * will be highlighted. */
|
|
|
|
|
-void render_view_settings(Canvas *const canvas, ProtoViewApp *app) {
|
|
|
|
|
- UNUSED(app);
|
|
|
|
|
- canvas_set_font(canvas, FontPrimary);
|
|
|
|
|
- if (app->current_view == ViewFrequencySettings)
|
|
|
|
|
- canvas_draw_str_with_border(canvas,1,10,"Frequency",ColorWhite,ColorBlack);
|
|
|
|
|
- else
|
|
|
|
|
- canvas_draw_str(canvas,1,10,"Frequency");
|
|
|
|
|
-
|
|
|
|
|
- if (app->current_view == ViewModulationSettings)
|
|
|
|
|
- canvas_draw_str_with_border(canvas,70,10,"Modulation",ColorWhite,ColorBlack);
|
|
|
|
|
- else
|
|
|
|
|
- canvas_draw_str(canvas,70,10,"Modulation");
|
|
|
|
|
- canvas_set_font(canvas, FontSecondary);
|
|
|
|
|
- canvas_draw_str(canvas,10,61,"Use up and down to modify");
|
|
|
|
|
-
|
|
|
|
|
- /* Show frequency. We can use big numbers font since it's just a number. */
|
|
|
|
|
- if (app->current_view == ViewFrequencySettings) {
|
|
|
|
|
- char buf[16];
|
|
|
|
|
- snprintf(buf,sizeof(buf),"%.2f",(double)app->frequency/1000000);
|
|
|
|
|
- canvas_set_font(canvas, FontBigNumbers);
|
|
|
|
|
- canvas_draw_str(canvas, 30, 40, buf);
|
|
|
|
|
- } else if (app->current_view == ViewModulationSettings) {
|
|
|
|
|
- int current = app->modulation;
|
|
|
|
|
- canvas_set_font(canvas, FontPrimary);
|
|
|
|
|
- canvas_draw_str(canvas, 33, 39, ProtoViewModulations[current].name);
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
/* The callback actually just passes the control to the actual active
|
|
/* The callback actually just passes the control to the actual active
|
|
|
* view callback, after setting up basic stuff like cleaning the screen
|
|
* view callback, after setting up basic stuff like cleaning the screen
|
|
|
* and setting color to black. */
|
|
* and setting color to black. */
|
|
@@ -380,79 +138,6 @@ static void timer_callback(void *ctx) {
|
|
|
scan_for_signal(app);
|
|
scan_for_signal(app);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/* Handle input for the raw pulses view. */
|
|
|
|
|
-void process_input_raw_pulses(ProtoViewApp *app, InputEvent input) {
|
|
|
|
|
- if (input.type == InputTypeRepeat) {
|
|
|
|
|
- /* Handle panning of the signal window. Long pressing
|
|
|
|
|
- * right will show successive samples, long pressing left
|
|
|
|
|
- * previous samples. */
|
|
|
|
|
- if (input.key == InputKeyRight) app->signal_offset++;
|
|
|
|
|
- else if (input.key == InputKeyLeft) app->signal_offset--;
|
|
|
|
|
- } else if (input.type == InputTypeShort) {
|
|
|
|
|
- if (input.key == InputKeyOk) {
|
|
|
|
|
- /* Reset the current sample to capture the next. */
|
|
|
|
|
- app->signal_bestlen = 0;
|
|
|
|
|
- app->signal_offset = 0;
|
|
|
|
|
- raw_samples_reset(DetectedSamples);
|
|
|
|
|
- raw_samples_reset(RawSamples);
|
|
|
|
|
- } else if (input.key == InputKeyDown) {
|
|
|
|
|
- /* Rescaling. The set becomes finer under 50us per pixel. */
|
|
|
|
|
- uint32_t scale_step = app->us_scale >= 50 ? 50 : 10;
|
|
|
|
|
- if (app->us_scale < 500) app->us_scale += scale_step;
|
|
|
|
|
- } else if (input.key == InputKeyUp) {
|
|
|
|
|
- uint32_t scale_step = app->us_scale > 50 ? 50 : 10;
|
|
|
|
|
- if (app->us_scale > 10) app->us_scale -= scale_step;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-/* Handle input for the settings view. */
|
|
|
|
|
-void process_input_settings(ProtoViewApp *app, InputEvent input) {
|
|
|
|
|
- /* Here we handle only up and down. Avoid any work if the user
|
|
|
|
|
- * pressed something else. */
|
|
|
|
|
- if (input.key != InputKeyDown && input.key != InputKeyUp) return;
|
|
|
|
|
-
|
|
|
|
|
- if (app->current_view == ViewFrequencySettings) {
|
|
|
|
|
- size_t curidx = 0, i;
|
|
|
|
|
- size_t count = subghz_setting_get_frequency_count(app->setting);
|
|
|
|
|
-
|
|
|
|
|
- /* Scan the list of frequencies to check for the index of the
|
|
|
|
|
- * currently set frequency. */
|
|
|
|
|
- for(i = 0; i < count; i++) {
|
|
|
|
|
- uint32_t freq = subghz_setting_get_frequency(app->setting,i);
|
|
|
|
|
- if (freq == app->frequency) {
|
|
|
|
|
- curidx = i;
|
|
|
|
|
- break;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- if (i == count) return; /* Should never happen. */
|
|
|
|
|
-
|
|
|
|
|
- if (input.key == InputKeyUp) {
|
|
|
|
|
- curidx = (curidx+1) % count;
|
|
|
|
|
- } else if (input.key == InputKeyDown) {
|
|
|
|
|
- curidx = curidx == 0 ? count-1 : curidx-1;
|
|
|
|
|
- }
|
|
|
|
|
- app->frequency = subghz_setting_get_frequency(app->setting,curidx);
|
|
|
|
|
- } else if (app->current_view == ViewModulationSettings) {
|
|
|
|
|
- uint32_t count = 0;
|
|
|
|
|
- uint32_t modid = app->modulation;
|
|
|
|
|
-
|
|
|
|
|
- while(ProtoViewModulations[count].name != NULL) count++;
|
|
|
|
|
- if (input.key == InputKeyUp) {
|
|
|
|
|
- modid = (modid+1) % count;
|
|
|
|
|
- } else if (input.key == InputKeyDown) {
|
|
|
|
|
- modid = modid == 0 ? count-1 : modid-1;
|
|
|
|
|
- }
|
|
|
|
|
- app->modulation = modid;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /* Apply changes. */
|
|
|
|
|
- FURI_LOG_E(TAG, "Setting view, setting frequency/modulation to %lu %s", app->frequency, ProtoViewModulations[app->modulation].name);
|
|
|
|
|
- radio_rx_end(app);
|
|
|
|
|
- radio_begin(app);
|
|
|
|
|
- radio_rx(app);
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
int32_t protoview_app_entry(void* p) {
|
|
int32_t protoview_app_entry(void* p) {
|
|
|
UNUSED(p);
|
|
UNUSED(p);
|
|
|
ProtoViewApp *app = protoview_app_alloc();
|
|
ProtoViewApp *app = protoview_app_alloc();
|
|
@@ -532,3 +217,4 @@ int32_t protoview_app_entry(void* p) {
|
|
|
protoview_app_free(app);
|
|
protoview_app_free(app);
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
|
|
+
|