|
| 1 | + |
| 2 | + |
| 3 | + |
| 4 | +// Custom function accessible by the API |
| 5 | +void ledarr_act_fct() { |
| 6 | + |
| 7 | + // here you can do something |
| 8 | + if (DEBUG) Serial.println("ledarr_act_fct"); |
| 9 | + isBusy = true; |
| 10 | + |
| 11 | + |
| 12 | + const char* LEDArrMode = jsonDocument["LEDArrMode"]; // "array", "full", "full", "single", "off", "left", "right", "top", "bottom", |
| 13 | + |
| 14 | + // individual pattern gets adressed |
| 15 | + // PYTHON: send_LEDMatrix_array(self, led_pattern, timeout=1) |
| 16 | + if (strcmp(LEDArrMode, "array") == 0) { |
| 17 | + if (DEBUG) Serial.println("pattern"); |
| 18 | + for (int iled = 0; iled < LED_ARRAY_NUM; iled++) { //Iterate through results |
| 19 | + int red = jsonDocument["red"][iled]; //Implicit cast |
| 20 | + int green = jsonDocument["green"][iled]; //Implicit cast |
| 21 | + int blue = jsonDocument["blue"][iled]; //Implicit cast |
| 22 | + set_led_RGB(iled, red, green, blue); |
| 23 | + } |
| 24 | + } |
| 25 | + // only if a single led will be updated, all others stay the same |
| 26 | + // PYTHON: send_LEDMatrix_single(self, indexled=0, intensity=(255,255,255), timeout=1) |
| 27 | + else if (strcmp(LEDArrMode, "single") == 0) { |
| 28 | + if (DEBUG) Serial.println("single"); |
| 29 | + int indexled = jsonDocument["indexled"]; |
| 30 | + int red = jsonDocument["red"]; //Implicit cast |
| 31 | + int green = jsonDocument["green"]; //Implicit cast |
| 32 | + int blue = jsonDocument["blue"]; //Implicit cast |
| 33 | + //if (DEBUG) Serial.print(red); Serial.print(green); Serial.println(blue); |
| 34 | + set_led_RGB(indexled, red, green, blue); |
| 35 | + } |
| 36 | + // only few leds will be updated, all others stay the same |
| 37 | + // PYTHON: send_LEDMatrix_multi(self, indexled=(0), intensity=((255,255,255)), Nleds=8*8, timeout=1) |
| 38 | + else if (strcmp(LEDArrMode, "multi") == 0) { |
| 39 | + if (DEBUG) Serial.println("multi"); |
| 40 | + int Nleds = jsonDocument["Nleds"]; |
| 41 | + for (int i = 0; i < Nleds; i++) { //Iterate through results |
| 42 | + int indexled = jsonDocument["indexled"][i]; |
| 43 | + int red = jsonDocument["red"][indexled]; //Implicit cast |
| 44 | + int green = jsonDocument["green"][indexled]; //Implicit cast |
| 45 | + int blue = jsonDocument["blue"][indexled]; //Implicit cast |
| 46 | + //if (DEBUG) Serial.print(red); Serial.print(green); Serial.println(blue); |
| 47 | + set_led_RGB(indexled, red, green, blue); } |
| 48 | + } |
| 49 | + // turn on all LEDs |
| 50 | + // PYTHON: send_LEDMatrix_full(self, intensity = (255,255,255),timeout=1) |
| 51 | + else if (strcmp(LEDArrMode, "full") == 0) { |
| 52 | + if (DEBUG) Serial.println("full"); |
| 53 | + int red = jsonDocument["red"]; |
| 54 | + int green = jsonDocument["green"]; |
| 55 | + int blue = jsonDocument["blue"]; |
| 56 | + set_all(red, green, blue); |
| 57 | + } |
| 58 | + // turn off all LEDs |
| 59 | + else if (strcmp(LEDArrMode, "left") == 0) { |
| 60 | + if (DEBUG) Serial.println("left"); |
| 61 | + int red = jsonDocument["red"]; |
| 62 | + int green = jsonDocument["green"]; |
| 63 | + int blue = jsonDocument["blue"]; |
| 64 | + int NLeds = jsonDocument["NLeds"]; |
| 65 | + set_left(NLeds, red, green, blue); |
| 66 | + } |
| 67 | + // turn off all LEDs |
| 68 | + else if (strcmp(LEDArrMode, "right") == 0) { |
| 69 | + if (DEBUG) Serial.println("right"); |
| 70 | + int red = jsonDocument["red"]; |
| 71 | + int green = jsonDocument["green"]; |
| 72 | + int blue = jsonDocument["blue"]; |
| 73 | + int NLeds = jsonDocument["NLeds"]; |
| 74 | + set_right(NLeds, red, green, blue); |
| 75 | + } |
| 76 | + // turn off all LEDs |
| 77 | + else if (strcmp(LEDArrMode, "top") == 0) { |
| 78 | + if (DEBUG) Serial.println("top"); |
| 79 | + int red = jsonDocument["red"]; |
| 80 | + int green = jsonDocument["green"]; |
| 81 | + int blue = jsonDocument["blue"]; |
| 82 | + int NLeds = jsonDocument["NLeds"]; |
| 83 | + set_top(NLeds, red, green, blue); |
| 84 | + } |
| 85 | + // turn off all LEDs |
| 86 | + else if (strcmp(LEDArrMode, "bottom") == 0) { |
| 87 | + if (DEBUG) Serial.println("bottom"); |
| 88 | + int red = jsonDocument["red"]; |
| 89 | + int green = jsonDocument["green"]; |
| 90 | + int blue = jsonDocument["blue"]; |
| 91 | + int NLeds = jsonDocument["NLeds"]; |
| 92 | + set_bottom(NLeds, red, green, blue); |
| 93 | + } |
| 94 | + jsonDocument.clear(); |
| 95 | + jsonDocument["return"] = 1; |
| 96 | + jsonDocument["LEDArrMode"] = LEDArrMode; |
| 97 | + isBusy = false; |
| 98 | + |
| 99 | +} |
| 100 | + |
| 101 | +void ledarr_set_fct() { |
| 102 | + |
| 103 | + Serial.println("Updating Hardware config of LED Array"); |
| 104 | + |
| 105 | + jsonDocument.clear(); |
| 106 | + jsonDocument["return"] = 1; |
| 107 | +} |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | +// Custom function accessible by the API |
| 112 | +void ledarr_get_fct() { |
| 113 | + jsonDocument.clear(); |
| 114 | + jsonDocument["LED_ARRAY_PIN"] = LED_ARRAY_PIN; |
| 115 | + jsonDocument["LED_ARRAY_NUM"] = LED_ARRAY_NUM; |
| 116 | + |
| 117 | + |
| 118 | +} |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | +/***************************************************************************************************/ |
| 123 | +/******************************************* LED Array *******************************************/ |
| 124 | +/***************************************************************************************************/ |
| 125 | +/*******************************FROM OCTOPI ********************************************************/ |
| 126 | + |
| 127 | +void set_led_RGB(int iLed, int R, int G, int B) { |
| 128 | + matrix->setPixelColor(iLed, matrix->Color(R, G, B)); // Set pixel's color (in RAM) |
| 129 | + matrix->show(); // Update strip to match |
| 130 | +} |
| 131 | + |
| 132 | +void setup_matrix() { |
| 133 | + // LED Matrix |
| 134 | + if(DEBUG) Serial.println("Setting up LED array"); |
| 135 | + if(DEBUG) Serial.println("LED_ARRAY_PIN: " + String(LED_ARRAY_PIN)); |
| 136 | + matrix = new Adafruit_NeoPixel(LED_ARRAY_NUM, LED_ARRAY_PIN, NEO_GRB + NEO_KHZ800); |
| 137 | + matrix->begin(); |
| 138 | + matrix->setBrightness(255); |
| 139 | + set_all(0,0,0); |
| 140 | + delay(100); |
| 141 | + set_all(100,100,100); |
| 142 | +} |
| 143 | + |
| 144 | +void set_all(int R, int G, int B) |
| 145 | +{ |
| 146 | + for (int i = 0; i < (LED_ARRAY_NUM); i++) { |
| 147 | + set_led_RGB(i, R, G, B); |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +void set_left(int NLed, int R, int G, int B){ |
| 152 | +if(NLed == NLED4x4){ |
| 153 | + for (int i = 0; i < (NLED4x4); i++) { |
| 154 | + set_led_RGB(i, LED_PATTERN_DPC_LEFT_4x4[i]*R, LED_PATTERN_DPC_LEFT_4x4[i]*G, LED_PATTERN_DPC_LEFT_4x4[i]*B); |
| 155 | +}} |
| 156 | +if(NLed == NLED8x8){ |
| 157 | + for (int i = 0; i < (NLED8x8); i++) { |
| 158 | + set_led_RGB(i, LED_PATTERN_DPC_LEFT_8x8[i]*R, LED_PATTERN_DPC_LEFT_8x8[i]*G, LED_PATTERN_DPC_LEFT_8x8[i]*B); |
| 159 | +}} |
| 160 | +} |
| 161 | + |
| 162 | +void set_right(int NLed, int R, int G, int B){ |
| 163 | +if(NLed == NLED4x4){ |
| 164 | + for (int i = 0; i < (NLED4x4); i++) { |
| 165 | + set_led_RGB(i, (1-LED_PATTERN_DPC_LEFT_4x4[i])*R, (1-LED_PATTERN_DPC_LEFT_4x4[i])*G, (1-LED_PATTERN_DPC_LEFT_4x4[i])*B); |
| 166 | +}} |
| 167 | +if(NLed == NLED8x8){ |
| 168 | + for (int i = 0; i < (NLED8x8); i++) { |
| 169 | + set_led_RGB(i, (1-LED_PATTERN_DPC_LEFT_8x8[i])*R, (1-LED_PATTERN_DPC_LEFT_8x8[i])*G, (1-LED_PATTERN_DPC_LEFT_8x8[i])*B); |
| 170 | +}} |
| 171 | +} |
| 172 | + |
| 173 | +void set_top(int NLed, int R, int G, int B){ |
| 174 | +if(NLed == NLED4x4){ |
| 175 | + for (int i = 0; i < (NLED4x4); i++) { |
| 176 | + set_led_RGB(i, (LED_PATTERN_DPC_TOP_4x4[i])*R, (LED_PATTERN_DPC_TOP_4x4[i])*G, (LED_PATTERN_DPC_TOP_4x4[i])*B); |
| 177 | +}} |
| 178 | +if(NLed == NLED8x8){ |
| 179 | + for (int i = 0; i < (NLED8x8); i++) { |
| 180 | + set_led_RGB(i, (LED_PATTERN_DPC_TOP_8x8[i])*R, (LED_PATTERN_DPC_TOP_8x8[i])*G, (LED_PATTERN_DPC_TOP_8x8[i])*B); |
| 181 | +}} |
| 182 | +} |
| 183 | + |
| 184 | +void set_bottom(int NLed, int R, int G, int B){ |
| 185 | +if(NLed == NLED4x4){ |
| 186 | + for (int i = 0; i < (NLED4x4); i++) { |
| 187 | + set_led_RGB(i, (1-LED_PATTERN_DPC_TOP_4x4[i])*R, (1-LED_PATTERN_DPC_TOP_4x4[i])*G, (1-LED_PATTERN_DPC_TOP_4x4[i])*B); |
| 188 | +}} |
| 189 | +if(NLed == NLED8x8){ |
| 190 | + for (int i = 0; i < (NLED8x8); i++) { |
| 191 | + set_led_RGB(i, (1-LED_PATTERN_DPC_TOP_8x8[i])*R, (1-LED_PATTERN_DPC_TOP_8x8[i])*G, (1-LED_PATTERN_DPC_TOP_8x8[i])*B); |
| 192 | +}} |
| 193 | +} |
| 194 | + |
| 195 | +void set_center(int R, int G, int B) |
| 196 | +{ |
| 197 | + /* |
| 198 | + matrix->fillScreen(matrix->Color(0, 0, 0)); |
| 199 | + matrix->drawPixel(4, 4, matrix->Color(R, G, B)); |
| 200 | + matrix->show(); |
| 201 | + */ |
| 202 | +} |
| 203 | + |
| 204 | +/* |
| 205 | + wrapper for HTTP requests |
| 206 | +*/ |
| 207 | +void ledarr_act_fct_http() { |
| 208 | + String body = server.arg("plain"); |
| 209 | + deserializeJson(jsonDocument, body); |
| 210 | + ledarr_act_fct(); |
| 211 | + serializeJson(jsonDocument, output); |
| 212 | + server.send(200, "application/json", output); |
| 213 | +} |
| 214 | + |
| 215 | +// wrapper for HTTP requests |
| 216 | +void ledarr_get_fct_http() { |
| 217 | + String body = server.arg("plain"); |
| 218 | + deserializeJson(jsonDocument, body); |
| 219 | + ledarr_get_fct(); |
| 220 | + serializeJson(jsonDocument, output); |
| 221 | + server.send(200, "application/json", output); |
| 222 | +} |
| 223 | + |
| 224 | +// wrapper for HTTP requests |
| 225 | +void ledarr_set_fct_http() { |
| 226 | + String body = server.arg("plain"); |
| 227 | + deserializeJson(jsonDocument, body); |
| 228 | + ledarr_set_fct(); |
| 229 | + serializeJson(jsonDocument, output); |
| 230 | + server.send(200, "application/json", output); |
| 231 | +} |
0 commit comments