Skip to content

Commit

Permalink
Add the whole app
Browse files Browse the repository at this point in the history
  • Loading branch information
biotinker committed Dec 2, 2022
1 parent 15a4a33 commit 740fe23
Show file tree
Hide file tree
Showing 25 changed files with 1,705 additions and 1 deletion.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
# flipperzero-gpioreader
This is a fork of the `gpio` app built into the flipper, with added functionality to read GPIO inputs
This is a fork of the `gpio` app built into the flipper, with added functionality to read GPIO inputs.

Supports pulling high or low.

Does not (yet) support analog reads.
12 changes: 12 additions & 0 deletions application.fam
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
App(
appid="gpioreader",
name="GPIO Reader",
apptype=FlipperAppType.EXTERNAL,
entry_point="gpio_app",
cdefines=["APP_GPIOREADER"],
requires=["gui"],
stack_size=1 * 1024,
order=50,
fap_libs=["assets"],
fap_category="GPIO",
)
111 changes: 111 additions & 0 deletions gpio_app.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#include "gpio_app_i.h"

#include <furi.h>
#include <furi_hal.h>

static bool gpio_app_custom_event_callback(void* context, uint32_t event) {
furi_assert(context);
GpioApp* app = context;
return scene_manager_handle_custom_event(app->scene_manager, event);
}

static bool gpio_app_back_event_callback(void* context) {
furi_assert(context);
GpioApp* app = context;
return scene_manager_handle_back_event(app->scene_manager);
}

static void gpio_app_tick_event_callback(void* context) {
furi_assert(context);
GpioApp* app = context;
scene_manager_handle_tick_event(app->scene_manager);
}

GpioApp* gpio_app_alloc() {
GpioApp* app = malloc(sizeof(GpioApp));

app->gui = furi_record_open(RECORD_GUI);

app->view_dispatcher = view_dispatcher_alloc();
app->scene_manager = scene_manager_alloc(&gpio_scene_handlers, app);
view_dispatcher_enable_queue(app->view_dispatcher);
view_dispatcher_set_event_callback_context(app->view_dispatcher, app);

view_dispatcher_set_custom_event_callback(
app->view_dispatcher, gpio_app_custom_event_callback);
view_dispatcher_set_navigation_event_callback(
app->view_dispatcher, gpio_app_back_event_callback);
view_dispatcher_set_tick_event_callback(
app->view_dispatcher, gpio_app_tick_event_callback, 100);

view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);

app->notifications = furi_record_open(RECORD_NOTIFICATION);

app->var_item_list = variable_item_list_alloc();
view_dispatcher_add_view(
app->view_dispatcher,
GpioAppViewVarItemList,
variable_item_list_get_view(app->var_item_list));
app->gpio_test = gpio_test_alloc();
view_dispatcher_add_view(
app->view_dispatcher, GpioAppViewGpioTest, gpio_test_get_view(app->gpio_test));
app->gpio_reader = gpio_reader_alloc();
view_dispatcher_add_view(
app->view_dispatcher, GpioAppViewGpioReader, gpio_reader_get_view(app->gpio_reader));

app->widget = widget_alloc();
view_dispatcher_add_view(
app->view_dispatcher, GpioAppViewUsbUartCloseRpc, widget_get_view(app->widget));

app->gpio_usb_uart = gpio_usb_uart_alloc();
view_dispatcher_add_view(
app->view_dispatcher, GpioAppViewUsbUart, gpio_usb_uart_get_view(app->gpio_usb_uart));

view_dispatcher_add_view(
app->view_dispatcher,
GpioAppViewUsbUartCfg,
variable_item_list_get_view(app->var_item_list));

scene_manager_next_scene(app->scene_manager, GpioSceneStart);

return app;
}

void gpio_app_free(GpioApp* app) {
furi_assert(app);

// Views
view_dispatcher_remove_view(app->view_dispatcher, GpioAppViewVarItemList);
view_dispatcher_remove_view(app->view_dispatcher, GpioAppViewGpioTest);
view_dispatcher_remove_view(app->view_dispatcher, GpioAppViewGpioReader);
view_dispatcher_remove_view(app->view_dispatcher, GpioAppViewUsbUart);
view_dispatcher_remove_view(app->view_dispatcher, GpioAppViewUsbUartCfg);
view_dispatcher_remove_view(app->view_dispatcher, GpioAppViewUsbUartCloseRpc);
variable_item_list_free(app->var_item_list);
widget_free(app->widget);
gpio_test_free(app->gpio_test);
gpio_reader_free(app->gpio_reader);
gpio_usb_uart_free(app->gpio_usb_uart);

// View dispatcher
view_dispatcher_free(app->view_dispatcher);
scene_manager_free(app->scene_manager);

// Close records
furi_record_close(RECORD_GUI);
furi_record_close(RECORD_NOTIFICATION);

free(app);
}

int32_t gpio_app(void* p) {
UNUSED(p);
GpioApp* gpio_app = gpio_app_alloc();

view_dispatcher_run(gpio_app->view_dispatcher);

gpio_app_free(gpio_app);

return 0;
}
11 changes: 11 additions & 0 deletions gpio_app.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#ifdef __cplusplus
extern "C" {
#endif

typedef struct GpioApp GpioApp;

#ifdef __cplusplus
}
#endif
44 changes: 44 additions & 0 deletions gpio_app_i.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#pragma once

#include "gpio_app.h"
#include "gpio_item.h"
#include "scenes/gpio_scene.h"
#include "gpio_custom_event.h"
#include "usb_uart_bridge.h"

#include <gui/gui.h>
#include <gui/view_dispatcher.h>
#include <gui/scene_manager.h>
#include <gui/modules/submenu.h>
#include <notification/notification_messages.h>
#include <gui/modules/variable_item_list.h>
#include <gui/modules/widget.h>
#include "views/gpio_test.h"
#include "views/gpio_reader.h"
#include "views/gpio_usb_uart.h"
#include <assets_icons.h>

struct GpioApp {
Gui* gui;
NotificationApp* notifications;
ViewDispatcher* view_dispatcher;
SceneManager* scene_manager;
Widget* widget;

VariableItemList* var_item_list;
VariableItem* var_item_flow;
GpioTest* gpio_test;
GpioReader* gpio_reader;
GpioUsbUart* gpio_usb_uart;
UsbUartBridge* usb_uart_bridge;
UsbUartConfig* usb_uart_cfg;
};

typedef enum {
GpioAppViewVarItemList,
GpioAppViewGpioTest,
GpioAppViewGpioReader,
GpioAppViewUsbUart,
GpioAppViewUsbUartCfg,
GpioAppViewUsbUartCloseRpc,
} GpioAppView;
14 changes: 14 additions & 0 deletions gpio_custom_event.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

typedef enum {
GpioStartEventOtgOff = 0,
GpioStartEventOtgOn,
GpioStartEventManualControl,
GpioStartEventReader,
GpioStartEventUsbUart,

GpioCustomEventErrorBack,

GpioUsbUartEventConfig,
GpioUsbUartEventConfigSet,
} GpioCustomEvent;
60 changes: 60 additions & 0 deletions gpio_item.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "gpio_item.h"

#include <furi_hal_resources.h>

typedef struct {
const char* name;
const GpioPin* pin;
} GpioItem;

static const GpioItem gpio_item[GPIO_ITEM_COUNT] = {
{"1.2: PA7", &gpio_ext_pa7},
{"1.3: PA6", &gpio_ext_pa6},
{"1.4: PA4", &gpio_ext_pa4},
{"1.5: PB3", &gpio_ext_pb3},
{"1.6: PB2", &gpio_ext_pb2},
{"1.7: PC3", &gpio_ext_pc3},
{"2.7: PC1", &gpio_ext_pc1},
{"2.8: PC0", &gpio_ext_pc0},
};

void gpio_item_configure_pin(uint8_t index, GpioMode mode, GpioPull pull) {
furi_assert(index < GPIO_ITEM_COUNT);
furi_hal_gpio_write(gpio_item[index].pin, false);
furi_hal_gpio_init(gpio_item[index].pin, mode, pull, GpioSpeedVeryHigh);
}

void gpio_item_configure_all_pins(GpioMode mode) {
GpioPull pull = GpioPullNo;
if(mode == GpioModeInput){
pull = GpioPullDown;
}
for(uint8_t i = 0; i < GPIO_ITEM_COUNT; i++) {
gpio_item_configure_pin(i, mode, pull);
}
}

void gpio_item_set_pin(uint8_t index, bool level) {
furi_assert(index < GPIO_ITEM_COUNT);
furi_hal_gpio_write(gpio_item[index].pin, level);
}

bool gpio_item_get_pin(uint8_t index) {
furi_assert(index < GPIO_ITEM_COUNT);
return furi_hal_gpio_read(gpio_item[index].pin);
}

void gpio_item_set_all_pins(bool level) {
for(uint8_t i = 0; i < GPIO_ITEM_COUNT; i++) {
gpio_item_set_pin(i, level);
}
}

const char* gpio_item_get_pin_name(uint8_t index) {
furi_assert(index < GPIO_ITEM_COUNT + 1);
if(index == GPIO_ITEM_COUNT) {
return "ALL";
} else {
return gpio_item[index].name;
}
}
17 changes: 17 additions & 0 deletions gpio_item.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <furi_hal_gpio.h>

#define GPIO_ITEM_COUNT 8

void gpio_item_configure_pin(uint8_t index, GpioMode mode, GpioPull pull);

void gpio_item_configure_all_pins(GpioMode mode);

void gpio_item_set_pin(uint8_t index, bool level);

void gpio_item_set_all_pins(bool level);

const char* gpio_item_get_pin_name(uint8_t index);

bool gpio_item_get_pin(uint8_t index);
30 changes: 30 additions & 0 deletions scenes/gpio_scene.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "gpio_scene.h"

// Generate scene on_enter handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_enter,
void (*const gpio_scene_on_enter_handlers[])(void*) = {
#include "gpio_scene_config.h"
};
#undef ADD_SCENE

// Generate scene on_event handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_event,
bool (*const gpio_scene_on_event_handlers[])(void* context, SceneManagerEvent event) = {
#include "gpio_scene_config.h"
};
#undef ADD_SCENE

// Generate scene on_exit handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_exit,
void (*const gpio_scene_on_exit_handlers[])(void* context) = {
#include "gpio_scene_config.h"
};
#undef ADD_SCENE

// Initialize scene handlers configuration structure
const SceneManagerHandlers gpio_scene_handlers = {
.on_enter_handlers = gpio_scene_on_enter_handlers,
.on_event_handlers = gpio_scene_on_event_handlers,
.on_exit_handlers = gpio_scene_on_exit_handlers,
.scene_num = GpioSceneNum,
};
29 changes: 29 additions & 0 deletions scenes/gpio_scene.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include <gui/scene_manager.h>

// Generate scene id and total number
#define ADD_SCENE(prefix, name, id) GpioScene##id,
typedef enum {
#include "gpio_scene_config.h"
GpioSceneNum,
} GpioScene;
#undef ADD_SCENE

extern const SceneManagerHandlers gpio_scene_handlers;

// Generate scene on_enter handlers declaration
#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_enter(void*);
#include "gpio_scene_config.h"
#undef ADD_SCENE

// Generate scene on_event handlers declaration
#define ADD_SCENE(prefix, name, id) \
bool prefix##_scene_##name##_on_event(void* context, SceneManagerEvent event);
#include "gpio_scene_config.h"
#undef ADD_SCENE

// Generate scene on_exit handlers declaration
#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_exit(void* context);
#include "gpio_scene_config.h"
#undef ADD_SCENE
6 changes: 6 additions & 0 deletions scenes/gpio_scene_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ADD_SCENE(gpio, start, Start)
ADD_SCENE(gpio, test, Test)
ADD_SCENE(gpio, reader, Reader)
ADD_SCENE(gpio, usb_uart, UsbUart)
ADD_SCENE(gpio, usb_uart_cfg, UsbUartCfg)
ADD_SCENE(gpio, usb_uart_close_rpc, UsbUartCloseRpc)
30 changes: 30 additions & 0 deletions scenes/gpio_scene_reader.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "../gpio_app_i.h"

void gpio_scene_reader_ok_callback(InputType type, void* context) {
furi_assert(context);
GpioApp* app = context;

if(type == InputTypePress) {
notification_message(app->notifications, &sequence_set_green_255);
} else if(type == InputTypeRelease) {
notification_message(app->notifications, &sequence_reset_green);
}
}

void gpio_scene_reader_on_enter(void* context) {
GpioApp* app = context;
gpio_item_configure_all_pins(GpioModeInput);
gpio_reader_set_ok_callback(app->gpio_reader, gpio_scene_reader_ok_callback, app);
view_dispatcher_switch_to_view(app->view_dispatcher, GpioAppViewGpioReader);
}

bool gpio_scene_reader_on_event(void* context, SceneManagerEvent event) {
UNUSED(context);
UNUSED(event);
return false;
}

void gpio_scene_reader_on_exit(void* context) {
UNUSED(context);
gpio_item_configure_all_pins(GpioModeAnalog);
}
Loading

0 comments on commit 740fe23

Please sign in to comment.