Skip to content
Snippets Groups Projects
Verified Commit 9f7713da authored by Anian Bühler's avatar Anian Bühler
Browse files

added neopixel effect-blocks

parent 94fabdb1
Branches master
No related tags found
No related merge requests found
package com.ardublock.translator.block.output;
import com.ardublock.translator.Translator;
import com.ardublock.translator.block.TranslatorBlock;
import com.ardublock.translator.block.exception.SocketNullException;
import com.ardublock.translator.block.exception.SubroutineNotDeclaredException;
public class NeopixelAinToColorBlock extends TranslatorBlock {
public NeopixelAinToColorBlock (Long blockId, Translator translator, String codePrefix, String codeSuffix, String label)
{
super(blockId, translator, codePrefix, codeSuffix, label);
}
private final static String colorFunction = "int ainToColor(int value, int color) {\n"
+ " int rgbValue = map(constrain(value, 0, 1023), 0, 1023, 0, 255);\n"
+ "\n"
+ " int colorValue1 = 0;\n"
+ " int colorValue2 = 0;\n"
+ " int colorValue3 = 0;\n"
+ "\n"
+ "\n"
+ " if (value <= 341) { // Bereich von 0 bis 341: R -> G\n"
+ " colorValue1 = 255 - rgbValue * 3;\n"
+ " colorValue2 = rgbValue * 3;\n"
+ " colorValue3 = 0;\n"
+ " } else if (value <= 682) { // Bereich von 342 bis 682: G -> B\n"
+ " rgbValue -= 85;\n"
+ " colorValue1 = 0;\n"
+ " colorValue2 = 255 - rgbValue * 3;\n"
+ " colorValue3 = rgbValue * 3;\n"
+ " } else { // Bereich von 683 bis 1023: B -> R\n"
+ " rgbValue -= 170;\n"
+ " colorValue1 = rgbValue * 3;\n"
+ " colorValue2 = 0;\n"
+ " colorValue3 = 255 - rgbValue * 3;\n"
+ " }\n"
+ "\n"
+ " switch (color) {\n"
+ " case 1:\n"
+ " return colorValue1;\n"
+ " case 2:\n"
+ " return colorValue2;\n"
+ " break;\n"
+ "\n"
+ " case 3:\n"
+ " return colorValue3;\n"
+ " break;\n"
+ "\n"
+ " return -1;\n"
+ " }\n"
+ "}";
//@Override
public String toCode() throws SocketNullException, SubroutineNotDeclaredException
{
String analogValue ;
String color;
translator.addDefinitionCommand(colorFunction);
TranslatorBlock tb_ain = this.getRequiredTranslatorBlockAtSocket(0);
TranslatorBlock tb_color = this.getRequiredTranslatorBlockAtSocket(1);
analogValue = tb_ain.toCode().replaceAll("\\s*_.new\\b\\s*", "");
color = tb_color.toCode().replaceAll("\\s*_.new\\b\\s*", "");
String ret = "ainToColor("+analogValue+", "+color+")";
return codePrefix + ret + codeSuffix;
}
}
package com.ardublock.translator.block.output;
import com.ardublock.translator.Translator;
import com.ardublock.translator.block.TranslatorBlock;
import com.ardublock.translator.block.exception.SocketNullException;
import com.ardublock.translator.block.exception.SubroutineNotDeclaredException;
public class NeopixelRainbowBlock extends TranslatorBlock {
public NeopixelRainbowBlock (Long blockId, Translator translator, String codePrefix, String codeSuffix, String label)
{
super(blockId, translator, codePrefix, codeSuffix, label);
}
//@Override
public String toCode() throws SocketNullException, SubroutineNotDeclaredException
{
String pin ;
String wait;
String rainbowFunction;
TranslatorBlock tb_pin = this.getRequiredTranslatorBlockAtSocket(0);
TranslatorBlock tb_wait = this.getRequiredTranslatorBlockAtSocket(1);
pin = tb_pin.toCode().replaceAll("\\s*_.new\\b\\s*", "");
wait = tb_wait.toCode().replaceAll("\\s*_.new\\b\\s*", "");
String ret = "rainbow("+wait+");\n";
rainbowFunction = "void rainbow(int wait) {\nfor(long firstPixelHue = 0; firstPixelHue < 65536; firstPixelHue += 256) {\nfor(int i=0; i<strip_pin"+pin+".numPixels(); i++) {\nint pixelHue = firstPixelHue + (i * 65536L / strip_pin"+pin+".numPixels());\nstrip_pin"+pin+".setPixelColor(i, strip_pin"+pin+".gamma32(strip_pin"+pin+".ColorHSV(pixelHue)));\n}\nstrip_pin"+pin+".show();\ndelay(wait);\n}\n}";
translator.addDefinitionCommand(rainbowFunction);
return codePrefix + ret + codeSuffix;
}
}
package com.ardublock.translator.block.output;
import com.ardublock.translator.Translator;
import com.ardublock.translator.block.TranslatorBlock;
import com.ardublock.translator.block.exception.SocketNullException;
import com.ardublock.translator.block.exception.SubroutineNotDeclaredException;
public class NeopixelRainbowChaseBlock extends TranslatorBlock {
public NeopixelRainbowChaseBlock (Long blockId, Translator translator, String codePrefix, String codeSuffix, String label)
{
super(blockId, translator, codePrefix, codeSuffix, label);
}
//@Override
public String toCode() throws SocketNullException, SubroutineNotDeclaredException
{
String pin ;
String wait;
String times;
String rainbowFunction;
TranslatorBlock tb_pin = this.getRequiredTranslatorBlockAtSocket(0);
TranslatorBlock tb_wait = this.getRequiredTranslatorBlockAtSocket(1);
TranslatorBlock tb_times = this.getRequiredTranslatorBlockAtSocket(2);
pin = tb_pin.toCode().replaceAll("\\s*_.new\\b\\s*", "");
wait = tb_wait.toCode().replaceAll("\\s*_.new\\b\\s*", "");
times = tb_times.toCode().replaceAll("\\s*_.new\\b\\s*", "");
String ret = "theaterChaseRainbow("+wait+", "+ times +");\n";
rainbowFunction = "void theaterChaseRainbow(int wait, int times) {\nint firstPixelHue = 0;\nfor(int a=0; a<times; a++) {\nfor(int b=0; b<3; b++) {\nstrip_pin"+pin+".clear();\nfor(int c=b; c<strip_pin"+pin+".numPixels(); c += 3) {\nint hue = firstPixelHue + c * 65536L / strip_pin"+pin+".numPixels();\nuint32_t color = strip_pin"+pin+".gamma32(strip_pin"+pin+".ColorHSV(hue));\nstrip_pin"+pin+".setPixelColor(c, color);\n}\nstrip_pin"+pin+".show();\ndelay(wait);\nfirstPixelHue += 65536 / 90;\n}\n}\n}";
translator.addDefinitionCommand(rainbowFunction);
return codePrefix + ret + codeSuffix;
}
}
package com.ardublock.translator.block.output;
import com.ardublock.translator.Translator;
import com.ardublock.translator.block.TranslatorBlock;
import com.ardublock.translator.block.exception.SocketNullException;
import com.ardublock.translator.block.exception.SubroutineNotDeclaredException;
public class NeopixelTheaterChaseBlock extends TranslatorBlock {
public NeopixelTheaterChaseBlock (Long blockId, Translator translator, String codePrefix, String codeSuffix, String label)
{
super(blockId, translator, codePrefix, codeSuffix, label);
}
//@Override
public String toCode() throws SocketNullException, SubroutineNotDeclaredException
{
String pin;
String red, green, blue;
String wait, times;
String theaterFunction;
TranslatorBlock tb_pin = this.getRequiredTranslatorBlockAtSocket(0);
TranslatorBlock tb_red = this.getRequiredTranslatorBlockAtSocket(1);
TranslatorBlock tb_green = this.getRequiredTranslatorBlockAtSocket(2);
TranslatorBlock tb_blue = this.getRequiredTranslatorBlockAtSocket(3);
TranslatorBlock tb_wait = this.getRequiredTranslatorBlockAtSocket(4);
TranslatorBlock tb_times = this.getRequiredTranslatorBlockAtSocket(5);
pin = tb_pin.toCode().replaceAll("\\s*_.new\\b\\s*", "");
red = tb_red.toCode().replaceAll("\\s*_.new\\b\\s*", "");
green = tb_green.toCode().replaceAll("\\s*_.new\\b\\s*", "");
blue = tb_blue.toCode().replaceAll("\\s*_.new\\b\\s*", "");
wait = tb_wait.toCode().replaceAll("\\s*_.new\\b\\s*", "");
times = tb_times.toCode().replaceAll("\\s*_.new\\b\\s*", "");
String ret = "theaterChase("+red+", "+green+", "+blue+", "+wait+");\n";
theaterFunction = "void theaterChase(uint8_t red, uint8_t green, uint8_t blue, int wait) {\nuint32_t color = strip_pin"+pin+".Color( red, green, blue);\nfor(int a=0; a<"+times+"; a++) { \nfor(int b=0; b<3; b++) {\nstrip_pin"+pin+".clear();\nfor(int c=b; c<strip_pin"+pin+".numPixels(); c += 3) {\nstrip_pin"+pin+".setPixelColor(c, color);\n}\nstrip_pin"+pin+".show();\ndelay(wait);\n}\n}\n}\n";
translator.addDefinitionCommand(theaterFunction);
return codePrefix + ret + codeSuffix;
}
}
package com.ardublock.translator.block.output;
import com.ardublock.translator.Translator;
import com.ardublock.translator.block.TranslatorBlock;
import com.ardublock.translator.block.exception.SocketNullException;
import com.ardublock.translator.block.exception.SubroutineNotDeclaredException;
public class NeopixelWipeBlock extends TranslatorBlock {
public NeopixelWipeBlock (Long blockId, Translator translator, String codePrefix, String codeSuffix, String label)
{
super(blockId, translator, codePrefix, codeSuffix, label);
}
//@Override
public String toCode() throws SocketNullException, SubroutineNotDeclaredException
{
String pin;
String red, green, blue;
String wait;
String wipeFunction;
TranslatorBlock tb_pin = this.getRequiredTranslatorBlockAtSocket(0);
TranslatorBlock tb_red = this.getRequiredTranslatorBlockAtSocket(1);
TranslatorBlock tb_green = this.getRequiredTranslatorBlockAtSocket(2);
TranslatorBlock tb_blue = this.getRequiredTranslatorBlockAtSocket(3);
TranslatorBlock tb_wait = this.getRequiredTranslatorBlockAtSocket(4);
pin = tb_pin.toCode().replaceAll("\\s*_.new\\b\\s*", "");
red = tb_red.toCode().replaceAll("\\s*_.new\\b\\s*", "");
green = tb_green.toCode().replaceAll("\\s*_.new\\b\\s*", "");
blue = tb_blue.toCode().replaceAll("\\s*_.new\\b\\s*", "");
wait = tb_wait.toCode().replaceAll("\\s*_.new\\b\\s*", "");
String ret = "colorWipe("+red+", "+green+", "+blue+", "+wait+");\n";
wipeFunction = "void colorWipe(uint8_t red, uint8_t green, uint8_t blue, int wait) {\nuint32_t color = strip_pin"+pin+".Color( red, green, blue);\nfor(int i=0; i<strip_pin"+pin+".numPixels(); i++) {\nstrip_pin"+pin+".setPixelColor(i, color);\nstrip_pin"+pin+".show();\ndelay(wait);\n}\n}\n";
translator.addDefinitionCommand(wipeFunction);
return codePrefix + ret + codeSuffix;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment