From 0d3b5aaecf1f71883cc196bf692b921e0782637e Mon Sep 17 00:00:00 2001 From: RogueMaster Date: Thu, 15 Dec 2022 16:58:10 -0500 Subject: [PATCH] new gpio reader --- ReadMe.md | 4 +- .../plugins/gpioreader/application.fam | 2 +- .../plugins/gpioreader2/GPIO_reader.c | 129 ++++++++++++++++++ .../plugins/gpioreader2/GPIO_reader_item.c | 26 ++++ .../plugins/gpioreader2/GPIO_reader_item.h | 42 ++++++ applications/plugins/gpioreader2/README.md | 3 + .../plugins/gpioreader2/application.fam | 11 ++ applications/plugins/gpioreader2/icon.png | Bin 0 -> 135 bytes 8 files changed, 215 insertions(+), 2 deletions(-) create mode 100644 applications/plugins/gpioreader2/GPIO_reader.c create mode 100644 applications/plugins/gpioreader2/GPIO_reader_item.c create mode 100644 applications/plugins/gpioreader2/GPIO_reader_item.h create mode 100644 applications/plugins/gpioreader2/README.md create mode 100644 applications/plugins/gpioreader2/application.fam create mode 100644 applications/plugins/gpioreader2/icon.png diff --git a/ReadMe.md b/ReadMe.md index 5f533ea5d..ce43381f1 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -20,6 +20,7 @@ Thank you to all the supporters! - Last Synced/Checked [OFW](https://github.com/flipperdevices/flipperzero-firmware), changes in [commits](https://github.com/flipperdevices/flipperzero-firmware/commits/dev): `2022-12-15 15:50 EST` - Updated: [NRF24 Scanner v1.8 (By vad7)](https://github.com/vad7/nrf24scan) - Added: [Count Down Timer (By 0w0mewo)](https://github.com/0w0mewo/fpz_cntdown_timer) +- Added: [GPIO Reader (Aurelilc) (By aureli1c)](https://github.com/aureli1c/flipperzero_GPIO_read) ## Install from Release FLASH STOCK FIRST BEFORE UPDATING TO CUSTOM FIRMWARE! @@ -198,7 +199,8 @@ $ ./fbt resources icons dolphin_ext - [DSTIKE Deauther (By SequoiaSan)](https://github.com/SequoiaSan/FlipperZero-Wifi-ESP8266-Deauther-Module/tree/FlipperZero-Module-v2/FlipperZeroModule/FlipperZero-ESP8266-Deauth-App) `Req: ESP8266` - [DTMF Dolphin (By litui)](https://github.com/litui/dtmf_dolphin) - [Flashlight (By xMasterX)](https://github.com/xMasterX/flipper-flashlight) -- [GPIO Reader (By biotinker)](https://github.com/biotinker/flipperzero-gpioreader) +- [GPIO Reader (biotinker) (By biotinker)](https://github.com/biotinker/flipperzero-gpioreader) +- [GPIO Reader (Aurelilc) (By aureli1c)](https://github.com/aureli1c/flipperzero_GPIO_read) - [GPS (By ezod)](https://github.com/ezod/flipperzero-gps) `Req: NMEA 0183` - [HEX Viewer (By QtRoS)](https://github.com/QtRoS/flipperzero-firmware) - [i2c Tools (By NaejEL)](https://github.com/NaejEL/flipperzero-i2ctools) diff --git a/applications/plugins/gpioreader/application.fam b/applications/plugins/gpioreader/application.fam index 4a6b46be3..5e1a2e737 100644 --- a/applications/plugins/gpioreader/application.fam +++ b/applications/plugins/gpioreader/application.fam @@ -1,6 +1,6 @@ App( appid="GPIOReader", - name="GPIO Reader", + name="GPIO Reader (biotinker)", apptype=FlipperAppType.EXTERNAL, entry_point="gpio_app", cdefines=["APP_GPIOREADER"], diff --git a/applications/plugins/gpioreader2/GPIO_reader.c b/applications/plugins/gpioreader2/GPIO_reader.c new file mode 100644 index 000000000..144147b67 --- /dev/null +++ b/applications/plugins/gpioreader2/GPIO_reader.c @@ -0,0 +1,129 @@ +#include +#include +#include +#include +#include "GPIO_reader_item.h" + +typedef enum { + EventTypeTick, + EventTypeKey, +} EventType; + +typedef struct { + EventType type; + InputEvent input; +} PluginEvent; + +typedef struct { + int pin; + int pullMode; +} PluginState; + + +static void render_callback(Canvas* const canvas, void* ctx) { + const PluginState* plugin_state = acquire_mutex((ValueMutex*)ctx, 25); + + canvas_set_font(canvas, FontPrimary); + canvas_draw_str_aligned(canvas, canvas_width(canvas) / 2, canvas_height(canvas) / 10, AlignCenter, AlignCenter, "GPIO reader"); + + canvas_set_font(canvas, FontSecondary); + canvas_draw_str_aligned(canvas, canvas_width(canvas) / 2, canvas_height(canvas) / 10 * 3, AlignCenter, AlignCenter, + gpio_item_get_pin_name(plugin_state->pin)); + + canvas_draw_str_aligned(canvas, canvas_width(canvas) / 2, canvas_height(canvas) / 10 * 5, AlignCenter, AlignCenter, + gpio_item_get_pull_mode(plugin_state->pullMode)); + + canvas_set_font(canvas, FontPrimary); + canvas_draw_str_aligned(canvas, canvas_width(canvas) / 2, canvas_height(canvas) / 10 * 8, AlignCenter, AlignCenter, + gpio_item_get_pin_level(plugin_state->pin)); + + 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 GPIO_reader_state_init(PluginState* const plugin_state) { + plugin_state->pin = 0; + plugin_state->pullMode = 0; + gpio_item_configure_pin(plugin_state->pin, plugin_state->pullMode); +} + +int32_t GPIO_reader_app(void* p) { + UNUSED(p); + FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent)); + + PluginState* plugin_state = malloc(sizeof(PluginState)); + GPIO_reader_state_init(plugin_state); + ValueMutex state_mutex; + if (!init_mutex(&state_mutex, plugin_state, sizeof(PluginState))) { + FURI_LOG_E("GPIO_reader", "cannot create mutex\r\n"); + 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("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 || event.input.type == InputTypeRepeat) { + switch(event.input.key) { + case InputKeyRight: + plugin_state->pin = (plugin_state->pin + 1) % GPIO_ITEM_COUNT; + gpio_item_configure_pin(plugin_state->pin, plugin_state->pullMode); + break; + case InputKeyLeft: + plugin_state->pin = (plugin_state->pin - 1 + GPIO_ITEM_COUNT) % GPIO_ITEM_COUNT; + gpio_item_configure_pin(plugin_state->pin, plugin_state->pullMode); + break; + case InputKeyUp: + plugin_state->pullMode = (plugin_state->pullMode + 1) % GPIO_PULL_COUNT; + gpio_item_configure_pin(plugin_state->pin, plugin_state->pullMode); + break; + case InputKeyDown: + plugin_state->pullMode = (plugin_state->pullMode - 1 + GPIO_PULL_COUNT) % GPIO_PULL_COUNT; + gpio_item_configure_pin(plugin_state->pin, plugin_state->pullMode); + break; + case InputKeyBack: + processing = false; + break; + default: + break; + } + } + } + } else { + FURI_LOG_D("GPIO_reader", "FuriMessageQueue: event timeout"); + // event timeout + } + + 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("gui"); + view_port_free(view_port); + furi_message_queue_free(event_queue); + + return 0; +} \ No newline at end of file diff --git a/applications/plugins/gpioreader2/GPIO_reader_item.c b/applications/plugins/gpioreader2/GPIO_reader_item.c new file mode 100644 index 000000000..15726722c --- /dev/null +++ b/applications/plugins/gpioreader2/GPIO_reader_item.c @@ -0,0 +1,26 @@ +#include "GPIO_reader_item.h" + +const char* gpio_item_get_pin_name(uint8_t index) { + furi_assert(index < GPIO_ITEM_COUNT); + return gpio_item[index].name; +} + +const char* gpio_item_get_pull_mode(uint8_t pull_mode) { + furi_assert(pull_mode < GPIO_PULL_COUNT); + return gpio_pull_mode[pull_mode].name; +} + +const char* gpio_item_get_pin_level(uint8_t index) { + furi_assert(index < GPIO_ITEM_COUNT); + //furi_hal_gpio_write(gpio_item[index].pin, level); + if (furi_hal_gpio_read(gpio_item[index].pin)){ + return "High"; + }else{ + return "Low"; + } +} + +void gpio_item_configure_pin(uint8_t index, uint8_t pull_mode) { + furi_assert(index < GPIO_ITEM_COUNT); + furi_hal_gpio_init(gpio_item[index].pin, GpioModeInput, gpio_pull_mode[pull_mode].pull, GpioSpeedVeryHigh); +} \ No newline at end of file diff --git a/applications/plugins/gpioreader2/GPIO_reader_item.h b/applications/plugins/gpioreader2/GPIO_reader_item.h new file mode 100644 index 000000000..da6bcc89f --- /dev/null +++ b/applications/plugins/gpioreader2/GPIO_reader_item.h @@ -0,0 +1,42 @@ +#ifndef GPIO_READER_ITEM +#define GPIO_READER_ITEM + +#include +#include + +#define GPIO_ITEM_COUNT 8 +#define GPIO_PULL_COUNT 3 + +typedef struct { + const char* name; + const GpioPin* pin; +} GpioItem; + +static const GpioItem gpio_item[GPIO_ITEM_COUNT] = { + {"2: PA7", &gpio_ext_pa7}, + {"3: PA6", &gpio_ext_pa6}, + {"4: PA4", &gpio_ext_pa4}, + {"5: PB3", &gpio_ext_pb3}, + {"6: PB2", &gpio_ext_pb2}, + {"7: PC3", &gpio_ext_pc3}, + {"15: PC1", &gpio_ext_pc1}, + {"16: PC0", &gpio_ext_pc0}, +}; + +typedef struct { + const char* name; + const GpioPull pull; +} GpioPullMode; + +static const GpioPullMode gpio_pull_mode[3] = { + {"high impedence", GpioPullNo}, + {"pull up", GpioPullUp}, + {"pull down", GpioPullDown}, +}; + +const char* gpio_item_get_pin_name(uint8_t index); +const char* gpio_item_get_pin_level(uint8_t index); +void gpio_item_configure_pin(uint8_t index, uint8_t pullMode); +const char* gpio_item_get_pull_mode(uint8_t pull_mode); + +#endif \ No newline at end of file diff --git a/applications/plugins/gpioreader2/README.md b/applications/plugins/gpioreader2/README.md new file mode 100644 index 000000000..c911cf313 --- /dev/null +++ b/applications/plugins/gpioreader2/README.md @@ -0,0 +1,3 @@ +# flipperzero_GPIO_read + +Plugin to read the GPIOs on the Flipper Zero. diff --git a/applications/plugins/gpioreader2/application.fam b/applications/plugins/gpioreader2/application.fam new file mode 100644 index 000000000..2ef695f1c --- /dev/null +++ b/applications/plugins/gpioreader2/application.fam @@ -0,0 +1,11 @@ +App( + appid="GPIOReader2", + name="GPIO Reader (Aurelilc)", + apptype=FlipperAppType.EXTERNAL, + entry_point="GPIO_reader_app", + requires=["gui"], + stack_size=1 * 1024, + fap_category="GPIO", + fap_icon="icon.png", + order=1, +) \ No newline at end of file diff --git a/applications/plugins/gpioreader2/icon.png b/applications/plugins/gpioreader2/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..201d033e74e36c5488a571920b3fee257c3e03ad GIT binary patch literal 135 zcmeAS@N?(olHy`uVBq!ia0vp^AT}2V8<6ZZI=>f4F%}28J29*~C-V}>vG;Uw4ABTq z{`3F;|9Uo+11C;!JX^?VY;Y+|@l4bV?+I(&H_rOSZdngu5+k1a4|d9Bh_Z irt#1_$AXE$%-QMLBh#tYK%*EuUHx3vIVChPFaQ90iY%J| literal 0 HcmV?d00001