PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
Public Types | Public Member Functions | Static Public Member Functions | List of all members
PropWare::Pin Class Reference

Utility class to handle general purpose I/O pins. More...

#include <PropWare/gpio/pin.h>

+ Inheritance diagram for PropWare::Pin:
+ Collaboration diagram for PropWare::Pin:

Public Types

enum  Channel {
  A,
  B
}
 
enum  Mask {
  P0 = BIT_0,
  P1 = BIT_1,
  P2 = BIT_2,
  P3 = BIT_3,
  P4 = BIT_4,
  P5 = BIT_5,
  P6 = BIT_6,
  P7 = BIT_7,
  P8 = BIT_8,
  P9 = BIT_9,
  P10 = BIT_10,
  P11 = BIT_11,
  P12 = BIT_12,
  P13 = BIT_13,
  P14 = BIT_14,
  P15 = BIT_15,
  P16 = BIT_16,
  P17 = BIT_17,
  P18 = BIT_18,
  P19 = BIT_19,
  P20 = BIT_20,
  P21 = BIT_21,
  P22 = BIT_22,
  P23 = BIT_23,
  P24 = BIT_24,
  P25 = BIT_25,
  P26 = BIT_26,
  P27 = BIT_27,
  P28 = BIT_28,
  P29 = BIT_29,
  P30 = BIT_30,
  P31 = BIT_31,
  NULL_PIN = 0
}
 
enum  Dir {
  Dir::IN = 0,
  Dir::OUT = -1
}
 

Public Member Functions

 Pin (const PropWare::Pin::Mask mask=NULL_PIN)
 Create a Pin variable. More...
 
 Pin (const Pin::Mask mask, const Pin::Dir direction)
 
void set_mask (const Pin::Mask mask)
 
void set_pin_number (const uint8_t pinNum)
 Set a Pin's mask based on the pin number (an integer, 0 through 31) More...
 
uint_fast8_t get_pin_number () const
 Get the pin's number (an integer, 0 through 31)
 
Pin::Mask get_mask () const
 
Channel get_channel () const
 
void set_channel (const Channel channel)
 
void write (const bool value) const
 Set or clear the pin programmatically. More...
 
bool read () const
 Read the value from a single pin and return its state. More...
 
void wait_until_high () const
 Hold cog execution until an input pin goes high. More...
 
void wait_until_low () const
 Hold cog execution until an input pin goes low. More...
 
void wait_until_toggle () const
 Hold cog execution until an input pin toggles. More...
 
bool is_switch_low (const uint16_t debounceDelayInMillis=3) const
 Allow easy switch-press detection of any pin; Includes de-bounce protection. More...
 
int rc_time (const bool state, const uint32_t timeout=SECOND/4)
 Set to input and measure the time it takes a signal to transition from a start state to the opposite state. More...
 
void start_hardware_pwm (const uint32_t frequency) const
 Output a PWM signal on this pin. More...
 
void stop_hardware_pwm () const
 Stop the hardware counter.
 
void set_dir (const PropWare::Port::Dir direction) const
 Set port as either input or output. More...
 
PropWare::Port::Dir get_dir () const
 Determine the direction this port is currently set to. More...
 
void set_dir_out () const
 Set the port for output.
 
void set_dir_in () const
 Set the port for input.
 
void set () const
 Set selected output port high (set all pins to 1) More...
 
void high () const
 
void on () const
 
void clear () const
 Clear selected output port (set it to 0) More...
 
void low () const
 
void off () const
 
void toggle () const
 Toggle the output value of a port. More...
 
void write_fast (const uint32_t value) const
 Allow easy writing to a port w/o destroying data elsewhere in the port; No shift is performed to align data with the desired output pins. More...
 

Static Public Member Functions

static void flash_pin (const Pin::Mask pinMask, const unsigned int iterations=10)
 Great for quick debugging to ensure a line of code is executed, this will quickly flash a given pin a specific number of times. More...
 
static uint8_t from_mask (const Mask mask)
 Determine which pin number based on a pin mask. More...
 
static Mask to_mask (const uint8_t pinNum)
 Return a Mask type based on a pin number. More...
 
static void flash_port (const uint32_t pinMask, unsigned int iterations=10)
 Great for quick debugging to ensure a line of code is executed, this will quickly flash a given set of pins a specific number of times. More...
 

Detailed Description

Utility class to handle general purpose I/O pins.

Examples
Blinky_Demo.cpp, Cogc_Demo.cpp, HD44780_Demo.cpp, I2CSlave_Demo.cpp, MCP2515_Demo.cpp, MCP3xxx_Demo.cpp, MultiCogBlinky_Demo.cpp, Runnable_Demo.cpp, SPI_Demo.cpp, SynchronousPrinter_Demo.cpp, UARTRX_Demo.cpp, UARTTX_Demo.cpp, and WS2812_Demo.cpp.

Definition at line 36 of file pin.h.

