diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaPRO.cpp b/Template Project/lib/ESPMegaPRO/ESPMegaPRO.cpp index 914e82c..e7a50a8 100644 --- a/Template Project/lib/ESPMegaPRO/ESPMegaPRO.cpp +++ b/Template Project/lib/ESPMegaPRO/ESPMegaPRO.cpp @@ -9,6 +9,10 @@ Adafruit_PWMServoDriver pwmBank = Adafruit_PWMServoDriver(PWM_BANK_ADDRESS); #ifdef ANALOG_CARD_ENABLE Adafruit_ADS1115 analogInputBankA; Adafruit_ADS1115 analogInputBankB; +MCP4725 DAC0(DAC0_ADDRESS); +MCP4725 DAC1(DAC1_ADDRESS); +MCP4725 DAC2(DAC2_ADDRESS); +MCP4725 DAC3(DAC3_ADDRESS); #endif void ESPMega_begin() @@ -32,7 +36,11 @@ void ESPMega_begin() #ifdef ANALOG_CARD_ENABLE analogInputBankA.begin(ANALOG_INPUT_BANK_A_ADDRESS); - analogInputBankA.begin(ANALOG_INPUT_BANK_B_ADDRESS); + analogInputBankB.begin(ANALOG_INPUT_BANK_B_ADDRESS); + DAC0.begin(); + DAC1.begin(); + DAC2.begin(); + DAC3.begin(); #endif } @@ -87,12 +95,31 @@ void IRAM_ATTR refreshInputBankB() } #ifdef ANALOG_CARD_ENABLE -uint16_t ESPMega_analogRead(int id) +int16_t ESPMega_analogRead(int id) { if (id >= 0 && id <= 3) - return analogInputBankA.readADC_SingleEnded(id); + return analogInputBankA.readADC_SingleEnded(3-id); else if (id >= 4 && id <= 7) - return analogInputBankB.readADC_SingleEnded(id - 4); + return analogInputBankB.readADC_SingleEnded(7-id); return 0; } +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; + } +} #endif \ No newline at end of file diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaPRO.h b/Template Project/lib/ESPMegaPRO/ESPMegaPRO.h index 9383d21..813f0d5 100644 --- a/Template Project/lib/ESPMegaPRO/ESPMegaPRO.h +++ b/Template Project/lib/ESPMegaPRO/ESPMegaPRO.h @@ -7,6 +7,7 @@ #include #ifdef ANALOG_CARD_ENABLE #include +#include #endif #define INPUT_BANK_A_ADDRESS 0x21 @@ -14,8 +15,12 @@ #define PWM_BANK_ADDRESS 0x5F #define OUTPUT_BANK_ADDRESS 0x21 #define EEPROM_ADDRESS 0x22 -#define ANALOG_INPUT_BANK_A_ADDRESS 0x60 -#define ANALOG_INPUT_BANK_B_ADDRESS 0x61 +#define ANALOG_INPUT_BANK_A_ADDRESS 0x48 +#define ANALOG_INPUT_BANK_B_ADDRESS 0x49 +#define DAC0_ADDRESS 0x60 +#define DAC1_ADDRESS 0x61 +#define DAC2_ADDRESS 0x62 +#define DAC3_ADDRESS 0x63 //#define USE_INTERRUPT #define INPUT_BANK_A_INTERRUPT 36 @@ -71,7 +76,15 @@ void IRAM_ATTR refreshInputBankB(); * @param id The number of the pin to be read * @return The value of the pin (0-4095) */ -uint16_t ESPMega_analogRead(int id); +int16_t ESPMega_analogRead(int id); +/** + * Write a True Analog Signal to one of the ESPMega Analog Card's + * Analog Output pins (AO0-AO3) + * + * @param id The number of the pin to write to + * @param value the analog value of the pin (0-4095) +*/ +void ESPMega_dacWrite(int id, int value); #endif #endif \ No newline at end of file diff --git a/Template Project/platformio.ini b/Template Project/platformio.ini index 670c7cb..01f71d0 100644 --- a/Template Project/platformio.ini +++ b/Template Project/platformio.ini @@ -19,4 +19,5 @@ lib_deps = adafruit/Adafruit PWM Servo Driver Library@^2.4.1 robtillaart/PCF8574@^0.3.7 arduino-libraries/Arduino_BuiltIn@^1.0.0 SPI + robtillaart/MCP4725@^0.3.7 monitor_speed = 115200 \ No newline at end of file diff --git a/Template Project/src/analogdemo.cpp b/Template Project/src/analogdemo.cpp index 45f908c..2ae38a2 100644 --- a/Template Project/src/analogdemo.cpp +++ b/Template Project/src/analogdemo.cpp @@ -1,10 +1,14 @@ #include -void setup() { - ESPMega_begin(); + +void setup() +{ Serial.begin(115200); + ESPMega_begin(); + ESPMega_dacWrite(0,2000); + ESPMega_dacWrite(1,2000); } -void loop() { - int a0 = ESPMega_analogRead(0); - Serial.println(a0); +void loop() +{ + delay(100); } \ No newline at end of file