From 7e11450076c6528e5b9ec6a4c318defdafdf945d Mon Sep 17 00:00:00 2001 From: Ghassan Yusuf Date: Sat, 21 Jun 2025 14:29:53 +0300 Subject: [PATCH] Upload files to "src/i2c/esp32s3_dual_i2c" --- src/i2c/esp32s3_dual_i2c/esp32s3_dual_i2c.ino | 111 ++++++++++-------- src/i2c/esp32s3_dual_i2c/functions.ino | 9 ++ src/i2c/esp32s3_dual_i2c/i2c_functions.ino | 52 ++++++++ 3 files changed, 126 insertions(+), 46 deletions(-) create mode 100644 src/i2c/esp32s3_dual_i2c/functions.ino create mode 100644 src/i2c/esp32s3_dual_i2c/i2c_functions.ino diff --git a/src/i2c/esp32s3_dual_i2c/esp32s3_dual_i2c.ino b/src/i2c/esp32s3_dual_i2c/esp32s3_dual_i2c.ino index cb1d57b..ee750ce 100644 --- a/src/i2c/esp32s3_dual_i2c/esp32s3_dual_i2c.ino +++ b/src/i2c/esp32s3_dual_i2c/esp32s3_dual_i2c.ino @@ -1,47 +1,66 @@ -#include -#include -#include - -#define OLED_WIDTH 128 -#define OLED_HEIGHT 32 -#define OLED_RESET -1 -#define OLED_ADDR 0x3C - -// Master bus 1 (OLED): SDA 2, SCL 3 -#define SDA_1 2 -#define SCL_1 3 - -// Master bus 2 (just for demo, not slave): SDA 4, SCL 5 -#define SDA_2 4 -#define SCL_2 5 - -TwoWire I2Cone = TwoWire(0); -Adafruit_SSD1306 display(OLED_WIDTH, OLED_HEIGHT, &I2Cone, OLED_RESET); - -TwoWire I2Ctwo = TwoWire(1); - -void setup() { - Serial.begin(115200); - - // Initialize first I2C bus (OLED) - I2Cone.begin(SDA_1, SCL_1, 100000); - if(!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) { - Serial.println("OLED allocation failed"); - while(1); - } - display.clearDisplay(); - display.setTextSize(1); - display.setTextColor(SSD1306_WHITE); - display.setCursor(0,0); - display.println("Hello, ESP32-S3!"); - display.display(); - - // Initialize second I2C bus (just for demo, not slave) - I2Ctwo.begin(0x55, SDA_2, SCL_2, 100000); - // You could scan for devices or use another I2C device here -} - -void loop() { - // Nothing to do here for this simple example - delay(1000); +#include +#include +#include + +#define OLED_WIDTH 128 +#define OLED_HEIGHT 32 +#define OLED_RESET -1 +#define OLED_ADDR 0x3C + +// Master bus 1 (OLED): SDA 2, SCL 3 +#define SDA_1 2 +#define SCL_1 3 + +// Master bus 2 (just for demo, not slave): SDA 4, SCL 5 +#define SDA_2 4 +#define SCL_2 5 + +// Master I2C +TwoWire I2Cone = TwoWire(0); + +// OLED Display +Adafruit_SSD1306 display(OLED_WIDTH, OLED_HEIGHT, &I2Cone, OLED_RESET); + +// Slave I2C +TwoWire I2Ctwo = TwoWire(1); + +// NEW Datatype For State +enum _state { NOT_READY, READY, SENDING }; + +// Variables +// _state state = NOT_READY; +_state state = READY; + + +void setup() { + + Serial.begin(115200); + Serial.println(); + Serial.println("ESP32S3 System Started !"); + + // Initialize first I2C bus (OLED) + I2Cone.begin(SDA_1, SCL_1, 100000); + + // Starting OLED + if(!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) { + Serial.println("OLED allocation failed"); + while(1); + } + + // Display OLED Message + oled_write("Hello, ESP32-S3!"); + + // On Receive Data + I2Ctwo.onReceive(i2c_received); + I2Ctwo.onRequest(i2c_request); + + // Initialize second I2C bus (just for demo, not slave) + I2Ctwo.begin(0x55, SDA_2, SCL_2, 100000); + // You could scan for devices or use another I2C device here + +} + +void loop() { + // Nothing to do here for this simple example + delay(1000); } \ No newline at end of file diff --git a/src/i2c/esp32s3_dual_i2c/functions.ino b/src/i2c/esp32s3_dual_i2c/functions.ino new file mode 100644 index 0000000..8b7235d --- /dev/null +++ b/src/i2c/esp32s3_dual_i2c/functions.ino @@ -0,0 +1,9 @@ + // OLED Display + void oled_write(String message) { + display.clearDisplay(); + display.setTextSize(1); + display.setTextColor(SSD1306_WHITE); + display.setCursor(0, 0); + display.println(message); + display.display(); + } \ No newline at end of file diff --git a/src/i2c/esp32s3_dual_i2c/i2c_functions.ino b/src/i2c/esp32s3_dual_i2c/i2c_functions.ino new file mode 100644 index 0000000..9143968 --- /dev/null +++ b/src/i2c/esp32s3_dual_i2c/i2c_functions.ino @@ -0,0 +1,52 @@ + + void i2c_received(int data) { + + // Read If Available + if(I2Ctwo.available()) { + + // Calculate Data Length + uint8_t datalength = I2Ctwo.read(); + + // Creating Data Array Based On Data Length + uint8_t data[datalength]; + + // Data Buffer + String message; + + // Assembling Message Data + for(int i=0; i Sending"); + state = NOT_READY; + + } else { + I2Ctwo.write(char('N')); + Serial.println("Requested Data : Not Ready"); + state = NOT_READY; + } + + } \ No newline at end of file