ESPMegaPRO-v3-SDK/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.cpp

302 lines
9.4 KiB
C++
Raw Normal View History

2023-12-27 16:15:11 +00:00
#include <DigitalInputCard.hpp>
2023-12-31 18:38:25 +00:00
/**
* @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
*/
2023-12-30 11:27:39 +00:00
DigitalInputCard::DigitalInputCard(uint8_t address_a, uint8_t address_b) : callbacks()
2023-12-27 18:25:35 +00:00
{
2023-12-27 16:15:11 +00:00
this->address_a = address_a;
this->address_b = address_b;
this->callbacks_handler_index = 0;
2023-12-27 16:15:11 +00:00
}
2023-12-31 18:38:25 +00:00
/**
* @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
*/
2023-12-27 18:25:35 +00:00
DigitalInputCard::DigitalInputCard(bool bit0, bool bit1, bool bit2, bool bit3, bool bit4, bool bit5)
{
2023-12-27 16:15:11 +00:00
this->address_a = 0x20;
this->address_b = 0x20;
2023-12-27 18:25:35 +00:00
this->inputBufferA = 0;
this->inputBufferB = 0;
if (bit0)
this->address_a += 1;
if (bit1)
this->address_a += 2;
if (bit2)
this->address_a += 4;
if (bit3)
this->address_b += 1;
if (bit4)
this->address_b += 2;
if (bit5)
this->address_b += 4;
2023-12-27 16:15:11 +00:00
}
2023-12-31 18:38:25 +00:00
/**
* @brief Initialize the Digital Input Card
*
* @return True if the initialization is successful, false otherwise
*/
2023-12-27 19:18:21 +00:00
bool DigitalInputCard::begin()
2023-12-27 18:25:35 +00:00
{
2023-12-27 16:15:11 +00:00
this->inputBankA = PCF8574(this->address_a);
this->inputBankB = PCF8574(this->address_b);
2023-12-27 19:18:21 +00:00
if (!this->inputBankA.begin()) {
2023-12-30 11:47:52 +00:00
ESP_LOGE("DigitalInputCard", "Input Card ERROR: Failed to install input bank A");
2023-12-27 19:18:21 +00:00
return false;
}
if (!this->inputBankB.begin()) {
2023-12-30 11:47:52 +00:00
ESP_LOGE("DigitalInputCard", "Input Card ERROR: Failed to install input bank B");
2023-12-27 19:18:21 +00:00
return false;
}
2023-12-27 18:25:35 +00:00
// Set the debounce time for all pins to 50ms
for (int i = 0; i < 16; i++)
{
this->debounceTime[i] = 50;
this->lastDebounceTime[i] = 0;
}
// Initialize the pin map to the default values
for (int i = 0; i < 16; i++)
{
this->pinMap[i] = i;
this->virtualPinMap[i] = i;
}
2023-12-27 19:18:21 +00:00
return true;
2023-12-27 16:15:11 +00:00
}
2023-12-31 18:38:25 +00:00
/**
* @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
*/
2023-12-27 18:25:35 +00:00
bool DigitalInputCard::digitalRead(uint8_t pin)
{
return this->digitalRead(pin, true);
2023-12-27 16:15:11 +00:00
}
2023-12-31 18:38:25 +00:00
/**
* @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
*/
2023-12-27 18:25:35 +00:00
bool DigitalInputCard::digitalRead(uint8_t pin, bool refresh)
{
pin = pinMap[pin];
2023-12-27 16:15:11 +00:00
// First check if the pin is in bank A or B
2023-12-27 18:25:35 +00:00
if (pin >= 0 && pin <= 7)
{
2023-12-27 16:15:11 +00:00
// Refresh the input buffers if refresh is true
2023-12-27 18:25:35 +00:00
if (refresh)
refreshInputBankA();
2023-12-27 16:15:11 +00:00
// Extract the bit from the buffer
return ((inputBufferA >> (7 - pin)) & 1);
2023-12-27 18:25:35 +00:00
}
else if (pin >= 8 && pin <= 15)
{
2023-12-27 16:15:11 +00:00
// Refresh the input buffers if refresh is true
2023-12-27 18:25:35 +00:00
if (refresh)
refreshInputBankB();
2023-12-27 16:15:11 +00:00
// Extract the bit from the buffer
return ((inputBufferB >> (15 - pin)) & 1);
}
2023-12-27 18:25:35 +00:00
return 255;
2023-12-27 16:15:11 +00:00
}
2023-12-27 18:25:35 +00:00
2023-12-31 18:38:25 +00:00
/**
* @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
*/
2023-12-27 18:25:35 +00:00
void DigitalInputCard::handlePinChange(int pin, uint8_t &currentBuffer, uint8_t &previousBuffer)
{
// Get the index of the pin in the pin map
uint8_t virtualPin = virtualPinMap[pin];
// Handle Bank A
if (((previousBuffer >> (7 - pin)) & 1) != ((currentBuffer >> (7 - pin)) & 1))
{
if (millis() - lastDebounceTime[pin] > debounceTime[pin])
{
lastDebounceTime[pin] = millis();
previousBuffer ^= (-((currentBuffer >> (7 - pin)) & 1) ^ previousBuffer) & (1UL << (7 - pin));
for(const auto& callback : callbacks)
callback.second(virtualPin, ((currentBuffer >> (7 - pin)) & 1));
2023-12-27 18:25:35 +00:00
}
}
// Handle Bank B
if (((previousBuffer >> (15 - pin)) & 1) != ((currentBuffer >> (15 - pin)) & 1))
{
if (millis() - lastDebounceTime[pin] > debounceTime[pin])
{
lastDebounceTime[pin] = millis();
previousBuffer ^= (-((currentBuffer >> (15 - pin)) & 1) ^ previousBuffer) & (1UL << (15 - pin));
for (const auto& callback : callbacks)
callback.second(virtualPin, ((currentBuffer >> (15 - pin)) & 1));
2023-12-27 18:25:35 +00:00
}
}
}
2023-12-31 18:38:25 +00:00
/**
* @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
*/
2023-12-27 16:15:11 +00:00
// Preform a loop to refresh the input buffers
2023-12-27 18:25:35 +00:00
void DigitalInputCard::loop()
{
2023-12-27 16:15:11 +00:00
// Refresh the input buffers
refreshInputBankA();
refreshInputBankB();
// Iterate over all pins and check if they changed
2023-12-27 18:25:35 +00:00
for (int i = 0; i < 16; i++)
{
2023-12-27 16:15:11 +00:00
// Check which bank the pin is in
2023-12-27 18:25:35 +00:00
if (i < 8)
{
handlePinChange(i, inputBufferA, previousInputBufferA);
}
else if (i >= 8 && i <= 15)
{
handlePinChange(i, inputBufferB, previousInputBufferB);
2023-12-27 16:15:11 +00:00
}
}
}
2023-12-31 18:38:25 +00:00
/**
* @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
*/
2023-12-27 18:25:35 +00:00
uint8_t DigitalInputCard::getInputBufferA()
{
// Rearrange the bits to match the pin map
uint8_t inputBufferA_rearranged = 0;
for (int i = 0; i < 8; i++)
{
inputBufferA_rearranged |= ((inputBufferA >> i) & 1) << (7 - i);
}
return inputBufferA_rearranged;
2023-12-27 16:15:11 +00:00
}
2023-12-31 18:38:25 +00:00
/**
* @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
*/
2023-12-27 18:25:35 +00:00
uint8_t DigitalInputCard::getInputBufferB()
{
// Rearrange the bits to match the pin map
uint8_t inputBufferB_rearranged = 0;
for (int i = 0; i < 8; i++)
{
inputBufferB_rearranged |= ((inputBufferB >> i) & 1) << (7 - i);
}
return inputBufferB_rearranged;
2023-12-27 16:15:11 +00:00
}
2023-12-31 18:38:25 +00:00
/**
* @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
*/
2023-12-31 06:32:38 +00:00
uint8_t DigitalInputCard::registerCallback(std::function<void(uint8_t, bool)> callback)
2023-12-27 18:25:35 +00:00
{
2023-12-31 06:32:38 +00:00
callbacks[this->callbacks_handler_index] = callback;
return this->callbacks_handler_index++;
2023-12-27 16:15:11 +00:00
}
2023-12-31 18:38:25 +00:00
/**
* @brief Read the input state from the input ic and store it in the input buffer for bank A
*/
2023-12-27 18:25:35 +00:00
void DigitalInputCard::refreshInputBankA()
{
2023-12-27 16:15:11 +00:00
inputBufferA = inputBankA.read8();
}
2023-12-31 18:38:25 +00:00
/**
* @brief Read the input state from the input ic and store it in the input buffer for bank B
*/
2023-12-27 18:25:35 +00:00
void DigitalInputCard::refreshInputBankB()
{
2023-12-27 16:15:11 +00:00
inputBufferB = inputBankB.read8();
2023-12-27 18:25:35 +00:00
}
2023-12-31 18:38:25 +00:00
/**
* @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
*/
2023-12-27 18:25:35 +00:00
void DigitalInputCard::setDebounceTime(uint8_t pin, uint32_t debounceTime)
{
pin = pinMap[pin];
this->debounceTime[pin] = debounceTime;
}
2023-12-31 18:38:25 +00:00
/**
* @brief Unregister a callback function
*
* @param handler The handler of the callback function to unregister
*/
void DigitalInputCard::unregisterCallback(uint8_t handler)
{
callbacks.erase(handler);
}
2023-12-27 18:25:35 +00:00
2023-12-31 18:38:25 +00:00
/**
* @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
*/
2023-12-27 18:25:35 +00:00
void DigitalInputCard::loadPinMap(uint8_t pinMap[16])
{
for (int i = 0; i < 16; i++)
{
// Load the pin map (physical pin to virtual pin)
this->pinMap[i] = pinMap[i];
// Load the virtual pin map (virtual pin to physical pin)
this->virtualPinMap[pinMap[i]] = i;
}
2023-12-28 04:41:20 +00:00
}
2023-12-31 18:38:25 +00:00
/**
* @brief Get the type of the card
*
* @return The type of the card
*/
2023-12-28 04:41:20 +00:00
uint8_t DigitalInputCard::getType()
{
return CARD_TYPE_DIGITAL_INPUT;
2023-12-27 16:15:11 +00:00
}