diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaPRO.cpp b/Template Project/lib/ESPMegaPRO/ESPMegaPRO.cpp index 2db5186..b776c88 100644 --- a/Template Project/lib/ESPMegaPRO/ESPMegaPRO.cpp +++ b/Template Project/lib/ESPMegaPRO/ESPMegaPRO.cpp @@ -5,6 +5,7 @@ uint8_t inputBufferB; PCF8574 inputBankA(INPUT_BANK_A_ADDRESS); PCF8574 inputBankB(INPUT_BANK_B_ADDRESS); +PCF8574 utilBank(UTIL_BANK_ADDRESS); Adafruit_PWMServoDriver pwmBank = Adafruit_PWMServoDriver(PWM_BANK_ADDRESS); FRAM ESPMega_FRAM; @@ -22,6 +23,7 @@ void ESPMega_begin() Wire.begin(14, 33); inputBankA.begin(); inputBankB.begin(); + utilBank.begin(); pwmBank.begin(); ESPMega_FRAM.begin(FRAM_ADDRESS); // ESPMegaPRO v3 use the PWMBank to drive Half Bridge @@ -168,11 +170,50 @@ bool ESPMega_updateTimeFromNTP() { ESPMega_setTime(timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec, timeinfo.tm_mday, timeinfo.tm_mon + 1, timeinfo.tm_year + 1900); - } return true; } return false; } -#endif \ No newline at end of file +#endif + +void ESPMega_mapIO(int pin, int channel) +{ + if (channel < 4 && channel >= 0) + { + if (pin == 4 || pin == 32) + { + utilBank.write(5, (channel - channel % 2) / 2); + utilBank.write(4, channel % 2); + } + if (pin == 12 || pin == 17) + { + utilBank.write(7, (channel - channel % 2) / 2); + utilBank.write(6, channel % 2); + } + } +} + +void ESPMega_RS485_begin() +{ + Serial1.begin(115200); + ESPMega_RS485_recieveEnable(); +} + +void ESPMega_RS485_transmitEnable() +{ + utilBank.write(0, 1); + utilBank.write(1, 0); +} + +void ESPMega_RS485_recieveEnable() +{ + utilBank.write(0, 0); + utilBank.write(1, 1); +} + +void ESPMega_RS485_disable() { + utilBank.write(0, 0); + utilBank.write(1, 0); +} diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaPRO.h b/Template Project/lib/ESPMegaPRO/ESPMegaPRO.h index c11688e..d3ac76d 100644 --- a/Template Project/lib/ESPMegaPRO/ESPMegaPRO.h +++ b/Template Project/lib/ESPMegaPRO/ESPMegaPRO.h @@ -16,6 +16,7 @@ #define INPUT_BANK_A_ADDRESS 0x21 #define INPUT_BANK_B_ADDRESS 0x22 +#define UTIL_BANK_ADDRESS 0x24 #define PWM_BANK_ADDRESS 0x5F #define RTC_ADDRESS 0x68 #define ANALOG_INPUT_BANK_A_ADDRESS 0x48 @@ -127,4 +128,17 @@ int16_t ESPMega_analogRead(int id); void ESPMega_dacWrite(int id, int value); #endif -#endif \ No newline at end of file +#endif + +/** + * Map a pair of GPIO pin to a Multiplexer Channel + * through the ESPMega's Muxer + * @param pin The number of a multiplexing-capable pin in a pair to map [12,17], [4,32] + * @param channel Muxer channel to map to (0-3) +*/ +void ESPMega_mapIO(int pin, int channel); + +void ESPMega_RS485_begin(); +void ESPMega_RS485_transmitEnable(); +void ESPMega_RS485_recieveEnable(); +void ESPMega_RS485_disable();