From e78b371e73ee88b3e59b1ee1446b1dbe5aa567c5 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 18 Dec 2016 23:37:20 +0100 Subject: [PATCH] random, set, off and pixels functions implemented --- pixelprojektor/pixelprojektor.ino | 98 +++++++++++++++++++++++++------ 1 file changed, 81 insertions(+), 17 deletions(-) diff --git a/pixelprojektor/pixelprojektor.ino b/pixelprojektor/pixelprojektor.ino index 311fb12..65505f8 100644 --- a/pixelprojektor/pixelprojektor.ino +++ b/pixelprojektor/pixelprojektor.ino @@ -11,23 +11,51 @@ Adafruit_NeoPixel strip = Adafruit_NeoPixel(64, PIN, NEO_GRB + NEO_KHZ800); HomieNode homieNode("pixel", "commands"); +int xyToPos(int x, int y){ //convert x y pixel position to matrix position + if (y%2==0){ + return (y*8+x); + }else{ + return (y*8+(7-x)); + } +} + +int numToPos(int num){ //convert pixel number to actual 8x8 matrix position + int x=num%8; + int y=num/8; + return xyToPos(x,y); +} + +uint32_t wheel(byte WheelPos) { + WheelPos = 255 - WheelPos; + if(WheelPos < 85) { + return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3); + } + if(WheelPos < 170) { + WheelPos -= 85; + return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3); + } + WheelPos -= 170; + return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0); +} + + void led_fill(uint32_t c) { for (int i=0; i < strip.numPixels(); i++) { - strip.setPixelColor(i, c); //turn every third pixel on + strip.setPixelColor(i, c); } strip.show(); } -bool effectHandler(const HomieRange& range, const String& value) { - int sep = value.indexOf("|"); - - - - Homie.getLogger() << "-> " << value << endl; - - //Serial.print("->"); Serial.println(value); +void led_random() +{ + for (int i=0; i < strip.numPixels(); i++) { + strip.setPixelColor(i, wheel(random(0,255))); + } + strip.show(); +} +uint32_t parseColor(String value){ if (value.charAt(0)=='#'){ //solid fill String color=value.substring(1); int number = (int) strtol( &color[0], NULL, 16); @@ -41,26 +69,62 @@ bool effectHandler(const HomieRange& range, const String& value) { //Serial.print("r=");Serial.print(r); //Serial.print(" g=");Serial.print(g); //Serial.print(" b=");Serial.println(b); + return strip.Color(r, g, b); - led_fill(strip.Color(r, g, b)); + } + return 0; +} + + +bool effectHandler(const HomieRange& range, const String& value) { + Homie.getLogger() << "-> " << value << endl; + int sep = value.indexOf("|"); + + String command=value.substring(0,sep); + String parameters=value.substring(sep+1); + Homie.getLogger() << "command=" << command << " parameters=" << parameters << endl; + + if (command.equals("fill")){ + led_fill(parseColor(parameters)); + }else if (command.equals("off")){ + led_fill(strip.Color(0, 0, 0)); + }else if (command.equals("random")){ + led_random(); + }else if (command.equals("set")){ + int x=parameters.substring(0,1).toInt(); + int y=parameters.substring(1,2).toInt(); + String cstr=parameters.substring(2,9); + strip.setPixelColor(xyToPos(x,y), parseColor(cstr)); + strip.show(); } + return true; } bool pixelsHandler(const HomieRange& range, const String& value) { - int sep = value.indexOf("|"); - - if(sep > 0) { + String remaining=value; + int i=0; + do{ + String current=remaining.substring(0,7); + Homie.getLogger() << i << ":" << current << endl; + uint32_t currentcolor=parseColor(current); + + strip.setPixelColor(i, currentcolor); + i++; - //Homie.getLogger() << "scroll " << value << " wait " << wait << endl; - - } else { + remaining=remaining.substring(7); - //Homie.getLogger() << "scroll " << value << endl; + }while(remaining.length()>2 && (i