Type Alias atmega_hal::port::Pin
source · pub type Pin<MODE, PIN = Dynamic> = Pin<MODE, PIN>;
Expand description
Type-alias for a pin type which can represent any concrete pin.
Sometimes it is easier to handle pins if they are all of the same type. By default,
each pin gets its own distinct type in avr-hal
, but by
downgrading, you can cast them into this
“dynamic” type. Do note, however, that using this dynamic type has a runtime cost.
Aliased Type§
struct Pin<MODE, PIN = Dynamic> { /* private fields */ }
Implementations
source§impl<PIN> Pin<Analog, PIN>where
PIN: PinOps,
impl<PIN> Pin<Analog, PIN>where
PIN: PinOps,
§Analog Input
Some pins can be configured as ADC channels. For those pins, analog_read()
can be used to
read the voltage. analog_read()
corresponds to a blocking ADC read:
let dp = atmega_hal::Peripherals::take().unwrap();
let pins = atmega_hal::pins!(dp);
let mut adc = atmega_hal::Adc::new(dp.ADC, Default::default());
let a0 = pins.pc0.into_analog_input(&mut adc);
let voltage = a0.analog_read(&mut adc);
// ^- this is equivalent to -v
let voltage = adc.read_blocking(&a0);
pub fn analog_read<H, ADC, CLOCK>(&self, adc: &mut Adc<H, ADC, CLOCK>) -> u16
sourcepub fn into_channel<H, ADC>(self) -> Channel<H, ADC>
pub fn into_channel<H, ADC>(self) -> Channel<H, ADC>
Convert this pin into a generic Channel
type.
The generic channel type can be used to store multiple channels in an array.
sourcepub fn into_digital<H, ADC, CLOCK>(
self,
adc: &mut Adc<H, ADC, CLOCK>
) -> Pin<Input<Floating>, PIN>
pub fn into_digital<H, ADC, CLOCK>( self, adc: &mut Adc<H, ADC, CLOCK> ) -> Pin<Input<Floating>, PIN>
Convert this pin to a floating digital input pin.
The pin is re-enabled in the digital input buffer and is no longer usable as an analog
input. You can get to other digital modes by calling one of the usual into_...
methods
on the return value of this function.
source§impl<PIN, IMODE> Pin<Input<IMODE>, PIN>where
PIN: PinOps,
impl<PIN, IMODE> Pin<Input<IMODE>, PIN>where
PIN: PinOps,
§Input-Mode Downgrading
There is a second kind of downgrading: In some cases it is not important whether an input pin
is configured as mode::PullUp
or mode::Floating
. For this, you can “forget” the
concrete input mode, leaving you with a type that is the same for pull-up or floating inputs:
use atmega_hal::port::{Pin, mode};
let dp = atmega_hal::Peripherals::take().unwrap();
let pins = atmega_hal::pins!(dp);
// This demo uses downgraded pins, but it works just as well
// with non-downgraded ones!
let input_pin1: Pin<mode::Input<mode::Floating>> = pins.pd0
.into_floating_input()
.downgrade();
let input_pin2: Pin<mode::Input<mode::Floating>> = pins.pd1
.into_pull_up_input()
.downgrade();
// With the input mode "forgotten", they have the same type now,
// even if electically different.
let any_inputs: [Pin<mode::Input>; 2] = [
input_pin1.forget_imode(),
input_pin2.forget_imode(),
];
sourcepub fn forget_imode(self) -> Pin<Input, PIN>
pub fn forget_imode(self) -> Pin<Input, PIN>
“Erase” type-level information about whether the pin is currently a pull-up or a floating input.
source§impl<PIN, MODE> Pin<MODE, PIN>
impl<PIN, MODE> Pin<MODE, PIN>
§Configuration
To change the mode of a pin, use one of the following conversion functions. They consume the
original Pin
and return one with the desired mode. Only when a pin is in the correct mode,
does it have the mode-relevant methods availailable (e.g. set_high()
is only available for
Output
pins).
sourcepub fn into_output(self) -> Pin<Output, PIN>
pub fn into_output(self) -> Pin<Output, PIN>
Convert this pin into an output pin, setting the state to low. See Digital Output.
sourcepub fn into_output_high(self) -> Pin<Output, PIN>
pub fn into_output_high(self) -> Pin<Output, PIN>
Convert this pin into an output pin, setting the state to high. See Digital Output.
sourcepub fn into_opendrain(self) -> Pin<OpenDrain, PIN>
pub fn into_opendrain(self) -> Pin<OpenDrain, PIN>
Convert this pin into an open-drain output pin, setting the state to low. See Digital Output Open Drain
sourcepub fn into_opendrain_high(self) -> Pin<OpenDrain, PIN>
pub fn into_opendrain_high(self) -> Pin<OpenDrain, PIN>
Convert this pin into an open-drain output pin, setting the state to high. See Digital Output Open Drain
sourcepub fn into_floating_input(self) -> Pin<Input<Floating>, PIN>
pub fn into_floating_input(self) -> Pin<Input<Floating>, PIN>
Convert this pin into a floating input pin. See Digital Input.
Note: To read deterministic values from the pin, it must be externally pulled to a defined level (either VCC or GND).
sourcepub fn into_pull_up_input(self) -> Pin<Input<PullUp>, PIN>
pub fn into_pull_up_input(self) -> Pin<Input<PullUp>, PIN>
Convert this pin into a pulled-up input pin. See Digital Input.
With no external circuit pulling the pin low, it will be read high.
sourcepub fn into_analog_input<H, ADC, CLOCK>(
self,
adc: &mut Adc<H, ADC, CLOCK>
) -> Pin<Analog, PIN>
pub fn into_analog_input<H, ADC, CLOCK>( self, adc: &mut Adc<H, ADC, CLOCK> ) -> Pin<Analog, PIN>
Convert this pin into an analog input (ADC channel). See Analog Input.
Some pins can be repurposed as ADC channels. For those pins, the into_analog_input()
method is available.
source§impl<PIN, MODE> Pin<MODE, PIN>
impl<PIN, MODE> Pin<MODE, PIN>
§Downgrading
For applications where the exact pin is irrelevant, a specific pin can be downgraded to a “dynamic pin” which can represent any pin:
use atmega_hal::port::{Pin, mode};
let dp = atmega_hal::Peripherals::take().unwrap();
let pins = atmega_hal::pins!(dp);
let any_output_pin1: Pin<mode::Output> = pins.pd0.into_output().downgrade();
let any_output_pin2: Pin<mode::Output> = pins.pd1.into_output().downgrade();
// Because they now have the same type, you can, for example, stuff them into an array:
let pins: [Pin<mode::Output>; 2] = [any_output_pin1, any_output_pin2];
source§impl<PIN> Pin<OpenDrain, PIN>where
PIN: PinOps,
impl<PIN> Pin<OpenDrain, PIN>where
PIN: PinOps,
§Digital Output Open Drain
source§impl<PIN> Pin<Output, PIN>where
PIN: PinOps,
impl<PIN> Pin<Output, PIN>where
PIN: PinOps,
§Digital Output
sourcepub fn is_set_high(&self) -> bool
pub fn is_set_high(&self) -> bool
Check whether the pin is set high.
Note: The electrical state of the pin might differ due to external circuitry.
sourcepub fn is_set_low(&self) -> bool
pub fn is_set_low(&self) -> bool
Check whether the pin is set low.
Note: The electrical state of the pin might differ due to external circuitry.
Trait Implementations
source§impl<PIN> ErrorType for Pin<OpenDrain, PIN>where
PIN: PinOps,
impl<PIN> ErrorType for Pin<OpenDrain, PIN>where
PIN: PinOps,
§type Error = Infallible
type Error = Infallible
source§impl<PIN> OutputPin for Pin<OpenDrain, PIN>where
PIN: PinOps,
impl<PIN> OutputPin for Pin<OpenDrain, PIN>where
PIN: PinOps,
source§impl<PIN> OutputPin for Pin<OpenDrain, PIN>where
PIN: PinOps,
impl<PIN> OutputPin for Pin<OpenDrain, PIN>where
PIN: PinOps,
source§impl<PIN> OutputPin for Pin<Output, PIN>where
PIN: PinOps,
impl<PIN> OutputPin for Pin<Output, PIN>where
PIN: PinOps,
source§impl<PIN> OutputPin for Pin<Output, PIN>where
PIN: PinOps,
impl<PIN> OutputPin for Pin<Output, PIN>where
PIN: PinOps,
source§impl<TC, PIN> SetDutyCycle for Pin<PwmOutput<TC>, PIN>
impl<TC, PIN> SetDutyCycle for Pin<PwmOutput<TC>, PIN>
source§fn max_duty_cycle(&self) -> u16
fn max_duty_cycle(&self) -> u16
source§fn set_duty_cycle(
&mut self,
duty: u16
) -> Result<(), <Pin<PwmOutput<TC>, PIN> as ErrorType>::Error>
fn set_duty_cycle( &mut self, duty: u16 ) -> Result<(), <Pin<PwmOutput<TC>, PIN> as ErrorType>::Error>
duty / max_duty
. Read more