Compare commits
6
Commits
3c828920de
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7e11450076 | ||
|
|
36185974eb | ||
|
|
9a7677a857 | ||
|
|
a37ff59dc5 | ||
|
|
b5c8409dcc | ||
|
|
ca9c816bb0 |
No files matched your search
Binary file not shown.
|
After Width: | Height: | Size: 438 KiB |
File renamed without changes.
+29
-9
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
## ESP32C3 (Master) Connection
|
## ESP32C3 (Master) Connection
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
```
|
```
|
||||||
SDA_PIN = 7
|
SDA_PIN = 7
|
||||||
@@ -14,13 +14,33 @@
|
|||||||

|

|
||||||
|
|
||||||
```
|
```
|
||||||
// Red Joystic
|
// Red Joystic Public - Slave Address
|
||||||
DIV_ADD = 0x10
|
#define DIV_ADD 0x10
|
||||||
SDA_PIN = 5
|
|
||||||
SCL_PIN = 4
|
// Blue Joystic Public - Slave Address
|
||||||
|
#define DIV_ADD 0x20
|
||||||
|
|
||||||
|
// PUBLIC -> I2C PINS
|
||||||
|
#define SDA_PIN 5
|
||||||
|
#define SCL_PIN 4
|
||||||
|
|
||||||
|
// LOCAL -> I2C PINS
|
||||||
|
#define SDA_PIN 5
|
||||||
|
#define SCL_PIN 4
|
||||||
|
|
||||||
|
// JOYSTIC
|
||||||
|
#define X 1
|
||||||
|
#define Y 2
|
||||||
|
#define B 3
|
||||||
|
|
||||||
|
// FAST LED
|
||||||
|
#define STRIP_PIN 8
|
||||||
|
#define STRIP_LEDS 16
|
||||||
|
|
||||||
|
// DOTMATRIC
|
||||||
|
#define DMX 8
|
||||||
|
#define DMY 8
|
||||||
|
#define DIN 10
|
||||||
|
#define CIN 9
|
||||||
|
|
||||||
// Blue Joystic
|
|
||||||
DIV_ADD = 0x20
|
|
||||||
SDA_PIN = 5
|
|
||||||
SCL_PIN = 4
|
|
||||||
```
|
```
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -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");
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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<message.length(); i++) {
|
||||||
|
|
||||||
|
// Send String Bits
|
||||||
|
Wire.write(message[i]);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Finally Closing Transmission
|
||||||
|
Wire.endTransmission();
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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() {
|
||||||
|
|||||||
@@ -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();
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in new issue
Block a user