half comment done
This commit is contained in:
parent
d62ffa3606
commit
039e65e6df
12 changed files with 736 additions and 38 deletions
|
@ -1,15 +1,33 @@
|
|||
#include <DigitalInputCard.hpp>
|
||||
|
||||
// Instantiate the card with the specified address
|
||||
/**
|
||||
* @brief Create a new Digital Input Card object with the specified address
|
||||
* @note If you are using the ESPMegaI/O board, you should use the dip switch constructor
|
||||
*
|
||||
* @param address_a The ESPMegaI/O address of bank A
|
||||
* @param address_b The ESPMegaI/O address of bank B
|
||||
*/
|
||||
DigitalInputCard::DigitalInputCard(uint8_t address_a, uint8_t address_b) : callbacks()
|
||||
{
|
||||
this->address_a = address_a;
|
||||
this->address_b = address_b;
|
||||
this->callbacks_handler_index = 0;
|
||||
}
|
||||
// Instantiate the card with the specified position on the dip switch
|
||||
// Bit 0,1,2 are for bank A
|
||||
// Bit 3,4,5 are for bank B
|
||||
|
||||
/**
|
||||
* @brief Create a new Digital Input Card object with the specified position on the dip switch
|
||||
*
|
||||
* @note The bit 0 are at the left of the dip switch
|
||||
*
|
||||
* @warning There are 6 switches on the dip switch, 3 for bank A and 3 for bank B, They should be unique for each bank accross all the cards
|
||||
*
|
||||
* @param bit0 The position of the first switch on the dip switch
|
||||
* @param bit1 The position of the second switch on the dip switch
|
||||
* @param bit2 The position of the third switch on the dip switch
|
||||
* @param bit3 The position of the fourth switch on the dip switch
|
||||
* @param bit4 The position of the fifth switch on the dip switch
|
||||
* @param bit5 The position of the sixth switch on the dip switch
|
||||
*/
|
||||
DigitalInputCard::DigitalInputCard(bool bit0, bool bit1, bool bit2, bool bit3, bool bit4, bool bit5)
|
||||
{
|
||||
this->address_a = 0x20;
|
||||
|
@ -29,7 +47,12 @@ DigitalInputCard::DigitalInputCard(bool bit0, bool bit1, bool bit2, bool bit3, b
|
|||
if (bit5)
|
||||
this->address_b += 4;
|
||||
}
|
||||
// Initialize the card
|
||||
|
||||
/**
|
||||
* @brief Initialize the Digital Input Card
|
||||
*
|
||||
* @return True if the initialization is successful, false otherwise
|
||||
*/
|
||||
bool DigitalInputCard::begin()
|
||||
{
|
||||
this->inputBankA = PCF8574(this->address_a);
|
||||
|
@ -56,12 +79,25 @@ bool DigitalInputCard::begin()
|
|||
}
|
||||
return true;
|
||||
}
|
||||
// Refresh and Read the input from the specified pin, always refresh the input buffers
|
||||
|
||||
/**
|
||||
* @brief Read the input from the specified pin, always refresh the input buffers
|
||||
*
|
||||
* @param pin The pin to read from
|
||||
* @return True if the pin is HIGH, false if the pin is LOW
|
||||
*/
|
||||
bool DigitalInputCard::digitalRead(uint8_t pin)
|
||||
{
|
||||
return this->digitalRead(pin, true);
|
||||
}
|
||||
// Read the input from the specified pin, also refresh the input buffers if refresh is true
|
||||
|
||||
/**
|
||||
* @brief Read the input from the specified pin, also refresh the input buffers if refresh is true
|
||||
*
|
||||
* @param pin The pin to read from
|
||||
* @param refresh If true, the input buffers will be refreshed before reading the pin
|
||||
* @return True if the pin is HIGH, false if the pin is LOW
|
||||
*/
|
||||
bool DigitalInputCard::digitalRead(uint8_t pin, bool refresh)
|
||||
{
|
||||
pin = pinMap[pin];
|
||||
|
@ -85,6 +121,15 @@ bool DigitalInputCard::digitalRead(uint8_t pin, bool refresh)
|
|||
return 255;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check if the specified pin changed since the last call to this function
|
||||
*
|
||||
* @note This function compares the current input buffer with the previous input buffer to detect changes
|
||||
*
|
||||
* @param pin The pin to check
|
||||
* @param currentBuffer The current input buffer
|
||||
* @param previousBuffer The previous input buffer
|
||||
*/
|
||||
void DigitalInputCard::handlePinChange(int pin, uint8_t ¤tBuffer, uint8_t &previousBuffer)
|
||||
{
|
||||
// Get the index of the pin in the pin map
|
||||
|
@ -113,7 +158,11 @@ void DigitalInputCard::handlePinChange(int pin, uint8_t ¤tBuffer, uint8_t
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief A loop to refresh the input buffers and check for pin changes
|
||||
*
|
||||
* @note Although this function can be called in the main loop, it is recommended install the card in ESPMega to automatically manage the loop
|
||||
*/
|
||||
// Preform a loop to refresh the input buffers
|
||||
void DigitalInputCard::loop()
|
||||
{
|
||||
|
@ -134,7 +183,12 @@ void DigitalInputCard::loop()
|
|||
}
|
||||
}
|
||||
}
|
||||
// Get the input buffer for bank A
|
||||
|
||||
/**
|
||||
* @brief Get the input buffer for bank A (the first 8 pins)
|
||||
*
|
||||
* @return The input buffer for bank A where the first bit is the first pin and the last bit is the last pin
|
||||
*/
|
||||
uint8_t DigitalInputCard::getInputBufferA()
|
||||
{
|
||||
// Rearrange the bits to match the pin map
|
||||
|
@ -145,7 +199,12 @@ uint8_t DigitalInputCard::getInputBufferA()
|
|||
}
|
||||
return inputBufferA_rearranged;
|
||||
}
|
||||
// Get the input buffer for bank B
|
||||
|
||||
/**
|
||||
* @brief Get the input buffer for bank B (the last 8 pins)
|
||||
*
|
||||
* @return The input buffer for bank B where the first bit is the first pin and the last bit is the last pin
|
||||
*/
|
||||
uint8_t DigitalInputCard::getInputBufferB()
|
||||
{
|
||||
// Rearrange the bits to match the pin map
|
||||
|
@ -156,35 +215,71 @@ uint8_t DigitalInputCard::getInputBufferB()
|
|||
}
|
||||
return inputBufferB_rearranged;
|
||||
}
|
||||
// Register a callback function to be called when a pin changes
|
||||
|
||||
/**
|
||||
* @brief Register a callback function to be called when a pin changes
|
||||
*
|
||||
* @param callback The callback function to be called
|
||||
* @return The handler of the callback function
|
||||
*/
|
||||
uint8_t DigitalInputCard::registerCallback(std::function<void(uint8_t, bool)> callback)
|
||||
{
|
||||
callbacks[this->callbacks_handler_index] = callback;
|
||||
return this->callbacks_handler_index++;
|
||||
}
|
||||
|
||||
// Refresh the input buffer for bank A
|
||||
/**
|
||||
* @brief Read the input state from the input ic and store it in the input buffer for bank A
|
||||
*/
|
||||
void DigitalInputCard::refreshInputBankA()
|
||||
{
|
||||
inputBufferA = inputBankA.read8();
|
||||
}
|
||||
// Refresh the input buffer for bank B
|
||||
|
||||
/**
|
||||
* @brief Read the input state from the input ic and store it in the input buffer for bank B
|
||||
*/
|
||||
void DigitalInputCard::refreshInputBankB()
|
||||
{
|
||||
inputBufferB = inputBankB.read8();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set the debounce time for the specified pin
|
||||
*
|
||||
* Debounce is the time in milliseconds that the pin should be stable before the callback function is called
|
||||
* This is useful to prevent false triggers when the input is noisy
|
||||
* An example of this is when the input is connected to a mechanical switch
|
||||
*
|
||||
* @param pin The pin to set the debounce time for
|
||||
* @param debounceTime The debounce time in milliseconds
|
||||
*/
|
||||
void DigitalInputCard::setDebounceTime(uint8_t pin, uint32_t debounceTime)
|
||||
{
|
||||
pin = pinMap[pin];
|
||||
this->debounceTime[pin] = debounceTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Unregister a callback function
|
||||
*
|
||||
* @param handler The handler of the callback function to unregister
|
||||
*/
|
||||
void DigitalInputCard::unregisterCallback(uint8_t handler)
|
||||
{
|
||||
callbacks.erase(handler);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Load the pin map for the card
|
||||
*
|
||||
* A pin map is an array of 16 elements that maps the physical pins to virtual pins
|
||||
* The virtual pins are the pins that are used in the callback functions and are used for all the functions in this class
|
||||
* The physical pins are the pins on the Input IC, This can be found on the schematic of the ESPMegaI/O board
|
||||
* This function is useful if you want to change the number identification of the pins to match your project needs
|
||||
*
|
||||
* @param pinMap The pin map to load
|
||||
*/
|
||||
void DigitalInputCard::loadPinMap(uint8_t pinMap[16])
|
||||
{
|
||||
for (int i = 0; i < 16; i++)
|
||||
|
@ -196,6 +291,11 @@ void DigitalInputCard::loadPinMap(uint8_t pinMap[16])
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get the type of the card
|
||||
*
|
||||
* @return The type of the card
|
||||
*/
|
||||
uint8_t DigitalInputCard::getType()
|
||||
{
|
||||
return CARD_TYPE_DIGITAL_INPUT;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue