-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
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", | ||
) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// 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 { | ||
bool is_on; | ||
} PluginState; | ||
|
||
static void render_callback(Canvas* const canvas, void* ctx) { | ||
const PluginState* plugin_state = acquire_mutex((ValueMutex*)ctx, 25); | ||
if(plugin_state == NULL) { | ||
return; | ||
} | ||
|
||
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"); | ||
} | ||
|
||
release_mutex((ValueMutex*)ctx, plugin_state); | ||
} | ||
|
||
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)); | ||
|
||
ValueMutex state_mutex; | ||
if(!init_mutex(&state_mutex, plugin_state, sizeof(PluginState))) { | ||
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, &state_mutex); | ||
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); | ||
|
||
PluginState* plugin_state = (PluginState*)acquire_mutex_block(&state_mutex); | ||
|
||
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; | ||
} | ||
} | ||
} | ||
} | ||
|
||
view_port_update(view_port); | ||
release_mutex(&state_mutex, plugin_state); | ||
} | ||
|
||
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); | ||
delete_mutex(&state_mutex); | ||
|
||
return 0; | ||
} |