diff --git a/src/i2c/esp32c3_master_i2c/esp32c3_master_i2c.ino b/src/i2c/esp32c3_master_i2c/esp32c3_master_i2c.ino index 584e618..8666702 100644 --- a/src/i2c/esp32c3_master_i2c/esp32c3_master_i2c.ino +++ b/src/i2c/esp32c3_master_i2c/esp32c3_master_i2c.ino @@ -1,77 +1,77 @@ -#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); - -// NEW Datatype For State -enum _state { NOT_READY, READY, SENDING }; - -// Variables -_state state = NOT_READY; - -void setup() { - - // Serial - Serial.begin(115200); - - // Initialize I2C bus (ESP32-C3 default pins) - Wire.begin(); - - // Scan I2C devices - Serial.println("Scanning I2C devices..."); - - // Scan I2C - scanI2C(); - - // Initialize OLED - if(!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) { - Serial.println("SSD1306 allocation failed"); - while(1); - } - - // Print On Display - oled_write("Hello, ESP32-C3!"); - -} - -void loop() { - - // Send Tex Message To Slave - i2c_send_string(0x55, Question("String Message")); - - // Request Data - Wire.requestFrom(0x55, 1); - - // Read Data Available - if(Wire.available()) { - if(char(Wire.read()) == 'N') { - Serial.println("Requested Data : Not Ready"); - state = NOT_READY; - } else { - Serial.println("Requested Data : Ready"); - state = READY; - } - } - - // Request Data - Wire.requestFrom(0x55, 6); - - // Prepare 6 Bytes Of Data - uint8_t data[6]; - - // Read Data Available - if(Wire.available()) { - for(int i=0; i<6; i++) { - data[i] = Wire.read(); - Serial.print(char(data[i])); - } - Serial.println(); - } - -} \ No newline at end of file +#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); + +// NEW Datatype For State +enum _state { NOT_READY, READY, SENDING }; + +// Variables +_state state = NOT_READY; + +void setup() { + + // Serial + Serial.begin(115200); + + // Initialize I2C bus (ESP32-C3 default pins) + Wire.begin(); + + // Scan I2C devices + Serial.println("Scanning I2C devices..."); + + // Scan I2C + scanI2C(); + + // Initialize OLED + if(!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) { + Serial.println("SSD1306 allocation failed"); + while(1); + } + + // Print On Display + oled_write("Hello, ESP32-C3!"); + +} + +void loop() { + + // Send Tex Message To Slave + i2c_send_string(0x55, Question("String Message")); + + // Request Data + Wire.requestFrom(0x55, 1); + + // Read Data Available + if(Wire.available()) { + if(char(Wire.read()) == 'N') { + Serial.println("Requested Data : Not Ready"); + state = NOT_READY; + } else { + Serial.println("Requested Data : Ready"); + state = READY; + } + } + + // Request Data + Wire.requestFrom(0x55, 6); + + // Prepare 6 Bytes Of Data + uint8_t data[6]; + + // Read Data Available + if(Wire.available()) { + for(int i=0; i<6; i++) { + data[i] = Wire.read(); + Serial.print(char(data[i])); + } + Serial.println(); + } + +} diff --git a/src/i2c/esp32c3_master_i2c/functions.ino b/src/i2c/esp32c3_master_i2c/functions.ino new file mode 100644 index 0000000..09fc5e2 --- /dev/null +++ b/src/i2c/esp32c3_master_i2c/functions.ino @@ -0,0 +1,51 @@ + // Question + String Question(String Message) { + + Serial.print(Message + " : "); + while(!Serial.available()); + + Message = Serial.readStringUntil('\n'); + while(Serial.available()) Serial.read(); + Message.trim(); + Serial.println(Message); + return Message; + + } + + // 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(); + } + + // I2C Scanner + 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"); + + } \ No newline at end of file diff --git a/src/i2c/esp32c3_master_i2c/i2c_functions.ino b/src/i2c/esp32c3_master_i2c/i2c_functions.ino new file mode 100644 index 0000000..e2f9891 --- /dev/null +++ b/src/i2c/esp32c3_master_i2c/i2c_functions.ino @@ -0,0 +1,22 @@ + + // This Function If Called Sends A Text Message Via I2C + void i2c_send_string(uint8_t Slave, String message) { + + // Sending Data + Wire.beginTransmission(Slave); + + // First Data Byte Is Length + Wire.write(message.length()); + + // Second Send Data One Byte At A Time + for(int i=0; i