Upload files to "src/i2c/esp32s3_dual_i2c"

This commit is contained in:
Ghassan Yusuf 2025-06-21 14:29:53 +03:00
parent 36185974eb
commit 7e11450076
3 changed files with 126 additions and 46 deletions

View File

@ -1,47 +1,66 @@
#include <Wire.h> #include <Wire.h>
#include <Adafruit_GFX.h> #include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h> #include <Adafruit_SSD1306.h>
#define OLED_WIDTH 128 #define OLED_WIDTH 128
#define OLED_HEIGHT 32 #define OLED_HEIGHT 32
#define OLED_RESET -1 #define OLED_RESET -1
#define OLED_ADDR 0x3C #define OLED_ADDR 0x3C
// Master bus 1 (OLED): SDA 2, SCL 3 // Master bus 1 (OLED): SDA 2, SCL 3
#define SDA_1 2 #define SDA_1 2
#define SCL_1 3 #define SCL_1 3
// Master bus 2 (just for demo, not slave): SDA 4, SCL 5 // Master bus 2 (just for demo, not slave): SDA 4, SCL 5
#define SDA_2 4 #define SDA_2 4
#define SCL_2 5 #define SCL_2 5
TwoWire I2Cone = TwoWire(0); // Master I2C
Adafruit_SSD1306 display(OLED_WIDTH, OLED_HEIGHT, &I2Cone, OLED_RESET); TwoWire I2Cone = TwoWire(0);
TwoWire I2Ctwo = TwoWire(1); // OLED Display
Adafruit_SSD1306 display(OLED_WIDTH, OLED_HEIGHT, &I2Cone, OLED_RESET);
void setup() {
Serial.begin(115200); // Slave I2C
TwoWire I2Ctwo = TwoWire(1);
// Initialize first I2C bus (OLED)
I2Cone.begin(SDA_1, SCL_1, 100000); // NEW Datatype For State
if(!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) { enum _state { NOT_READY, READY, SENDING };
Serial.println("OLED allocation failed");
while(1); // Variables
} // _state state = NOT_READY;
display.clearDisplay(); _state state = READY;
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0); void setup() {
display.println("Hello, ESP32-S3!");
display.display(); Serial.begin(115200);
Serial.println();
// Initialize second I2C bus (just for demo, not slave) Serial.println("ESP32S3 System Started !");
I2Ctwo.begin(0x55, SDA_2, SCL_2, 100000);
// You could scan for devices or use another I2C device here // Initialize first I2C bus (OLED)
} I2Cone.begin(SDA_1, SCL_1, 100000);
void loop() { // Starting OLED
// Nothing to do here for this simple example if(!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) {
delay(1000); 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);
} }

View File

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

View File

@ -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<datalength; i++) {
message += char(I2Ctwo.read());
}
// Printe Received Message
Serial.println("Message : " + message);
}
}
void i2c_request() {
if(state == READY) {
I2Ctwo.write(char('Y'));
Serial.println("Requested Data : Ready");
state = SENDING;
} else if(state == SENDING) {
uint8_t data[6] = { 'H', 'E', 'L', 'L', 'O', 'W' };
for(int i=0; i<6; i++) {
I2Ctwo.write(data[i]);
Serial.print(data[i]);
}
Serial.println("Requested Data : Ready -> Sending");
state = NOT_READY;
} else {
I2Ctwo.write(char('N'));
Serial.println("Requested Data : Not Ready");
state = NOT_READY;
}
}