diff --git a/Template Project/.vscode/settings.json b/Template Project/.vscode/settings.json index de67d8b..9de6690 100644 --- a/Template Project/.vscode/settings.json +++ b/Template Project/.vscode/settings.json @@ -7,6 +7,9 @@ "unordered_set": "cpp", "vector": "cpp", "string_view": "cpp", - "initializer_list": "cpp" - } + "initializer_list": "cpp", + "adafruit_ads1x15.h": "c" + }, + "cmake.configureOnOpen": true, + "cmake.sourceDirectory": "D:/Git/ESPMegaPRO-v3-SDK/Template Project/.pio/libdeps/wt32-eth01/Adafruit BusIO" } \ No newline at end of file diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaPRO.cpp b/Template Project/lib/ESPMegaPRO/ESPMegaPRO.cpp index 5f04603..2db5186 100644 --- a/Template Project/lib/ESPMegaPRO/ESPMegaPRO.cpp +++ b/Template Project/lib/ESPMegaPRO/ESPMegaPRO.cpp @@ -5,57 +5,86 @@ uint8_t inputBufferB; PCF8574 inputBankA(INPUT_BANK_A_ADDRESS); PCF8574 inputBankB(INPUT_BANK_B_ADDRESS); -Adafruit_PWMServoDriver pwmBank = Adafruit_PWMServoDriver(0x5F); +Adafruit_PWMServoDriver pwmBank = Adafruit_PWMServoDriver(PWM_BANK_ADDRESS); +FRAM ESPMega_FRAM; + +#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() { Wire.begin(14, 33); - inputBankA.begin(); inputBankB.begin(); pwmBank.begin(); + ESPMega_FRAM.begin(FRAM_ADDRESS); + // ESPMegaPRO v3 use the PWMBank to drive Half Bridge + // Push Pull Output is required. + pwmBank.setOutputMode(true); - #ifdef USE_INTERRUPT - attachInterrupt(digitalPinToInterrupt(INPUT_BANK_A_INTERRUPT),refreshInputBankA,FALLING); - attachInterrupt(digitalPinToInterrupt(INPUT_BANK_B_INTERRUPT),refreshInputBankB,FALLING); - #endif +#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 + +#ifdef ANALOG_CARD_ENABLE + analogInputBankA.begin(ANALOG_INPUT_BANK_A_ADDRESS); + analogInputBankB.begin(ANALOG_INPUT_BANK_B_ADDRESS); + DAC0.begin(); + DAC1.begin(); + DAC2.begin(); + DAC3.begin(); +#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 >> (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 +#ifndef USE_INTERRUPT + refreshInputBankB(); // Only poll if interrupt is not enabled +#endif + 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); } void ESPMega_digitalWrite(int id, bool value) { - if(value) + if (value) pwmBank.setPin(id, 4095); else pwmBank.setPin(id, 0); @@ -69,4 +98,81 @@ void IRAM_ATTR refreshInputBankA() void IRAM_ATTR refreshInputBankB() { inputBufferB = inputBankB.read8(); -} \ No newline at end of file +} + +rtctime_t ESPMega_getTime() +{ + 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; + time.year = timeElement.Year + 1970; + return time; +} + +void ESPMega_setTime(int hours, int minutes, int seconds, int day, int month, int year) +{ + tmElements_t timeElement; + timeElement.Hour = hours; + timeElement.Minute = minutes; + timeElement.Second = seconds; + timeElement.Day = day; + timeElement.Month = month; + timeElement.Year = year - 1970; + RTC.write(timeElement); +} + +#ifdef ANALOG_CARD_ENABLE +int16_t ESPMega_analogRead(int id) +{ + if (id >= 0 && id <= 3) + return analogInputBankA.readADC_SingleEnded(3 - id); + else if (id >= 4 && id <= 7) + 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; + } +} + +bool ESPMega_updateTimeFromNTP() +{ + struct tm timeinfo; + if (getLocalTime(&timeinfo)) + { + 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; + } + return false; +} + +#endif \ No newline at end of file diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaPRO.h b/Template Project/lib/ESPMegaPRO/ESPMegaPRO.h index 054af53..c11688e 100644 --- a/Template Project/lib/ESPMegaPRO/ESPMegaPRO.h +++ b/Template Project/lib/ESPMegaPRO/ESPMegaPRO.h @@ -1,19 +1,44 @@ #ifndef ESPMEGA #define ESPMEGA - +#define ANALOG_CARD_ENABLE #include #include #include #include +#include +#include +#include +#include +#ifdef ANALOG_CARD_ENABLE +#include +#include +#endif -#define INPUT_BANK_A_ADDRESS 0x20 -#define INPUT_BANK_B_ADDRESS 0x21 -#define OUTPUT_BANK_ADDRESS 0x21 -#define EEPROM_ADDRESS 0x22 +#define INPUT_BANK_A_ADDRESS 0x21 +#define INPUT_BANK_B_ADDRESS 0x22 +#define PWM_BANK_ADDRESS 0x5F +#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 FRAM_ADDRESS 0x56 //#define USE_INTERRUPT -//#define INPUT_BANK_A_INTERRUPT 35 -//#define INPUT_BANK_B_INTERRUPT 39 +#define INPUT_BANK_A_INTERRUPT 36 +#define INPUT_BANK_B_INTERRUPT 39 +extern FRAM ESPMega_FRAM; +#define ESPMega_configNTP configTime +struct rtctime_t { + uint8_t hours; + uint8_t minutes; + uint8_t seconds; + uint8_t day; + uint8_t month; + uint16_t year; +}; /** * Initiate ESPMega PRO Internal Components @@ -58,4 +83,48 @@ void ESPMega_digitalWrite(int id, bool value); void IRAM_ATTR refreshInputBankA(); void IRAM_ATTR refreshInputBankB(); +/** + * Get time from the onboard RTC as a struct + * + * @return Time Element Struct +*/ +rtctime_t ESPMega_getTime(); +/** + * Set the onboard RTC's time + * + * @param hours + * @param minutes + * @param seconds + * @param day Day of the month + * @param month Month in numerical form + * @param year Years in AD +*/ +void ESPMega_setTime(int hours,int minutes, int seconds, int day, int month, int year); + +/** + * Update the onboard RTC's time + * by using time from the NTP server + * configured with ESPMega_configNTP(); + * @return true when updated successfully. +*/ +bool ESPMega_updateTimeFromNTP(); + +#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) +*/ +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 66bd033..fc695a6 100644 --- a/Template Project/platformio.ini +++ b/Template Project/platformio.ini @@ -12,5 +12,15 @@ platform = espressif32 board = wt32-eth01 framework = arduino +;siwats/ESPMegaPROR3@1.0.1 lib_deps = adafruit/Adafruit PWM Servo Driver Library@^2.4.1 - robtillaart/PCF8574@^0.3.7 \ No newline at end of file + adafruit/Adafruit ADS1X15@^2.4.0 + adafruit/Adafruit BusIO + robtillaart/PCF8574@^0.3.7 + arduino-libraries/Arduino_BuiltIn@^1.0.0 + SPI@^2.0.0 + robtillaart/MCP4725@^0.3.7 + robtillaart/FRAM_I2C@^0.6.1 + paulstoffregen/Time@^1.6.1 + paulstoffregen/DS1307RTC@0.0.0-alpha+sha.c2590c0033 +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..7627678 --- /dev/null +++ b/Template Project/src/analogdemo.cpp @@ -0,0 +1,22 @@ +#include +#define ADC + +void setup() +{ + Serial.begin(115200); + ESPMega_begin(); +} + +void loop() +{ + #ifdef DAC + for(int i=0;i<4096;i++) { + ESPMega_dacWrite(0,i); + double dac_val_sine = 2047.5*sin(i*2*PI/2047.5)+2047.5; + ESPMega_dacWrite(1,(int)dac_val_sine); + } + #endif + #ifdef ADC + Serial.printf("A0: %d",ESPMega_analogRead(0)); + #endif +} \ No newline at end of file diff --git a/Template Project/src/fram_demo.cpp.disabled b/Template Project/src/fram_demo.cpp.disabled new file mode 100644 index 0000000..bd77c2b --- /dev/null +++ b/Template Project/src/fram_demo.cpp.disabled @@ -0,0 +1,37 @@ +#include +void setup() +{ + ESPMega_begin(); + Serial.begin(115200); + uint8_t a = 35; + uint8_t b = 42; + uint8_t c = 66; + uint8_t d = 251; + + // ESPMega_FRAM.write8(0,a); + // ESPMega_FRAM.write8(1,b); + // ESPMega_FRAM.write8(2,c); + // ESPMega_FRAM.write8(3,d); + + uint8_t e = ESPMega_FRAM.read8(0); + uint8_t f = ESPMega_FRAM.read8(1); + uint8_t g = ESPMega_FRAM.read8(2); + uint8_t h = ESPMega_FRAM.read8(3); + Serial.println(e); + Serial.println(f); + Serial.println(g); + Serial.println(h); + char ll[2000] = "Everyone has a dream they strive to achieve, and so does the musically talented Kanon Shibuya. However, due to her stage fright, Kanon fails to make it into Yuigaoka Girls' High School's music program and instead ends up in the general curriculum. Even though Kanon makes the conscious decision to quit music altogether, her classmate Tang Keke rekindles Kanon's passion for music with her own: a passion for school idols."; + + // ESPMega_FRAM.write(4,(uint8_t*)ll,2000); + + char llr[2000]; + ESPMega_FRAM.read(4,(uint8_t*)llr,2000); + Serial.println(llr); + + +} + +void loop() +{ +} \ No newline at end of file diff --git a/Template Project/src/i2c_scanner.cpp.disabled b/Template Project/src/i2c_scanner.cpp.disabled index a22a3a5..be6d04e 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 // @@ -33,10 +33,10 @@ void setup() { + Wire.setClock(50000); Wire.begin(14,33); - - Serial.begin(9600); - while (!Serial); // Leonardo: wait for serial monitor + + Serial.begin(115200); // Leonardo: wait for serial monitor Serial.println("\nI2C Scanner"); } diff --git a/Template Project/src/input_test.cpp.disabled b/Template Project/src/input_test.cpp.disabled new file mode 100644 index 0000000..c2e2915 --- /dev/null +++ b/Template Project/src/input_test.cpp.disabled @@ -0,0 +1,17 @@ +#include "ESPMegaPRO.h" + +void setup() { + ESPMega_begin(); + Serial.begin(115200); +} + +void loop() { + if(ESPMega_digitalRead(11)) { + ESPMega_digitalWrite(8, HIGH); + Serial.println("11HIGH"); + } else { + ESPMega_digitalWrite(8, LOW); + Serial.println("11LOW"); + } + ESPMega_loop(); +} \ No newline at end of file diff --git a/Template Project/src/main.cpp b/Template Project/src/main.cpp deleted file mode 100644 index 98f2bb3..0000000 --- a/Template Project/src/main.cpp +++ /dev/null @@ -1,9 +0,0 @@ -#include - -void setup() { - ESPMega_begin(); -} - -void loop() { - ESPMega_loop(); -} \ No newline at end of file diff --git a/Template Project/src/rtc_demo.cpp.disabled b/Template Project/src/rtc_demo.cpp.disabled new file mode 100644 index 0000000..9e8990e --- /dev/null +++ b/Template Project/src/rtc_demo.cpp.disabled @@ -0,0 +1,30 @@ +#include +#include + +uint8_t utc_offset = 7; + +IPAddress IP(192, 168, 0, 241); +IPAddress SUBNET(255, 255, 255, 0); +IPAddress GATEWAY(192, 168, 0, 1); +IPAddress DNS(10, 192, 1, 1); + +void setup() +{ + ESPMega_begin(); + Serial.begin(115200); + ETH.begin(); + ETH.config(IP, GATEWAY, SUBNET, DNS, DNS); + delay(5000); + char ntp[19]; + DNS.toString().toCharArray(ntp, 19); + ESPMega_configNTP(utc_offset * 3600, 0, ntp); + ESPMega_updateTimeFromNTP(); + //ESPMega_setTime(0,10,52,9,11,2008); +} + +void loop() +{ + rtctime_t time = ESPMega_getTime(); + Serial.printf("RTC: %02d:%02d:%02d %02d/%02d/%04d\n", time.hours, time.minutes, time.seconds, time.day, time.month, time.year); + delay(1000); +} \ No newline at end of file