Add Blink LED example

This commit is contained in:
Derek Jamison 2023-06-11 20:37:51 -05:00
parent 7a0540971e
commit 4c304ac075
4 changed files with 101 additions and 0 deletions

44
gpio/gpio_blink/README.md Normal file
View File

@ -0,0 +1,44 @@
# GPIO_BLINK
This is the classic "Hello World" of GPIO, where we blink an LED. You will need an LED (any color is fine) and a resistor (220 ohms - 1K should be fine). Connect the long side of the LED into pin A7. Connect the short size of the LED to one side of the resistor (doesn't matter which side). Connect the other side of the resistor to any of the pin GND.
Run the application and the LED should start blinking. Hold the back button to exit.
## How it works
- For list of pins see \firmware\targets\f18\furi_hal\furi_hal_resources.c
- We define a variable for our LED...
```c
const GpioPin* const pin_led = &gpio_ext_pa7;
```
- We initialize the pin for output.
- GpioModeOutputPushPull means true = +3.3V, false = 0V.
- GpioModeOutputOpenDrain means true = floating, false = 0V.
```c
furi_hal_gpio_init_simple(pin_led, GpioModeOutputOpenDrain);
```
- We turn the LED on (3v3 volts on the pin).
```c
furi_hal_gpio_write(pin_led, true);
```
- We wait 500ms.
```c
furi_delay_ms(500);
```
- We turn the LED off (GND volts on the pin).
```c
furi_hal_gpio_write(pin_led, false);
```
- We wait 500ms.
```c
furi_delay_ms(500);
```
- We loop if the back button is not currently pressed.
```c
while(furi_hal_gpio_read(pin_back))
```

View File

@ -0,0 +1,10 @@
App(
appid="gpio_blink",
name="GPIO Blink",
apptype=FlipperAppType.EXTERNAL,
entry_point="gpio_blink_app",
requires=["gui"],
stack_size=2 * 1024,
fap_icon="blink.png",
fap_category="GPIO",
)

47
gpio/gpio_blink/blink.c Normal file
View File

@ -0,0 +1,47 @@
#include <furi.h>
#include <gui/gui.h>
// For list of pins see \firmware\targets\f18\furi_hal\furi_hal_resources.c
const GpioPin* const pin_led = &gpio_ext_pa7;
const GpioPin* const pin_back = &gpio_button_back;
static void my_draw_callback(Canvas* canvas, void* context) {
UNUSED(context);
canvas_set_font(canvas, FontPrimary);
canvas_draw_str(canvas, 5, 8, "GPIO - BLINK LED DEMO");
canvas_draw_str(canvas, 5, 22, "Connect long LED to pin");
canvas_draw_str(canvas, 5, 32, "A7. Connect short LED");
canvas_draw_str(canvas, 5, 42, "to 220 ohm resistor.");
canvas_draw_str(canvas, 5, 52, "Connect other end of");
canvas_draw_str(canvas, 5, 62, "resistor to pin GND.");
}
int gpio_blink_app(void* p) {
UNUSED(p);
// Show directions to user.
Gui* gui = furi_record_open(RECORD_GUI);
ViewPort* view_port = view_port_alloc();
view_port_draw_callback_set(view_port, my_draw_callback, NULL);
gui_add_view_port(gui, view_port, GuiLayerFullscreen);
// Initialize the LED pin as output.
// GpioModeOutputPushPull means true = +3.3V, false = 0V.
// GpioModeOutputOpenDrain means true = floating, false = 0V.
furi_hal_gpio_init_simple(pin_led, GpioModeOutputOpenDrain);
do {
furi_hal_gpio_write(pin_led, true);
furi_delay_ms(500);
furi_hal_gpio_write(pin_led, false);
furi_delay_ms(500);
// Hold the back button to exit (since we only scan it when restarting loop).
} while(furi_hal_gpio_read(pin_back));
// Typically when a pin is no longer in use, it is set to analog mode.
furi_hal_gpio_init_simple(pin_led, GpioModeAnalog);
// Remove the directions from the screen.
gui_remove_view_port(gui, view_port);
return 0;
}

BIN
gpio/gpio_blink/blink.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB