Преглед изворни кода

Add flashlight from https://github.com/xMasterX/all-the-plugins

git-subtree-dir: flashlight
git-subtree-mainline: 9512eae281fc744d39ca08d3ec6004a7a7e89778
git-subtree-split: 44262957d297757ae587802f8772b1b841df88a8
Willy-JL пре 2 година
родитељ
комит
f6c5e154c8

+ 1 - 0
flashlight/.gitsubtree

@@ -0,0 +1 @@
+https://github.com/xMasterX/all-the-plugins dev apps_source_code/flipper-flashlight

+ 21 - 0
flashlight/LICENSE

@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2022 MX
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.

+ 7 - 0
flashlight/README.md

@@ -0,0 +1,7 @@
+# Flashlight Plugin for Flipper Zero
+
+Simple Flashlight special for @Svaarich by @xMasterX
+
+Enables 3.3v on pin 7/C3 and leaves it on when you exit app
+
+**Connect LED to (+ -> 7/C3) | (GND -> GND)**

+ 18 - 0
flashlight/application.fam

@@ -0,0 +1,18 @@
+App(
+    appid="flashlight",
+    name="Flashlight",
+    apptype=FlipperAppType.EXTERNAL,
+    entry_point="flashlight_app",
+    cdefines=["APP_FLASHLIGHT"],
+    requires=[
+        "gui",
+    ],
+    stack_size=2 * 1024,
+    order=20,
+    fap_icon="flash10px.png",
+    fap_category="GPIO",
+    fap_author="@xMasterX",
+    fap_weburl="https://github.com/xMasterX/flipper-flashlight",
+    fap_version="1.1",
+    fap_description="Enables 3.3v on pin 7/C3 when you press Ok and leaves it on when you exit app",
+)

BIN
flashlight/flash10px.png


+ 130 - 0
flashlight/flashlight.c

@@ -0,0 +1,130 @@
+// by @xMasterX
+
+#include <furi.h>
+#include <furi_hal_power.h>
+#include <gui/gui.h>
+#include <input/input.h>
+#include <stdlib.h>
+#include <gui/elements.h>
+
+typedef enum {
+    EventTypeTick,
+    EventTypeKey,
+} EventType;
+
+typedef struct {
+    EventType type;
+    InputEvent input;
+} PluginEvent;
+
+typedef struct {
+    FuriMutex* mutex;
+    bool is_on;
+} PluginState;
+
+static void render_callback(Canvas* const canvas, void* ctx) {
+    furi_assert(ctx);
+    const PluginState* plugin_state = ctx;
+    furi_mutex_acquire(plugin_state->mutex, FuriWaitForever);
+
+    canvas_set_font(canvas, FontPrimary);
+    elements_multiline_text_aligned(canvas, 64, 2, AlignCenter, AlignTop, "Flashlight");
+
+    canvas_set_font(canvas, FontSecondary);
+
+    if(!plugin_state->is_on) {
+        elements_multiline_text_aligned(
+            canvas, 64, 28, AlignCenter, AlignTop, "Press OK button turn on");
+    } else {
+        elements_multiline_text_aligned(canvas, 64, 28, AlignCenter, AlignTop, "Light is on!");
+        elements_multiline_text_aligned(
+            canvas, 64, 40, AlignCenter, AlignTop, "Press OK button to off");
+    }
+
+    furi_mutex_release(plugin_state->mutex);
+}
+
+static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
+    furi_assert(event_queue);
+
+    PluginEvent event = {.type = EventTypeKey, .input = *input_event};
+    furi_message_queue_put(event_queue, &event, FuriWaitForever);
+}
+
+static void flash_toggle(PluginState* const plugin_state) {
+    furi_hal_gpio_write(&gpio_ext_pc3, false);
+    furi_hal_gpio_init(&gpio_ext_pc3, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
+
+    if(plugin_state->is_on) {
+        furi_hal_gpio_write(&gpio_ext_pc3, false);
+        plugin_state->is_on = false;
+    } else {
+        furi_hal_gpio_write(&gpio_ext_pc3, true);
+        plugin_state->is_on = true;
+    }
+}
+
+int32_t flashlight_app() {
+    FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
+
+    PluginState* plugin_state = malloc(sizeof(PluginState));
+
+    plugin_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
+    if(!plugin_state->mutex) {
+        FURI_LOG_E("flashlight", "cannot create mutex\r\n");
+        furi_message_queue_free(event_queue);
+        free(plugin_state);
+        return 255;
+    }
+
+    // Set system callbacks
+    ViewPort* view_port = view_port_alloc();
+    view_port_draw_callback_set(view_port, render_callback, plugin_state);
+    view_port_input_callback_set(view_port, input_callback, event_queue);
+
+    // Open GUI and register view_port
+    Gui* gui = furi_record_open(RECORD_GUI);
+    gui_add_view_port(gui, view_port, GuiLayerFullscreen);
+
+    PluginEvent event;
+    for(bool processing = true; processing;) {
+        FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
+
+        furi_mutex_acquire(plugin_state->mutex, FuriWaitForever);
+
+        if(event_status == FuriStatusOk) {
+            // press events
+            if(event.type == EventTypeKey) {
+                if(event.input.type == InputTypePress) {
+                    switch(event.input.key) {
+                    case InputKeyUp:
+                    case InputKeyDown:
+                    case InputKeyRight:
+                    case InputKeyLeft:
+                        break;
+                    case InputKeyOk:
+                        flash_toggle(plugin_state);
+                        break;
+                    case InputKeyBack:
+                        processing = false;
+                        break;
+                    default:
+                        break;
+                    }
+                }
+            }
+        }
+
+        furi_mutex_release(plugin_state->mutex);
+        view_port_update(view_port);
+    }
+
+    view_port_enabled_set(view_port, false);
+    gui_remove_view_port(gui, view_port);
+    furi_record_close(RECORD_GUI);
+    view_port_free(view_port);
+    furi_message_queue_free(event_queue);
+    furi_mutex_free(plugin_state->mutex);
+
+    return 0;
+}

BIN
flashlight/img/1.png