Hi,
I'm currently working on a similar project.
The best is to write a driver for the board - in this case you can cover all the specialieties of your board.
Use a well tested PCF8574 arduino library which covers the ESP32 too - some older libriaries are crashing the ESP!
I prefer PCF8574 library from Renzo Mischianti - works well with a lot of boards - Thanks to Renzo for his work!
small sample snippet from my driver:
#include <PCF8574.h>
#define I2C_ADDR_INPUT 0x22 //--- Addr of Input PCF8574
#define I2C_ADDR_OUTPUT 0x24 //--- Addr of Output PCF8574
//--- Init PCF8574 Inputs and outputs
PCF8574 D_INPUTS(I2C_ADDR_INPUT,I2C_SDA,I2C_SCL);
PCF8574 D_OUTPUTS(I2C_ADDR_OUTPUT,I2C_SDA,I2C_SCL);
void setup() {
//--- Set the pins from the Input-PCF8574 to INPUT
for (uint8_t i = 0; i <= 7; i++) {
D_INPUTS.pinMode(i, INPUT);
}
D_INPUTS.begin();
//--- Set the pins from the OUTPUT-PCF8574 to OUTPUT and HIGH
for (uint8_t i = 0; i <= 5; i++) {
D_OUTPUTS.pinMode(i, OUTPUT);
//--- relays are active LOW
D_OUTPUTS.digitalWrite(i, HIGH);
}
D_OUTPUTS.begin();
}
void updateInputBuffers()
{
for (int i = 0; i < NUM_DISCRETE_INPUT; i++)
{
if (bool_input[i/8][i%8] != NULL) {
*bool_input[i/8][i%8] = D_INPUTS.digitalRead(pinMask_DIN[i]);
}
}
}
void updateOutputBuffers()
{
for (int i = 0; i < NUM_DISCRETE_OUTPUT; i++)
{
if (bool_output[i/8][i%8] != NULL){
D_OUTPUTS.digitalWrite(pinMask_DOUT[i], !(*bool_output[i/8][i%8]));
}
}
}
Then you need an entry in the hals.json like this sample:
"myboard": {
"core": "esp32:esp32",
"default_ain": "36, 39",
"default_aout": "26, 25",
"default_din": "P0, P1, P2, P3, P4, P5, P6, P7",
"default_dout": "P0, P1, P2, P3, P4, P5",
"define": "BOARD_ESP32",
"last_update": 0,
"platform": "esp32:esp32:esp32:UploadSpeed=921600,PartitionScheme=huge_app,FlashMode=qio,FlashFreq=80",
"source": "myboard.cpp",
"version": "3.0.4"
},and you need a myboard.hal file with an entry like this sample:
ESP32 WROOM esp32:esp32:esp32, BOARD_ESP32
Thats it.
Be aware, the code snippets are not complete - take a look on the other driver files to get an idea how to program a driver !
Cheers,
Dieter