initial push

This commit is contained in:
2020-07-06 23:41:26 +02:00
parent 595a577232
commit f1378361ee
233 changed files with 19916 additions and 1 deletions

View File

@@ -0,0 +1,103 @@
//
// Echo Serial Led Strip Controller
//
// Serial Connection Variables
//
const byte serialBufferSize = 100;
char serialBuffer[serialBufferSize];
const char startMarker = '<';
const char endMarker = '>';
byte serialBytesReceived = 0;
boolean serialReadInProgress = false;
boolean newSerialData = false;
char serialData[serialBufferSize] = {0};
char commandLimiter[2] = ":";
void setup() {
// Init Serial Connection
Serial.begin(115200);
// Init LED Strip
initLedStrip();
// Send Init Complete signal
Serial.println("<1:1:1>");
}
void loop() {
// Handle Serial Comms
handleSerial();
// Handle Led Strip
handleLedStrip();
}
// Serial Handling
//
void handleSerial() {
if(Serial.available() > 0) {
char x = Serial.read();
// the order of these IF clauses is significant
if (x == endMarker) {
serialReadInProgress = false;
newSerialData = true;
serialBuffer[serialBytesReceived] = 0;
parseData();
}
if(serialReadInProgress) {
serialBuffer[serialBytesReceived] = x;
serialBytesReceived ++;
if (serialBytesReceived == serialBufferSize) {
serialBytesReceived = serialBufferSize - 1;
}
}
if (x == startMarker) {
serialBytesReceived = 0;
serialReadInProgress = true;
}
}
}
void parseData() {
char * strtokIndex;
char deviceUUID[10];
char command[20];
char data[69];
// Get Device UUID
strtokIndex = strtok(serialBuffer, ":");
strcpy(deviceUUID, strtokIndex);
strtokIndex = strtok(NULL, ":");
strcpy(command, strtokIndex);
strtokIndex = strtok(NULL, ":");
strcpy(data, strtokIndex);
handleCommand(deviceUUID, command, data);
}
void sendSerialCommand(char* deviceUUID, char* command, char* data) {
char serialCommandBuffer[serialBufferSize];
char _startMarker[2] = "<";
char _endMarker[2] = ">";
sprintf(serialCommandBuffer, "%s%s:%s:%s%s", _startMarker, deviceUUID, command, data, _endMarker);
Serial.println(serialCommandBuffer);
}
void handleCommand(char* deviceUUID, char* command, char* data) {
//
// Led Strip
//
if (strcmp(deviceUUID, "34gr-54jf") == 0) {
ledStripCommand(deviceUUID, command, data);
}
}

View File

