From 94443393d08f56ddca7f47470890fced73add53b Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Sat, 26 Aug 2023 23:16:51 +0700 Subject: [PATCH] analog input capability --- .../lib/ESPMegaPRO/ESPMegaPRO.cpp | 56 +++++++++++++------ Template Project/lib/ESPMegaPRO/ESPMegaPRO.h | 17 +++++- Template Project/platformio.ini | 4 ++ Template Project/src/analogdemo.cpp | 10 ++++ ...input_test.cpp => input_test.cpp.disabled} | 0 5 files changed, 68 insertions(+), 19 deletions(-) create mode 100644 Template Project/src/analogdemo.cpp rename Template Project/src/{input_test.cpp => input_test.cpp.disabled} (100%) diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaPRO.cpp b/Template Project/lib/ESPMegaPRO/ESPMegaPRO.cpp index a64a058..914e82c 100644 --- a/Template Project/lib/ESPMegaPRO/ESPMegaPRO.cpp +++ b/Template Project/lib/ESPMegaPRO/ESPMegaPRO.cpp @@ -6,6 +6,10 @@ uint8_t inputBufferB; PCF8574 inputBankA(INPUT_BANK_A_ADDRESS); PCF8574 inputBankB(INPUT_BANK_B_ADDRESS); Adafruit_PWMServoDriver pwmBank = Adafruit_PWMServoDriver(PWM_BANK_ADDRESS); +#ifdef ANALOG_CARD_ENABLE +Adafruit_ADS1115 analogInputBankA; +Adafruit_ADS1115 analogInputBankB; +#endif void ESPMega_begin() { @@ -17,39 +21,44 @@ void ESPMega_begin() // ESPMegaPRO v3 use the PWMBank to drive Half Bridge // Push Pull Output is required. - pwmBank.setOutputMode(true); + pwmBank.setOutputMode(true); - #ifdef USE_INTERRUPT +#ifdef USE_INTERRUPT pinMode(INPUT_BANK_A_INTERRUPT, INPUT_PULLUP); pinMode(INPUT_BANK_B_INTERRUPT, INPUT_PULLUP); - attachInterrupt(digitalPinToInterrupt(INPUT_BANK_A_INTERRUPT),refreshInputBankA,FALLING); - attachInterrupt(digitalPinToInterrupt(INPUT_BANK_B_INTERRUPT),refreshInputBankB,FALLING); - #endif + 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); + analogInputBankA.begin(ANALOG_INPUT_BANK_B_ADDRESS); +#endif } -void ESPMega_loop() { - +void ESPMega_loop() +{ } bool ESPMega_digitalRead(int id) { if (id >= 0 && id <= 7) { - #ifndef USE_INTERRUPT - refreshInputBankA(); //Only poll if interrupt is not enabled - #endif +#ifndef USE_INTERRUPT + refreshInputBankA(); // Only poll if interrupt is not enabled +#endif - return ((inputBufferA>>id)&1); //Extract bit from buffer + return ((inputBufferA >> id) & 1); // Extract bit from buffer } if (id >= 8 && id <= 15) { - id-=8; + id -= 8; - #ifndef USE_INTERRUPT - refreshInputBankB(); //Only poll if interrupt is not enabled - #endif +#ifndef USE_INTERRUPT + refreshInputBankB(); // Only poll if interrupt is not enabled +#endif - return ((inputBufferB>>id)&1); //Extract bit from buffer + return ((inputBufferB >> id) & 1); // Extract bit from buffer } return false; } @@ -61,7 +70,7 @@ void ESPMega_analogWrite(int id, int value) void ESPMega_digitalWrite(int id, bool value) { - if(value) + if (value) pwmBank.setPin(id, 4095); else pwmBank.setPin(id, 0); @@ -75,4 +84,15 @@ void IRAM_ATTR refreshInputBankA() void IRAM_ATTR refreshInputBankB() { inputBufferB = inputBankB.read8(); -} \ No newline at end of file +} + +#ifdef ANALOG_CARD_ENABLE +uint16_t ESPMega_analogRead(int id) +{ + if (id >= 0 && id <= 3) + return analogInputBankA.readADC_SingleEnded(id); + else if (id >= 4 && id <= 7) + return analogInputBankB.readADC_SingleEnded(id - 4); + return 0; +} +#endif \ No newline at end of file diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaPRO.h b/Template Project/lib/ESPMegaPRO/ESPMegaPRO.h index 23e8288..9383d21 100644 --- a/Template Project/lib/ESPMegaPRO/ESPMegaPRO.h +++ b/Template Project/lib/ESPMegaPRO/ESPMegaPRO.h @@ -1,16 +1,21 @@ #ifndef ESPMEGA #define ESPMEGA - +#define ANALOG_CARD_ENABLE #include #include #include #include +#ifdef ANALOG_CARD_ENABLE +#include +#endif #define INPUT_BANK_A_ADDRESS 0x21 #define INPUT_BANK_B_ADDRESS 0x22 #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 USE_INTERRUPT #define INPUT_BANK_A_INTERRUPT 36 @@ -59,4 +64,14 @@ void ESPMega_digitalWrite(int id, bool value); void IRAM_ATTR refreshInputBankA(); void IRAM_ATTR refreshInputBankB(); +#ifdef ANALOG_CARD_ENABLE +/** + * Read one of the ESPMega Analog Card's Analog Input pins (A0-A7) + * + * @param id The number of the pin to be read + * @return The value of the pin (0-4095) +*/ +uint16_t ESPMega_analogRead(int id); +#endif + #endif \ No newline at end of file diff --git a/Template Project/platformio.ini b/Template Project/platformio.ini index 9bb78fe..670c7cb 100644 --- a/Template Project/platformio.ini +++ b/Template Project/platformio.ini @@ -14,5 +14,9 @@ board = wt32-eth01 framework = arduino ;siwats/ESPMegaPROR3@1.0.1 lib_deps = adafruit/Adafruit PWM Servo Driver Library@^2.4.1 + adafruit/Adafruit ADS1X15@^2.4.0 + adafruit/Adafruit BusIO robtillaart/PCF8574@^0.3.7 + arduino-libraries/Arduino_BuiltIn@^1.0.0 + SPI monitor_speed = 115200 \ No newline at end of file diff --git a/Template Project/src/analogdemo.cpp b/Template Project/src/analogdemo.cpp new file mode 100644 index 0000000..45f908c --- /dev/null +++ b/Template Project/src/analogdemo.cpp @@ -0,0 +1,10 @@ +#include +void setup() { + ESPMega_begin(); + Serial.begin(115200); +} + +void loop() { + int a0 = ESPMega_analogRead(0); + Serial.println(a0); +} \ No newline at end of file diff --git a/Template Project/src/input_test.cpp b/Template Project/src/input_test.cpp.disabled similarity index 100% rename from Template Project/src/input_test.cpp rename to Template Project/src/input_test.cpp.disabled