Update src/i2c/esp32c3_master_i2c/esp32c3_master_i2c.ino
This commit is contained in:
parent
a37ff59dc5
commit
9a7677a857
@ -9,7 +9,15 @@
|
|||||||
|
|
||||||
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
|
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() {
|
void setup() {
|
||||||
|
|
||||||
|
// Serial
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
|
||||||
// Initialize I2C bus (ESP32-C3 default pins)
|
// Initialize I2C bus (ESP32-C3 default pins)
|
||||||
@ -17,46 +25,53 @@ void setup() {
|
|||||||
|
|
||||||
// Scan I2C devices
|
// Scan I2C devices
|
||||||
Serial.println("Scanning I2C devices...");
|
Serial.println("Scanning I2C devices...");
|
||||||
// scanI2C();
|
|
||||||
|
// Scan I2C
|
||||||
|
scanI2C();
|
||||||
|
|
||||||
// Initialize OLED
|
// Initialize OLED
|
||||||
if(!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) {
|
if(!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) {
|
||||||
Serial.println("SSD1306 allocation failed");
|
Serial.println("SSD1306 allocation failed");
|
||||||
while(1);
|
while(1);
|
||||||
}
|
}
|
||||||
display.clearDisplay();
|
|
||||||
display.setTextSize(1);
|
|
||||||
display.setTextColor(SSD1306_WHITE);
|
|
||||||
display.setCursor(0,0);
|
|
||||||
display.println("Hello, ESP32-C3!");
|
|
||||||
display.display();
|
|
||||||
}
|
|
||||||
|
|
||||||
void scanI2C() {
|
// Print On Display
|
||||||
byte error, address;
|
oled_write("Hello, ESP32-C3!");
|
||||||
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() {
|
void loop() {
|
||||||
// Nothing to do here for this simple example
|
|
||||||
scanI2C();
|
// Send Tex Message To Slave
|
||||||
delay(1000);
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user