hal::Pin Class Reference
Inheritance diagram for hal::Pin:

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.clear();
//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.get_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

 Pin (port_t port, u32 pin, bool ismask=false)
 
 Pin (mcu_pin_t p)
 
int clear () const
 
bool get_value () const
 
int init (u32 o_flags)
 
 operator bool ()
 
Pinoperator= (bool on)
 
u32 pinmask () const
 
int set () const
 
int set_attr (u32 o_flags) const
 
int set_input (u32 o_flags=0)
 
int set_output (u32 o_flags=0)
 
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'>
 Periph (core_periph_t periph, port_t port)
 
int get_info (pio_info_t &info) const
 
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
 
int set_priority (s8 priority, u32 o_events, int channel=0)
 
- Public Member Functions inherited from hal::PeriphObject
int close ()
 
int ioctl (int req, void *arg) const
 
int open (int o_mode=READWRITE)
 
int read (void *buf, int nbyte) const
 
int read (sys::Aio &aio) const
 
int seek (int loc, int whence) const
 
int write (const void *buf, int nbyte) const
 
int write (sys::Aio &aio) const
 
- Public Member Functions inherited from hal::Device
 Device ()
 
int set_signal_action (const DeviceSignal &signal, u32 o_events, u32 channel)
 
- Public Member Functions inherited from sys::File
int create (const char *path, bool overwrite=true, int perms=0666)
 
int fileno () const
 
int flags () const
 
char * gets (var::String &s, char term= '\n') const
 
int ioctl (int req, const void *arg) const
 
int ioctl (int req) const
 
int ioctl (int req, int arg) const
 
int loc () const
 
int open (const char *name, int access, int perms)
 
int open_append (const char *path)
 
int open_readonly (const char *path)
 
int open_readwrite (const char *path)
 
int read (int loc, void *buf, int nbyte) const
 
int readline (char *buf, int nbyte, int timeout_msec, char terminator= '\n') const
 
void set_fileno (int fd)
 
u32 size () const
 
int write (int loc, const void *buf, int nbyte) const
 
int write (const var::String &str) const
 
- Public Member Functions inherited from api::WorkObject
void clear_error_number () const
 
int error_number () const
 

Additional Inherited Members

- Public Types inherited from hal::PeriphObject
typedef unsigned int port_t
 
- Public Types inherited from sys::File
- Static Public Member Functions inherited from sys::File
static int access (const char *path, int o_access)
 
static int copy (var::String &source_path, var::String &dest_path)
 
static bool exists (const char *path)
 
static const char * name (const char *path)
 
static int remove (const char *path)
 
static int rename (var::String &old_path, var::String &new_path)
 
static u32 size (const char *path)
 
static int stat (const char *path, struct stat *st)
 
static const char * suffix (const char *path)
 
- Static Public Member Functions inherited from api::ApiObject
static const char * api_version ()
 
- Protected Member Functions inherited from hal::PeriphObject
int open (const char *name, int flags)
 

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()
hal::Pin::operator bool ( )
inline

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");
}
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
int hal::Pin::set_input ( u32  o_flags = 0)
inline

Initializes the pin as an input.

Parameters
o_flagsOptionally set FLAG_IS_PULLUP, etc
Returns
Zero on success
int hal::Pin::set_output ( u32  o_flags = 0)
inline

Initializes the pin as an input.

Parameters
o_flagsOptionally set FLAG_IS_OPENDRAIN, etc
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