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

66 lines
2.6 KiB
C++
Raw Permalink Normal View History

2023-12-27 16:15:11 +00:00
#pragma once
2023-12-27 18:25:35 +00:00
#include <ExpansionCard.hpp>
2023-12-27 16:15:11 +00:00
#include <PCF8574.h>
#include <map>
2023-12-27 16:15:11 +00:00
2023-12-31 18:38:25 +00:00
// Card Type
2023-12-28 04:41:20 +00:00
#define CARD_TYPE_DIGITAL_INPUT 0x01
2023-12-31 18:56:49 +00:00
/**
* @brief ESPMegaPRO Digital Input Card
*
* This class represents the ESPMegaPRO Digital Input Card.
* It allows you to read the state of the digital inputs from the ESPMegaPRO Digital Input Card.
* It also allows you to register callback functions to be called when a pin changes.
* The callback function also support debouncing.
*
*/
2023-12-27 18:25:35 +00:00
class DigitalInputCard : public ExpansionCard {
2023-12-27 16:15:11 +00:00
public:
// Instantiate the card with the specified address
DigitalInputCard(uint8_t address_a, uint8_t address_b);
// Instantiate the card with the specified position on the dip switch
DigitalInputCard(bool bit0, bool bit1, bool bit2, bool bit3, bool bit4, bool bit5);
// Initialize the card
2023-12-27 19:18:21 +00:00
bool begin();
2023-12-27 16:15:11 +00:00
// Refresh and Read the input from the specified pin, always refresh the input buffers
2023-12-27 18:25:35 +00:00
bool digitalRead(uint8_t pin);
2023-12-27 16:15:11 +00:00
// Read the input from the specified pin, also refresh the input buffers if refresh is true
2023-12-27 18:25:35 +00:00
bool digitalRead(uint8_t pin, bool refresh);
2023-12-27 16:15:11 +00:00
// Preform a loop to refresh the input buffers
void loop();
// Get the input buffer for bank A
uint8_t getInputBufferA();
// Get the input buffer for bank B
uint8_t getInputBufferB();
2023-12-27 18:25:35 +00:00
// Set the debounce time for the specified pin
void setDebounceTime(uint8_t pin, uint32_t debounceTime);
2023-12-27 16:15:11 +00:00
// Register a callback function to be called when a pin changes
2023-12-31 06:32:38 +00:00
uint8_t registerCallback(std::function<void(uint8_t, bool)> callback);
2023-12-27 18:25:35 +00:00
// Unregister the callback function
void unregisterCallback(uint8_t handler);
2023-12-27 18:25:35 +00:00
// Load a new pin map
void loadPinMap(uint8_t pinMap[16]);
2023-12-28 04:41:20 +00:00
// Get type of card
uint8_t getType();
2023-12-27 16:15:11 +00:00
private:
PCF8574 inputBankA;
PCF8574 inputBankB;
uint8_t address_a;
uint8_t address_b;
uint8_t inputBufferA;
uint8_t inputBufferB;
2023-12-27 18:25:35 +00:00
uint8_t previousInputBufferA;
uint8_t previousInputBufferB;
uint32_t debounceTime[16];
uint32_t lastDebounceTime[16];
// A map of the physical pin to the virtual pin
uint8_t pinMap[16];
// A map of the virtual pin to the physical pin
uint8_t virtualPinMap[16];
uint8_t callbacks_handler_index = 0;
std::map<uint8_t, std::function<void(uint8_t, bool)>> callbacks;
2023-12-27 16:15:11 +00:00
void refreshInputBankA();
void refreshInputBankB();
2023-12-27 18:25:35 +00:00
void handlePinChange(int pin, uint8_t& currentBuffer, uint8_t& previousBuffer);
2023-12-27 16:15:11 +00:00
};