[FL-1884] GPIO application (#732)

* gpio: rename gpio application
* bq25896: add reading OTG config
* furi-hal-power: add is_otg_enabled API
* gpio: introduce new GPIO app, add OTG enable / disable
* variable-item-list: add enter callback
* gpio: add output test view and scene
* gpio app: fix GpioItemTester -> GpioItemTest

Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
gornekich
2021-09-30 23:03:28 +03:00
committed by GitHub
parent 7a89791b2b
commit 638f276308
22 changed files with 574 additions and 155 deletions

76
applications/gpio/gpio_app.c Executable file
View File

@@ -0,0 +1,76 @@
#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);
}
GpioApp* gpio_app_alloc() {
GpioApp* app = furi_alloc(sizeof(GpioApp));
app->gui = furi_record_open("gui");
app->notifications = furi_record_open("notification");
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_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
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));
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);
variable_item_list_free(app->var_item_list);
view_dispatcher_remove_view(app->view_dispatcher, GpioAppViewGpioTest);
gpio_test_free(app->gpio_test);
// View dispatcher
view_dispatcher_free(app->view_dispatcher);
scene_manager_free(app->scene_manager);
// Close records
furi_record_close("gui");
furi_record_close("notification");
free(app);
}
int32_t gpio_app(void* p) {
GpioApp* gpio_app = gpio_app_alloc();
view_dispatcher_run(gpio_app->view_dispatcher);
gpio_app_free(gpio_app);
return 0;
}

View File

@@ -0,0 +1,11 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
typedef struct GpioApp GpioApp;
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,28 @@
#pragma once
#include "gpio_app.h"
#include "gpio_item.h"
#include "scenes/gpio_scene.h"
#include <gui/gui.h>
#include <gui/view_dispatcher.h>
#include <gui/scene_manager.h>
#include <notification/notification-messages.h>
#include <gui/modules/variable-item-list.h>
#include "views/gpio_test.h"
struct GpioApp {
Gui* gui;
ViewDispatcher* view_dispatcher;
SceneManager* scene_manager;
NotificationApp* notifications;
VariableItemList* var_item_list;
GpioTest* gpio_test;
};
typedef enum {
GpioAppViewVarItemList,
GpioAppViewGpioTest,
} GpioAppView;

View File

@@ -0,0 +1,51 @@
#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) {
furi_assert(index < GPIO_ITEM_COUNT);
hal_gpio_write(gpio_item[index].pin, false);
hal_gpio_init(gpio_item[index].pin, mode, GpioPullNo, GpioSpeedVeryHigh);
}
void gpio_item_configure_all_pins(GpioMode mode) {
for(uint8_t i = 0; i < GPIO_ITEM_COUNT; i++) {
gpio_item_configure_pin(i, mode);
}
}
void gpio_item_set_pin(uint8_t index, bool level) {
furi_assert(index < GPIO_ITEM_COUNT);
hal_gpio_write(gpio_item[index].pin, level);
}
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;
}
}

View File

@@ -0,0 +1,15 @@
#pragma once
#include <furi-hal-gpio.h>
#define GPIO_ITEM_COUNT 8
void gpio_item_configure_pin(uint8_t index, GpioMode mode);
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);

View File

@@ -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,
};

View File

@@ -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

View File

@@ -0,0 +1,2 @@
ADD_SCENE(gpio, start, Start)
ADD_SCENE(gpio, test, Test)

View File

