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

182 lines
4.4 KiB
C++
Raw Permalink 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-09-28 07:22:19 +00:00
FRAM ESPMega_FRAM;
2023-09-16 09:44:07 +00:00
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-09-28 07:22:19 +00:00
ESPMega_FRAM.begin(FRAM_ADDRESS);
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-09-09 15:44:08 +00:00
return ((inputBufferA >> (7 - 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
#ifndef USE_INTERRUPT
refreshInputBankB(); // Only poll if interrupt is not enabled
#endif
2023-09-09 15:44:08 +00:00
if (id >= 8 && id <= 11)
return ((inputBufferB >> (15 - id)) & 1); // Extract bit from buffer
else if (id >= 12 && id <= 15)
return ((inputBufferB >> (id - 12)) & 1);
2023-01-22 16:09:48 +00:00
}
return false;
}
void ESPMega_analogWrite(int id, int value)
{
2023-09-09 15:44:08 +00:00
if (id >= 0 && id <= 7)
id += 8;
else if (id >= 8 && id <= 15)
id -= 8;
2023-01-22 16:09:48 +00:00
pwmBank.setPin(id, value);
}
void ESPMega_digitalWrite(int id, bool value)
{
2024-01-11 15:26:57 +00:00
if (id >= 0 && id <= 7)
id += 8;
else if (id >= 8 && id <= 15)
id -= 8;
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
}
2023-09-28 06:57:51 +00:00
rtctime_t ESPMega_getTime()
{
2023-09-16 09:44:07 +00:00
tmElements_t timeElement;
RTC.read(timeElement);
rtctime_t time;
time.hours = timeElement.Hour;
time.minutes = timeElement.Minute;
time.seconds = timeElement.Second;
time.day = timeElement.Day;
time.month = timeElement.Month;
2023-09-28 06:57:51 +00:00
time.year = timeElement.Year + 1970;
2023-09-16 09:44:07 +00:00
return time;
}
2023-09-28 06:57:51 +00:00
void ESPMega_setTime(int hours, int minutes, int seconds, int day, int month, int year)
{
2023-09-16 09:44:07 +00:00
tmElements_t timeElement;
timeElement.Hour = hours;
timeElement.Minute = minutes;
timeElement.Second = seconds;
timeElement.Day = day;
timeElement.Month = month;
2023-09-28 06:57:51 +00:00
timeElement.Year = year - 1970;
2023-09-16 09:44:07 +00:00
RTC.write(timeElement);
}
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-09-09 15:44:08 +00:00
return analogInputBankA.readADC_SingleEnded(3 - id);
2023-08-26 16:16:51 +00:00
else if (id >= 4 && id <= 7)
2023-09-09 15:44:08 +00:00
return analogInputBankB.readADC_SingleEnded(7 - id);
2023-08-26 16:16:51 +00:00
return 0;
}
2023-09-09 15:44:08 +00:00
void ESPMega_dacWrite(int id, int value)
{
2023-08-27 16:42:08 +00:00
switch (id)
{
case 0:
DAC0.setValue(value);
break;
case 1:
DAC1.setValue(value);
break;
case 2:
DAC2.setValue(value);
2023-09-09 15:44:08 +00:00
break;
2023-08-27 16:42:08 +00:00
case 3:
DAC3.setValue(value);
break;
default:
break;
}
}
2023-09-09 15:44:08 +00:00
2023-09-28 06:57:51 +00:00
bool ESPMega_updateTimeFromNTP()
2023-09-09 15:44:08 +00:00
{
2023-09-28 06:57:51 +00:00
struct tm timeinfo;
if (getLocalTime(&timeinfo))
2023-09-09 15:44:08 +00:00
{
2023-09-28 06:57:51 +00:00
rtctime_t rtctime = ESPMega_getTime();
if (rtctime.hours != timeinfo.tm_hour || rtctime.minutes != timeinfo.tm_min ||
rtctime.seconds != timeinfo.tm_sec || rtctime.day != timeinfo.tm_mday ||
rtctime.month != timeinfo.tm_mon + 1 || rtctime.year != timeinfo.tm_year + 1900)
{
ESPMega_setTime(timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec,
timeinfo.tm_mday, timeinfo.tm_mon + 1, timeinfo.tm_year + 1900);
}
return true;
2023-09-09 15:44:08 +00:00
}
2023-09-28 06:57:51 +00:00
return false;
}
2023-09-09 15:44:08 +00:00
2023-08-26 16:16:51 +00:00
#endif