Member Enumeration Documentation

◆ Dir

enum PropWare::Port::Dir
stronginherited

Direction of GPIO pin

Enumerator
IN 

Set pin(s) as input

OUT 

Set pin(s) as output

Definition at line 82 of file port.h.

◆ Mask

enum PropWare::Port::Mask
inherited

Bit-mask of GPIO pins

Enumerator
P0 

GPIO pin 0

P1 

GPIO pin 1

P2 

GPIO pin 2

P3 

GPIO pin 3

P4 

GPIO pin 4

P5 

GPIO pin 5

P6 

GPIO pin 6

P7 

GPIO pin 7

P8 

GPIO pin 8

P9 

GPIO pin 9

P10 

GPIO pin 10

P11 

GPIO pin 11

P12 

GPIO pin 12

P13 

GPIO pin 13

P14 

GPIO pin 14

P15 

GPIO pin 15

P16 

GPIO pin 16

P17 

GPIO pin 17

P18 

GPIO pin 18

P19 

GPIO pin 19

P20 

GPIO pin 20

P21 

GPIO pin 21

P22 

GPIO pin 22

P23 

GPIO pin 23

P24 

GPIO pin 24

P25 

GPIO pin 25

P26 

GPIO pin 26

P27 

GPIO pin 27

P28 

GPIO pin 28

P29 

GPIO pin 29

P30 

GPIO pin 30

P31 

GPIO pin 31

NULL_PIN 

Null pin; Marks end of Mask array

Definition at line 43 of file port.h.

Constructor & Destructor Documentation

◆ Pin() [1/2]

PropWare::Pin::Pin ( const PropWare::Pin::Mask  mask = NULL_PIN)

Create a Pin variable.

Parameters
[in]maskBit-mask of pin; One of PropWare::Pin::Mask

Definition at line 61 of file pin.h.

◆ Pin() [2/2]

PropWare::Pin::Pin ( const Pin::Mask  mask,
const Pin::Dir  direction 
)
Parameters
[in]maskBit-mask of pin; One of PropWare::Pin::Mask
[in]directionDirection to initialize pin; One of PropWare::Pin::Dir

Definition at line 71 of file pin.h.

Member Function Documentation

◆ clear()

void PropWare::Port::clear ( ) const
inherited

Clear selected output port (set it to 0)

Precondition
If port is not set as output, statement will have no affect

Definition at line 249 of file port.h.

+ Here is the caller graph for this function:

◆ flash_pin()

static void PropWare::Pin::flash_pin ( const Pin::Mask  pinMask,
const unsigned int  iterations = 10 
)
static

Great for quick debugging to ensure a line of code is executed, this will quickly flash a given pin a specific number of times.

Parameters
[in]pinMaskPin that should be flashed
[in]iterationsNumber of times that the pin should flicker on and back off again

Definition at line 51 of file pin.h.

+ Here is the call graph for this function:

◆ flash_port()

static void PropWare::Port::flash_port ( const uint32_t  pinMask,
unsigned int  iterations = 10 
)
staticinherited

Great for quick debugging to ensure a line of code is executed, this will quickly flash a given set of pins a specific number of times.

Parameters
[in]pinMaskPins that should be flashed
[in]iterationsNumber of times that the pins should flicker on and back off again

Definition at line 129 of file port.h.

+ Here is the caller graph for this function:

◆ from_mask()

static uint8_t PropWare::Port::from_mask ( const Mask  mask)
staticinherited

Determine which pin number based on a pin mask.

Note
Return value is 0-indexed
Parameters
[in]maskValue with only a single bit set high representing Propeller pin (i.e.: 0x80 would be pin 7)
Returns
Return the pin number of pin

Definition at line 98 of file port.h.

+ Here is the caller graph for this function:

◆ get_dir()

PropWare::Port::Dir PropWare::Port::get_dir ( ) const
inherited

Determine the direction this port is currently set to.

Returns
Returns either PropWare::Port::OUT or PropWare::Port::IN

Definition at line 200 of file port.h.

◆ high()

void PropWare::Port::high ( ) const
inherited
See also
PropWare::Port::set()

Definition at line 233 of file port.h.

+ Here is the call graph for this function:

◆ is_switch_low()

bool PropWare::Pin::is_switch_low ( const uint16_t  debounceDelayInMillis = 3) const

Allow easy switch-press detection of any pin; Includes de-bounce protection.

Parameters
[in]debounceDelayInMillisSet the de-bounce delay in units of milliseconds
Returns
Returns 1 or 0 depending on whether the switch was pressed

Definition at line 171 of file pin.h.

+ Here is the call graph for this function:

◆ low()

void PropWare::Port::low ( ) const
inherited
See also
PropWare::Port::clear()

Definition at line 256 of file port.h.

+ Here is the call graph for this function:

◆ off()

void PropWare::Port::off ( ) const
inherited
See also
PropWare::Port::clear()