@@ -0,0 +1,92 @@
#include "../gpio_app_i.h"
#include "furi-hal-power.h"
#define GPIO_SCENE_START_CUSTOM_EVENT_OTG_OFF (0UL)
#define GPIO_SCENE_START_CUSTOM_EVENT_OTG_ON (1UL)
#define GPIO_SCENE_START_CUSTOM_EVENT_TEST (2UL)
enum GpioItem {
GpioItemOtg,
GpioItemTest,
};
enum GpioOtg {
GpioOtgOff,
GpioOtgOn,
GpioOtgSettingsNum,
};
const char* const gpio_otg_text[GpioOtgSettingsNum] = {
"Off",
"On",
};
static void gpio_scene_start_var_list_enter_callback(void* context, uint32_t index) {
furi_assert(context);
GpioApp* app = context;
if(index == GpioItemTest) {
view_dispatcher_send_custom_event(
app->view_dispatcher, GPIO_SCENE_START_CUSTOM_EVENT_TEST);
}
}
static void gpio_scene_start_var_list_change_callback(VariableItem* item) {
GpioApp* app = variable_item_get_context(item);
uint8_t index = variable_item_get_current_value_index(item);
variable_item_set_current_value_text(item, gpio_otg_text[index]);
if(index == GpioOtgOff) {
view_dispatcher_send_custom_event(
app->view_dispatcher, GPIO_SCENE_START_CUSTOM_EVENT_OTG_OFF);
} else if(index == GpioOtgOn) {
view_dispatcher_send_custom_event(
app->view_dispatcher, GPIO_SCENE_START_CUSTOM_EVENT_OTG_ON);
}
}
void gpio_scene_start_on_enter(void* context) {
GpioApp* app = context;
VariableItemList* var_item_list = app->var_item_list;
VariableItem* item;
variable_item_list_set_enter_callback(
var_item_list, gpio_scene_start_var_list_enter_callback, app);
item = variable_item_list_add(
var_item_list,
"5V on GPIO",
GpioOtgSettingsNum,
gpio_scene_start_var_list_change_callback,
app);
if(furi_hal_power_is_otg_enabled()) {
variable_item_set_current_value_index(item, GpioOtgOn);
variable_item_set_current_value_text(item, gpio_otg_text[GpioOtgOn]);
} else {
variable_item_set_current_value_index(item, GpioOtgOff);
variable_item_set_current_value_text(item, gpio_otg_text[GpioOtgOff]);
}
variable_item_list_add(var_item_list, "GPIO tester", 0, NULL, NULL);
view_dispatcher_switch_to_view(app->view_dispatcher, GpioAppViewVarItemList);
}
bool gpio_scene_start_on_event(void* context, SceneManagerEvent event) {
GpioApp* app = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == GPIO_SCENE_START_CUSTOM_EVENT_OTG_ON) {
furi_hal_power_enable_otg();
} else if(event.event == GPIO_SCENE_START_CUSTOM_EVENT_OTG_OFF) {
furi_hal_power_disable_otg();
} else if(event.event == GPIO_SCENE_START_CUSTOM_EVENT_TEST) {
scene_manager_next_scene(app->scene_manager, GpioSceneTest);
}
consumed = true;
}
return consumed;
}
void gpio_scene_start_on_exit(void* context) {
GpioApp* app = context;
variable_item_list_clean(app->var_item_list);
}

View File

@@ -0,0 +1,27 @@
#include "../gpio_app_i.h"
void gpio_scene_test_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_test_on_enter(void* context) {
GpioApp* app = context;
gpio_item_configure_all_pins(GpioModeOutputPushPull);
gpio_test_set_ok_callback(app->gpio_test, gpio_scene_test_ok_callback, app);
view_dispatcher_switch_to_view(app->view_dispatcher, GpioAppViewGpioTest);
}
bool gpio_scene_test_on_event(void* context, SceneManagerEvent event) {
return false;
}
void gpio_scene_test_on_exit(void* context) {
gpio_item_configure_all_pins(GpioModeAnalog);
}

View File

