Modify the code

This commit is contained in:
Fatima Idrees 2025-05-13 11:11:33 +03:00
parent b939819e0e
commit 168dd7b49b
5 changed files with 232 additions and 146 deletions

View File

@ -1,55 +1,121 @@
#include <Wire.h> #include <Wire.h>
#define I2C_DEV_ADDR 0x55 // Must match slave #define SLAVE_ADDRESS 0x08
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
Wire.begin(); // default pins for ESP32-C3: SDA=8, SCL=9 Wire.begin();
Serial.println("Enter number of pixels:");
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() { void loop() {
static int numPixels = 0; // Nothing here
static bool pixelsSet = false; }
// Step 1: Get number of pixels from user // --------- Helper Functions ---------
if (!pixelsSet && Serial.available()) {
numPixels = Serial.parseInt(); void setPixel(uint8_t pixel, uint8_t r, uint8_t g, uint8_t b) {
if (numPixels > 0 && numPixels <= 100) { // reasonable limit Wire.beginTransmission(SLAVE_ADDRESS);
Wire.beginTransmission(I2C_DEV_ADDR); Wire.write(0xA0); // Command: Set individual pixel
Wire.write(0x01); // Command: set pixel count Wire.write(pixel);
Wire.write(numPixels); Wire.write(r);
Wire.endTransmission(); Wire.write(g);
pixelsSet = true; Wire.write(b);
Serial.println("Enter command: (example: ALL 255 0 0 128) or (PIXEL 3 0 255 0 255)"); Wire.endTransmission();
Serial.println("Format:"); Serial.print("Set pixel "); Serial.print(pixel);
Serial.println("ALL R G B I"); Serial.print(" to RGB("); Serial.print(r); Serial.print(","); Serial.print(g); Serial.print(","); Serial.print(b); Serial.println(")");
Serial.println("PIXEL N R G B I"); }
} else {
Serial.println("Invalid pixel count. Try again."); 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);
// Step 2: Get lighting commands from user Wire.write(g);
if (pixelsSet && Serial.available()) { Wire.write(b);
String cmd = Serial.readStringUntil('\n'); Wire.endTransmission();
cmd.trim(); Serial.print("Set all pixels to RGB("); Serial.print(r); Serial.print(","); Serial.print(g); Serial.print(","); Serial.print(b); Serial.println(")");
}
if (cmd.startsWith("ALL")) {
int r, g, b, i; void setBrightness(uint8_t brightness) {
sscanf(cmd.c_str(), "ALL %d %d %d %d", &r, &g, &b, &i); Wire.beginTransmission(SLAVE_ADDRESS);
Wire.beginTransmission(I2C_DEV_ADDR); Wire.write(0xA2); // Command: Set brightness
Wire.write(0x02); // Command: set all pixels Wire.write(brightness);
Wire.write(r); Wire.write(g); Wire.write(b); Wire.write(i); Wire.endTransmission();
Wire.endTransmission(); Serial.print("Set brightness to "); Serial.println(brightness);
} 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); void clearAll() {
Wire.beginTransmission(I2C_DEV_ADDR); Wire.beginTransmission(SLAVE_ADDRESS);
Wire.write(0x03); // Command: set one pixel Wire.write(0xB0); // Command: Clear all
Wire.write(n); Wire.write(r); Wire.write(g); Wire.write(b); Wire.write(i); Wire.endTransmission();
Wire.endTransmission(); Serial.println("Cleared all pixels (off)");
} }
Serial.println("Command sent.");
} 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);
} }

View File

