Modify the code
This commit is contained in:
1 parent
b939819e0e
commit
168dd7b49b
5 files changed
+232
-146
No files matched your search
+112
-46
@@ -1,55 +1,121 @@
|
||||
#include <Wire.h>
|
||||
|
||||
#define I2C_DEV_ADDR 0x55 // Must match slave
|
||||
#define SLAVE_ADDRESS 0x08
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
Wire.begin(); // default pins for ESP32-C3: SDA=8, SCL=9
|
||||
Serial.println("Enter number of pixels:");
|
||||
Wire.begin();
|
||||
|
||||
delay(1000);
|
||||
Serial.println("Testing I2C LED Strip Slave...");
|
||||
|
||||
// 1. Set device name (optional, not in slave code above, but included for completeness)
|
||||
// setDeviceName("LivingRoomLEDs");
|
||||
|
||||
// 2. Set number of LEDs to 10 (for testing)
|
||||
setLedCount(10);
|
||||
delay(200);
|
||||
|
||||
// 3. Set brightness to 128 (half)
|
||||
setBrightness(128);
|
||||
delay(200);
|
||||
|
||||
// 4. Set all pixels to red
|
||||
setAllPixels(255, 0, 0);
|
||||
delay(1000);
|
||||
|
||||
// 5. Set pixel 0 to green
|
||||
setPixel(0, 0, 255, 0);
|
||||
delay(500);
|
||||
|
||||
// 6. Set pixel 1 to blue
|
||||
setPixel(1, 0, 0, 255);
|
||||
delay(500);
|
||||
|
||||
// 7. Set all pixels to white
|
||||
setAllPixels(255, 255, 255);
|
||||
delay(1000);
|
||||
|
||||
// 8. Clear all pixels (turn off)
|
||||
clearAll();
|
||||
delay(500);
|
||||
|
||||
// 9. Request status from slave and print it
|
||||
requestStatus();
|
||||
|
||||
Serial.println("Test complete.");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
static int numPixels = 0;
|
||||
static bool pixelsSet = false;
|
||||
|
||||
// Step 1: Get number of pixels from user
|
||||
if (!pixelsSet && Serial.available()) {
|
||||
numPixels = Serial.parseInt();
|
||||
if (numPixels > 0 && numPixels <= 100) { // reasonable limit
|
||||
Wire.beginTransmission(I2C_DEV_ADDR);
|
||||
Wire.write(0x01); // Command: set pixel count
|
||||
Wire.write(numPixels);
|
||||
Wire.endTransmission();
|
||||
pixelsSet = true;
|
||||
Serial.println("Enter command: (example: ALL 255 0 0 128) or (PIXEL 3 0 255 0 255)");
|
||||
Serial.println("Format:");
|
||||
Serial.println("ALL R G B I");
|
||||
Serial.println("PIXEL N R G B I");
|
||||
} else {
|
||||
Serial.println("Invalid pixel count. Try again.");
|
||||
}
|
||||
}
|
||||
|
||||
// Step 2: Get lighting commands from user
|
||||
if (pixelsSet && Serial.available()) {
|
||||
String cmd = Serial.readStringUntil('\n');
|
||||
cmd.trim();
|
||||
|
||||
if (cmd.startsWith("ALL")) {
|
||||
int r, g, b, i;
|
||||
sscanf(cmd.c_str(), "ALL %d %d %d %d", &r, &g, &b, &i);
|
||||
Wire.beginTransmission(I2C_DEV_ADDR);
|
||||
Wire.write(0x02); // Command: set all pixels
|
||||
Wire.write(r); Wire.write(g); Wire.write(b); Wire.write(i);
|
||||
Wire.endTransmission();
|
||||
} else if (cmd.startsWith("PIXEL")) {
|
||||
int n, r, g, b, i;
|
||||
sscanf(cmd.c_str(), "PIXEL %d %d %d %d %d", &n, &r, &g, &b, &i);
|
||||
Wire.beginTransmission(I2C_DEV_ADDR);
|
||||
Wire.write(0x03); // Command: set one pixel
|
||||
Wire.write(n); Wire.write(r); Wire.write(g); Wire.write(b); Wire.write(i);
|
||||
Wire.endTransmission();
|
||||
}
|
||||
Serial.println("Command sent.");
|
||||
}
|
||||
// Nothing here
|
||||
}
|
||||
|
||||
// --------- Helper Functions ---------
|
||||
|
||||
void setPixel(uint8_t pixel, uint8_t r, uint8_t g, uint8_t b) {
|
||||
Wire.beginTransmission(SLAVE_ADDRESS);
|
||||
Wire.write(0xA0); // Command: Set individual pixel
|
||||
Wire.write(pixel);
|
||||
Wire.write(r);
|
||||
Wire.write(g);
|
||||
Wire.write(b);
|
||||
Wire.endTransmission();
|
||||
Serial.print("Set pixel "); Serial.print(pixel);
|
||||
Serial.print(" to RGB("); Serial.print(r); Serial.print(","); Serial.print(g); Serial.print(","); Serial.print(b); Serial.println(")");
|
||||
}
|
||||
|
||||
void setAllPixels(uint8_t r, uint8_t g, uint8_t b) {
|
||||
Wire.beginTransmission(SLAVE_ADDRESS);
|
||||
Wire.write(0xA1); // Command: Set all pixels
|
||||
Wire.write(r);
|
||||
Wire.write(g);
|
||||
Wire.write(b);
|
||||
Wire.endTransmission();
|
||||
Serial.print("Set all pixels to RGB("); Serial.print(r); Serial.print(","); Serial.print(g); Serial.print(","); Serial.print(b); Serial.println(")");
|
||||
}
|
||||
|
||||
void setBrightness(uint8_t brightness) {
|
||||
Wire.beginTransmission(SLAVE_ADDRESS);
|
||||
Wire.write(0xA2); // Command: Set brightness
|
||||
Wire.write(brightness);
|
||||
Wire.endTransmission();
|
||||
Serial.print("Set brightness to "); Serial.println(brightness);
|
||||
}
|
||||
|
||||
void clearAll() {
|
||||
Wire.beginTransmission(SLAVE_ADDRESS);
|
||||
Wire.write(0xB0); // Command: Clear all
|
||||
Wire.endTransmission();
|
||||
Serial.println("Cleared all pixels (off)");
|
||||
}
|
||||
|
||||
void setLedCount(uint16_t count) {
|
||||
Wire.beginTransmission(SLAVE_ADDRESS);
|
||||
Wire.write(0xD0); // Command: Set LED count
|
||||
Wire.write((count >> 8) & 0xFF); // High byte
|
||||
Wire.write(count & 0xFF); // Low byte
|
||||
Wire.endTransmission();
|
||||
Serial.print("Set LED count to "); Serial.println(count);
|
||||
}
|
||||
|
||||
// Optional: Set device name (if supported in slave code)
|
||||
void setDeviceName(const char* name) {
|
||||
Wire.beginTransmission(SLAVE_ADDRESS);
|
||||
Wire.write(0xD0); // Command: Set device name
|
||||
for (uint8_t i = 0; i < strlen(name); i++) {
|
||||
Wire.write(name[i]);
|
||||
}
|
||||
Wire.endTransmission();
|
||||
Serial.print("Set device name to "); Serial.println(name);
|
||||
}
|
||||
|
||||
void requestStatus() {
|
||||
Wire.requestFrom(SLAVE_ADDRESS, 40); // Request up to 40 bytes (enough for status string)
|
||||
String status = "";
|
||||
while (Wire.available()) {
|
||||
char c = Wire.read();
|
||||
status += c;
|
||||
}
|
||||
Serial.print("Status from slave: ");
|
||||
Serial.println(status);
|
||||
}
|
||||
Reference in new issue
Block a user