ShiftRegGPIOXpander Library for ESP32 (Arduino) v3.0.0
A library that provides an easy mechanism to add GPIO digital output pins by using Shift Registers
Loading...
Searching...
No Matches
ShiftRegGPIOXpander_ESP32.h
Go to the documentation of this file.
1
42#ifndef _SHIFTREGGPIOXPANDER_ESP32_H_
43#define _SHIFTREGGPIOXPANDER_ESP32_H_
44
45#include <Arduino.h>
46#include <stdint.h>
47
48class SRGXVPort;
49
60
61private:
62 uint8_t _ds{};
63 uint8_t _sh_cp{};
64 uint8_t _st_cp{};
65
73 bool _copyMainToAux(const bool &overWriteIfExists = true);
79 void _discardAux();
89 bool _moveAuxToMain();
101 bool _sendAllSRCntnt();
111 bool _sendSnglSRCntnt(const uint8_t &data);
112
113protected:
114 SemaphoreHandle_t _SRGXAuxBffrMtx; // Mutex to protect the Auxiliary Buffer from concurrent access
115 SemaphoreHandle_t _SRGXMnBffrMtx; // Mutex to protect the Main Buffer from concurrent access
116
117 uint8_t* _mainBuffrArryPtr{};
118 uint8_t* _auxBuffrArryPtr{nullptr};
119 uint8_t _maxSRGXPin{};
120 uint8_t _srQty{};
121
122public:
145 ShiftRegGPIOXpander(uint8_t ds, uint8_t sh_cp, uint8_t st_cp, uint8_t srQty = 1);
163 bool begin(uint8_t* initCntnt = nullptr);
178 bool copyMainToAux(const bool &overWriteIfExists = true);
190 SRGXVPort createSRGXVPort(const uint8_t &strtPin, const uint8_t &pinsQty);
208 bool digitalReadSgmntSr(const uint8_t &strtPin, const uint8_t &pinsQty, uint16_t &bffrSgmnt);
222 uint8_t digitalReadSr(const uint8_t &srPin);
238 bool digitalToggleSr(const uint8_t &srPin);
250 bool digitalToggleSrAll();
266 bool digitalToggleSrMask(uint8_t* toggleMask);
280 bool digitalToggleSrToAux(const uint8_t &srPin);
294 bool digitalWriteSr(const uint8_t &srPin, const uint8_t &value);
333 bool digitalWriteSrMaskReset(uint8_t* resetMask);
351 bool digitalWriteSrMaskSet(uint8_t* setMask);
366 bool digitalWriteSrToAux(const uint8_t srPin, const uint8_t value);
376 bool discardAux();
382 void end();
392 bool flipBit(const uint8_t &srPin);
400 uint8_t* getMainBuffPtr();
410 uint8_t getMaxSRGXPin();
418 uint8_t getSrQty();
430 bool isValid(SRGXVPort &VPort);
443 bool moveAuxToMain();
453 bool resetBit(const uint8_t &srPin);
463 bool setBit(const uint8_t &srPin);
473 bool stampMaskOverMain(uint8_t* maskPtr, uint8_t* valsPtr);
488 bool stampOverMain(uint8_t* newCntntPtr);
508 bool stampSgmntOverMain(uint8_t* newSgmntPtr, const uint8_t &strtPin, const uint8_t &pinsQty);
509};
510
511//==========================================================>>
512
530class SRGXVPort: public ShiftRegGPIOXpander{
531 /*Allows the ShiftRegGPIOXpander class to access the private members of the SRGXVPort
532 class. In this case the ShiftRegGPIOXpander class will be able instantiate SRGXVPort
533 objects, as the SRGXVPort class constructor is protected to avoid instantiation by
534 the user but through the use the ShiftRegGPIOXpander::createSRGXVPort
535 (uint8_t&, uint8_t&) method to create a SRGXVPort object.*/
536 friend class ShiftRegGPIOXpander;
537 /*_maxPortPinsQty: Maximum number of pins that can be used in a virtual port, the
538 maximum number of pins that can be used in a virtual port is defined as 16, library
539 developers choice.*/
540 const static uint8_t _maxPortPinsQty{16};
541
542private:
543 ShiftRegGPIOXpander* _SRGXPtr{nullptr};
544 uint8_t _strtPin{0};
545 uint8_t _pinsQty{0};
546
547 uint8_t* _srgxStampMskPtr{nullptr}; // Pointer to the mask used to stamp the virtual port over the Main Buffer of the ShiftRegGPIOXpander object.
548 uint16_t _vportBuffer{0};
549 /*_vportMaxVal: Maximum value that can be set in the virtual port, calculated as
550 (2^pinsQty) - 1, where pinsQty is the number of pins that compose the virtual port.
551 The value is used to enforce the range of valid values. */
552 uint16_t _vportMaxVal{0};
553 bool _begun{false}; // Flag to indicate if the virtual port has been begun, i.e. if the begin() method has been called and the initial state of the virtual port has been set.
554 bool _buildSRGXVPortMsk(uint8_t* &maskPtr);
555
556protected:
557 SemaphoreHandle_t _SRGXVPortMskMtx; // Mutex to protect the Auxiliary Buffer from concurrent access
558
562 SRGXVPort();
574 SRGXVPort(ShiftRegGPIOXpander* SRGXPtr, uint8_t strtPin, uint8_t pinsQty);
575
576public:
580 ~SRGXVPort();
586 bool begin(uint16_t initCntnt);
594 uint8_t digitalReadSr(const uint8_t &srPin);
605 bool digitalWriteSr(const uint8_t &srPin, const uint8_t &value);
616 bool flipBit(const uint8_t &srPin);
622 ShiftRegGPIOXpander* getSRGXPtr();
630 uint8_t* getStampMask();
638 uint16_t getVPortMaxVal();
646 uint16_t readPort();
654 bool resetBit(const uint8_t &srPin);
662 bool setBit(const uint8_t &srPin);
671 bool writePort(uint16_t newPortVal);
672};
673
674//==========================================================>>
675
676#endif //ShiftRegGPIOXpander_ESP32_H_
A class that models Virtual Ports from the resources provided by a ShiftRegGPIOXpander object.
Definition ShiftRegGPIOXpander_ESP32.h:530
uint8_t digitalReadSr(const uint8_t &srPin)
Reads the state of a specific pin in the virtual port.
Definition ShiftRegGPIOXpander_ESP32.cpp:708
bool writePort(uint16_t newPortVal)
Sets the state of the pins in the Main Buffer (i.e. the GPIOXpander pins) according to the provided v...
Definition ShiftRegGPIOXpander_ESP32.cpp:791
uint16_t getVPortMaxVal()
Returns the maximum value that can be set in the virtual port.
Definition ShiftRegGPIOXpander_ESP32.cpp:752
bool begin(uint16_t initCntnt)
Begins the virtual port, setting the initial state of the VPort pins.
Definition ShiftRegGPIOXpander_ESP32.cpp:664
uint8_t * getStampMask()
Returns a pointer to the mask built to stamp the virtual port over the Main Buffer of the ShiftRegGPI...
Definition ShiftRegGPIOXpander_ESP32.cpp:747
bool flipBit(const uint8_t &srPin)
Toggles the state of a specific pin in the virtual port.
Definition ShiftRegGPIOXpander_ESP32.cpp:731
bool resetBit(const uint8_t &srPin)
Sets the state of a pin to LOW (0x00/Reset) in the virtual port.
Definition ShiftRegGPIOXpander_ESP32.cpp:767
uint16_t readPort()
Reads the state of the virtual port as an integer value.
Definition ShiftRegGPIOXpander_ESP32.cpp:757
bool digitalWriteSr(const uint8_t &srPin, const uint8_t &value)
Sets the state of a specific pin in the virtual port, either HIGH (0x01/Set) or LOW (0x00/Reset).
Definition ShiftRegGPIOXpander_ESP32.cpp:720
SRGXVPort()
Default constructor.
Definition ShiftRegGPIOXpander_ESP32.cpp:639
bool setBit(const uint8_t &srPin)
Sets the state of a pin to HIGH (0x01/Set) in the virtual port.
Definition ShiftRegGPIOXpander_ESP32.cpp:779
~SRGXVPort()
Class destructor.
Definition ShiftRegGPIOXpander_ESP32.cpp:656
ShiftRegGPIOXpander * getSRGXPtr()
Returns a pointer to the ShiftRegGPIOXpander object that provides the resources for the virtual port.
Definition ShiftRegGPIOXpander_ESP32.cpp:742
bool begin(uint8_t *initCntnt=nullptr)
GPIOXpander object setup and activation.
Definition ShiftRegGPIOXpander_ESP32.cpp:67
bool stampOverMain(uint8_t *newCntntPtr)
Sets all of the output pins of the shift register to new provided values at once.
Definition ShiftRegGPIOXpander_ESP32.cpp:587
bool digitalWriteSrMaskSet(uint8_t *setMask)
Modifies the Main buffer contents by setting simultaneously certain pins.
Definition ShiftRegGPIOXpander_ESP32.cpp:352
bool isValid(SRGXVPort &VPort)
Checks if the provided SRGXVPort object is valid.
Definition ShiftRegGPIOXpander_ESP32.cpp:457
bool resetBit(const uint8_t &srPin)
Sets a specific pin to LOW (0x00/Reset) in the Main Buffer.
Definition ShiftRegGPIOXpander_ESP32.cpp:493
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.
Definition ShiftRegGPIOXpander_ESP32.cpp:138
uint8_t getSrQty()
Return the quantity of shift registers composing the GPIOXtender object.
Definition ShiftRegGPIOXpander_ESP32.cpp:452
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.
Definition ShiftRegGPIOXpander_ESP32.cpp:380
bool digitalToggleSr(const uint8_t &srPin)
Toggles the state of a specific pin.
Definition ShiftRegGPIOXpander_ESP32.cpp:179
uint8_t getMaxSRGXPin()
Return the greatest valid pin number.
Definition ShiftRegGPIOXpander_ESP32.cpp:447
bool setBit(const uint8_t &srPin)
Sets a specific pin to HIGH (0x01/Set) in the Main Buffer.
Definition ShiftRegGPIOXpander_ESP32.cpp:541
bool copyMainToAux(const bool &overWriteIfExists=true)
Copies the Buffer content to the Auxiliary Buffer.
Definition ShiftRegGPIOXpander_ESP32.cpp:112
bool moveAuxToMain()
Moves the data in the Auxiliary to the Main.
Definition ShiftRegGPIOXpander_ESP32.cpp:477
ShiftRegGPIOXpander()
Class default constructor.
Definition ShiftRegGPIOXpander_ESP32.cpp:45
bool digitalWriteSr(const uint8_t &srPin, const uint8_t &value)
Set a specific pin to either HIGH (0x01) or LOW (0x00).
Definition ShiftRegGPIOXpander_ESP32.cpp:265
~ShiftRegGPIOXpander()
Class destructor.
Definition ShiftRegGPIOXpander_ESP32.cpp:55
bool stampSgmntOverMain(uint8_t *newSgmntPtr, const uint8_t &strtPin, const uint8_t &pinsQty)
Sets the value of a set of consecutive pins (the segment) in the Main Buffer.
Definition ShiftRegGPIOXpander_ESP32.cpp:614
bool digitalWriteSrAllSet()
Sets all the pins to HIGH (0x01).
Definition ShiftRegGPIOXpander_ESP32.cpp:306
uint8_t * getMainBuffPtr()
Retrieves the pointer to the Main Buffer.
Definition ShiftRegGPIOXpander_ESP32.cpp:442
bool stampMaskOverMain(uint8_t *maskPtr, uint8_t *valsPtr)
Sets the value of several scattered (or not) pins in the Main Buffer, according to the provided mask ...
Definition ShiftRegGPIOXpander_ESP32.cpp:552
bool digitalToggleSrMask(uint8_t *toggleMask)
Toggles the state of the pins in the Main Buffer according to the provided mask.
Definition ShiftRegGPIOXpander_ESP32.cpp:218
bool digitalToggleSrAll()
Toggles the state of all the pins.
Definition ShiftRegGPIOXpander_ESP32.cpp:199
bool flipBit(const uint8_t &srPin)
Toggles the state of a specific pin in the Main Buffer.
Definition ShiftRegGPIOXpander_ESP32.cpp:431
bool digitalWriteSrAllReset()
Sets all the pins to LOW (0x00/Reset).
Definition ShiftRegGPIOXpander_ESP32.cpp:288
bool digitalToggleSrToAux(const uint8_t &srPin)
Toggles the state of a specific pin in the Auxiliary Buffer.
Definition ShiftRegGPIOXpander_ESP32.cpp:246
SRGXVPort createSRGXVPort(const uint8_t &strtPin, const uint8_t &pinsQty)
Instantiate a SRGXVPort object.
Definition ShiftRegGPIOXpander_ESP32.cpp:131
uint8_t digitalReadSr(const uint8_t &srPin)
Returns the state of the requested pin.
Definition ShiftRegGPIOXpander_ESP32.cpp:161
void end()
Method provided for ending any relevant activation procedures made by the begin(uint8_t*) method.
Definition ShiftRegGPIOXpander_ESP32.cpp:426
bool discardAux()
Deletes the Auxiliary Buffer.
Definition ShiftRegGPIOXpander_ESP32.cpp:411
bool digitalWriteSrMaskReset(uint8_t *resetMask)
Modifies the Main buffer contents by resetting simultaneously certain pins.
Definition ShiftRegGPIOXpander_ESP32.cpp:324