@ -1,56 +1,132 @@
#include <Wire.h> #include <Wire.h>
#include <EEPROM.h>
#include <Adafruit_NeoPixel.h> #include <Adafruit_NeoPixel.h>
#define I2C_DEV_ADDR 0x55 #define DEFAULT_ADDRESS 0x08
#define NEOPIXEL_PIN 19 // Change as needed #define DEFAULT_LED_COUNT 30
#define MAX_PIXELS 100 #define MAX_NAME_LENGTH 24
#define LED_PIN 6
Adafruit_NeoPixel strip(MAX_PIXELS, NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800); struct Config {
uint8_t address;
char deviceName[MAX_NAME_LENGTH+1];
uint16_t ledCount;
uint8_t brightness;
};
uint8_t pixelCount = 1; Config config;
Adafruit_NeoPixel strip;
void receiveEvent(int len) { void handleCommand(int numBytes);
delay(50); // Add more delay for stability as requested void saveConfig();
void loadConfig();
if (len < 1) return; void updateStrip();
uint8_t cmd = Wire.read();
if (cmd == 0x01 && len >= 2) { // Set pixel count
pixelCount = Wire.read();
strip.updateLength(pixelCount);
strip.begin();
strip.show();
}
else if (cmd == 0x02 && len >= 5) { // Set all pixels: R G B I
uint8_t r = Wire.read();
uint8_t g = Wire.read();
uint8_t b = Wire.read();
uint8_t intensity = Wire.read();
for (int i = 0; i < pixelCount; i++) {
strip.setPixelColor(i, strip.Color(r * intensity / 255, g * intensity / 255, b * intensity / 255));
}
strip.show();
}
else if (cmd == 0x03 && len >= 6) { // Set one pixel: N R G B I
uint8_t n = Wire.read();
uint8_t r = Wire.read();
uint8_t g = Wire.read();
uint8_t b = Wire.read();
uint8_t intensity = Wire.read();
if (n < pixelCount) {
strip.setPixelColor(n, strip.Color(r * intensity / 255, g * intensity / 255, b * intensity / 255));
strip.show();
}
}
}
void setup() { void setup() {
Serial.begin(115200);
EEPROM.begin(sizeof(Config));
loadConfig();
strip = Adafruit_NeoPixel(config.ledCount, LED_PIN, NEO_GRB + NEO_KHZ800);
strip.begin(); strip.begin();
strip.show(); strip.setBrightness(config.brightness);
Wire.begin(I2C_DEV_ADDR, 8, 9); // ESP32-S3 default I2C pins: SDA=8, SCL=9 strip.show(); // Initialize all off
Wire.onReceive(receiveEvent);
Wire.onReceive(handleCommand);
Wire.onRequest([]() {
String response = String(config.address) + ",LED-Strip," +
config.deviceName + "," +
String(config.ledCount) + "," +
String(config.brightness);
Wire.print(response);
});
Wire.begin(config.address);
} }
void loop() { void loop() { /* Empty */ }
delay(100); // Main loop delay, not critical
void handleCommand(int numBytes) {
uint8_t cmd = Wire.read();
switch(cmd) {
case 0xA0: // Set individual pixel
if(numBytes >= 5) {
uint8_t pixel = Wire.read();
uint8_t r = Wire.read();
uint8_t g = Wire.read();
uint8_t b = Wire.read();
if(pixel < config.ledCount) {
strip.setPixelColor(pixel, strip.Color(r, g, b));
updateStrip();
}
}
break;
case 0xA1: // Set all pixels
if(numBytes >= 4) {
uint8_t r = Wire.read();
uint8_t g = Wire.read();
uint8_t b = Wire.read();
for(int i=0; i<config.ledCount; i++) {
strip.setPixelColor(i, strip.Color(r, g, b));
}
updateStrip();
}
break;
case 0xA2: // Set brightness
if(numBytes >= 2) {
config.brightness = Wire.read();
strip.setBrightness(config.brightness);
saveConfig();
updateStrip();
}
break;
case 0xB0: // Clear all
strip.clear();
updateStrip();
break;
case 0xC0: // Set address
if(numBytes >= 2) {
config.address = Wire.read();
saveConfig();
Wire.begin(config.address);
}
break;
case 0xD0: // Set LED count
if(numBytes >= 3) {
config.ledCount = Wire.read() << 8;
config.ledCount |= Wire.read();
saveConfig();
strip.updateLength(config.ledCount);
updateStrip();
}
break;
}
}
void updateStrip() {
strip.show();
}
void loadConfig() {
EEPROM.get(0, config);
if(config.address < 0x08 || config.address > 0x77) {
config.address = DEFAULT_ADDRESS;
config.ledCount = DEFAULT_LED_COUNT;
config.brightness = 255;
strcpy(config.deviceName, "LED Controller");
saveConfig();
}
}
void saveConfig() {
EEPROM.put(0, config);
EEPROM.commit();
} }

View File

@ -1,6 +0,0 @@
#ifndef I2C_COMMANDS_H
#define I2C_COMMANDS_H
#define CMD_CHANGE_ADDR 0xAA // Use the same value as in your slave
#endif

View File

@ -1,36 +0,0 @@
#include "I2CMasterUtils.h"
#include "I2CCommands.h"
void I2CMasterUtils::scanI2C(Stream &output) {
output.println("Scanning for I2C devices...");
for (uint8_t addr = 1; addr < 127; addr++) {
Wire.beginTransmission(addr);
if (Wire.endTransmission() == 0) {
output.print("Found device at 0x");
output.println(addr, HEX);
}
}
}
void I2CMasterUtils::changeSlaveAddress(uint8_t oldAddr, uint8_t newAddr, Stream &output) {
Wire.beginTransmission(oldAddr);
Wire.write(CMD_CHANGE_ADDR); // Use the command code from I2CCommands.h
Wire.write(newAddr);
Wire.endTransmission();
output.print("Sent address change command to 0x");
output.print(oldAddr, HEX);
output.print(" -> 0x");
output.println(newAddr, HEX);
}
bool I2CMasterUtils::parseChangeAddressCommand(const String &input, uint8_t &oldAddr, uint8_t &newAddr) {
if (!input.startsWith("ch")) return false;
int o, n;
int matched = sscanf(input.c_str(), "ch %x %x", &o, &n);
if (matched == 2) {
oldAddr = (uint8_t)o;
newAddr = (uint8_t)n;
return true;
}
return false;
}

View File

@ -1,14 +0,0 @@
#ifndef I2CMASTERUTILS_H
#define I2CMASTERUTILS_H
#include <Arduino.h>
#include <Wire.h>
class I2CMasterUtils {
public:
static void scanI2C(Stream &output = Serial);
static void changeSlaveAddress(uint8_t oldAddr, uint8_t newAddr, Stream &output = Serial);
static bool parseChangeAddressCommand(const String &input, uint8_t &oldAddr, uint8_t &newAddr);
};
#endif