-
Notifications
You must be signed in to change notification settings - Fork 3
/
flashlight.c
134 lines (108 loc) · 3.98 KB
/
flashlight.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// 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>
#include "flashlight_icons.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, 4, AlignCenter, AlignTop, "Flashlight");
canvas_set_font(canvas, FontSecondary);
canvas_draw_icon(canvas, 0, 17, &I_led_connections);
if(!plugin_state->is_on) {
elements_multiline_text_aligned(
canvas, 64, 44, AlignCenter, AlignTop, "Press OK button turn on");
} else {
elements_multiline_text_aligned(canvas, 64, 38, AlignCenter, AlignTop, "Light is on!");
elements_multiline_text_aligned(
canvas, 64, 50, 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;
}