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 <Adafruit_PWMServoDriver.h>
|
2023-12-28 12:35:53 +00:00
|
|
|
#include <FRAM.h>
|
2023-12-31 06:29:11 +00:00
|
|
|
#include <map>
|
2023-12-28 04:41:20 +00:00
|
|
|
|
2023-12-28 05:46:39 +00:00
|
|
|
// Protocol for digital output card
|
|
|
|
// Note that pin is always 2 characters long and padded with 0 if necessary
|
|
|
|
// Set pin state topic: <pin>/set/state payload: 0/1
|
|
|
|
// Set pin pwm topic: <pin>/set/value payload: 0-4095
|
|
|
|
// Publish pin state topic: <pin>/state payload: 0/1
|
|
|
|
// Publish pin pwm topic: <pin>/value payload: 0-4095
|
|
|
|
// Publish all topic: requeststate payload: N/A
|
|
|
|
// Enable/disable publish topic: publish_enable payload: 0/1
|
|
|
|
|
2023-12-31 19:39:51 +00:00
|
|
|
// MQTT Topics
|
2023-12-28 05:46:39 +00:00
|
|
|
#define SET_STATE_TOPIC "/set/state"
|
|
|
|
#define SET_VALUE_TOPIC "/set/value"
|
|
|
|
#define STATE_TOPIC "/state"
|
|
|
|
#define VALUE_TOPIC "/value"
|
|
|
|
#define REQUEST_STATE_TOPIC "requeststate"
|
|
|
|
#define PUBLISH_ENABLE_TOPIC "publish_enable"
|
|
|
|
|
2023-12-31 19:39:51 +00:00
|
|
|
// Card type
|
2023-12-28 04:41:20 +00:00
|
|
|
#define CARD_TYPE_DIGITAL_OUTPUT 0x00
|
|
|
|
|
2023-12-31 19:39:51 +00:00
|
|
|
/**
|
|
|
|
* @brief The DigitalOutputCard class is a class for controlling the Digital Output Card.
|
|
|
|
*
|
|
|
|
* This Digital Output Card has 16 digital outputs.
|
|
|
|
* All outputs are PWM capable.
|
|
|
|
* ALl outputs are 12V Push-Pull outputs.
|
|
|
|
* Outputs is grouped in 4 groups of 4 outputs.(0-3, 4-7, 8-11, 12-15)
|
|
|
|
* Each pin is capable of 0.6A, however each group's total current is limited to 1.2A.
|
|
|
|
*
|
|
|
|
* @warning You should not instantiate this class directly, instead use ESPMegaIO's registerCard function.
|
|
|
|
*/
|
2023-12-27 18:25:35 +00:00
|
|
|
class DigitalOutputCard : public ExpansionCard
|
2023-12-27 16:15:11 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
// Instantiate the card with the specified address
|
|
|
|
DigitalOutputCard(uint8_t address);
|
|
|
|
// Instantiate the card with the specified position on the dip switch
|
|
|
|
DigitalOutputCard(bool bit0, bool bit1, bool bit2, bool bit3, bool bit4);
|
|
|
|
// Initialize the card
|
2023-12-27 19:18:21 +00:00
|
|
|
bool begin();
|
|
|
|
// Dummy loop function
|
|
|
|
void loop();
|
2023-12-27 16:15:11 +00:00
|
|
|
// Set the output to the specified state
|
2023-12-28 10:03:47 +00:00
|
|
|
// This function set both the state and the pwm value
|
2023-12-27 16:15:11 +00:00
|
|
|
void digitalWrite(uint8_t pin, bool state);
|
|
|
|
// Set the output to the specified pwm value
|
2023-12-28 10:03:47 +00:00
|
|
|
// This function set both the state and the pwm value
|
2023-12-27 16:15:11 +00:00
|
|
|
void analogWrite(uint8_t pin, uint16_t value);
|
2023-12-28 10:03:47 +00:00
|
|
|
// Set the state of the specified pin
|
|
|
|
void setState(uint8_t pin, bool state);
|
|
|
|
// Set the pwm value of the specified pin
|
|
|
|
void setValue(uint8_t pin, uint16_t value);
|
2023-12-28 05:46:39 +00:00
|
|
|
// Get the state of the specified pin
|
|
|
|
bool getState(uint8_t pin);
|
|
|
|
// Get the pwm value of the specified pin
|
|
|
|
uint16_t getValue(uint8_t pin);
|
2023-12-28 12:01:37 +00:00
|
|
|
// Register a callback function that will be called when the state of a pin changes
|
2023-12-31 06:32:38 +00:00
|
|
|
uint8_t registerChangeCallback(std::function<void(uint8_t, bool, uint16_t)> callback);
|
2023-12-31 06:41:48 +00:00
|
|
|
// Unregister the callback function
|
|
|
|
void unregisterChangeCallback(uint8_t handler);
|
2023-12-28 12:01:37 +00:00
|
|
|
// Load a new pin map
|
2023-12-28 12:35:53 +00:00
|
|
|
void loadPinMap(uint8_t pinMap[16]);
|
|
|
|
// Bind the fram object to the card
|
|
|
|
// The Output card use the fram in this layout:
|
|
|
|
// [2 bytes] 0-1 : state
|
|
|
|
// [32 bytes] 2-33 : value
|
|
|
|
void bindFRAM(FRAM *fram, uint16_t address);
|
|
|
|
// Save the state and value to the fram
|
|
|
|
void saveToFRAM();
|
|
|
|
// Load the state and value from the fram
|
|
|
|
void loadFromFRAM();
|
|
|
|
// Set the auto save to fram flag
|
|
|
|
void setAutoSaveToFRAM(bool autoSave);
|
|
|
|
// Save a single pin value to fram
|
|
|
|
void savePinValueToFRAM(uint8_t pin);
|
|
|
|
// Save state to fram
|
|
|
|
void saveStateToFRAM();
|
|
|
|
// Save value to fram
|
|
|
|
void saveValueToFRAM();
|
2023-12-28 04:41:20 +00:00
|
|
|
// Get type of card
|
|
|
|
uint8_t getType();
|
2023-12-27 16:15:11 +00:00
|
|
|
private:
|
2023-12-28 12:35:53 +00:00
|
|
|
// FRAM address
|
|
|
|
uint16_t framAddress;
|
|
|
|
// FRAM is binded
|
|
|
|
bool framBinded = false;
|
|
|
|
// Auto save to fram
|
|
|
|
bool framAutoSave = false;
|
|
|
|
// The fram object pointer
|
|
|
|
FRAM *fram;
|
2023-12-28 12:01:37 +00:00
|
|
|
// The pwm driver
|
2023-12-27 16:15:11 +00:00
|
|
|
Adafruit_PWMServoDriver pwm;
|
2023-12-28 12:01:37 +00:00
|
|
|
// The address of the card
|
2023-12-27 16:15:11 +00:00
|
|
|
uint8_t address;
|
2023-12-28 12:01:37 +00:00
|
|
|
// The state of the card
|
2023-12-28 05:46:39 +00:00
|
|
|
bool state_buffer[16];
|
2023-12-28 12:01:37 +00:00
|
|
|
// The pwm value of the card
|
2023-12-28 05:46:39 +00:00
|
|
|
uint16_t value_buffer[16];
|
2023-12-28 12:01:37 +00:00
|
|
|
// The callback function
|
2023-12-31 06:29:11 +00:00
|
|
|
uint8_t callbacks_handler_index = 0;
|
|
|
|
std::map<uint8_t, std::function<void(uint8_t, bool, uint16_t)>> change_callbacks;
|
2023-12-28 12:01:37 +00:00
|
|
|
// Physical pin to virtual pin map
|
|
|
|
uint8_t pinMap[16];
|
2023-12-28 12:35:53 +00:00
|
|
|
// Return 16 bit value representing all 16 channels
|
|
|
|
uint16_t packStates();
|
|
|
|
// Unpack the 16 bit value to the state buffer
|
|
|
|
void unpackStates(uint16_t states);
|
2023-12-28 12:01:37 +00:00
|
|
|
// Virtual pin to physical pin map
|
|
|
|
uint8_t virtualPinMap[16];
|
2023-12-27 16:15:11 +00:00
|
|
|
};
|