ShiftRegGPIOXpander Library for Arduino v3.1.0
A library that provides an easy mechanism to add GPIO digital output pins by using Shift Registers
|
A class that models a GPIO outputs pins expander through the use of 8-bits Serial In Paralell Out (SIPO) shift registers. More...
#include <ShiftRegGPIOXpander_AVR.h>
Public Member Functions | |
ShiftRegGPIOXpander () | |
Class default constructor. | |
ShiftRegGPIOXpander (uint8_t ds, uint8_t sh_cp, uint8_t st_cp, uint8_t srQty=1) | |
Class constructor. | |
~ShiftRegGPIOXpander () | |
Class destructor. | |
bool | begin (uint8_t *initCntnt=nullptr) |
GPIOXpander object setup and activation. | |
bool | copyMainToAux (const bool &overWriteIfExists=true) |
Copies the Buffer content to the Auxiliary Buffer. | |
SRGXVPort | createSRGXVPort (const uint8_t &strtPin, const uint8_t &pinsQty) |
Instantiate a SRGXVPort object. | |
bool | digitalReadSgmntSr (const uint8_t &strtPin, const uint8_t &pinsQty, uint16_t &bffrSgmnt) |
Returns a 16-bits value containing a zero-based segment of the Main Buffer. | |
int | digitalRead (const uint8_t &srPin) |
Returns the state of the requested pin of the ShiftRegGPIOXpander. | |
uint8_t | digitalReadSr (const uint8_t &srPin) |
Returns the state of the requested pin. | |
bool | digitalToggleSr (const uint8_t &srPin) |
Toggles the state of a specific pin. | |
bool | digitalToggleSrAll () |
Toggles the state of all the pins. | |
bool | digitalToggleSrMask (uint8_t *toggleMask) |
Toggles the state of the pins in the Main Buffer according to the provided mask. | |
bool | digitalToggleSrToAux (const uint8_t &srPin) |
Toggles the state of a specific pin in the Auxiliary Buffer. | |
void | digitalWrite (const uint8_t &srPin, const uint8_t &value) |
Set a specific pin to either HIGH (0x01) or LOW (0x00). | |
bool | digitalWriteSr (const uint8_t &srPin, const uint8_t &value) |
Set a specific pin to either HIGH (0x01) or LOW (0x00). | |
bool | digitalWriteSrAllReset () |
Sets all the pins to LOW (0x00/Reset). | |
bool | digitalWriteSrAllSet () |
Sets all the pins to HIGH (0x01). | |
bool | digitalWriteSrMaskReset (uint8_t *resetMask) |
Modifies the Main buffer contents by resetting simultaneously certain pins. | |
bool | digitalWriteSrMaskSet (uint8_t *setMask) |
Modifies the Main buffer contents by setting simultaneously certain pins. | |
bool | digitalWriteSrToAux (const uint8_t srPin, const uint8_t value) |
Set a specific pin to either HIGH (0x01) or LOW (0x00) in the Auxiliary Buffer. | |
bool | discardAux () |
Deletes the Auxiliary Buffer. | |
void | end () |
Method provided for ending any relevant activation procedures made by the begin(uint8_t*) method. | |
bool | flipBit (const uint8_t &srPin) |
Toggles the state of a specific pin in the Main Buffer. | |
uint8_t * | getMainBuffPtr () |
Retrieves the pointer to the Main Buffer. | |
uint8_t | getMaxSRGXPin () |
Return the greatest valid pin number. | |
uint8_t | getSrQty () |
Return the quantity of shift registers composing the GPIOXtender object. | |
bool | isValid (SRGXVPort &VPort) |
Checks if the provided SRGXVPort object is valid. | |
bool | moveAuxToMain () |
Moves the data in the Auxiliary to the Main. | |
bool | resetBit (const uint8_t &srPin) |
Sets a specific pin to LOW (0x00/Reset) in the Main Buffer. | |
bool | setBit (const uint8_t &srPin) |
Sets a specific pin to HIGH (0x01/Set) in the Main Buffer. | |
bool | stampMaskOverMain (uint8_t *maskPtr, uint8_t *valsPtr) |
Sets the value of several pins, scattered or contiguous, in the Main Buffer, according to the provided mask and values. | |
bool | stampOverMain (uint8_t *newCntntPtr) |
Sets all of the output pins of the shift register to new provided values at once. | |
bool | stampSgmntOverMain (uint8_t *newSgmntPtr, const uint8_t &strtPin, const uint8_t &pinsQty) |
Sets the value of a number of consecutive pins (the segment) in the Main Buffer. |
A class that models a GPIO outputs pins expander through the use of 8-bits Serial In Paralell Out (SIPO) shift registers.
The GPIO pins expansion modeled adds digital output pins managed by the use of an API similar to the built in Arduino platform tools. As the hardware is built using daisy-chained 74HCx595 shift registers, the connection pins to the first shift register are needed as parameters to build the object, as is the number of shift registers daisy-chain connected.
Being those three parameters hardware construction related, no mechanisms are provided to modify them after the object is created.
ShiftRegGPIOXpander::ShiftRegGPIOXpander | ( | uint8_t | ds, |
uint8_t | sh_cp, | ||
uint8_t | st_cp, | ||
uint8_t | srQty = 1 ) |
Class constructor.
Instantiates a ShiftRegGPIOXpander object, the parameters provide the pins used to communicate with the shift registers and the number of shift registers composing the expander. 8-bits will be added with every shift register connected in a daisy-chain arrangement.
ds | MCU GPIO pin connected to the DS pin -a.k.a. serial data input (DIO)- pin of the 74HCx595 to send data serially to the expander |
sh_cp | MCU GPIO pin connected to the SH_CP pin -a.k.a. shift register clock input- of the 74HCx595 to manage the communication's clock line to the expander |
st_cp | MCU GPIO pin connected to the ST_CP pin -a.k.a. storage register clock input- to set (latch) the output pins from the internal buffer of the expander |
srQty | Optional parameter. Quantity of shift registers set in daisy-chain configuration composing the expander. |
ShiftRegGPIOXpander::~ShiftRegGPIOXpander | ( | ) |
Class destructor.
Takes care of resources releasing
bool ShiftRegGPIOXpander::begin | ( | uint8_t * | initCntnt = nullptr | ) |
GPIOXpander object setup and activation.
This method sets the controller pins configuration, and optionally sets the initial pin values for the GPIOXpander pins.
initCntnt | Optional parameter. Initial value to be loaded into the Main Buffer, and thus will be the inital state of the Shift Register output pins. The value is provided in the form of a uint8_t*, and the constructor expects the data to be set in the memory area from the pointed address to the pointed address + (srQty - 1) consecutive bytes. If the parameter is not provided, or set to nullptr the inital value to be loaded into the Main Buffer will be 0x00 to all the shift registers positions, making all pins' output 0/LOW/RESET. |
true | The operation was successful, the GPIOXpander object is ready to be used. |
false | The operation failed, either because the pins could not be set, or the mutexes could not be created. |
bool ShiftRegGPIOXpander::copyMainToAux | ( | const bool & | overWriteIfExists = true | ) |
Copies the Buffer content to the Auxiliary Buffer.
overWriteIfExists | Indicates the authorization to overwrite the Auxiliary with the contents of the Main Buffer if the Auxiliary exists at the moment of the invocation. |
true | The Auxiliary was non-existent, or existed and the parameter allowing overwriting was true. |
false | The Auxiliary was existent and the parameter allowing overwriting was false, generating a failure in the operation. |
SRGXVPort ShiftRegGPIOXpander::createSRGXVPort | ( | const uint8_t & | strtPin, |
const uint8_t & | pinsQty ) |
Instantiate a SRGXVPort object.
The method will create a SRGXVPort object, a virtual port that will allow the user to manipulate a set of pins as a single entity, with it's pins numbered from 0 to pinsQty - 1, where pinsQty is the number of pins that compose the virtual port.
strtPin | ShiftRegGPIOXpander pin number from which the virtual port will start. The valid range is 0 <= strtPin <= getMaxSRGXPin(). |
pinsQty | Number of pins that will compose the virtual port. The valid range is 1 <= pinsQty <= 16 |
int ShiftRegGPIOXpander::digitalRead | ( | const uint8_t & | srPin | ) |
Returns the state of the requested pin of the ShiftRegGPIOXpander.
srPin | Pin whose current value is required. |
0x00 | The pin state was LOW |
0x01 | The pin state was HIGH |
-1 | ERROR, the pin number was beyond implemented number of pins of the ShiftRegGPIOXpander object. |
bool ShiftRegGPIOXpander::digitalReadSgmntSr | ( | const uint8_t & | strtPin, |
const uint8_t & | pinsQty, | ||
uint16_t & | bffrSgmnt ) |
Returns a 16-bits value containing a zero-based segment of the Main Buffer.
The method will return a 16-bits value containing the bits from the Main Buffer delimited by the parameters provided, formatted as a zero-based segment (or right aligned value), meaning that the first bit of the segment will hold the value of the strtPin parameter, and subsequent bits will hold the values of the following pins, up to the pinsQty parameter.
strtPin | The first pin number from which the segment will be taken. The valid range is 0 <= strtPin <= getMaxSRGXPin(). |
pinsQty | The number of pins that will compose the segment. The valid range is 1 <= pinsQty <= (_maxSRGXPin - strtPin + 1). |
buffSgmnt | A reference to a uint16_t variable where the segment will be stored. The variable must be initialized before calling the method, and it will be set to 0 before setting the segment bits. |
true | The segment was successfully retrieved and stored in the buffSgmnt variable. |
false | The parameters provided were not valid, or the operation failed for any other reason. |
uint8_t ShiftRegGPIOXpander::digitalReadSr | ( | const uint8_t & | srPin | ) |
Returns the state of the requested pin.
srPin | Pin whose current value is required. |
0x00 | The pin state was LOW |
0x01 | The pin state was HIGH |
0xFF | ERROR, the pin number was beyond implemented limit |
bool ShiftRegGPIOXpander::digitalToggleSr | ( | const uint8_t & | srPin | ) |
Toggles the state of a specific pin.
The method will set the pin to LOW (0x00) if it was HIGH (0x01) and vice versa. The method will flush the buffer, so the change will be reflected on the GPIO pin.
srPin | A positive value indicating which pin to toggle. The valid range is 0 <= srPin <= getMaxSRGXPin() |
true | The operation was successful, the pin was toggled in the Main Buffer and the change was flushed to the GPIO pin. |
false | The operation failed, because the pin number was beyond the implemented limit. |
bool ShiftRegGPIOXpander::digitalToggleSrAll | ( | ) |
Toggles the state of all the pins.
The method will set all the pins to LOW (0x00) if they were HIGH (0x01) and vice versa. The method will flush the buffer, so the change will be reflected on all the GPIO pins.
true | Always, the operation was successful, all the pins were toggled in the Main Buffer and the change was flushed to the GPIO pins. The boolean return type is kept for versions compatibility. |
bool ShiftRegGPIOXpander::digitalToggleSrMask | ( | uint8_t * | toggleMask | ) |
Toggles the state of the pins in the Main Buffer according to the provided mask.
The pins to be toggled are provided as a pointer to a mask parameter. Every bit position set (HIGH, 0x01) on the mask will be modified in the Main Buffer, reset pins (LOW, 0x00) positions of the mask will remain unmodified in the Main Buffer. The modification performed will be toggling the bit position.
toggleMask | Pointer to the array containing the mask to modify the Main. |
true | The operation was successful, the pins were toggled in the Main Buffer and the change was flushed to the GPIO pins. |
false | The operation failed, because the mask was null. |
bool ShiftRegGPIOXpander::digitalToggleSrToAux | ( | const uint8_t & | srPin | ) |
Toggles the state of a specific pin in the Auxiliary Buffer.
srPin | A positive value indicating which pin to toggle. The valid range is 0 <= srPin <= getMaxSRGXPin(). |
true | The operation was successful, the pin was toggled in the Auxiliary Buffer. |
false | The operation failed because the pin number was beyond the implemented limit. |
void ShiftRegGPIOXpander::digitalWrite | ( | const uint8_t & | srPin, |
const uint8_t & | value ) |
Set a specific pin to either HIGH (0x01) or LOW (0x00).
srPin | a positive value indicating which pin to set. The valid range is 0 <= srPin <= getMaxSRGXPin() |
value | Value to set the indicated Pin. |
bool ShiftRegGPIOXpander::digitalWriteSr | ( | const uint8_t & | srPin, |
const uint8_t & | value ) |
Set a specific pin to either HIGH (0x01) or LOW (0x00).
srPin | a positive value indicating which pin to set. The valid range is 0 <= srPin <= getMaxSRGXPin() |
value | Value to set the indicated Pin. |
true | The operation was successful, the pin was set in the Main Buffer and the change was flushed to the GPIO pin. |
false | The operation failed because the pin number was beyond the implemented limit. |
bool ShiftRegGPIOXpander::digitalWriteSrAllReset | ( | ) |
Sets all the pins to LOW (0x00/Reset).
true | Always, the operation was successful, all the pins were set to LOW in the Main Buffer and the change was flushed to the GPIO pins. The boolean return type is kept for compatibility with other library versions. |
bool ShiftRegGPIOXpander::digitalWriteSrAllSet | ( | ) |
Sets all the pins to HIGH (0x01).
true | Always, the operation was successful, all the pins were set to HIGH in the Main Buffer and the change was flushed to the GPIO pins. The boolean return type is for compatibility with other library versions. |
bool ShiftRegGPIOXpander::digitalWriteSrMaskReset | ( | uint8_t * | resetMask | ) |
Modifies the Main buffer contents by resetting simultaneously certain pins.
The pins to be reset are provided as a pointer to a mask parameter. Every bit position set (HIGH, 0x01) on the mask will be modified in the Main Buffer, reset pins (LOW, 0x00) positions of the mask will remain unmodified in the Main Buffer. The modification performed will be setting the bit position to LOW (0x00).
resetMask | Pointer to the array containing the mask to modify the Main. |
true | The operation was successful, the pins were reset in the Main Buffer and the change was flushed to the GPIO pins. |
false | The operation failed because the mask pointer was null. |
bool ShiftRegGPIOXpander::digitalWriteSrMaskSet | ( | uint8_t * | setMask | ) |
Modifies the Main buffer contents by setting simultaneously certain pins.
The pins to be set are provided as a parameter pointer to a mask. Every bit position set (HIGH, 0x01) on the mask will be modified in the Main Buffer, reset pins (LOW, 0x00) positions of the mask will remain unmodified in the Main Buffer. The modification performed will be setting the bit position to HIGH (0x01).
setMask | Pointer to the array containing the mask to modify the Main. |
true | The operation was successful, the pins were set in the Main Buffer and the change was flushed to the GPIO pins. |
false | The operation failed because the mask pointer was null. |
bool ShiftRegGPIOXpander::digitalWriteSrToAux | ( | const uint8_t | srPin, |
const uint8_t | value ) |
Set a specific pin to either HIGH (0x01) or LOW (0x00) in the Auxiliary Buffer.
srPin | a positive value indicating which pin to set. The valid range is 0 <= srPin <= getMaxSRGXPin() |
value | Value to set for the indicated Pin. |
true | The operation was successful, the pin was set in the Auxiliary Buffer. |
false | The operation failed because the pin number was beyond the implemented limit. |
bool ShiftRegGPIOXpander::discardAux | ( | ) |
Deletes the Auxiliary Buffer.
Discards the contents of the Auxiliary Buffer, frees the memory allocated to it and nullifies the corresponding memory pointer. If the Auxiliary Buffer was not created, the method will do nothing. If the Auxiliary is not transferred to the Main Buffer before invoking this method, the modified contents of the Auxiliary will be lost.
true | Always, the Auxiliary Buffer was successfully discarded, and the memory allocated to it was freed. The boolean type return value is kept for library versions compatibility |
void ShiftRegGPIOXpander::end | ( | ) |
Method provided for ending any relevant activation procedures made by the begin(uint8_t*) method.
The method will be invoked as part of the class destructor.
bool ShiftRegGPIOXpander::flipBit | ( | const uint8_t & | srPin | ) |
Toggles the state of a specific pin in the Main Buffer.
srPin | Pin whose state is to be toggled. The valid range is 0 <= srPin <= getMaxSRGXPin() |
true | The pin was in the valid range and was toggled in the Main Buffer. |
false | The pin was not in the valid range, and no action was taken. |
uint8_t * ShiftRegGPIOXpander::getMainBuffPtr | ( | ) |
Retrieves the pointer to the Main Buffer.
uint8_t ShiftRegGPIOXpander::getMaxSRGXPin | ( | ) |
Return the greatest valid pin number.
The greatest valid pin number is directly related to the quantity of shift registers connected, and is equal to the number of shift registers multiplied by 8 minus 1, (getSrQty() * 8) - 1.
uint8_t ShiftRegGPIOXpander::getSrQty | ( | ) |
Return the quantity of shift registers composing the GPIOXtender object.
The value is passed as a parameter in the class constructor.
bool ShiftRegGPIOXpander::isValid | ( | SRGXVPort & | VPort | ) |
Checks if the provided SRGXVPort object is valid.
The method is useful to verify after a createVXVPort(uint8_t&, uint8_t&) invocation that the SRGXVPort object created is valid, i.e. that it was created with a valid ShiftRegGPIOXpander object pointer and that the parameters provided to create the SRGXVPort object were valid.
VPort | SRGXVPort object to be checked for validity. |
bool ShiftRegGPIOXpander::moveAuxToMain | ( | ) |
Moves the data in the Auxiliary to the Main.
Moving the contents from the Auxiliary to the Main implies several steps:
true | There was an Auxiliary and it's value could be moved. |
false | There was no Auxiliary present, no data have been moved. |
bool ShiftRegGPIOXpander::resetBit | ( | const uint8_t & | srPin | ) |
Sets a specific pin to LOW (0x00/Reset) in the Main Buffer.
srPin | Pin whose state is to be reset. The valid range is 0 <= srPin <= getMaxSRGXPin() |
true | The pin was in the valid range and was reset to LOW (0x00/Reset) in the Main Buffer. |
false | The pin was not in the valid range, and no action was taken. |
bool ShiftRegGPIOXpander::setBit | ( | const uint8_t & | srPin | ) |
Sets a specific pin to HIGH (0x01/Set) in the Main Buffer.
srPin | Pin whose state is to be set. The valid range is 0 <= srPin <= getMaxSRGXPin() |
true | The pin was in the valid range and was set to HIGH (0x01/Set) in the Main Buffer. |
false | The pin was not in the valid range, and no action was taken. |
bool ShiftRegGPIOXpander::stampMaskOverMain | ( | uint8_t * | maskPtr, |
uint8_t * | valsPtr ) |
Sets the value of several pins, scattered or contiguous, in the Main Buffer, according to the provided mask and values.
maskPtr | Pointer to the memory area containing the mask to be applied to the Main Buffer. The mask is a bit array where each bit position set (HIGH, 0x01) indicates that the corresponding pin in the Main Buffer will be modified with the value provided in the newValsPtr parameter. |
valsPtr | Pointer to the memory area containing the values to be set in the Main Buffer. The values are provided as a bit array, where each bit position corresponds to the pin in the Main Buffer that will be modified according to the mask provided in the newMaskPtr parameter: for the array positions set in the mask, the value of the bit in the newValsPtr will be set in the corresponding pin in the Main Buffer, for the array positions not set in the mask, the value in the Main Buffer will remain unchanged. |
true | Main Buffer was modified with the new values and flushed to the shift registers. |
false | Main Buffer was not modified, either because the parameters provided were not valid. |
bool ShiftRegGPIOXpander::stampOverMain | ( | uint8_t * | newCntntPtr | ) |
Sets all of the output pins of the shift register to new provided values at once.
The method gives a tool to change all the shift registers pins in one operation instead of pin by pin. The method uses the provided pointer as a data source to overwrite the Main Buffer with the new contents.
newCntntPtr | A uint8_t* pointing the memory area containing the new values to be set to the buffer. |
true | The Buffer was overwritten and the new contents flushed. |
false | The parameter passed was a nullptr/NULL and no overwriting was possible. |
bool ShiftRegGPIOXpander::stampSgmntOverMain | ( | uint8_t * | newSgmntPtr, |
const uint8_t & | strtPin, | ||
const uint8_t & | pinsQty ) |
Sets the value of a number of consecutive pins (the segment) in the Main Buffer.
Each pin value is set or reset according to the value provided in the data array pointed by the newSgmntPtr parameter as source.
newSgmntPtr | Pointer to the memory area containing the new values to be set to the segment of pins in the Main Buffer. The value to be set in each pin is provided as a bit in the data pointed by the newSgmntPtr parameter, where the first bit in the data pointed by newSgmntPtr will be set to the first pin in the segment, and so on. For that logic to work the data must be right aligned (LSb of the segment placed in position 0) in the data pointed by newSgmntPtr. |
strtPin | First pin number in the Main buffer to be overwritten. The valid range is 0 <= strtPin <= getMaxSRGXPin(). |
pinsQty | The number of pins to be set in the segment. The valid range is 1 <= pinsQty <= (getMaxSRGXPin() - strtPin + 1). |
true | The segment was set in the Main Buffer and the Buffer was flushed. |
false | The segment could not be set in the Main Buffer because the parameters provided were not valid. |