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

@ -15,30 +15,49 @@
#define SDA_2 4 #define SDA_2 4
#define SCL_2 5 #define SCL_2 5
// Master I2C
TwoWire I2Cone = TwoWire(0); TwoWire I2Cone = TwoWire(0);
// OLED Display
Adafruit_SSD1306 display(OLED_WIDTH, OLED_HEIGHT, &I2Cone, OLED_RESET); Adafruit_SSD1306 display(OLED_WIDTH, OLED_HEIGHT, &I2Cone, OLED_RESET);
// Slave I2C
TwoWire I2Ctwo = TwoWire(1); TwoWire I2Ctwo = TwoWire(1);
// NEW Datatype For State
enum _state { NOT_READY, READY, SENDING };
// Variables
// _state state = NOT_READY;
_state state = READY;
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
Serial.println();
Serial.println("ESP32S3 System Started !");
// Initialize first I2C bus (OLED) // Initialize first I2C bus (OLED)
I2Cone.begin(SDA_1, SCL_1, 100000); I2Cone.begin(SDA_1, SCL_1, 100000);
// Starting OLED
if(!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) { if(!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) {
Serial.println("OLED allocation failed"); Serial.println("OLED allocation failed");
while(1); while(1);
} }
display.clearDisplay();
display.setTextSize(1); // Display OLED Message
display.setTextColor(SSD1306_WHITE); oled_write("Hello, ESP32-S3!");
display.setCursor(0,0);
display.println("Hello, ESP32-S3!"); // On Receive Data
display.display(); I2Ctwo.onReceive(i2c_received);
I2Ctwo.onRequest(i2c_request);
// Initialize second I2C bus (just for demo, not slave) // Initialize second I2C bus (just for demo, not slave)
I2Ctwo.begin(0x55, SDA_2, SCL_2, 100000); I2Ctwo.begin(0x55, SDA_2, SCL_2, 100000);
// You could scan for devices or use another I2C device here // You could scan for devices or use another I2C device here
} }
void loop() { void loop() {

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;
}
}