ESPMegaPRO-v3-SDK/Template Project/lib/ESPMegaPRO/ESPMegaPRO.cpp

125 lines
2.7 KiB
C++
Raw Normal View History

2023-01-22 16:09:48 +00:00
#include <ESPMegaPRO.h>
uint8_t inputBufferA;
uint8_t inputBufferB;
PCF8574 inputBankA(INPUT_BANK_A_ADDRESS);
PCF8574 inputBankB(INPUT_BANK_B_ADDRESS);
2023-08-26 05:47:28 +00:00
Adafruit_PWMServoDriver pwmBank = Adafruit_PWMServoDriver(PWM_BANK_ADDRESS);
2023-08-26 16:16:51 +00:00
#ifdef ANALOG_CARD_ENABLE
Adafruit_ADS1115 analogInputBankA;
Adafruit_ADS1115 analogInputBankB;
2023-08-27 16:42:08 +00:00
MCP4725 DAC0(DAC0_ADDRESS);
MCP4725 DAC1(DAC1_ADDRESS);
MCP4725 DAC2(DAC2_ADDRESS);
MCP4725 DAC3(DAC3_ADDRESS);
2023-08-26 16:16:51 +00:00
#endif
2023-01-22 16:09:48 +00:00
void ESPMega_begin()
{
Wire.begin(14, 33);
inputBankA.begin();
inputBankB.begin();
pwmBank.begin();
2023-08-26 05:47:28 +00:00
// ESPMegaPRO v3 use the PWMBank to drive Half Bridge
// Push Pull Output is required.
2023-08-26 16:16:51 +00:00
pwmBank.setOutputMode(true);
2023-08-26 05:47:28 +00:00
2023-08-26 16:16:51 +00:00
#ifdef USE_INTERRUPT
2023-08-26 05:47:28 +00:00
pinMode(INPUT_BANK_A_INTERRUPT, INPUT_PULLUP);
pinMode(INPUT_BANK_B_INTERRUPT, INPUT_PULLUP);
2023-08-26 16:16:51 +00:00
attachInterrupt(digitalPinToInterrupt(INPUT_BANK_A_INTERRUPT), refreshInputBankA, FALLING);
attachInterrupt(digitalPinToInterrupt(INPUT_BANK_B_INTERRUPT), refreshInputBankB, FALLING);
#endif
#ifdef ANALOG_CARD_ENABLE
analogInputBankA.begin(ANALOG_INPUT_BANK_A_ADDRESS);
2023-08-27 16:42:08 +00:00
analogInputBankB.begin(ANALOG_INPUT_BANK_B_ADDRESS);
DAC0.begin();
DAC1.begin();
DAC2.begin();
DAC3.begin();
2023-08-26 16:16:51 +00:00
#endif
2023-01-22 16:09:48 +00:00
}
2023-08-26 16:16:51 +00:00
void ESPMega_loop()
{
2023-01-22 16:09:48 +00:00
}
bool ESPMega_digitalRead(int id)
{
if (id >= 0 && id <= 7)
{
2023-08-26 16:16:51 +00:00
#ifndef USE_INTERRUPT
refreshInputBankA(); // Only poll if interrupt is not enabled
#endif
2023-01-22 16:09:48 +00:00
2023-08-26 16:16:51 +00:00
return ((inputBufferA >> id) & 1); // Extract bit from buffer
2023-01-22 16:09:48 +00:00
}
if (id >= 8 && id <= 15)
{
2023-08-26 16:16:51 +00:00
id -= 8;
2023-01-22 16:09:48 +00:00
2023-08-26 16:16:51 +00:00
#ifndef USE_INTERRUPT
refreshInputBankB(); // Only poll if interrupt is not enabled
#endif
2023-01-22 16:09:48 +00:00
2023-08-26 16:16:51 +00:00
return ((inputBufferB >> id) & 1); // Extract bit from buffer
2023-01-22 16:09:48 +00:00
}
return false;
}
void ESPMega_analogWrite(int id, int value)
{
pwmBank.setPin(id, value);
}
void ESPMega_digitalWrite(int id, bool value)
{
2023-08-26 16:16:51 +00:00
if (value)
2023-01-22 16:09:48 +00:00
pwmBank.setPin(id, 4095);
else
pwmBank.setPin(id, 0);
}
void IRAM_ATTR refreshInputBankA()
{
inputBufferA = inputBankA.read8();
}
void IRAM_ATTR refreshInputBankB()
{
inputBufferB = inputBankB.read8();
2023-08-26 16:16:51 +00:00
}
#ifdef ANALOG_CARD_ENABLE
2023-08-27 16:42:08 +00:00
int16_t ESPMega_analogRead(int id)
2023-08-26 16:16:51 +00:00
{
if (id >= 0 && id <= 3)
2023-08-27 16:42:08 +00:00
return analogInputBankA.readADC_SingleEnded(3-id);
2023-08-26 16:16:51 +00:00
else if (id >= 4 && id <= 7)
2023-08-27 16:42:08 +00:00
return analogInputBankB.readADC_SingleEnded(7-id);
2023-08-26 16:16:51 +00:00
return 0;
}
2023-08-27 16:42:08 +00:00
void ESPMega_dacWrite(int id, int value) {
switch (id)
{
case 0:
DAC0.setValue(value);
break;
case 1:
DAC1.setValue(value);
break;
case 2:
DAC2.setValue(value);
break;
case 3:
DAC3.setValue(value);
break;
default:
break;
}
}
2023-08-26 16:16:51 +00:00
#endif