Definition at line 263 of file port.h.

+ Here is the call graph for this function:

◆ on()

void PropWare::Port::on ( ) const
inherited
See also
PropWare::Port::set()

Definition at line 240 of file port.h.

+ Here is the call graph for this function:

◆ rc_time()

int PropWare::Pin::rc_time ( const bool  state,
const uint32_t  timeout = SECOND / 4 
)

Set to input and measure the time it takes a signal to transition from a start state to the opposite state.

Named rc_time because it is often used to measure a resistor-capacitor circuit's tendency to "decay" to either ground or 5 V (depending on wiring). Default time increments are specified in 1 microsecond units. Unit size can be changed with a call to set_io_dt function. The pulse will be positive if the I/O pin is transmitting a low signal before the call.

Parameters
[in]stateStarting pin state; true for high, false for low
[in]timeoutTime to wait (in clock ticks) before canceling the function call [Default = 1/4 second]
Returns
Time from starting pin (in clock ticks)

Definition at line 198 of file pin.h.

◆ read()

bool PropWare::Pin::read ( ) const

Read the value from a single pin and return its state.

Returns
True if the pin is high, False if the pin is low

Definition at line 130 of file pin.h.

+ Here is the caller graph for this function:

◆ set()

void PropWare::Port::set ( ) const
inherited

Set selected output port high (set all pins to 1)

Precondition
If port is not set as output, statement will have no affect

Definition at line 226 of file port.h.

+ Here is the caller graph for this function:

◆ set_dir()

void PropWare::Port::set_dir ( const PropWare::Port::Dir  direction) const
inherited

Set port as either input or output.

Parameters
[in]directionI/O direction to set selected pins; must be one of PropWare::Port::IN or PropWare::Port::OUT

Definition at line 191 of file port.h.

+ Here is the caller graph for this function:

◆ set_mask()

void PropWare::Pin::set_mask ( const Pin::Mask  mask)
See also
PropWare::Port::set_mask()

Definition at line 80 of file pin.h.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ set_pin_number()

void PropWare::Pin::set_pin_number ( const uint8_t  pinNum)

Set a Pin's mask based on the pin number (an integer, 0 through 31)

Parameters
[in]pinNumAn integer 0-31 representing GPIO pins P0-P31

Definition at line 90 of file pin.h.

+ Here is the call graph for this function:

◆ start_hardware_pwm()

void PropWare::Pin::start_hardware_pwm ( const uint32_t  frequency) const

Output a PWM signal on this pin.

Use the Propeller's built-in hardware counters to generate a PWM signal on the pin. This method does not set the pin direction, so be sure to invoke PropWare::Pin::set_dir_out() prior to invoking this method.

Parameters
[in]frequencyFrequency in Hertz (Hz)

Definition at line 243 of file pin.h.

+ Here is the call graph for this function:

◆ to_mask()

static Mask PropWare::Port::to_mask ( const uint8_t  pinNum)
staticinherited

Return a Mask type based on a pin number.

Parameters
[in]pinNum0-indexed pin number
Returns
Bit mask representing the requested pin. If pinNum > 31, Mask::NULL_PIN is returned

Definition at line 115 of file port.h.

+ Here is the caller graph for this function:

◆ toggle()

void PropWare::Port::toggle ( ) const
inherited

Toggle the output value of a port.

Precondition
If port is not set as output, statement will have no affect

Definition at line 272 of file port.h.

◆ wait_until_high()

void PropWare::Pin::wait_until_high ( ) const

Hold cog execution until an input pin goes high.

Precondition
Pin must be configured as input; You will have very sad and undesirable results if your pin is an output at the time of calling this function

Definition at line 140 of file pin.h.

◆ wait_until_low()

void PropWare::Pin::wait_until_low ( ) const

Hold cog execution until an input pin goes low.

Precondition
Pin must be configured as input; You will have very sad and undesirable results if your pin is an output at the time of calling this function

Definition at line 150 of file pin.h.

◆ wait_until_toggle()

void PropWare::Pin::wait_until_toggle ( ) const

Hold cog execution until an input pin toggles.

Precondition
Pin must be configured as input; You will have very sad and undesirable results if your pin is an output at the time of calling this function

Definition at line 160 of file pin.h.

◆ write()

void PropWare::Pin::write ( const bool  value) const

Set or clear the pin programmatically.

Parameters
[in]Truefor a high pin, false for a low pin

Definition at line 118 of file pin.h.

+ Here is the call graph for this function:

◆ write_fast()

void PropWare::Port::write_fast ( const uint32_t  value) const
inherited

Allow easy writing to a port w/o destroying data elsewhere in the port; No shift is performed to align data with the desired output pins.

Parameters
[in]valuevalue to be bit-masked and then written to the port

Definition at line 282 of file port.h.

+ Here is the caller graph for this function:

The documentation for this class was generated from the following file: