hal::Pin Class Reference

Documentation

This class controls the operation of a single pin. It works similarly to the Pio class, but the mask is built-in.

Here is an example of using the Pin class:

#include <sapi/hal.hpp>
Pin pin(1,0); //control pin P1.0
//Or if working with the pinmask Pin pin(1, 1<<0, true);
pin.init(Pin::OUTPUT); //initialize as an output
//These three all set the pin high
pin = true;
pin.set();
//These turn the pin off
pin = false;
pin.clr();
//now convert to an input
pin.set_attr(Pin::INPUT | Pin::PULLUP); //or use Pin::FLOAT, Pin::PULLDOWN, etc
//or to init as an input use pin.init(Pin::INPUT);
//now check to see if the value is high or low
if( pin.value() == true ){
//pin is high
} else {
//pin is low
}
pin.close(); //close the associated file descriptor (pin keeps its IO properties and state)

Public Member Functions

int clear () const
 
bool get_value () const
 
int init (u32 o_flags)
 
Pinoperator= (bool on)
 
 Pin (port_t port, u32 pin, bool ismask=false)
 
 Pin (mcu_pin_t p)
 
u32 pinmask () const
 
int set () const
 
int set_attr (u32 o_flags) const
 
void set_value (bool value) const
 
- Public Member Functions inherited from hal::Pio
int assign (u32 mask) const
 
int clear_mask (u32 mask) const
 
u32 get_value () const
 
int init (u32 o_flags, u32 o_pinmask)
 
int set_attr (u32 o_flags, u32 o_pinmask) const
 
int set_mask (u32 mask) const
 
int set_value (unsigned int value) const
 
- Public Member Functions inherited from hal::Periph< pio_info_t, pio_attr_t, 'p'>
int get_version () const
 
int init ()
 
int set_action (u32 channel, u32 o_events, s8 prio=0, mcu_callback_t callback=0, void *context=0) const
 
int set_attr () const
 

Constructor & Destructor Documentation

hal::Pin::Pin ( port_t  port,
u32  pin,
bool  ismask = false 
)
inline

Initializes the object with a port/pin combination.

hal::Pin::Pin ( mcu_pin_t  p)
inline

Contructs a new pin object from an mcu_pin_t data structure.

Member Function Documentation

int hal::Pin::clear ( ) const
inline

Clear the pin low (assign value 0)

bool hal::Pin::get_value ( ) const
inline

Gets the value of the pin (true is high, false is low).

int hal::Pin::init ( u32  o_flags)
inline

Initializes the pin with the specified mode.

Parameters
o_flagsThe mode to start the pin (e.g., Pin::INPUT | Pin::PULLUP)

Hardware intialization opens the device for reading/writing and then sets the device attributes as specified.

The following is an example of initializing a pin.

#include <sapi/hal.hpp>
Pin p(0,1); //Port 0, pin 1
p.init(Pin::INPUT | Pin::PULLUP);
if( p.get_value() == 0 ){
//this means something is driving the pin low
}
See also
open(), set_attr()
Pin& hal::Pin::operator= ( bool  on)
inline

Assigns a boolean to the pin (true is high, false is low).

u32 hal::Pin::pinmask ( ) const
inline

Accesses the pin's associated Pio pinmask.

int hal::Pin::set ( ) const
inline

Sets the pin high (assign value 1)

int hal::Pin::set_attr ( u32  o_flags) const
inline

Sets the pin attributes.

Parameters
o_flagsThe pin mode (e.g., Pin::INPUT)
Returns
Zero on success
void hal::Pin::set_value ( bool  value) const
inline

Assigns a boolean to the pin.

Parameters
valueIf true, sets the pin high