|
| 1 | +#ifdef PIF_TOOL_CHAIN |
| 2 | + #include <Arduino.h> |
| 3 | + // other includes with full pathes |
| 4 | + #include "ledStrip/strip.h" |
| 5 | +#else |
| 6 | + // other includes with short pathes |
| 7 | + // example : #include "led7.h" |
| 8 | + #include "strip.h" |
| 9 | +#endif |
| 10 | + |
| 11 | +#ifndef DEFAULT_BAUDRATE |
| 12 | + #define DEFAULT_BAUDRATE 115200 |
| 13 | +#endif |
| 14 | + |
| 15 | +#define STRIP_PIN 8 |
| 16 | + |
| 17 | +void setup(void) { |
| 18 | + Serial.begin(DEFAULT_BAUDRATE); |
| 19 | + |
| 20 | + Serial.println("ready"); |
| 21 | +} |
| 22 | + |
| 23 | +bool initialized = false; |
| 24 | +byte stripLen; |
| 25 | +byte light = 128; |
| 26 | +// buffer for command reads |
| 27 | +byte data[60]; |
| 28 | + |
| 29 | +bool readBytes(byte *buffer, int len, bool ascii) { |
| 30 | + if (ascii) { |
| 31 | + int i = 0, v = 0; |
| 32 | + while(i < len) { |
| 33 | + int c = Serial.read(); |
| 34 | + if (c == -1 || c == '\r') { |
| 35 | + continue; |
| 36 | + } else if (c == ',' || c == ';' || c == '\n') { |
| 37 | + buffer[i] = v; |
| 38 | + v = 0; |
| 39 | + i++; |
| 40 | + } else if (c >= '0' && c <= '9') { |
| 41 | + v = v * 10 + c - '0'; |
| 42 | + } else { |
| 43 | + return false; |
| 44 | + } |
| 45 | + } |
| 46 | + } else { |
| 47 | + int c; |
| 48 | + for (int i = 0; i < len; i++) { |
| 49 | + while((c = Serial.read()) == -1); |
| 50 | + buffer[i] = c; |
| 51 | + } |
| 52 | + } |
| 53 | + return true; |
| 54 | +} |
| 55 | + |
| 56 | +void rainbow() { |
| 57 | + for (byte c = 1; c <= 255 - stripLen; c++) { |
| 58 | + for (byte p = 0; p < stripLen; p++) { |
| 59 | + stripSetH(p, c + p); |
| 60 | + } |
| 61 | + stripUpdate(); |
| 62 | + delay(10); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +void loop() { |
| 67 | + if (Serial.available()) { |
| 68 | + int b = Serial.read(); |
| 69 | + switch(b) { |
| 70 | + case '!': // rainbow |
| 71 | + if (initialized) { |
| 72 | + rainbow(); |
| 73 | + } |
| 74 | + break; |
| 75 | + case '?': // return status |
| 76 | + if (initialized) { |
| 77 | + Serial.println(stripLen); |
| 78 | + } else { |
| 79 | + Serial.println(0); |
| 80 | + } |
| 81 | + break; |
| 82 | + case 'i': // set strip len |
| 83 | + case 'I': |
| 84 | + if (readBytes(data, 1, b >= 'a' && b <= 'z')) { |
| 85 | + stripLen = data[0]; |
| 86 | + stripInit(stripLen, STRIP_PIN); |
| 87 | + stripUpdate(); // Initialize all pixels to 'off' |
| 88 | + initialized = true; |
| 89 | + } |
| 90 | + break; |
| 91 | + case 'z': // switch off |
| 92 | + case 'Z': // switch off |
| 93 | + if (initialized) { |
| 94 | + stripOff(); |
| 95 | + } |
| 96 | + break; |
| 97 | + case 'l': // set light intensity |
| 98 | + case 'L': |
| 99 | + if (readBytes(data, 1, b >= 'a' && b <= 'z')) { |
| 100 | + light = data[0]; |
| 101 | + } |
| 102 | + break; |
| 103 | + case 'a': // all of same color |
| 104 | + case 'A': |
| 105 | + if (readBytes(data, 1, b >= 'a' && b <= 'z')) { |
| 106 | + Color c; |
| 107 | + HLtoRGB(data[0], light, &c); |
| 108 | + Serial.print(c.r); Serial.print(", "); |
| 109 | + Serial.print(c.g); Serial.print(", "); |
| 110 | + Serial.println(c.b); |
| 111 | + if (initialized) { |
| 112 | + stripAll(c); |
| 113 | + } |
| 114 | + } |
| 115 | + break; |
| 116 | + case 'c': // list of colors |
| 117 | + case 'C': |
| 118 | + if (readBytes(data, stripLen, b >= 'a' && b <= 'z')) { |
| 119 | + if (initialized) { |
| 120 | + for(int i = 0; i < stripLen; i++) { |
| 121 | + stripSetHL(i, data[i], light); |
| 122 | + } |
| 123 | + stripUpdate(); |
| 124 | + } |
| 125 | + } |
| 126 | + break; |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments