new effect randomfade, added a few effect parameters and commands

This commit is contained in:
interfisch 2016-12-23 01:43:06 +01:00
parent d4b5b41a78
commit 0da6f7a29d
1 changed files with 75 additions and 9 deletions

View File

@ -17,7 +17,13 @@ uint8_t effect=0;
uint8_t movingPoint_x=3; uint8_t movingPoint_x=3;
uint8_t movingPoint_y=3; uint8_t movingPoint_y=3;
uint8_t wheelPos=0; uint8_t wheelPos=0;
uint8_t wheelPosSlow=0; //for slower wheelPos increment than 1
int wheelSpeed=16; //16=+1/frame
int smoothing=80; //0 to 100. 100=no change (ultrasmooth), 0=no smoothing.
int strength=50; //how much pixels to apply color to
#define EFFECT_SPIRAL 2 #define EFFECT_SPIRAL 2
#define EFFECT_RANDOMFADE 3
int fadespeedmax=5; //1 to 255
@ -137,10 +143,10 @@ uint8_t getAverage(uint8_t array[NUMPIXELS], uint8_t i, int x, int y)
void led_smooth() void led_smooth()
{ {
for (int i=0; i < strip.numPixels(); i++) { for (int i=0; i < strip.numPixels(); i++) {
uint8_t avgbrightness=pixelR_buffer[i]/3+pixelG_buffer[i]/3+pixelB_buffer[i]/3; //uint8_t avgbrightness=pixelR_buffer[i]/3+pixelG_buffer[i]/3+pixelB_buffer[i]/3;
pixelR_buffer[i]=0.8*pixelR[i] + 0.2*getAverage(pixelR,i, 0,0); pixelR_buffer[i]=(smoothing/100.0)*pixelR[i] + (1.0-(smoothing/100.0))*getAverage(pixelR,i, 0,0);
pixelG_buffer[i]=0.8*pixelG[i] + 0.2*getAverage(pixelG,i, 0,0); pixelG_buffer[i]=(smoothing/100.0)*pixelG[i] + (1.0-(smoothing/100.0))*getAverage(pixelG,i, 0,0);
pixelB_buffer[i]=0.8*pixelB[i] + 0.2*getAverage(pixelB,i, 0,0); pixelB_buffer[i]=(smoothing/100.0)*pixelB[i] + (1.0-(smoothing/100.0))*getAverage(pixelB,i, 0,0);
} }
showBuffer(); showBuffer();
@ -149,7 +155,10 @@ void led_smooth()
void led_movingPoint() void led_movingPoint()
{ {
uint32_t c=wheel(wheelPos); uint32_t c=wheel(wheelPos);
wheelPos=(wheelPos+1)%255; wheelPosSlow+=wheelSpeed;
wheelPos=(wheelPos+ (wheelPosSlow/10) )%255;
wheelPosSlow=wheelPosSlow%16;
uint8_t r = (uint8_t)(c >> 16); uint8_t r = (uint8_t)(c >> 16);
uint8_t g = (uint8_t)(c >> 8); uint8_t g = (uint8_t)(c >> 8);
uint8_t b = (uint8_t)c; uint8_t b = (uint8_t)c;
@ -174,7 +183,7 @@ void led_movingPoint()
uint8_t startx=movingPoint_x; uint8_t startx=movingPoint_x;
uint8_t starty=movingPoint_y; uint8_t starty=movingPoint_y;
for (int i=0;i<50;i++){ for (int i=0;i<strength;i++){
movingPoint_x=startx+8+random(-random(0,2+1),random(0,2+1)+1); movingPoint_x=startx+8+random(-random(0,2+1),random(0,2+1)+1);
movingPoint_y=starty+8+random(-random(0,2+1),random(0,2+1)+1); movingPoint_y=starty+8+random(-random(0,2+1),random(0,2+1)+1);
@ -236,11 +245,30 @@ void led_spiral()
{ {
wheelPos++; wheelPos++;
for (int i=0; i < strip.numPixels(); i++) { for (int i=0; i < strip.numPixels(); i++) {
strip.setPixelColor(i,wheel((wheelPos+i*10)%255)); //strip.setPixelColor(i,wheel((wheelPos+i*5)%255));
} }
strip.show(); strip.show();
} }
void led_randomfade()
{
for (int i=0; i < strip.numPixels(); i++) {
pixelR_buffer[i]+=random(0,random(0,fadespeedmax+1)+1); //use buffer red channel for color wheel
strip.setPixelColor(i,wheel(pixelR_buffer[i]));
}
strip.show();
}
void set_randomBuffer()
{
for (int i=0; i < strip.numPixels(); i++) {
pixelR_buffer[i]=random(0,256);
pixelG_buffer[i]=random(0,256);
pixelB_buffer[i]=random(0,256);
}
}
uint32_t parseColor(String value){ uint32_t parseColor(String value){
@ -281,14 +309,33 @@ bool effectHandler(const HomieRange& range, const String& value) {
}else if (command.equals("random")){ }else if (command.equals("random")){
effect=EFFECT_NONE; effect=EFFECT_NONE;
led_random(); led_random();
}else if (command.equals("set")){ }else if (command.equals("set")){ //example: set|37#ff003a
effect=EFFECT_NONE; effect=EFFECT_NONE;
int x=parameters.substring(0,1).toInt(); int x=parameters.substring(0,1).toInt();
int y=parameters.substring(1,2).toInt(); int y=parameters.substring(1,2).toInt();
String cstr=parameters.substring(2,9); String cstr=parameters.substring(2,9);
strip.setPixelColor(xyToPos(x,y), parseColor(cstr)); strip.setPixelColor(xyToPos(x,y), parseColor(cstr));
strip.show(); strip.show();
}else if (command.equals("smooth")){ }else if (command.equals("smooth")){ //example: smooth|[wheelspeed]|[smoothing]|[strength] wheelspeed=1-255, smoothing=0-100, strength=1-255
int sepparam = parameters.indexOf("|");
int p1=parameters.substring(0,sepparam).toInt();
String parameters_part2=parameters.substring(sepparam+1);
sepparam = parameters_part2.indexOf("|");
int p2=parameters_part2.substring(0,sepparam).toInt();
int p3=parameters_part2.substring(sepparam+1).toInt();
wheelSpeed=16; //default, speed=+1 /frame
if (p1>0){
wheelSpeed=p1;
}
smoothing=80;
if (p2>0){
smoothing=p2;
}
strength=50;
if (p3>0){
strength=p3;
}
Homie.getLogger() << "-- p1=" << p1 << " p2=" << p2 << " p3=" << p3 << endl;
effect=EFFECT_SMOOTH; effect=EFFECT_SMOOTH;
bufferClear(); bufferClear();
showBuffer(); showBuffer();
@ -298,6 +345,22 @@ bool effectHandler(const HomieRange& range, const String& value) {
bufferClear(); bufferClear();
showBuffer(); showBuffer();
strip.show(); strip.show();
}else if (command.equals("clearbuffer")){
bufferClear();
showBuffer();
strip.show();
}else if (command.equals("randomfade")){ //example: randomfade|5
int sepparam = parameters.indexOf("|");
int p1=parameters.substring(0,sepparam).toInt();
fadespeedmax=5;
if (p1>0){
fadespeedmax=p1;
}
effect=EFFECT_RANDOMFADE;
set_randomBuffer(); //initialize random
}else if (command.equals("randombuffer")){
set_randomBuffer(); //set random
showBuffer();
} }
@ -368,6 +431,9 @@ void loop() {
case EFFECT_SPIRAL: case EFFECT_SPIRAL:
led_spiral(); led_spiral();
break; break;
case EFFECT_RANDOMFADE:
led_randomfade();
break;
} }
lastMillis=currentMillis; lastMillis=currentMillis;