MX 2 лет назад
Родитель
Сommit
8a5189892e
4 измененных файлов с 28 добавлено и 16 удалено
  1. 1 1
      application.fam
  2. 16 6
      ifttt_virtual_button.c
  3. 3 1
      ifttt_virtual_button.h
  4. 8 8
      views/send_view.c

+ 1 - 1
application.fam

@@ -1,6 +1,6 @@
 App(
     appid="esp8266_ifttt_virtual_button",
-    name="[ESP] IFTTT Button",
+    name="[ESP8266] IFTTT Btn",
     apptype=FlipperAppType.EXTERNAL,
     entry_point="ifttt_virtual_button_app",
     cdefines=["APP_IFTTT_VIRTUAL_BUTTON"],

+ 16 - 6
ifttt_virtual_button.c

@@ -55,8 +55,8 @@ Settings* load_settings() {
     if(storage_common_stat(storage, CONFIG_FILE_PATH, NULL) != FSE_OK) {
         if(flipper_format_file_open_new(file, CONFIG_FILE_PATH)) {
             save_settings_file(file, settings);
+            flipper_format_file_close(file);
         }
-        flipper_format_file_close(file);
     } else {
         if(flipper_format_file_open_existing(file, CONFIG_FILE_PATH)) {
             uint32_t value;
@@ -78,8 +78,8 @@ Settings* load_settings() {
                     strcpy(settings->save_event, furi_string_get_cstr(text_event_value));
                 }
             }
+            flipper_format_file_close(file);
         }
-        flipper_format_file_close(file);
     }
 
     furi_string_free(text_ssid_value);
@@ -91,7 +91,10 @@ Settings* load_settings() {
     return settings;
 }
 
-void send_serial_command_config(ESerialCommand command, Settings* settings) {
+void send_serial_command_config(
+    FuriHalSerialHandle* serial_handle,
+    ESerialCommand command,
+    Settings* settings) {
     uint8_t data[1] = {0};
 
     char config_tmp[100];
@@ -126,7 +129,7 @@ void send_serial_command_config(ESerialCommand command, Settings* settings) {
             return;
         }
 
-        furi_hal_uart_tx(FuriHalUartIdUSART1, data, 1);
+        furi_hal_serial_tx(serial_handle, data, 1);
     }
 }
 
@@ -155,6 +158,10 @@ VirtualButtonApp* ifttt_virtual_button_app_alloc(uint32_t first_scene) {
     app->gui = furi_record_open(RECORD_GUI);
     app->power = furi_record_open(RECORD_POWER);
 
+    app->serial_handle = furi_hal_serial_control_acquire(FuriHalSerialIdUsart);
+    furi_check(app->serial_handle);
+    furi_hal_serial_init(app->serial_handle, FLIPPERZERO_SERIAL_BAUD);
+
     // View dispatcher
     app->view_dispatcher = view_dispatcher_alloc();
     app->scene_manager = scene_manager_alloc(&virtual_button_scene_handlers, app);
@@ -169,7 +176,7 @@ VirtualButtonApp* ifttt_virtual_button_app_alloc(uint32_t first_scene) {
     view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
 
     // Views
-    app->sen_view = send_view_alloc();
+    app->sen_view = send_view_alloc(app->serial_handle);
     view_dispatcher_add_view(
         app->view_dispatcher, VirtualButtonAppViewSendView, send_view_get_view(app->sen_view));
 
@@ -212,6 +219,9 @@ void ifttt_virtual_button_app_free(VirtualButtonApp* app) {
     furi_record_close(RECORD_POWER);
     furi_record_close(RECORD_GUI);
 
+    furi_hal_serial_deinit(app->serial_handle);
+    furi_hal_serial_control_release(app->serial_handle);
+
     free(app);
 }
 
@@ -228,7 +238,7 @@ int32_t ifttt_virtual_button_app(void* p) {
     uint32_t first_scene = VirtualButtonAppSceneStart;
     VirtualButtonApp* app = ifttt_virtual_button_app_alloc(first_scene);
     memcpy(&app->settings, load_settings(), sizeof(Settings));
-    send_serial_command_config(ESerialCommand_Config, &(app->settings));
+    send_serial_command_config(app->serial_handle, ESerialCommand_Config, &(app->settings));
 
     view_dispatcher_run(app->view_dispatcher);
     ifttt_virtual_button_app_free(app);

+ 3 - 1
ifttt_virtual_button.h

@@ -13,7 +13,8 @@
 #include <flipper_format/flipper_format.h>
 #include <flipper_format/flipper_format_i.h>
 #include <storage/storage.h>
-#include <furi_hal_uart.h>
+#include <furi_hal_serial_control.h>
+#include <furi_hal_serial.h>
 #include "scenes/virtual_button_scene.h"
 
 #define APP_NAME "[ESP8266] IFTTT Virtual Button"
@@ -43,6 +44,7 @@ typedef struct {
     DialogEx* dialog;
     PowerInfo info;
     Settings settings;
+    FuriHalSerialHandle* serial_handle;
 } VirtualButtonApp;
 
 typedef enum {

+ 8 - 8
views/send_view.c

@@ -3,16 +3,16 @@
 #include <gui/elements.h>
 #include <notification/notification.h>
 #include <notification/notification_messages.h>
-#include <furi_hal_uart.h>
+#include <furi_hal_serial_control.h>
+#include <furi_hal_serial.h>
 #include <string.h>
 #include <stdio.h>
 
-#define FLIPPERZERO_SERIAL_BAUD 115200
-
 typedef enum ESerialCommand { ESerialCommand_Send } ESerialCommand;
 
 struct SendView {
     View* view;
+    FuriHalSerialHandle* serial_handle;
 };
 
 typedef struct {
@@ -26,7 +26,7 @@ static void Shake(void) {
     furi_record_close(RECORD_NOTIFICATION);
 }
 
-void send_serial_command_send(ESerialCommand command) {
+void send_serial_command_send(FuriHalSerialHandle* serial_handle, ESerialCommand command) {
     uint8_t data[1] = {0};
 
     char name[10] = "send";
@@ -40,7 +40,7 @@ void send_serial_command_send(ESerialCommand command) {
             return;
         };
 
-        furi_hal_uart_tx(FuriHalUartIdUSART1, data, 1);
+        furi_hal_serial_tx(serial_handle, data, 1);
     }
 }
 
@@ -72,7 +72,7 @@ static void send_view_process(SendView* send_view, InputEvent* event) {
                 } else if(event->key == InputKeyRight) {
                     model->right_pressed = true;
                     Shake();
-                    send_serial_command_send(ESerialCommand_Send);
+                    send_serial_command_send(send_view->serial_handle, ESerialCommand_Send);
                 } else if(event->key == InputKeyOk) {
                 } else if(event->key == InputKeyBack) {
                 }
@@ -107,14 +107,14 @@ static bool send_view_input_callback(InputEvent* event, void* context) {
     return consumed;
 }
 
-SendView* send_view_alloc() {
+SendView* send_view_alloc(FuriHalSerialHandle* serial_handle) {
     SendView* send_view = malloc(sizeof(SendView));
     send_view->view = view_alloc();
+    send_view->serial_handle = serial_handle;
     view_set_context(send_view->view, send_view);
     view_allocate_model(send_view->view, ViewModelTypeLocking, sizeof(SendViewModel));
     view_set_draw_callback(send_view->view, send_view_draw_callback);
     view_set_input_callback(send_view->view, send_view_input_callback);
-    furi_hal_uart_set_br(FuriHalUartIdUSART1, FLIPPERZERO_SERIAL_BAUD);
 
     return send_view;
 }