2025-05-13 11:11:33 +03:00

122 lines
3.1 KiB
C++

#include <Wire.h>
#define SLAVE_ADDRESS 0x08
void setup() {
Serial.begin(115200);
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() {
// 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);
}