Changed strings to cstrings

This commit is contained in:
GHOSCHT 2021-01-01 15:35:25 +01:00
parent 62bf284182
commit 8b247b058d

View file

@ -8,17 +8,20 @@ const int bjtCount = 4;
const int bjtPin[bjtCount] = {6, 5, 3, 10}; const int bjtPin[bjtCount] = {6, 5, 3, 10};
int bjtState[bjtCount] = {255, 255, 255, 255}; //255 -> bjt max "open" int bjtState[bjtCount] = {255, 255, 255, 255}; //255 -> bjt max "open"
String receivedSerialData; char receivedSerialData[4];
char receivedI2cData[4];
String pendingSerialData; String pendingSerialData;
String receivedI2cData;
void changeLights(String data) void changeLights(char data[])
{ {
for (int i = 0; i < bjtCount; i++) for (int i = 0; i < bjtCount; i++)
{ {
if (data.indexOf(String(i)) == 0) //changed to i2c for testing char numChar[2];
itoa(i, numChar, 10);
if (data[0] == numChar[0])
{ {
if (data.indexOf("t") == 1) //changed to i2c for testing if (data[1] == 't')
{ {
if (bjtState[i] != 0) if (bjtState[i] != 0)
{ {
@ -29,11 +32,11 @@ void changeLights(String data)
bjtState[i] = 255; bjtState[i] = 255;
} }
} }
else if (data.indexOf("i") == 1) else if (data[1] == 'i')
{ {
bjtState[i] += 5; bjtState[i] += 5;
} }
else if (data.indexOf("d") == 1) else if (data[1] == 'd')
{ {
bjtState[i] -= 5; bjtState[i] -= 5;
} }
@ -51,12 +54,12 @@ void changeLights(String data)
} }
//set absolute state for all //set absolute state for all
if (data == "off") if (!strcmp(data, "off"))
{ {
bjtState[i] = 0; bjtState[i] = 0;
} }
if (data == "on") if (!strcmp(data, "on"))
{ {
bjtState[i] = 255; bjtState[i] = 255;
} }
@ -65,8 +68,7 @@ void changeLights(String data)
EEPROM.update(i, bjtState[i]); EEPROM.update(i, bjtState[i]);
} }
receivedSerialData = ""; memset(data, ' ', 3);
receivedI2cData = "";
} }
void receiveEvent(int byteCount) void receiveEvent(int byteCount)
@ -76,9 +78,8 @@ void receiveEvent(int byteCount)
{ {
buffer[i] = Wire.read(); buffer[i] = Wire.read();
} }
receivedI2cData = String(buffer); memset(receivedI2cData, ' ', 3);
strcpy(receivedI2cData, buffer);
changeLights(receivedI2cData);
} }
void setup() void setup()
@ -92,7 +93,7 @@ void setup()
{ {
pinMode(bjtPin[i], OUTPUT); pinMode(bjtPin[i], OUTPUT);
bjtState[i] = EEPROM.read(i); bjtState[i] = EEPROM.read(i);
digitalWrite(bjtPin[i], bjtState[i]); analogWrite(bjtPin[i], bjtState[i]);
} }
} }
@ -100,11 +101,11 @@ void loop()
{ {
while (Serial.available()) while (Serial.available())
{ {
receivedSerialData = Serial.readString(); //FORMAT: index of light + t oggle, i ncrease, d ecrease -> eg. 0t strcpy(receivedSerialData, Serial.readString().c_str());
Serial.flush();
} }
changeLights(receivedSerialData); changeLights(receivedSerialData);
changeLights(receivedI2cData);
pendingSerialData = ""; pendingSerialData = "";
for (int i = 0; i < bjtCount; i++) for (int i = 0; i < bjtCount; i++)
@ -113,5 +114,6 @@ void loop()
pendingSerialData += String(bjtState[i]) + ","; pendingSerialData += String(bjtState[i]) + ",";
interrupts(); interrupts();
} }
pendingSerialData.remove(pendingSerialData.length() - 1);
Serial.println(pendingSerialData); Serial.println(pendingSerialData);
} }