This commit is contained in:
Siwat Sirichai 2023-10-12 19:43:28 +07:00
commit abeaf96288
10 changed files with 328 additions and 43 deletions

View File

@ -7,6 +7,9 @@
"unordered_set": "cpp", "unordered_set": "cpp",
"vector": "cpp", "vector": "cpp",
"string_view": "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"
} }

View File

@ -5,57 +5,86 @@ uint8_t inputBufferB;
PCF8574 inputBankA(INPUT_BANK_A_ADDRESS); PCF8574 inputBankA(INPUT_BANK_A_ADDRESS);
PCF8574 inputBankB(INPUT_BANK_B_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() void ESPMega_begin()
{ {
Wire.begin(14, 33); Wire.begin(14, 33);
inputBankA.begin(); inputBankA.begin();
inputBankB.begin(); inputBankB.begin();
pwmBank.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 #ifdef USE_INTERRUPT
attachInterrupt(digitalPinToInterrupt(INPUT_BANK_A_INTERRUPT),refreshInputBankA,FALLING); pinMode(INPUT_BANK_A_INTERRUPT, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(INPUT_BANK_B_INTERRUPT),refreshInputBankB,FALLING); pinMode(INPUT_BANK_B_INTERRUPT, INPUT_PULLUP);
#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);
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) bool ESPMega_digitalRead(int id)
{ {
if (id >= 0 && id <= 7) if (id >= 0 && id <= 7)
{ {
#ifndef USE_INTERRUPT #ifndef USE_INTERRUPT
refreshInputBankA(); //Only poll if interrupt is not enabled refreshInputBankA(); // Only poll if interrupt is not enabled
#endif #endif
return ((inputBufferA>>id)&1); //Extract bit from buffer return ((inputBufferA >> (7 - id)) & 1); // Extract bit from buffer
} }
if (id >= 8 && id <= 15) if (id >= 8 && id <= 15)
{ {
id-=8;
#ifndef USE_INTERRUPT #ifndef USE_INTERRUPT
refreshInputBankB(); //Only poll if interrupt is not enabled refreshInputBankB(); // Only poll if interrupt is not enabled
#endif #endif
if (id >= 8 && id <= 11)
return ((inputBufferB>>id)&1); //Extract bit from buffer return ((inputBufferB >> (15 - id)) & 1); // Extract bit from buffer
else if (id >= 12 && id <= 15)
return ((inputBufferB >> (id - 12)) & 1);
} }
return false; return false;
} }
void ESPMega_analogWrite(int id, int value) 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); pwmBank.setPin(id, value);
} }
void ESPMega_digitalWrite(int id, bool value) void ESPMega_digitalWrite(int id, bool value)
{ {
if(value) if (value)
pwmBank.setPin(id, 4095); pwmBank.setPin(id, 4095);
else else
pwmBank.setPin(id, 0); pwmBank.setPin(id, 0);
@ -69,4 +98,81 @@ void IRAM_ATTR refreshInputBankA()
void IRAM_ATTR refreshInputBankB() void IRAM_ATTR refreshInputBankB()
{ {
inputBufferB = inputBankB.read8(); inputBufferB = inputBankB.read8();
} }
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

View File

@ -1,19 +1,44 @@
#ifndef ESPMEGA #ifndef ESPMEGA
#define ESPMEGA #define ESPMEGA
#define ANALOG_CARD_ENABLE
#include <Arduino.h> #include <Arduino.h>
#include <Wire.h> #include <Wire.h>
#include <Adafruit_PWMServoDriver.h> #include <Adafruit_PWMServoDriver.h>
#include <PCF8574.h> #include <PCF8574.h>
#include <FRAM.h>
#include <TimeLib.h>
#include <DS1307RTC.h>
#include <time.h>
#ifdef ANALOG_CARD_ENABLE
#include <Adafruit_ADS1X15.h>
#include <MCP4725.h>
#endif
#define INPUT_BANK_A_ADDRESS 0x20 #define INPUT_BANK_A_ADDRESS 0x21
#define INPUT_BANK_B_ADDRESS 0x21 #define INPUT_BANK_B_ADDRESS 0x22
#define OUTPUT_BANK_ADDRESS 0x21 #define PWM_BANK_ADDRESS 0x5F
#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 FRAM_ADDRESS 0x56
//#define USE_INTERRUPT //#define USE_INTERRUPT
//#define INPUT_BANK_A_INTERRUPT 35 #define INPUT_BANK_A_INTERRUPT 36
//#define INPUT_BANK_B_INTERRUPT 39 #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 * Initiate ESPMega PRO Internal Components
@ -58,4 +83,48 @@ void ESPMega_digitalWrite(int id, bool value);
void IRAM_ATTR refreshInputBankA(); void IRAM_ATTR refreshInputBankA();
void IRAM_ATTR refreshInputBankB(); 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 #endif

View File

@ -12,5 +12,15 @@
platform = espressif32 platform = espressif32
board = wt32-eth01 board = wt32-eth01
framework = arduino framework = arduino
;siwats/ESPMegaPROR3@1.0.1
lib_deps = adafruit/Adafruit PWM Servo Driver Library@^2.4.1 lib_deps = adafruit/Adafruit PWM Servo Driver Library@^2.4.1
robtillaart/PCF8574@^0.3.7 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

View File

@ -0,0 +1,22 @@
#include <ESPMegaPRO.h>
#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
}

View File

@ -0,0 +1,37 @@
#include <ESPMegaPRO.h>
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()
{
}

View File

@ -1,4 +1,4 @@
#include <Arduino.h> #include <ESPMegaPRO.h>
// -------------------------------------- // --------------------------------------
// i2c_scanner // i2c_scanner
// //
@ -33,10 +33,10 @@
void setup() void setup()
{ {
Wire.setClock(50000);
Wire.begin(14,33); Wire.begin(14,33);
Serial.begin(9600); Serial.begin(115200); // Leonardo: wait for serial monitor
while (!Serial); // Leonardo: wait for serial monitor
Serial.println("\nI2C Scanner"); Serial.println("\nI2C Scanner");
} }

View File

@ -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();
}

View File

@ -1,9 +0,0 @@
#include <ESPMegaPRO.h>
void setup() {
ESPMega_begin();
}
void loop() {
ESPMega_loop();
}

View File

@ -0,0 +1,30 @@
#include <ESPMegaPRO.h>
#include <ETH.h>
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);
}