Sfoglia il codice sorgente

Merge nrf24channelscanner from https://github.com/xMasterX/all-the-plugins

Willy-JL 2 anni fa
parent
commit
583d268332

BIN
nrf24channelscanner/.flipcorg/banner.png


BIN
nrf24channelscanner/.flipcorg/gallery/screen1.png


BIN
nrf24channelscanner/.flipcorg/gallery/screen_open.png


+ 2 - 4
nrf24channelscanner/application.fam

@@ -1,14 +1,12 @@
 App(
     appid="nrf24channelscanner",
-    name="[NRF24] Channel Scan",
+    name="[NRF24] Channel Scanner",
     apptype=FlipperAppType.EXTERNAL,
     entry_point="nrf24channelscanner_main",
-    fap_author="HTotoo",
-    fap_weburl="https://github.com/htotoo/NRF24ChannelScanner",
     stack_size=2 * 1024,
     requires=["gui"],
     fap_category="GPIO",
-    fap_version=(1, 3),
+    fap_version=(1, 1),
     fap_icon_assets="images",
     fap_icon="fapicon.png",
     fap_description="Scans 2.4Ghz frequency for usage data.",

+ 1 - 26
nrf24channelscanner/lib/nrf24/nrf24.c

@@ -6,20 +6,6 @@
 #include <string.h>
 
 void nrf24_init() {
-    // this is needed if multiple SPI devices are connected to the same bus but with different CS pins
-	#ifdef XTREME_SETTINGS_PATH
-	
-    if(xtreme_settings.spi_nrf24_handle == SpiDefault) {
-        furi_hal_gpio_init_simple(&gpio_ext_pc3, GpioModeOutputPushPull);
-        furi_hal_gpio_write(&gpio_ext_pc3, true);
-    } else if(xtreme_settings.spi_nrf24_handle == SpiExtra) {
-        furi_hal_gpio_init_simple(&gpio_ext_pa4, GpioModeOutputPushPull);
-        furi_hal_gpio_write(&gpio_ext_pa4, true);
-    }
-	#else
-		furi_hal_gpio_init_simple(&gpio_ext_pc3, GpioModeOutputPushPull);
-		furi_hal_gpio_write(&gpio_ext_pc3, true);
-	#endif
     furi_hal_spi_bus_handle_init(nrf24_HANDLE);
     furi_hal_spi_acquire(nrf24_HANDLE);
     furi_hal_gpio_init(nrf24_CE_PIN, GpioModeOutputPushPull, GpioPullUp, GpioSpeedVeryHigh);
@@ -31,17 +17,6 @@ void nrf24_deinit() {
     furi_hal_spi_bus_handle_deinit(nrf24_HANDLE);
     furi_hal_gpio_write(nrf24_CE_PIN, false);
     furi_hal_gpio_init(nrf24_CE_PIN, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
-
-	#ifdef XTREME_SETTINGS_PATH
-		// resetting the CS pins to floating
-		if(xtreme_settings.spi_nrf24_handle == SpiDefault) {
-			furi_hal_gpio_init_simple(&gpio_ext_pc3, GpioModeAnalog);
-		} else if(xtreme_settings.spi_nrf24_handle == SpiExtra) {
-			furi_hal_gpio_init_simple(&gpio_ext_pa4, GpioModeAnalog);
-		}
-	#else
-		furi_hal_gpio_init_simple(&gpio_ext_pc3, GpioModeAnalog);
-	#endif
 }
 
 void nrf24_spi_trx(
@@ -123,4 +98,4 @@ bool nrf24_check_connected(FuriHalSpiBusHandle* handle) {
     } else {
         return false;
     }
-}
+}

+ 2 - 10
nrf24channelscanner/lib/nrf24/nrf24.h

@@ -2,9 +2,6 @@
 #include <stdbool.h>
 #include <stdint.h>
 #include <furi_hal_spi.h>
-#if __has_include("xtreme.h") 
-#include <xtreme.h>
-#endif
 
 #ifdef __cplusplus
 extern "C" {
@@ -44,13 +41,8 @@ extern "C" {
 
 #define nrf24_TIMEOUT 500
 #define nrf24_CE_PIN &gpio_ext_pb2
-#ifdef XTREME_SETTINGS_PATH
-#define nrf24_HANDLE                                                                         \
-    (xtreme_settings.spi_nrf24_handle == SpiDefault ? &furi_hal_spi_bus_handle_external : \
-                                                         &furi_hal_spi_bus_handle_external_extra)
-#else
 #define nrf24_HANDLE &furi_hal_spi_bus_handle_external
-#endif
+
 /* Low level API */
 
 /** Write device register
@@ -131,4 +123,4 @@ bool nrf24_check_connected(FuriHalSpiBusHandle* handle);
 
 #ifdef __cplusplus
 }
-#endif
+#endif

+ 41 - 45
nrf24channelscanner/nrf24channelscanner.c

@@ -1,13 +1,12 @@
 #include <stdio.h>
 #include <furi.h>
-#include <furi_hal_power.h>
+#include <furi_hal.h>
 #include <gui/gui.h>
 #include <input/input.h>
 #include <gui/elements.h>
 #include <notification/notification_messages.h>
 #include <nrf24.h>
 #include "nrf24channelscanner_icons.h"
-#include <assets_icons.h>
 
 const uint8_t num_channels = 128;
 static uint8_t nrf24values[128] = {0}; //to store channel data
@@ -17,12 +16,10 @@ bool szuz = true; //to show welcome screen
 static bool isScanning = false; //to track the progress
 static bool stopNrfScan = false; //to exit thread
 
-static bool isInfiniteScan = false; //to prevent stop scan when OK long pressed
-
 static bool threadStoppedsoFree = false; //indicate if I can free the thread from ram.
 static uint8_t currCh = 0; //for the progress bar or the channel selector
 
-static int delayPerChan = 150; //can set via up / down.
+static int delayPerChan = 5; //can set via up / down.
 
 bool showFreq = true;
 
@@ -66,31 +63,38 @@ static void draw_callback(Canvas* canvas, void* ctx) {
     //draw hello mesage
     if(szuz) {
         canvas_set_font(canvas, FontSecondary);
-        canvas_draw_str(canvas, 1, 22, "OK: scan / stop. Long: infinite.");
-        canvas_draw_str(canvas, 1, 33, "Up / Down to change channel time.");
-        canvas_draw_str(canvas, 1, 44, "Left / Right to select channel");
-        canvas_draw_str(canvas, 1, 56, "  to get it's frequency");
+        canvas_draw_str(canvas, 1, 22, "Up / Down to change channel time.");
+        canvas_draw_str(canvas, 1, 36, "Left / Right to select channel,");
+        canvas_draw_str(canvas, 1, 48, "to get it's frequency");
     }
 
     //draw freq ir the progress
     canvas_set_font(canvas, FontSecondary);
     if(isScanning) {
-        if(isInfiniteScan)
-            canvas_draw_str(canvas, 37, 8, "scanning...");
-        else
-            canvas_draw_str(canvas, 37, 8, "scanning");
-
+        canvas_draw_str(canvas, 37, 8, "scanning");
     } else {
         if(showFreq) {
             int freq = 2400 + currCh;
-            char strfreq[10] = {0};
-            snprintf(strfreq, sizeof(strfreq), "%d MHZ", freq);
+            char strfreq[10] = {32};
+            itoa(freq, strfreq, 10);
+            strfreq[4] = ' ';
+            strfreq[5] = 'M';
+            strfreq[6] = 'H';
+            strfreq[7] = 'Z';
+            strfreq[8] = 0;
             canvas_draw_str(canvas, 40, 8, strfreq);
         } else {
             //show delay
             int dly = delayPerChan;
-            char strdel[10] = {0};
-            snprintf(strdel, sizeof(strdel), "%d us", dly);
+            char strdel[10] = {32};
+            itoa(dly, strdel, 10);
+            if(dly < 10) strdel[1] = ' ';
+            if(dly < 100) strdel[2] = ' ';
+            if(dly < 1000) strdel[3] = ' ';
+            strdel[4] = ' ';
+            strdel[5] = 'm';
+            strdel[6] = 's';
+            strdel[7] = 0;
             canvas_draw_str(canvas, 40, 8, strdel);
         }
     }
@@ -120,7 +124,7 @@ static int32_t scanner(void* context) {
     nrf24_set_rx_mode(nrf24_HANDLE, false);
     nrf24_write_reg(nrf24_HANDLE, REG_EN_AA, 0x0);
     nrf24_write_reg(nrf24_HANDLE, REG_RF_SETUP, 0x0f);
-    while(true) { //scan until stopped somehow
+    for(uint8_t j = 0; j < 15;) { //scan until stopped!
         if(stopNrfScan) break;
         for(uint8_t i = 0; i < num_channels; i++) {
             if(stopNrfScan) break;
@@ -129,20 +133,13 @@ static int32_t scanner(void* context) {
             nrf24_set_rx_mode(nrf24_HANDLE, true);
             for(uint8_t ii = 0; ii < 3; ++ii) {
                 nrf24_flush_rx(nrf24_HANDLE);
-                furi_delay_us(delayPerChan);
+                furi_delay_ms(delayPerChan);
                 tmp = nrf24_get_rdp(nrf24_HANDLE);
-                if(tmp > 0 && nrf24values[i] < 65) {
-                    nrf24values[i]++; //don't overrun it
-                }
-                if(nrf24values[i] > 50 && !isInfiniteScan) {
-                    stopNrfScan = true; //stop, bc maxed, but only when not infinite
-                }
+                if(tmp > 0) nrf24values[i]++;
+                if(nrf24values[i] > 50) j = 254; //stop, bc maxed
             }
         }
-        furi_delay_ms(1);
-        //for screen refresh.
     }
-    //cleanup
     nrf24_set_idle(nrf24_HANDLE);
     isScanning = false;
     threadStoppedsoFree = true;
@@ -158,9 +155,10 @@ void ChangeFreq(int delta) {
 
 void ChangeDelay(int delta) {
     delayPerChan += delta;
-    if(delayPerChan > 4000) delayPerChan = 4000;
-    if(delayPerChan < 120) delayPerChan = 120;
-    if(delayPerChan == 170) delayPerChan = 150; //rounding for the next
+    if(delayPerChan > 100) delayPerChan = 100;
+    if(delayPerChan < 1) delayPerChan = 1;
+    if(delayPerChan == 11) delayPerChan = 10; //to get it rounded :)
+    if(delayPerChan == 6) delayPerChan = 5; //to get it rounded :)
     showFreq = false;
 }
 
@@ -171,13 +169,14 @@ int32_t nrf24channelscanner_main(void* p) {
     Event event;
     FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(Event));
 
-    //turn on 5v for some modules
     uint8_t attempts = 0;
+    bool otg_was_enabled = furi_hal_power_is_otg_enabled();
     while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
         furi_hal_power_enable_otg();
         furi_delay_ms(10);
     }
 
+
     nrf24_init();
 
     ViewPort* view_port = view_port_alloc();
@@ -194,8 +193,7 @@ int32_t nrf24channelscanner_main(void* p) {
 
         if(event.type == EventTypeKey) {
             szuz = false; //hit any button, so hide welcome screen
-            if((event.input.type == InputTypeShort || event.input.type == InputTypeLong) &&
-               event.input.key == InputKeyBack) {
+            if(event.input.type == InputTypeLong && event.input.key == InputKeyBack) {
                 if(isScanning) {
                     stopNrfScan = true; //if running, stop it.
                     notification_message(notification, &sequence_blink_yellow_100);
@@ -204,9 +202,7 @@ int32_t nrf24channelscanner_main(void* p) {
                 }
                 break;
             }
-            //isInfiniteScan
-            if((event.input.type == InputTypeShort || event.input.type == InputTypeLong) &&
-               event.input.key == InputKeyOk) {
+            if(event.input.type == InputTypeShort && event.input.key == InputKeyOk) {
                 if(isScanning) {
                     notification_message(notification, &sequence_blink_yellow_100);
                     stopNrfScan = true;
@@ -220,7 +216,6 @@ int32_t nrf24channelscanner_main(void* p) {
                     threadStoppedsoFree = false;
                     ifNotFoundNrf = false;
                     notification_message(notification, &sequence_blink_green_100);
-                    isInfiniteScan = (event.input.type == InputTypeLong);
                     thread = furi_thread_alloc();
                     furi_thread_set_name(thread, "nrfscannerth");
                     furi_thread_set_stack_size(thread, 1024);
@@ -233,10 +228,10 @@ int32_t nrf24channelscanner_main(void* p) {
             }
             //change the delay
             if(event.input.type == InputTypeShort && event.input.key == InputKeyUp) {
-                ChangeDelay(50);
+                ChangeDelay(5);
             }
             if(event.input.type == InputTypeShort && event.input.key == InputKeyDown) {
-                ChangeDelay(-50);
+                ChangeDelay(-5);
             }
 
             if(!isScanning) {
@@ -257,13 +252,14 @@ int32_t nrf24channelscanner_main(void* p) {
         }
     }
     nrf24_deinit();
+
+    if(furi_hal_power_is_otg_enabled() && !otg_was_enabled) {
+        furi_hal_power_disable_otg();
+    }
+    
     furi_message_queue_free(event_queue);
     gui_remove_view_port(gui, view_port);
     view_port_free(view_port);
     furi_record_close(RECORD_GUI);
-    //turn off 5v
-    if(furi_hal_power_is_otg_enabled()) {
-        furi_hal_power_disable_otg();
-    }
     return 0;
 }