add code comment

This commit is contained in:
Siwat Sirichai 2023-01-22 23:09:48 +07:00
parent a0290e715e
commit 1097e8df1d
8 changed files with 286 additions and 27 deletions

View file

@ -0,0 +1,72 @@
#include <ESPMegaPRO.h>
uint8_t inputBufferA;
uint8_t inputBufferB;
PCF8574 inputBankA(INPUT_BANK_A_ADDRESS);
PCF8574 inputBankB(INPUT_BANK_B_ADDRESS);
Adafruit_PWMServoDriver pwmBank = Adafruit_PWMServoDriver(0x5F);
void ESPMega_begin()
{
Wire.begin(14, 33);
inputBankA.begin();
inputBankB.begin();
pwmBank.begin();
#ifdef USE_INTERRUPT
attachInterrupt(digitalPinToInterrupt(INPUT_BANK_A_INTERRUPT),refreshInputBankA,FALLING);
attachInterrupt(digitalPinToInterrupt(INPUT_BANK_B_INTERRUPT),refreshInputBankB,FALLING);
#endif
}
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
return ((inputBufferA>>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
}
return false;
}
void ESPMega_analogWrite(int id, int value)
{
pwmBank.setPin(id, value);
}
void ESPMega_digitalWrite(int id, bool value)
{
if(value)
pwmBank.setPin(id, 4095);
else
pwmBank.setPin(id, 0);
}
void IRAM_ATTR refreshInputBankA()
{
inputBufferA = inputBankA.read8();
}
void IRAM_ATTR refreshInputBankB()
{
inputBufferB = inputBankB.read8();
}

View file

@ -0,0 +1,61 @@
#ifndef ESPMEGA
#define ESPMEGA
#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
#include <PCF8574.h>
#define INPUT_BANK_A_ADDRESS 0x20
#define INPUT_BANK_B_ADDRESS 0x21
#define OUTPUT_BANK_ADDRESS 0x21
#define EEPROM_ADDRESS 0x22
//#define USE_INTERRUPT
//#define INPUT_BANK_A_INTERRUPT 35
//#define INPUT_BANK_B_INTERRUPT 39
/**
* Initiate ESPMega PRO Internal Components
*
* This function will initiate the PWM bank, Input banks, and the EEPROM.
*/
void ESPMega_begin();
/**
* Run ESPMega PRO Internal Routines
*
* This function must be called regularly for the correct
* operation of the ESPMega!
*/
void ESPMega_loop();
/**
* Read one of the ESPMega's Digital Input pins (I0-I15)
*
* @param id The number of the pin to be read
* @return The state of the pin (HIGH/LOW)
*/
bool ESPMega_digitalRead(int id);
/**
* Write a pulse wave modulated signal to one of the ESPMega's
* PWM pins (P0-P15)
*
* @param id The number of the pin to write to
* @param value the "HIGH" duty cycle of the PWM wave (0-4095)
*/
void ESPMega_analogWrite(int id, int value);
/**
* Write a digital signal to one of the ESPMega's
* PWM pins (P0-P15)
*
* @param id The number of the pin to write to
* @param value the new state for the pin (HIGH/LOW)
*/
void ESPMega_digitalWrite(int id, bool value);
void IRAM_ATTR refreshInputBankA();
void IRAM_ATTR refreshInputBankB();
#endif