diff --git a/simulation/src/de/c3ma/ollo/mockup/ESP8266Ws2812.java b/simulation/src/de/c3ma/ollo/mockup/ESP8266Ws2812.java index d739b3d..50b55ec 100644 --- a/simulation/src/de/c3ma/ollo/mockup/ESP8266Ws2812.java +++ b/simulation/src/de/c3ma/ollo/mockup/ESP8266Ws2812.java @@ -1,5 +1,6 @@ package de.c3ma.ollo.mockup; +import java.awt.Color; import java.io.File; import javax.swing.SwingUtilities; @@ -16,6 +17,7 @@ import org.luaj.vm2.lib.ZeroArgFunction; import de.c3ma.ollo.LuaSimulation; import de.c3ma.ollo.LuaThreadTmr; import de.c3ma.ollo.mockup.ui.WS2812Layout; +import de.c3ma.ollo.mockup.ui.WS2812Layout.Element; /** * created at 28.12.2017 - 23:34:04
@@ -58,6 +60,12 @@ public class ESP8266Ws2812 extends TwoArgFunction { if (arg.isstring()) { LuaString jstring = arg.checkstring(); final int length = jstring.rawlen(); + + if (ESP8266Ws2812.layout == null) { + System.err.println("[WS2812] Not initialized (" + length + "bytes to be updated)"); + return LuaValue.valueOf(false); + } + if ((length % 3) == 0) { final byte[] array = jstring.m_bytes; SwingUtilities.invokeLater(new Runnable() { @@ -74,11 +82,6 @@ public class ESP8266Ws2812 extends TwoArgFunction { } }); } - - if (ESP8266Ws2812.layout == null) { - System.out.println("[WS2812] write length:" + length); - } else { - } return LuaValue.valueOf(true); } else { System.out.println("[WS2812] write no string given"); @@ -102,6 +105,7 @@ public class ESP8266Ws2812 extends TwoArgFunction { final LuaTable rgbBuffer = new LuaTable(); rgbBuffer.set("fill", new bufferFill()); rgbBuffer.set("set", new bufferWrite()); + rgbBuffer.set("get", new bufferRead()); System.out.println("[WS2812] " + leds + "leds (" + bytesPerLeds + "bytes per led)"); return rgbBuffer; } else { @@ -162,4 +166,27 @@ public class ESP8266Ws2812 extends TwoArgFunction { } } } + + private class bufferRead extends OneArgFunction { + + @Override + public LuaValue call(LuaValue arg) { + final int offset = arg.toint(); + if (ESP8266Ws2812.layout != null) { + + Element e = ESP8266Ws2812.layout.getLED(offset); + if (e != null) { + Color color = e.getColor(); + final byte[] array = new byte[3]; + array[0] = (byte) color.getRed(); + array[1] = (byte) color.getGreen(); + array[2] = (byte) color.getBlue(); + return LuaString.valueOf(array); + } + } + + System.err.println("[WS2812] reading " + offset + " impossible"); + return LuaValue.NIL; + } + } } diff --git a/simulation/src/de/c3ma/ollo/mockup/ui/WS2812Layout.java b/simulation/src/de/c3ma/ollo/mockup/ui/WS2812Layout.java index 278f3a5..c43fd4c 100644 --- a/simulation/src/de/c3ma/ollo/mockup/ui/WS2812Layout.java +++ b/simulation/src/de/c3ma/ollo/mockup/ui/WS2812Layout.java @@ -288,6 +288,10 @@ public class WS2812Layout extends JFrame { this.setForeground(new Color(red, green, blue)); this.repaint(); } + + public Color getColor() { + return this.getForeground(); + } @Override public String toString() { @@ -320,6 +324,23 @@ public class WS2812Layout extends JFrame { } } + public Element getLED(int index) { + if (mElements != null) { + int i = (index / mColumn); + int j = (index % mColumn); + if (i % 2 == 1) { + j = (mColumn-1) - j; + } + if ((i < mElements.length) && (j < mElements[i].length) && (mElements[i][j] != null)) { + return mElements[i][j]; + } else { + return null; + } + } else { + return null; + } + } + public void fillLEDs(int r, int g, int b) { if (mElements != null) { for(int i=0;(i < mElements.length); i++) {