card installation checking

This commit is contained in:
Siwat Sirichai 2023-12-28 02:18:21 +07:00
parent 1862887d30
commit 1481c08d3a
10 changed files with 122 additions and 37 deletions

View File

@ -3,7 +3,9 @@
AnalogCard::AnalogCard() : dac0(DAC0_ADDRESS),
dac1(DAC1_ADDRESS),
dac2(DAC2_ADDRESS),
dac3(DAC3_ADDRESS)
dac3(DAC3_ADDRESS),
analogInputBankA(),
analogInputBankB()
{
}
@ -36,16 +38,45 @@ uint16_t AnalogCard::analogRead(uint8_t pin)
return this->analogInputBankB.readADC_SingleEnded(pin - 4);
}
}
void AnalogCard::begin()
bool AnalogCard::begin()
{
this->dac0 = MCP4725(DAC0_ADDRESS);
this->dac1 = MCP4725(DAC1_ADDRESS);
this->dac2 = MCP4725(DAC2_ADDRESS);
this->dac3 = MCP4725(DAC3_ADDRESS);
this->dac0.begin();
this->dac1.begin();
this->dac2.begin();
this->dac3.begin();
this->analogInputBankA.begin(ANALOG_INPUT_BANK_A_ADDRESS);
this->analogInputBankB.begin(ANALOG_INPUT_BANK_B_ADDRESS);
if (!this->dac0.begin())
{
Serial.println("Card Analog ERROR: Failed to install DAC0");
return false;
}
if (!this->dac1.begin())
{
Serial.println("Card Analog ERROR: Failed to install DAC1");
return false;
}
if (!this->dac2.begin())
{
Serial.println("Card Analog ERROR: Failed to install DAC2");
return false;
}
if (!this->dac3.begin())
{
Serial.println("Card Analog ERROR: Failed to install DAC3");
return false;
}
if (!this->analogInputBankA.begin())
{
Serial.println("Card Analog ERROR: Failed to install analog input bank A");
return false;
}
if (!this->analogInputBankB.begin())
{
Serial.println("Card Analog ERROR: Failed to install analog input bank B");
return false;
}
return true;
}
void AnalogCard::loop()
{
}

View File

