Skip to content

Commit

Permalink
View specific input handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
antirez committed Dec 30, 2022
1 parent d750c80 commit be41b40
Showing 1 changed file with 56 additions and 18 deletions.
74 changes: 56 additions & 18 deletions app.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ void render_view_raw_pulses(Canvas *const canvas, ProtoViewApp *app) {
snprintf(buf,sizeof(buf),"%luus",
(unsigned long)DetectedSamples->short_pulse_dur);
canvas_set_font(canvas, FontSecondary);
canvas_draw_str_with_border(canvas, 100, 63, buf);
canvas_draw_str_with_border(canvas, 97, 63, buf);
}

/* Renders a single view with frequency and modulation setting. However
Expand All @@ -217,6 +217,9 @@ void render_view_settings(Canvas *const canvas, ProtoViewApp *app) {
canvas_draw_str(canvas,70,10,"Modulation");
}

/* The callback actually just passes the control to the actual active
* view callback, after setting up basic stuff like cleaning the screen
* and setting color to black. */
static void render_callback(Canvas *const canvas, void *ctx) {
ProtoViewApp *app = ctx;

Expand All @@ -225,10 +228,12 @@ static void render_callback(Canvas *const canvas, void *ctx) {
canvas_draw_box(canvas, 0, 0, 127, 63);
canvas_set_color(canvas, ColorBlack);

/* Call who is in charge right now. */
switch(app->current_view) {
case ViewRawPulses: render_view_raw_pulses(canvas,app); break;
case ViewFrequencySettings: render_view_settings(canvas,app); break;
case ViewModulationSettings: render_view_settings(canvas,app); break;
case ViewFrequencySettings:
case ViewModulationSettings:
render_view_settings(canvas,app); break;
case ViewLast: furi_crash(TAG " ViewLast selected"); break;
}
}
Expand All @@ -245,6 +250,8 @@ static void input_callback(InputEvent* input_event, void* ctx)
}
}

/* Allocate the application state and initialize a number of stuff.
* This is called in the entry point to create the application state. */
ProtoViewApp* protoview_app_alloc() {
ProtoViewApp *app = malloc(sizeof(ProtoViewApp));

Expand Down Expand Up @@ -295,32 +302,36 @@ ProtoViewApp* protoview_app_alloc() {
return app;
}

/* Free what the application allocated. It is not clear to me if the
* Flipper OS, once the application exits, will be able to reclaim space
* even if we forget to free something here. */
void protoview_app_free(ProtoViewApp *app) {
furi_assert(app);

//CC1101 off
// Put CC1101 on sleep.
radio_sleep(app);

// View
// View related.
view_port_enabled_set(app->view_port, false);
gui_remove_view_port(app->gui, app->view_port);
view_port_free(app->view_port);
furi_record_close(RECORD_GUI);
furi_message_queue_free(app->event_queue);
app->gui = NULL;

//setting
// Frequency setting.
subghz_setting_free(app->setting);

//Worker
// Worker stuff.
subghz_receiver_free(app->txrx->receiver);
subghz_environment_free(app->txrx->environment);
subghz_worker_free(app->txrx->worker);
free(app->txrx);

furi_hal_power_suppress_charge_exit();
// Raw samples buffers.
raw_samples_free(RawSamples);
raw_samples_free(DetectedSamples);
furi_hal_power_suppress_charge_exit();

free(app);
}
Expand All @@ -333,6 +344,29 @@ static void timer_callback(void *ctx) {
scan_for_signal(app);
}

/* Handle input for the raw pulses view. */
void process_input_raw_pulses(ProtoViewApp *app, InputEvent input) {
if (input.key == InputKeyOk) {
/* Reset the current sample to capture the next. */
app->signal_bestlen = 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) {
UNUSED(input);
UNUSED(app);
}

int32_t protoview_app_entry(void* p) {
UNUSED(p);
ProtoViewApp *app = protoview_app_alloc();
Expand All @@ -356,18 +390,11 @@ int32_t protoview_app_entry(void* p) {
if (qstat == FuriStatusOk) {
FURI_LOG_E(TAG, "Main Loop - Input: %u", input.key);

/* Handle navigation here. Then handle view-specific inputs
* in the view specific handling function. */
if (input.key == InputKeyBack) {
/* Exit the app. */
app->running = 0;
} else if (input.key == InputKeyOk) {
app->signal_bestlen = 0;
raw_samples_reset(DetectedSamples);
raw_samples_reset(RawSamples);
} else if (input.key == InputKeyDown) {
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;
} else if (input.key == InputKeyRight) {
/* Go to the next view. */
app->current_view++;
Expand All @@ -378,6 +405,17 @@ int32_t protoview_app_entry(void* p) {
app->current_view = ViewLast-1;
else
app->current_view--;
} else {
switch(app->current_view) {
case ViewRawPulses:
process_input_raw_pulses(app,input);
break;
case ViewFrequencySettings:
case ViewModulationSettings:
process_input_settings(app,input);
break;
case ViewLast: furi_crash(TAG " ViewLast selected"); break;
}
}
} else {
static int c = 0;
Expand Down

0 comments on commit be41b40

Please sign in to comment.