Revert "Buffer boundary check - test 1"

This reverts commit 1c0c744983.
This commit is contained in:
GHOSCHT 2021-08-26 14:48:10 +02:00
parent d20c4f6f28
commit ffc306b59e

View file

@ -1,123 +1,119 @@
#include <Arduino.h> #include <Arduino.h>
#include <EEPROM.h> #include <EEPROM.h>
#include <Wire.h> #include <Wire.h>
#define SLAVE_ADDR 9 #define SLAVE_ADDR 9
const int bjtCount = 4; 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"
char receivedSerialData[4]; char receivedSerialData[4];
char receivedI2cData[4]; char receivedI2cData[4];
String pendingSerialData; String pendingSerialData;
void changeLights(char data[]) void changeLights(char data[])
{ {
for (int i = 0; i < bjtCount; i++) for (int i = 0; i < bjtCount; i++)
{ {
char numChar[2]; char numChar[2];
itoa(i, numChar, 10); itoa(i, numChar, 10);
if (data[0] == numChar[0]) if (data[0] == numChar[0])
{ {
if (data[1] == 't') if (data[1] == 't')
{ {
if (bjtState[i] != 0) if (bjtState[i] != 0)
{ {
bjtState[i] = 0; bjtState[i] = 0;
} }
else else
{ {
bjtState[i] = 255; bjtState[i] = 255;
} }
} }
else if (data[1] == 'i') else if (data[1] == 'i')
{ {
bjtState[i] += 5; bjtState[i] += 5;
} }
else if (data[1] == 'd') else if (data[1] == 'd')
{ {
bjtState[i] -= 5; bjtState[i] -= 5;
} }
} }
//clamp state between 0 and 255 //clamp state between 0 and 255
if (bjtState[i] > 255) if (bjtState[i] > 255)
{ {
bjtState[i] = 255; bjtState[i] = 255;
} }
if (bjtState[i] < 0) if (bjtState[i] < 0)
{ {
bjtState[i] = 0; bjtState[i] = 0;
} }
//set absolute state for all //set absolute state for all
if (!strcmp(data, "off")) if (!strcmp(data, "off"))
{ {
bjtState[i] = 0; bjtState[i] = 0;
} }
if (!strcmp(data, "on")) if (!strcmp(data, "on"))
{ {
bjtState[i] = 255; bjtState[i] = 255;
} }
analogWrite(bjtPin[i], bjtState[i]); analogWrite(bjtPin[i], bjtState[i]);
EEPROM.update(i, bjtState[i]); EEPROM.update(i, bjtState[i]);
} }
memset(data, ' ', 3); memset(data, ' ', 3);
} }
void receiveEvent(int byteCount) void receiveEvent(int byteCount)
{ {
char buffer[byteCount]; char buffer[byteCount];
for (int i = 0; i < byteCount; i++) for (int i = 0; i < byteCount; i++)
{ {
if (i == byteCount) buffer[i] = Wire.read();
break; }
memset(receivedI2cData, ' ', 3);
buffer[i] = Wire.read(); strcpy(receivedI2cData, buffer);
} }
memset(receivedI2cData, ' ', 3); void setup()
strcpy(receivedI2cData, buffer); {
} Serial.begin(9600);
Serial.setTimeout(5);
void setup() Wire.begin(SLAVE_ADDR);
{ Wire.onReceive(receiveEvent);
Serial.begin(9600);
Serial.setTimeout(5); for (int i = 0; i < bjtCount; i++)
Wire.begin(SLAVE_ADDR); {
Wire.onReceive(receiveEvent); pinMode(bjtPin[i], OUTPUT);
bjtState[i] = EEPROM.read(i);
for (int i = 0; i < bjtCount; i++) analogWrite(bjtPin[i], bjtState[i]);
{ }
pinMode(bjtPin[i], OUTPUT); }
bjtState[i] = EEPROM.read(i);
analogWrite(bjtPin[i], bjtState[i]); void loop()
} {
} while (Serial.available())
{
void loop() strcpy(receivedSerialData, Serial.readString().c_str());
{ }
while (Serial.available())
{ changeLights(receivedSerialData);
strcpy(receivedSerialData, Serial.readString().c_str()); changeLights(receivedI2cData);
}
pendingSerialData = "";
changeLights(receivedSerialData); for (int i = 0; i < bjtCount; i++)
changeLights(receivedI2cData); {
noInterrupts();
pendingSerialData = ""; pendingSerialData += String(bjtState[i]) + ",";
for (int i = 0; i < bjtCount; i++) interrupts();
{ }
noInterrupts(); pendingSerialData.remove(pendingSerialData.length() - 1);
pendingSerialData += String(bjtState[i]) + ","; Serial.println(pendingSerialData);
interrupts();
}
pendingSerialData.remove(pendingSerialData.length() - 1);
Serial.println(pendingSerialData);
} }