@@ -0,0 +1,130 @@
#include "gpio_test.h"
#include "../gpio_item.h"
#include <gui/elements.h>
struct GpioTest {
View* view;
GpioTestOkCallback callback;
void* context;
};
typedef struct {
uint8_t pin_idx;
} GpioTestModel;
static bool gpio_test_process_left(GpioTest* gpio_test);
static bool gpio_test_process_right(GpioTest* gpio_test);
static bool gpio_test_process_ok(GpioTest* gpio_test, InputEvent* event);
static void gpio_test_draw_callback(Canvas* canvas, void* _model) {
GpioTestModel* model = _model;
canvas_set_font(canvas, FontPrimary);
elements_multiline_text_aligned(canvas, 64, 2, AlignCenter, AlignTop, "Gpio Output mode test");
canvas_set_font(canvas, FontSecondary);
elements_multiline_text_aligned(
canvas, 64, 16, AlignCenter, AlignTop, "Press < or > to change pin");
elements_multiline_text_aligned(
canvas, 64, 32, AlignCenter, AlignTop, gpio_item_get_pin_name(model->pin_idx));
}
static bool gpio_test_input_callback(InputEvent* event, void* context) {
furi_assert(context);
GpioTest* gpio_test = context;
bool consumed = false;
if(event->type == InputTypeShort) {
if(event->key == InputKeyRight) {
consumed = gpio_test_process_right(gpio_test);
} else if(event->key == InputKeyLeft) {
consumed = gpio_test_process_left(gpio_test);
}
} else if(event->key == InputKeyOk) {
consumed = gpio_test_process_ok(gpio_test, event);
}
return consumed;
}
static bool gpio_test_process_left(GpioTest* gpio_test) {
with_view_model(
gpio_test->view, (GpioTestModel * model) {
if(model->pin_idx) {
model->pin_idx--;
}
return true;
});
return true;
}
static bool gpio_test_process_right(GpioTest* gpio_test) {
with_view_model(
gpio_test->view, (GpioTestModel * model) {
if(model->pin_idx < GPIO_ITEM_COUNT) {
model->pin_idx++;
}
return true;
});
return true;
}
static bool gpio_test_process_ok(GpioTest* gpio_test, InputEvent* event) {
bool consumed = false;
with_view_model(
gpio_test->view, (GpioTestModel * model) {
if(event->type == InputTypePress) {
if(model->pin_idx < GPIO_ITEM_COUNT) {
gpio_item_set_pin(model->pin_idx, true);
} else {
gpio_item_set_all_pins(true);
}
consumed = true;
} else if(event->type == InputTypeRelease) {
if(model->pin_idx < GPIO_ITEM_COUNT) {
gpio_item_set_pin(model->pin_idx, false);
} else {
gpio_item_set_all_pins(false);
}
consumed = true;
}
gpio_test->callback(event->type, gpio_test->context);
return true;
});
return consumed;
}
GpioTest* gpio_test_alloc() {
GpioTest* gpio_test = furi_alloc(sizeof(GpioTest));
gpio_test->view = view_alloc();
view_allocate_model(gpio_test->view, ViewModelTypeLocking, sizeof(GpioTestModel));
view_set_context(gpio_test->view, gpio_test);
view_set_draw_callback(gpio_test->view, gpio_test_draw_callback);
view_set_input_callback(gpio_test->view, gpio_test_input_callback);
return gpio_test;
}
void gpio_test_free(GpioTest* gpio_test) {
furi_assert(gpio_test);
view_free(gpio_test->view);
free(gpio_test);
}
View* gpio_test_get_view(GpioTest* gpio_test) {
furi_assert(gpio_test);
return gpio_test->view;
}
void gpio_test_set_ok_callback(GpioTest* gpio_test, GpioTestOkCallback callback, void* context) {
furi_assert(gpio_test);
furi_assert(callback);
with_view_model(
gpio_test->view, (GpioTestModel * model) {
gpio_test->callback = callback;
gpio_test->context = context;
return false;
});
}

View File

@@ -0,0 +1,14 @@
#pragma once
#include <gui/view.h>
typedef struct GpioTest GpioTest;
typedef void (*GpioTestOkCallback)(InputType type, void* context);
GpioTest* gpio_test_alloc();
void gpio_test_free(GpioTest* gpio_test);
View* gpio_test_get_view(GpioTest* gpio_test);
void gpio_test_set_ok_callback(GpioTest* gpio_test, GpioTestOkCallback callback, void* context);