Handle Input
Using same wiring from the blinky chapter, let's now use external input to make the LED blink.
- Access
exercises/handle_inputin order to editsrc/main.rs.
Warning
This exercise is slightly more involved, in the sense you'll need to add more code. But there is always the solution if it gets too hard.
Exercise
We will use the button labelled BOOT linked to GPIO9 to toggle the LED.
-
As per usual, we will be editing
src/main.rs. -
We first need to fill in the boilerplate. Once that's done, this line might be of help:
// you will also have other `esp_hal` submodules besides `gpio`. +use esp_hal::gpio::{Input, InputConfig, Level, Output, OutputConfig, Pull},You may get inspired by the previous exercises and the boilerplate page.
-
Create an
Output::newpassing GPIO7 peripheral,Level::Highand defaultOutputConfig.- Assign it to the
ledvariable. - Hint: with peripherals initialised, GPIO7 is a field in that struct.
- Assign it to the
-
Create an
Input::newpassing GPIO9 peripheral.- Use default
InputConfig, overwrite withas_pull(Pull::High). - Assign it to the
btnvariable.
- Use default
-
Add the logic inside
loop:- When pressing it should turn the
ledon, and delay it 2 seconds. - Then it turns itself off.
- When pressing it should turn the
The examples/handle_input.rs contains a solution. You can run it with the following command cargo run --example handle_input --release.