From 9f7713da575076d8b37d0094ae36dbe8de2d5946 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Anian=20B=C3=BChler?=
 <anian.buehler@reutlingen-university.de>
Date: Mon, 15 Jul 2024 17:53:48 +0200
Subject: [PATCH] added neopixel effect-blocks

---
 .../block/output/NeopixelAinToColorBlock.java | 72 +++++++++++++++++++
 .../block/output/NeopixelRainbowBlock.java    | 35 +++++++++
 .../output/NeopixelRainbowChaseBlock.java     | 38 ++++++++++
 .../output/NeopixelTheaterChaseBlock.java     | 44 ++++++++++++
 .../block/output/NeopixelWipeBlock.java       | 43 +++++++++++
 5 files changed, 232 insertions(+)
 create mode 100644 src/main/java/com/ardublock/translator/block/output/NeopixelAinToColorBlock.java
 create mode 100644 src/main/java/com/ardublock/translator/block/output/NeopixelRainbowBlock.java
 create mode 100644 src/main/java/com/ardublock/translator/block/output/NeopixelRainbowChaseBlock.java
 create mode 100644 src/main/java/com/ardublock/translator/block/output/NeopixelTheaterChaseBlock.java
 create mode 100644 src/main/java/com/ardublock/translator/block/output/NeopixelWipeBlock.java

diff --git a/src/main/java/com/ardublock/translator/block/output/NeopixelAinToColorBlock.java b/src/main/java/com/ardublock/translator/block/output/NeopixelAinToColorBlock.java
new file mode 100644
index 0000000..71650d9
--- /dev/null
+++ b/src/main/java/com/ardublock/translator/block/output/NeopixelAinToColorBlock.java
@@ -0,0 +1,72 @@
+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;				
+		}
+}
diff --git a/src/main/java/com/ardublock/translator/block/output/NeopixelRainbowBlock.java b/src/main/java/com/ardublock/translator/block/output/NeopixelRainbowBlock.java
new file mode 100644
index 0000000..7865685
--- /dev/null
+++ b/src/main/java/com/ardublock/translator/block/output/NeopixelRainbowBlock.java
@@ -0,0 +1,35 @@
+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;
+				
+		}
+}
diff --git a/src/main/java/com/ardublock/translator/block/output/NeopixelRainbowChaseBlock.java b/src/main/java/com/ardublock/translator/block/output/NeopixelRainbowChaseBlock.java
new file mode 100644
index 0000000..03e4eff
--- /dev/null
+++ b/src/main/java/com/ardublock/translator/block/output/NeopixelRainbowChaseBlock.java
@@ -0,0 +1,38 @@
+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;
+		}
+}
diff --git a/src/main/java/com/ardublock/translator/block/output/NeopixelTheaterChaseBlock.java b/src/main/java/com/ardublock/translator/block/output/NeopixelTheaterChaseBlock.java
new file mode 100644
index 0000000..7640862
--- /dev/null
+++ b/src/main/java/com/ardublock/translator/block/output/NeopixelTheaterChaseBlock.java
@@ -0,0 +1,44 @@
+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;
+				
+		}
+}
diff --git a/src/main/java/com/ardublock/translator/block/output/NeopixelWipeBlock.java b/src/main/java/com/ardublock/translator/block/output/NeopixelWipeBlock.java
new file mode 100644
index 0000000..142441a
--- /dev/null
+++ b/src/main/java/com/ardublock/translator/block/output/NeopixelWipeBlock.java
@@ -0,0 +1,43 @@
+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;
+				
+		}
+}
-- 
GitLab