@ -15,7 +15,8 @@ class AnalogCard : public ExpansionCard {
AnalogCard();
void dacWrite(uint8_t pin, uint16_t value);
uint16_t analogRead(uint8_t pin);
void begin();
bool begin();
void loop();
private:
MCP4725 dac0;
MCP4725 dac1;

View File

@ -30,12 +30,18 @@ DigitalInputCard::DigitalInputCard(bool bit0, bool bit1, bool bit2, bool bit3, b
this->address_b += 4;
}
// Initialize the card
void DigitalInputCard::begin()
bool DigitalInputCard::begin()
{
this->inputBankA = PCF8574(this->address_a);
this->inputBankB = PCF8574(this->address_b);
this->inputBankA.begin();
this->inputBankB.begin();
if (!this->inputBankA.begin()) {
Serial.println("Input Card ERROR: Failed to install input bank A");
return false;
}
if (!this->inputBankB.begin()) {
Serial.println("Input Card ERROR: Failed to install input bank B");
return false;
}
// Set the debounce time for all pins to 50ms
for (int i = 0; i < 16; i++)
{
@ -48,6 +54,7 @@ void DigitalInputCard::begin()
this->pinMap[i] = i;
this->virtualPinMap[i] = i;
}
return true;
}
// Refresh and Read the input from the specified pin, always refresh the input buffers
bool DigitalInputCard::digitalRead(uint8_t pin)

View File

@ -9,7 +9,7 @@ class DigitalInputCard : public ExpansionCard {
// 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
void begin();
bool begin();
// Refresh and Read the input from the specified pin, always refresh the input buffers
bool digitalRead(uint8_t pin);
// Read the input from the specified pin, also refresh the input buffers if refresh is true

View File

@ -15,9 +15,14 @@ DigitalOutputCard::DigitalOutputCard(bool bit0, bool bit1, bool bit2, bool bit3,
}
// Initialize the card
void DigitalOutputCard::begin() {
bool DigitalOutputCard::begin() {
this->pwm = Adafruit_PWMServoDriver(this->address);
this->pwm.begin();
pwm.reset();
pwm.setOutputMode(true);
// Output card don't send ack, we can't check if it's connected
// so we just return true
return true;
}
// Set the output to the specified state
void DigitalOutputCard::digitalWrite(uint8_t pin, bool state) {
@ -30,3 +35,7 @@ void DigitalOutputCard::analogWrite(uint8_t pin, uint16_t value) {
// Set the pwm value
this->pwm.setPin(pin, value);
}
// Dummy loop function
void DigitalOutputCard::loop() {
}

View File

@ -9,7 +9,9 @@ public:
// 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
void begin();
bool begin();
// Dummy loop function
void loop();
// Set the output to the specified state
void digitalWrite(uint8_t pin, bool state);
// Set the output to the specified pwm value

View File

@ -2,28 +2,49 @@
ESPMegaPRO::ESPMegaPRO() {
}
void ESPMegaPRO::begin() {
bool ESPMegaPRO::begin() {
Wire.begin(14, 33);
inputs.begin();
Serial.begin(115200);
if(!inputs.begin()) {
Serial.println("Failed to initialize inputs");
Serial.println("Is this an ESPMegaPRO device?");
return false;
}
outputs.begin();
fram.begin(FRAM_ADDRESS);
if(!fram.begin(FRAM_ADDRESS)) {
Serial.println("Failed to initialize FRAM");
Serial.println("Is this an ESPMegaPRO device?");
return false;
}
uint8_t pinMap[16] = {0, 1, 2, 3, 4, 5, 6, 7, 15, 14, 13, 12, 11, 10, 9, 8};
inputs.loadPinMap(pinMap);
return true;
}
void ESPMegaPRO::loop() {
inputs.loop();
outputs.loop();
for (int i = 0; i < 256; i++) {
for (int i = 0; i < 255; i++) {
if (cardInstalled[i]) {
cards[i]->loop();
}
}
}
void ESPMegaPRO::installCard(uint8_t slot, ExpansionCard* card) {
bool ESPMegaPRO::installCard(uint8_t slot, ExpansionCard* card) {
if (slot > 255) return;
if (cardInstalled[slot]) {
Serial.println("Card already installed");
return false;
}
if (!card->begin()) {
Serial.print("Failed to install card at slot ");
Serial.println(slot);
return false;
}
cards[slot] = card;
cardInstalled[slot] = true;
cardCount++;
card->begin();
return true;
}
bool ESPMegaPRO::updateTimeFromNTP() {
struct tm timeinfo;

View File

@ -29,9 +29,9 @@ struct rtctime_t {
class ESPMegaPRO {
public:
ESPMegaPRO();
void begin();
bool begin();
void loop();
void installCard(uint8_t slot, ExpansionCard* card);
bool installCard(uint8_t slot, ExpansionCard* card);
bool updateTimeFromNTP();
rtctime_t getTime();
void setTime(int hours, int minutes, int seconds, int day, int month, int year);

View File

@ -5,7 +5,7 @@ class ExpansionCard {
public:
// Instantiate the card with the specified address
ExpansionCard() {}
virtual void begin() {}
virtual bool begin() {}
// Preform a loop to refresh the input buffers
virtual void loop() {}
};

View File

@ -5,19 +5,21 @@
// This code demonstrates how to use the cards
ESPMegaPRO espmega = ESPMegaPRO();
AnalogCard analogCard = AnalogCard();
void inputCallback(uint8_t pin, bool state) {
void inputCallback(uint8_t pin, bool state)
{
Serial.print("Input ");
Serial.print(pin);
Serial.print(" changed to ");
Serial.println(state);
}
void printInputs() {
for (int i = 0; i < 16; i++) {
void printInputs()
{
for (int i = 0; i < 16; i++)
{
Serial.print("Input ");
Serial.print(i);
Serial.print(": ");
@ -26,37 +28,49 @@ void printInputs() {
}
}
void setup() {
Serial.begin(115200);
void setup()
{
// Instantiate ESPMega
espmega.begin();
Serial.println("ESPMega initialized");
// Read all the inputs and print them
printInputs();
// Turn on all the outputs
for (int i = 0; i < 16; i++) {
for (int i = 0; i < 16; i++)
{
espmega.outputs.digitalWrite(i, true);
}
// Set the debounce time for all inputs to 200ms
for (int i = 0; i < 16; i++) {
for (int i = 0; i < 16; i++)
{
espmega.inputs.setDebounceTime(i, 200);
}
// Register the callback function
espmega.inputs.registerCallback(inputCallback);
// Install the analog card
espmega.installCard(0, &analogCard);
Serial.println("Installing analog card");
if (espmega.installCard(0, &analogCard))
{
Serial.println("Analog card installed");
}
else
{
Serial.println("Failed to install analog card");
}
}
unsigned long previousMillis = 0;
const unsigned long interval = 1000; // 1 second
void loop() {
void loop()
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
printInputs();
}
espmega.loop();
}