Browse Source

Exit/enter view callbacks. Stop subghz worker in direct sampling mode.

antirez 3 years ago
parent
commit
27edce5f69
3 changed files with 51 additions and 6 deletions
  1. 24 6
      app.c
  2. 8 0
      app.h
  3. 19 0
      view_direct_sampling.c

+ 24 - 6
app.c

@@ -69,6 +69,28 @@ static void input_callback(InputEvent* input_event, void* ctx)
     furi_message_queue_put(app->event_queue,input_event,FuriWaitForever);
     furi_message_queue_put(app->event_queue,input_event,FuriWaitForever);
 }
 }
 
 
+
+/* Called to switch view (when left/right is pressed). Handles
+ * changing the current view ID and calling the enter/exit view
+ * callbacks if needed. */
+static void app_switch_view(ProtoViewApp *app, SwitchViewDirection dir) {
+    ProtoViewCurrentView old = app->current_view;
+    if (dir == AppNextView) {
+        app->current_view++;
+        if (app->current_view == ViewLast) app->current_view = 0;
+    } else if (dir == AppPrevView) {
+        if (app->current_view == 0)
+            app->current_view = ViewLast-1;
+        else
+            app->current_view--;
+    }
+    ProtoViewCurrentView new = app->current_view;
+
+    /* Call the enter/exit view callbacks if needed. */
+    if (old == ViewDirectSampling) view_exit_direct_sampling(app);
+    if (new == ViewDirectSampling) view_enter_direct_sampling(app);
+}
+
 /* Allocate the application state and initialize a number of stuff.
 /* Allocate the application state and initialize a number of stuff.
  * This is called in the entry point to create the application state. */
  * This is called in the entry point to create the application state. */
 ProtoViewApp* protoview_app_alloc() {
 ProtoViewApp* protoview_app_alloc() {
@@ -205,16 +227,12 @@ int32_t protoview_app_entry(void* p) {
                        input.key == InputKeyRight)
                        input.key == InputKeyRight)
             {
             {
                 /* Go to the next view. */
                 /* Go to the next view. */
-                app->current_view++;
-                if (app->current_view == ViewLast) app->current_view = 0;
+                app_switch_view(app,AppNextView);
             } else if (input.type == InputTypeShort &&
             } else if (input.type == InputTypeShort &&
                        input.key == InputKeyLeft)
                        input.key == InputKeyLeft)
             {
             {
                 /* Go to the previous view. */
                 /* Go to the previous view. */
-                if (app->current_view == 0)
-                    app->current_view = ViewLast-1;
-                else
-                    app->current_view--;
+                app_switch_view(app,AppPrevView);
             } else {
             } else {
                 /* This is where we pass the control to the currently
                 /* This is where we pass the control to the currently
                  * active view input processing. */
                  * active view input processing. */

+ 8 - 0
app.h

@@ -47,6 +47,12 @@ typedef enum {
     ViewLast, /* Just a sentinel to wrap around. */
     ViewLast, /* Just a sentinel to wrap around. */
 } ProtoViewCurrentView;
 } ProtoViewCurrentView;
 
 
+/* Used by app_switch_view() */
+typedef enum {
+    AppNextView,
+    AppPrevView
+} SwitchViewDirection;
+
 typedef struct {
 typedef struct {
     const char *name;
     const char *name;
     FuriHalSubGhzPreset preset;
     FuriHalSubGhzPreset preset;
@@ -152,6 +158,8 @@ void render_view_info(Canvas *const canvas, ProtoViewApp *app);
 void process_input_info(ProtoViewApp *app, InputEvent input);
 void process_input_info(ProtoViewApp *app, InputEvent input);
 void render_view_direct_sampling(Canvas *const canvas, ProtoViewApp *app);
 void render_view_direct_sampling(Canvas *const canvas, ProtoViewApp *app);
 void process_input_direct_sampling(ProtoViewApp *app, InputEvent input);
 void process_input_direct_sampling(ProtoViewApp *app, InputEvent input);
+void view_enter_direct_sampling(ProtoViewApp *app);
+void view_exit_direct_sampling(ProtoViewApp *app);
 
 
 /* ui.c */
 /* ui.c */
 void canvas_draw_str_with_border(Canvas* canvas, uint8_t x, uint8_t y, const char* str, Color text_color, Color border_color);
 void canvas_draw_str_with_border(Canvas* canvas, uint8_t x, uint8_t y, const char* str, Color text_color, Color border_color);

+ 19 - 0
view_direct_sampling.c

@@ -13,6 +13,10 @@ void render_view_direct_sampling(Canvas *const canvas, ProtoViewApp *app) {
         for (int x = 0; x < 128; x++) {
         for (int x = 0; x < 128; x++) {
             bool level = furi_hal_gpio_read(&gpio_cc1101_g0);
             bool level = furi_hal_gpio_read(&gpio_cc1101_g0);
             if (level) canvas_draw_dot(canvas,x,y);
             if (level) canvas_draw_dot(canvas,x,y);
+            /* Busy loop: this is a terrible approach as it blocks
+             * everything else, but for now it's the best we can do
+             * to obtain direct data with some spacing. */
+            uint32_t x = 500; while(x--);
         }
         }
     }
     }
     canvas_set_font(canvas, FontSecondary);
     canvas_set_font(canvas, FontSecondary);
@@ -25,3 +29,18 @@ void process_input_direct_sampling(ProtoViewApp *app, InputEvent input) {
     UNUSED(app);
     UNUSED(app);
     UNUSED(input);
     UNUSED(input);
 }
 }
+
+/* Enter view. Stop the subghz thread to prevent access as we read
+ * the CC1101 data directly. */
+void view_enter_direct_sampling(ProtoViewApp *app) {
+    if (app->txrx->txrx_state == TxRxStateRx) {
+        subghz_worker_stop(app->txrx->worker);
+    }
+}
+
+/* Exit view. Restore the subghz thread. */
+void view_exit_direct_sampling(ProtoViewApp *app) {
+    if (app->txrx->txrx_state == TxRxStateRx) {
+        subghz_worker_start(app->txrx->worker);
+    }
+}