class::hal::Pin

class hal::Pin
  : public hal::Pio

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

pin.initialize(Pin::OUTPUT); //initialize as an output

//set the pin high
pin = true;
pin.set();

//These turn the pin off
pin = false;
pin.clear();

//now convert to an input
pin.set_attributes(Pin::INPUT | Pin::PULLUP); //or use Pin::FLOAT, Pin::PULLDOWN, etc
//or to initialize as an input use pin.initialize(Pin::INPUT);

 //now check to see if the value is high or low
if( pin.get_value() == true ){
 //pin is high
} else {
 //pin is low
}

pin.close(); //close the associated file descriptor (pin keeps its IO properties and state)

Summary

[Pin](#classhal_1_1_pin_1ab549a3ad795ac739e6f5dfadeabcf10a)(  

const arg::PortNumber port,
const arg::PinNumber pin,
const arg::IsMcuPinMask is_mask
)

[Pin](#classhal_1_1_pin_1aeb48ba3d494fd76390139a50a13b0aee)(  

const arg::ImplicitMcuPin p
)

int clear() const

bool get_value() const

int initialize(

u32 o_flags
)

bool is_floating(

u32 o_restore_flags
) const

[operator bool](#classhal_1_1_pin_1aee7993b7dd9269d54f9719037a0e809a)() *const*

virtual const Pin & operator<<(

const chrono::MicroTime & a
) const

const Pin & operator<<(

bool value
) const

const Pin & operator=(

bool value
) const

u32 pinmask() const

int set() const

int set_attributes(

u32 o_flags
) const

int set_input(

u32 o_flags
)

int set_output(

u32 o_flags
)

void set_value(

bool value
) const

static mcu_pin_t from_string(

const var::ConstString & port_pin
)

static bool is_floating(

mcu_pin_t pin
)

Members

Pin(

const arg::PortNumber port,
const arg::PinNumber pin,
const arg::IsMcuPinMask is_mask
)

Constructs the object with a port/pin combination.


Pin(

const arg::ImplicitMcuPin p
)

Contructs a new pin object from an mcu_pin_t data structure.


int clear() const

Clear the pin low (assign value 0)


bool get_value() const

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


int initialize(

u32 o_flags
)

Initializes the pin with the specified mode.

Parameters

  • o_flags The 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()


bool is_floating(

u32 o_restore_flags
) const


operator bool() const

Returns true if the pin is high and false if it is low.

This allows the following code to work:

Pin button;

if( button ){
  printf("Button is logic high\n");
} else {
  printf("Button is logic low\n");
}

virtual const Pin & operator<<(

const chrono::MicroTime & a
) const


const Pin & operator<<(

bool value
) const

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


const Pin & operator=(

bool value
) const

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


u32 pinmask() const

Accesses the pin’s associated Pio pinmask.


int set() const

Sets the pin high (assign value 1)


int set_attributes(

u32 o_flags
) const

Sets the pin attributes.

Parameters

  • o_flags The pin mode (e.g., Pin::INPUT)

Returns

Zero on success


int set_input(

u32 o_flags
)

Initializes the pin as an input.

Parameters

  • o_flags Optionally set FLAG_IS_PULLUP, etc

Returns

Zero on success


int set_output(

u32 o_flags
)

Initializes the pin as an input.

Parameters

  • o_flags Optionally set FLAG_IS_OPENDRAIN, etc

Returns

Zero on success


void set_value(

bool value
) const

Assigns a boolean to the pin.

Parameters

  • value If true, sets the pin high

static mcu_pin_t from_string(

const var::ConstString & port_pin
)


static bool is_floating(

mcu_pin_t pin
)


X

Thanks for Coming!

Subscribe to news and updates