@@ -0,0 +1,222 @@
#define ledStripPinR 5
#define ledStripPinG 6
#define ledStripPinB 9
int currentLedR = 0;
int currentLedG = 0;
int currentLedB = 0;
int targetLedR = 0;
int targetLedG = 0;
int targetLedB = 0;
int fadeDelay = 5;
// Modes:
// 0 = Off
// 1 = Static
// 2 = Fade
// 3 = Dynamic
int currentMode = 2;
void handleLedStrip () {
// Check if we need to handle target colors
if (currentLedR == targetLedR && currentLedG == targetLedG && currentLedB == targetLedB) {
return;
}
// Check which mode we are in, and change values based on that
if (currentMode == 1) {
// Static Mode
analogWrite(ledStripPinR, targetLedR);
analogWrite(ledStripPinG, targetLedG);
analogWrite(ledStripPinB, targetLedB);
}
if (currentMode == 2) {
// Fade Mode
//
// Red
//
// Target val is greater, add to current led value
if (targetLedR > currentLedR) {
currentLedR = currentLedR + 1;
}
// Target val is smaller, remove from current led value
if (targetLedR < currentLedR) {
currentLedR = currentLedR - 1;
}
//
// Green
//
// Target val is greater, add to current led value
if (targetLedG > currentLedG) {
currentLedG = currentLedG + 1;
}
// Target val is smaller, remove from current led value
if (targetLedG < currentLedG) {
currentLedG = currentLedG - 1;
}
//
// Blue
//
// Target val is greater, add to current led value
if (targetLedB > currentLedB) {
currentLedB = currentLedB + 1;
}
// Target val is smaller, remove from current led value
if (targetLedB < currentLedB) {
currentLedB = currentLedB - 1;
}
analogWrite(ledStripPinR, currentLedR);
analogWrite(ledStripPinG, currentLedG);
analogWrite(ledStripPinB, currentLedB);
if (currentLedR == targetLedR && currentLedG == targetLedG && currentLedB == targetLedB) {
char deviceUUID[10] = "34gr-54jf";
char command[20] = "target_reached";
char data[69] = "0";
sendSerialCommand(deviceUUID, command, data);
}
delay(fadeDelay);
}
}
void ledStripCommand(char* deviceUUID, char* command, char* data) {
if (strcmp(command, "clear_color") == 0) {
targetLedR = 0;
targetLedG = 0;
targetLedB = 0;
}
if (strcmp(command, "set_color") == 0) {
setColor(data);
}
if (strcmp(command, "set_mode") == 0) {
setMode(data);
}
if (strcmp(command, "set_mode") == 0) {
setMode(data);
}
if (strcmp(command, "set_fade_delay") == 0) {
setFadeDelay(data);
}
}
void setColor(char* dataBuffer) {
char * strtokIndex;
char rValueRaw[4];
char gValueRaw[4];
char bValueRaw[4];
// Get Red Value
strtokIndex = strtok(dataBuffer, ",");
strcpy(rValueRaw, strtokIndex);
// Get Green Value
strtokIndex = strtok(NULL, ",");
strcpy(gValueRaw, strtokIndex);
// Get Blue Value
strtokIndex = strtok(NULL, ",");
strcpy(bValueRaw, strtokIndex);
targetLedR = atoi(rValueRaw);
targetLedG = atoi(gValueRaw);
targetLedB = atoi(bValueRaw);
}
void setMode(char* dataBuffer) {
if (strcmp(dataBuffer, "static") == 0) {
currentMode = 1;
}
if (strcmp(dataBuffer, "fade") == 0) {
currentMode = 2;
}
}
void setFadeDelay(char* dataBuffer) {
fadeDelay = atoi(dataBuffer);
}
void initLedStrip() {
// Define Pin Outputs
pinMode(ledStripPinR, OUTPUT);
pinMode(ledStripPinG, OUTPUT);
pinMode(ledStripPinB, OUTPUT);
// Write 0 to all rgbs
analogWrite(ledStripPinR, 0);
analogWrite(ledStripPinG, 0);
analogWrite(ledStripPinB, 0);
// Loop Each Color
//
// Red
for (int colorIndex = 0; colorIndex < 256; colorIndex++) {
analogWrite(ledStripPinR, colorIndex);
delay(3);
}
for (int colorIndex = 255; colorIndex > -1; colorIndex--) {
analogWrite(ledStripPinR, colorIndex);
delay(3);
}
// Green
for (int colorIndex = 0; colorIndex < 256; colorIndex++) {
analogWrite(ledStripPinG, colorIndex);
delay(3);
}
for (int colorIndex = 255; colorIndex > -1; colorIndex--) {
analogWrite(ledStripPinG, colorIndex);
delay(3);
}
// Blue
for (int colorIndex = 0; colorIndex < 256; colorIndex++) {
analogWrite(ledStripPinB, colorIndex);
delay(3);
}
for (int colorIndex = 255; colorIndex > -1; colorIndex--) {
analogWrite(ledStripPinB, colorIndex);
delay(3);
}
// All
for (int colorIndex = 0; colorIndex < 256; colorIndex++) {
analogWrite(ledStripPinR, colorIndex);
analogWrite(ledStripPinG, colorIndex);
analogWrite(ledStripPinB, colorIndex);
delay(3);
}
for (int colorIndex = 255; colorIndex > -1; colorIndex--) {
analogWrite(ledStripPinR, colorIndex);
analogWrite(ledStripPinG, colorIndex);
analogWrite(ledStripPinB, colorIndex);
delay(3);
}
}

View File

@@ -0,0 +1,52 @@
//
// Helper functions
//
char* dtostrf(double number, signed char width, unsigned char prec, char *s) {
if(isnan(number)) {
strcpy(s, "nan");
return s;
}
if(isinf(number)) {
strcpy(s, "inf");
return s;
}
if(number > 4294967040.0 || number < -4294967040.0) {
strcpy(s, "ovf");
return s;
}
char* out = s;
// Handle negative numbers
if(number < 0.0) {
*out = '-';
++out;
number = -number;
}
// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for(uint8_t i = 0; i < prec; ++i)
rounding /= 10.0;
number += rounding;
// Extract the integer part of the number and print it
unsigned long int_part = (unsigned long) number;
double remainder = number - (double) int_part;
out += sprintf(out, "%d", int_part);
// Print the decimal point, but only if there are digits beyond
if(prec > 0) {
*out = '.';
++out;
}
while(prec-- > 0) {
remainder *= 10.0;
}
sprintf(out, "%d", (int) remainder);
return s;
}