From 4931548171921bde7bc69aa5c9ba034bb3392e68 Mon Sep 17 00:00:00 2001 From: ghassan Date: Sat, 14 Jun 2025 22:08:32 +0300 Subject: [PATCH] latest update --- src/game/i2c_master_test/i2c_master_test.ino | 257 +++++++++++++++++- .../esp32c3_master_i2c/esp32c3_master_i2c.ino | 62 +++++ src/i2c/esp32s3_dual_i2c/esp32s3_dual_i2c.ino | 47 ++++ .../i2c_blue_joystic/i2c_blue_joystic.ino | 56 +++- src/joystic/i2c_red_joystic/header.h | 40 +++ 5 files changed, 441 insertions(+), 21 deletions(-) create mode 100644 src/i2c/esp32c3_master_i2c/esp32c3_master_i2c.ino create mode 100644 src/i2c/esp32s3_dual_i2c/esp32s3_dual_i2c.ino create mode 100644 src/joystic/i2c_red_joystic/header.h diff --git a/src/game/i2c_master_test/i2c_master_test.ino b/src/game/i2c_master_test/i2c_master_test.ino index bf15b49..698cb5b 100644 --- a/src/game/i2c_master_test/i2c_master_test.ino +++ b/src/game/i2c_master_test/i2c_master_test.ino @@ -1,30 +1,78 @@ + // INCLUDE WIRE #include - // Custom I2C pins for ESP32C3 - #define I2C_SDA_PIN 7 - #define I2C_SCL_PIN 6 + // Custom I2C PINS FOR ESP32C3 + // #define I2C_SDA_PIN 7 + // #define I2C_SCL_PIN 6 + // #define I2C_SDA_PIN 8 + // #define I2C_SCL_PIN 9 + + // LIST OF COMMANDS + #define COMMAND_DICE 0x01 + #define COMMAND_UPDATE 0x02 + #define COMMAND_PERMISSION 0x03 + #define COMMAND_DENY 0x04 + + // PLAYER + struct _player { + uint8_t x; + uint8_t y; + uint8_t h; + }; + + // MAP STATUS + struct _map { + _player red; + _player blue; + }; + + // MAP INSTANCE + _map MAP; + +//============================================================ +// +//============================================================ void setup() { Serial.begin(115200); - Wire.setPins(I2C_SDA_PIN, I2C_SCL_PIN); + // Wire.setPins(I2C_SDA_PIN, I2C_SCL_PIN); Wire.begin(); + delay(100); Serial.println("Master ready. Scanning for slaves..."); scanI2C(); } +//============================================================ +// +//============================================================ + void loop() { - if (Serial.available()) { - int value = Serial.parseInt(); - if (value >= 0 && value <= 255) { - Serial.print("Sending value: "); - Serial.println(value); - Wire.beginTransmission(0x10); // Change to 0x20 for the second slave - Wire.write(value); - Wire.endTransmission(); - } - } + + + Wire.beginTransmission(0x10); + Wire.write(2); + Wire.endTransmission(); + + delay(1000); + + // Questions For You + // uint8_t address = Question("Joystic Address ").toInt(); + // uint8_t command = Question("Command Code ").toInt(); + + // // Select Command You Where Looking For + // switch(command) { + // case 1: Dice(address); break; + // case 2: Update(address); break; + // case 3: Permission(address); break; + // case 4: Deny(address); break; + // } + } +//============================================================ +// +//============================================================ + void scanI2C() { byte error, address; int nDevices = 0; @@ -40,4 +88,183 @@ } if (nDevices == 0) Serial.println("No I2C devices found"); - } \ No newline at end of file + } + +//============================================================ +// +//============================================================ + + void send_i2c_data(uint8_t address, uint8_t command, uint8_t *data) { + + Serial.print ("Address : 0x"); + Serial.println(address, HEX); + Wire.beginTransmission(address); + + Serial.print ("Command : 0x"); + Serial.println(command, HEX); + Wire.write(command); + + size_t length = sizeof(data); + + Serial.print ("Data : "); + + for(int i=0; i<=length; i++) { + Serial.print("0x"); + Serial.print(data[i], HEX); + Wire.write(data[i]); + Serial.print(", "); + } + + Serial.println(); + Wire.endTransmission(); + + } + +//============================================================ +// +//============================================================ + + uint8_t Dice(uint8_t address) { + + // Print Data On Serial - Display Address + Serial.print("Address : 0x"); Serial.println(address, HEX); + + // Print Data On Serial - Display Command + Serial.print("Command : 0x"); Serial.println(COMMAND_DICE, HEX); + + // Sending The Command + Wire.beginTransmission(address); // Sending To Slave Address + Wire.write(COMMAND_DICE); // Sending Command - DiceRoll + Wire.endTransmission(); // Stop Transmission + + // Request Data From Slave + Wire.requestFrom(address, 1); // request 1 byte from slave + + // Dice Value + uint8_t value; + + // Method One + while(!Wire.available()); + value = Wire.read(); + + // Method Two + // if(Wire.available()) { + // value = Wire.read(); + // } + + // Stop Transmission + Wire.endTransmission(); + + // Print Value And Return Value + Serial.println("Dice Value : " + String(value)); + + // Return Value + return value; + + } + +//============================================================ +// +//============================================================ + + void Update(uint8_t address) { + + // Print Data On Serial - Display Address + Serial.print("Address : 0x"); Serial.println(address, HEX); + + // Print Data On Serial - Display Command + Serial.print("Command : 0x"); Serial.println(COMMAND_UPDATE, HEX); + + // Sending The Command + Wire.beginTransmission(address); // Sending To Slave Address + Wire.write(COMMAND_DICE); // Sending Command - DiceRoll + Wire.write(MAP.red.x); // Sending RED x Position + Wire.write(MAP.red.y); // Sending RED y Position + Wire.write(MAP.red.h); // Sending RED Health + Wire.write(MAP.blue.x); // Sending BLUE x Position + Wire.write(MAP.blue.y); // Sending BLUE y Position + Wire.write(MAP.blue.h); // Sending BLUE Health + Wire.endTransmission(); // Stop Transmission + + } + +//============================================================ +// +//============================================================ + + uint8_t* Permission(uint8_t address) { + + // Print Data On Serial - Display Address + Serial.print("Address : 0x"); Serial.println(address, HEX); + + // Print Data On Serial - Display Command + Serial.print("Command : 0x"); Serial.println(COMMAND_PERMISSION, HEX); + + // Sending The Command + Wire.beginTransmission(address); // Sending To Slave Address + Wire.write(COMMAND_PERMISSION); // Sending Command - Permission + Wire.endTransmission(); // Stop Transmission + + // Request Data From Slave + Wire.requestFrom(address, 7); // Request 7 Byte From Slave + + // Dice Value + uint8_t value[6]; + + // Method One + // while(!Wire.available()); + // value = Wire.read(); + + // Print Value And Return Value + Serial.println("Dice Value : "); + + // Method Two + if(Wire.available()) { + for(int i=0; i<6; i++) { + value[i] = Wire.read(); + Serial.println(" Value : " + String(value[i])); + } + } + + // Stop Transmission + Wire.endTransmission(); + + // Return Value + return value; + + } + +//============================================================ +// +//============================================================ + + void Deny(uint8_t address) { + + Serial.print ("Address : 0x"); Serial.println(address, HEX); + Serial.print("Command : 0x"); Serial.println(COMMAND_DENY, HEX); + + Wire.beginTransmission(address); + Wire.write(COMMAND_DENY); + Wire.endTransmission(); + + } + +//============================================================ +// +//============================================================ + + String Question(String Msg) { + + Serial.print(Msg + " : "); + while(!Serial.available()); + Msg = Serial.readStringUntil('\n'); + while(Serial.available()) { Serial.read(); } + Msg.trim(); + Serial.println(Msg); + return Msg; + + } + +//============================================================ +// +//============================================================ \ No newline at end of file diff --git a/src/i2c/esp32c3_master_i2c/esp32c3_master_i2c.ino b/src/i2c/esp32c3_master_i2c/esp32c3_master_i2c.ino new file mode 100644 index 0000000..50fdfb8 --- /dev/null +++ b/src/i2c/esp32c3_master_i2c/esp32c3_master_i2c.ino @@ -0,0 +1,62 @@ +#include +#include +#include + +#define SCREEN_WIDTH 128 +#define SCREEN_HEIGHT 32 +#define OLED_RESET -1 // No reset pin +#define OLED_ADDR 0x3C + +Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); + +void setup() { + Serial.begin(115200); + + // Initialize I2C bus (ESP32-C3 default pins) + Wire.begin(); + + // Scan I2C devices + Serial.println("Scanning I2C devices..."); + // scanI2C(); + + // Initialize OLED + if(!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) { + Serial.println("SSD1306 allocation failed"); + while(1); + } + display.clearDisplay(); + display.setTextSize(1); + display.setTextColor(SSD1306_WHITE); + display.setCursor(0,0); + display.println("Hello, ESP32-C3!"); + display.display(); +} + +void scanI2C() { + byte error, address; + int nDevices = 0; + for(address = 1; address < 127; address++ ) { + Wire.beginTransmission(address); + error = Wire.endTransmission(); + if (error == 0) { + Serial.print("I2C device found at address 0x"); + if (address<16) Serial.print("0"); + Serial.println(address, HEX); + nDevices++; + } else if (error==4) { + Serial.print("Unknown error at address 0x"); + if (address<16) Serial.print("0"); + Serial.println(address, HEX); + } + } + if (nDevices == 0) + Serial.println("No I2C devices found"); + else + Serial.println("Scan complete"); +} + +void loop() { + // Nothing to do here for this simple example + scanI2C(); + delay(1000); +} diff --git a/src/i2c/esp32s3_dual_i2c/esp32s3_dual_i2c.ino b/src/i2c/esp32s3_dual_i2c/esp32s3_dual_i2c.ino new file mode 100644 index 0000000..cb1d57b --- /dev/null +++ b/src/i2c/esp32s3_dual_i2c/esp32s3_dual_i2c.ino @@ -0,0 +1,47 @@ +#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); +} \ No newline at end of file diff --git a/src/joystic/i2c_blue_joystic/i2c_blue_joystic.ino b/src/joystic/i2c_blue_joystic/i2c_blue_joystic.ino index a47359d..a4df729 100644 --- a/src/joystic/i2c_blue_joystic/i2c_blue_joystic.ino +++ b/src/joystic/i2c_blue_joystic/i2c_blue_joystic.ino @@ -3,7 +3,7 @@ // Custom I2C pins for ESP32-S3 #define I2C_SDA_PIN 5 #define I2C_SCL_PIN 4 - #define SLAVE_ADDR 0x20 // Use 0x20 for the second slave + #define SLAVE_ADDR 0x10 // Use 0x20 for the second slave uint8_t receivedValue = 0; @@ -12,7 +12,9 @@ Wire.setPins(I2C_SDA_PIN, I2C_SCL_PIN); Wire.begin(SLAVE_ADDR); Wire.onReceive(receiveEvent); - Serial.print("Slave ready at address 0x20"); + Wire.onRequest(void (*)()) + Serial.print("Slave ready at address "); + Serial.println(SLAVE_ADDR, HEX); } void loop() { @@ -20,9 +22,51 @@ } void receiveEvent(int numBytes) { - if (Wire.available()) { - receivedValue = Wire.read(); - Serial.print("Received value: "); - Serial.println(receivedValue); + + // Serial.println("Number Of Bytes " + String(numBytes)); + uint8_t data[numBytes]; + + // Serial.print ("Received Value "); + for(int i=0; i Dice Roll"); + receivedValue = 0x01; + // Print Message On OLED Saying Roll The Dice + // Send Back Dice Number + Wire.write(Serial.readString().toInt()); + break; + + case 0x02: // Update + Serial.println("Command -> Update Joystic"); + receivedValue = 0x02; + // The Rest Of The Data Fills The DotMatrix & Health Bars + break; + + case 0x03: // Permision - Request Data + Serial.println("Command -> Permission"); + receivedValue = 0x03; + // Read Movement From Joystic & When Button Is Pressed Send Joystic Data Back To Master + // Send New Updated Data To Master + break; + + case 0x04: // Deny + Serial.println("Command -> Deny"); + receivedValue = 0x04; + // Dont Allow Joystic Movments + break; + + } + } diff --git a/src/joystic/i2c_red_joystic/header.h b/src/joystic/i2c_red_joystic/header.h new file mode 100644 index 0000000..8feed84 --- /dev/null +++ b/src/joystic/i2c_red_joystic/header.h @@ -0,0 +1,40 @@ + #include + #include + #include + #include + #include + #include + +// ============ PIN DEFINITIONS (ESP32-S3 Super Mini) ============ + + #define JOY_X_PIN A1 // Example: GP2, adjust to your wiring + #define JOY_Y_PIN A2 // Example: GP3, adjust to your wiring + #define JOY_BTN_PIN 3 // Example: GP8, adjust to your wiring + + #define SLAVE_ADDR 0x10 // Use 0x10 for the second slave + #define PUBLIC_I2C_SDA 5 // Example: GP9, SDA for OLED/APDS + #define PUBLIC_I2C_SCL 4 // Example: GP10, SCL for OLED/APDS + + #define LOCAL_I2C_SDA 12 // Example: GP9, SDA for OLED/APDS + #define LOCAL_I2C_SCL 11 // Example: GP10, SCL for OLED/APDS + + #define NUM_LEDS 16 + #define LED_DATA_PIN 8 // Example: GP11, adjust to your wiring + + #define DOTSTAR_WIDTH 8 + #define DOTSTAR_HEIGHT 8 + #define DOTSTAR_DATA 10 // Example: GP12, adjust to your wiring + #define DOTSTAR_CLK 9 // Example: GP13, adjust to your wiring + + #define ONBOARD_RGB_LED 48 // Onboard WS2812 RGB LED on GP48 + +// ============ DEVICE OBJECTS ============= + + Adafruit_SSD1306 display(128, 64, &Wire, -1); + CRGB leds[NUM_LEDS]; + + Adafruit_DotStarMatrix dotstar = Adafruit_DotStarMatrix( + DOTSTAR_WIDTH, DOTSTAR_HEIGHT, DOTSTAR_DATA, DOTSTAR_CLK, + DS_MATRIX_TOP + DS_MATRIX_LEFT + DS_MATRIX_ROWS + DS_MATRIX_PROGRESSIVE, + DOTSTAR_BRG + ); \ No newline at end of file