Macro esp_ward::create_joystick
source · macro_rules! create_joystick { ($peripherals: expr, $pins: expr, $pin_select: expr ) => { ... }; }
Expand description
Macro for creating a Joystick
instance.
Unlike a function, this macro can take ownership of parts of the
Peripherals
without consuming the whole Peripherals
struct. This allows
setting up the ADC configuration for the joystick’s analog pins and passing
it along for ADC initialization with peripherals.ADC1
without running into
Rust’s ownership errors.
Functions in Rust take ownership or borrow the entire value they are given,
which would not allow us to partially consume Peripherals
. This macro,
however, performs the setup inline where it’s invoked and thus avoids the
mentioned ownership issue.
§Arguments
$peripherals
- Theesp-hal
Peripherals
instance.$pins
- Theesp-hal
GPIO pins split fromPeripherals
.$pin_select
- The GPIO pin used for the joystick’s select button.
§Usage
This macro’s name still holds “naming convention” of
“create_<device_type>”, if sensor/peripheral does not work on top of
I2C/SPI
buses. This macro should be used where you have access to
the Peripherals
and the split pins, and it will return a tuple containing
the Joystick
instance and the initialized ADC.
/// let peripherals = take_periph!();
let system = take_system!(peripherals);
let (clocks, pins) = init_chip!(peripherals, system);
let (joystick, adc1) = create_joystick!(peripherals, pins, pin_select);