From a927c74393adb71ce98771931ba65d41a2ff3b9f Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Sat, 9 Sep 2023 22:44:08 +0700 Subject: [PATCH] update base code to rev3.3a --- Template Project/.vscode/settings.json | 6 ++- .../lib/ESPMegaPRO/ESPMegaPRO.cpp | 48 +++++++++++++++---- Template Project/lib/ESPMegaPRO/ESPMegaPRO.h | 9 +++- Template Project/platformio.ini | 4 +- ...analogdemo.cpp => analogdemo.cpp.disabled} | 0 Template Project/src/i2c_scanner.cpp.disabled | 2 +- Template Project/src/rtc_demo.cpp | 8 ++++ 7 files changed, 61 insertions(+), 16 deletions(-) rename Template Project/src/{analogdemo.cpp => analogdemo.cpp.disabled} (100%) create mode 100644 Template Project/src/rtc_demo.cpp diff --git a/Template Project/.vscode/settings.json b/Template Project/.vscode/settings.json index de67d8b..ef8ef5e 100644 --- a/Template Project/.vscode/settings.json +++ b/Template Project/.vscode/settings.json @@ -7,6 +7,8 @@ "unordered_set": "cpp", "vector": "cpp", "string_view": "cpp", - "initializer_list": "cpp" - } + "initializer_list": "cpp", + "adafruit_ads1x15.h": "c" + }, + "cmake.configureOnOpen": true } \ No newline at end of file diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaPRO.cpp b/Template Project/lib/ESPMegaPRO/ESPMegaPRO.cpp index e7a50a8..59cc5c6 100644 --- a/Template Project/lib/ESPMegaPRO/ESPMegaPRO.cpp +++ b/Template Project/lib/ESPMegaPRO/ESPMegaPRO.cpp @@ -6,6 +6,7 @@ uint8_t inputBufferB; PCF8574 inputBankA(INPUT_BANK_A_ADDRESS); PCF8574 inputBankB(INPUT_BANK_B_ADDRESS); Adafruit_PWMServoDriver pwmBank = Adafruit_PWMServoDriver(PWM_BANK_ADDRESS); +I2C_eeprom ESPMega_EEPROM(EEPROM_ADDRESS); #ifdef ANALOG_CARD_ENABLE Adafruit_ADS1115 analogInputBankA; Adafruit_ADS1115 analogInputBankB; @@ -18,11 +19,11 @@ MCP4725 DAC3(DAC3_ADDRESS); void ESPMega_begin() { Wire.begin(14, 33); - inputBankA.begin(); inputBankB.begin(); pwmBank.begin(); - + ESPMega_RTC.begin(); + ESPMega_EEPROM.begin(); // ESPMegaPRO v3 use the PWMBank to drive Half Bridge // Push Pull Output is required. pwmBank.setOutputMode(true); @@ -56,23 +57,28 @@ bool ESPMega_digitalRead(int id) refreshInputBankA(); // Only poll if interrupt is not enabled #endif - return ((inputBufferA >> id) & 1); // Extract bit from buffer + return ((inputBufferA >> (7 - id)) & 1); // Extract bit from buffer } if (id >= 8 && id <= 15) { - id -= 8; #ifndef USE_INTERRUPT refreshInputBankB(); // Only poll if interrupt is not enabled #endif - - return ((inputBufferB >> id) & 1); // Extract bit from buffer + 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); } return false; } void ESPMega_analogWrite(int id, int value) { + if (id >= 0 && id <= 7) + id += 8; + else if (id >= 8 && id <= 15) + id -= 8; pwmBank.setPin(id, value); } @@ -98,12 +104,13 @@ void IRAM_ATTR refreshInputBankB() int16_t ESPMega_analogRead(int id) { if (id >= 0 && id <= 3) - return analogInputBankA.readADC_SingleEnded(3-id); + return analogInputBankA.readADC_SingleEnded(3 - id); else if (id >= 4 && id <= 7) - return analogInputBankB.readADC_SingleEnded(7-id); + return analogInputBankB.readADC_SingleEnded(7 - id); return 0; } -void ESPMega_dacWrite(int id, int value) { +void ESPMega_dacWrite(int id, int value) +{ switch (id) { case 0: @@ -114,7 +121,7 @@ void ESPMega_dacWrite(int id, int value) { break; case 2: DAC2.setValue(value); - break; + break; case 3: DAC3.setValue(value); break; @@ -122,4 +129,25 @@ void ESPMega_dacWrite(int id, int value) { break; } } + +/* void ESPMega_rtcNTPUpdate() +{ + ntpTimeClient.update(); + if (ntpTimeClient.updated()) + { + int day = ntpTimeClient.getDay(); + int month = ntpTimeClient.getMonth(); + int year = ntpTimeClient.getYear(); + int hours = ntpTimeClient.getHours(); + int minutes = ntpTimeClient.getMinutes(); + int seconds = ntpTimeClient.getSeconds(); + ESPMega_RTC.setYear(year); + ESPMega_RTC.setMonth(month); + ESPMega_RTC.setDay(day); + ESPMega_RTC.setHour(hours); + ESPMega_RTC.setMinute(minutes); + ESPMega_RTC.setSecond(seconds); + } +} */ + #endif \ No newline at end of file diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaPRO.h b/Template Project/lib/ESPMegaPRO/ESPMegaPRO.h index 813f0d5..07ec186 100644 --- a/Template Project/lib/ESPMegaPRO/ESPMegaPRO.h +++ b/Template Project/lib/ESPMegaPRO/ESPMegaPRO.h @@ -8,23 +8,28 @@ #ifdef ANALOG_CARD_ENABLE #include #include +#include +#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 RTC_ADDRESS 0x68 #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 EEPROM_ADDRESS 0x70 + +#define ESPMega_RTC rtc //#define USE_INTERRUPT #define INPUT_BANK_A_INTERRUPT 36 #define INPUT_BANK_B_INTERRUPT 39 +extern I2C_eeprom ESPMega_EEPROM; /** * Initiate ESPMega PRO Internal Components diff --git a/Template Project/platformio.ini b/Template Project/platformio.ini index 01f71d0..d6420ce 100644 --- a/Template Project/platformio.ini +++ b/Template Project/platformio.ini @@ -18,6 +18,8 @@ lib_deps = adafruit/Adafruit PWM Servo Driver Library@^2.4.1 adafruit/Adafruit BusIO robtillaart/PCF8574@^0.3.7 arduino-libraries/Arduino_BuiltIn@^1.0.0 - SPI + SPI@^2.0.0 robtillaart/MCP4725@^0.3.7 + robtillaart/I2C_EEPROM@^1.7.3 + sparkfun/SparkFun DS1307 Real-Time Clock (RTC)@^1.0.1 monitor_speed = 115200 \ No newline at end of file diff --git a/Template Project/src/analogdemo.cpp b/Template Project/src/analogdemo.cpp.disabled similarity index 100% rename from Template Project/src/analogdemo.cpp rename to Template Project/src/analogdemo.cpp.disabled diff --git a/Template Project/src/i2c_scanner.cpp.disabled b/Template Project/src/i2c_scanner.cpp.disabled index 74263cc..7f1f5e7 100644 --- a/Template Project/src/i2c_scanner.cpp.disabled +++ b/Template Project/src/i2c_scanner.cpp.disabled @@ -1,4 +1,4 @@ -#include +#include // -------------------------------------- // i2c_scanner // diff --git a/Template Project/src/rtc_demo.cpp b/Template Project/src/rtc_demo.cpp new file mode 100644 index 0000000..0fdadee --- /dev/null +++ b/Template Project/src/rtc_demo.cpp @@ -0,0 +1,8 @@ +#include +void setup() { + ESPMega_begin(); +} + +void loop() { + delay(5000); +} \ No newline at end of file