analog input capability

This commit is contained in:
Siwat Sirichai 2023-08-26 23:16:51 +07:00
parent 81f0e80f8c
commit 94443393d0
5 changed files with 68 additions and 19 deletions

View File

@ -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()
{
@ -19,37 +23,42 @@ void ESPMega_begin()
// Push Pull Output is required.
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);
@ -76,3 +85,14 @@ void IRAM_ATTR refreshInputBankB()
{
inputBufferB = inputBankB.read8();
}
#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

View File

@ -1,16 +1,21 @@
#ifndef ESPMEGA
#define ESPMEGA
#define ANALOG_CARD_ENABLE
#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
#include <PCF8574.h>
#ifdef ANALOG_CARD_ENABLE
#include <Adafruit_ADS1X15.h>
#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

View File

@ -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

View File

@ -0,0 +1,10 @@
#include <ESPMegaPRO.h>
void setup() {
ESPMega_begin();
Serial.begin(115200);
}
void loop() {
int a0 = ESPMega_analogRead(0);
Serial.println(a0);
}