Kai did magic

This commit is contained in:
C3MA 2018-01-12 20:38:35 +01:00
parent a83600281d
commit 78a9027ad7
6 changed files with 341 additions and 329 deletions

View File

@ -4,6 +4,5 @@
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="libs/luaj-jme-3.0.1.jar" sourcepath="libs/luaj-3.0.1/src"/> <classpathentry kind="lib" path="libs/luaj-jme-3.0.1.jar" sourcepath="libs/luaj-3.0.1/src"/>
<classpathentry kind="lib" path="libs/luaj-jse-3.0.1.jar"/> <classpathentry kind="lib" path="libs/luaj-jse-3.0.1.jar"/>
<classpathentry kind="lib" path="libs/luaj-sources-3.0.1.jar"/>
<classpathentry kind="output" path="bin"/> <classpathentry kind="output" path="bin"/>
</classpath> </classpath>

View File

@ -9,5 +9,5 @@ package de.c3ma.ollo;
*/ */
public interface LuaSimulation { public interface LuaSimulation {
public void reboottriggered(); public void rebootTriggered();
} }

View File

@ -5,6 +5,7 @@ import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import javax.management.RuntimeErrorException; import javax.management.RuntimeErrorException;
import javax.swing.SwingUtilities;
import org.luaj.vm2.Globals; import org.luaj.vm2.Globals;
import org.luaj.vm2.LuaValue; import org.luaj.vm2.LuaValue;
@ -30,127 +31,130 @@ import de.c3ma.ollo.mockup.ESP8266Ws2812;
*/ */
public class WS2812Simulation implements LuaSimulation { public class WS2812Simulation implements LuaSimulation {
private Globals globals = JsePlatform.standardGlobals(); private Globals globals = JsePlatform.standardGlobals();
private ESP8266Tmr espTmr = new ESP8266Tmr(); private ESP8266Tmr espTmr = new ESP8266Tmr();
private ESP8266File espFile = new ESP8266File(); private ESP8266File espFile = new ESP8266File();
private ESP8266Node espNode = new ESP8266Node(this); private ESP8266Node espNode = new ESP8266Node(this);
private DoFileFunction doFile = new DoFileFunction(globals); private DoFileFunction doFile = new DoFileFunction(globals);
private ESP8266Ws2812 ws2812 = new ESP8266Ws2812(); private ESP8266Ws2812 ws2812 = new ESP8266Ws2812();
private String scriptName; private String scriptName;
public WS2812Simulation(File sourceFolder) { public WS2812Simulation(File sourceFolder) {
globals.load(new ESP8266Uart()); globals.load(new ESP8266Uart());
globals.load(ws2812); globals.load(ws2812);
globals.load(espTmr); globals.load(espTmr);
globals.load(espFile); globals.load(espFile);
globals.load(espNode); globals.load(espNode);
globals.load(new ESP8266Wifi()); globals.load(new ESP8266Wifi());
globals.load(new ESP8266Net()); globals.load(new ESP8266Net());
globals.load(new ESP8266Time()); globals.load(new ESP8266Time());
globals.set("dofile", doFile); globals.set("dofile", doFile);
try { try {
File tempFile = File.createTempFile("NodemcuSimuFile", ""); File tempFile = File.createTempFile("NodemcuSimuFile", "");
File tempDir = new File(tempFile.getParent() + File.separator + "Nodemcu" + System.currentTimeMillis()); File tempDir = new File(tempFile.getParent() + File.separator + "Nodemcu" + System.currentTimeMillis());
tempDir.mkdir(); tempDir.mkdir();
System.out.println("[Nodemcu] Directory is " + tempDir.getAbsolutePath());
System.out.println("[Nodemcu] Directory is " + tempDir.getAbsolutePath()); // Copy all files into the temporary folder
for (File f : sourceFolder.listFiles()) {
Files.copy(f.toPath(), new File(tempDir.getAbsolutePath() + File.separator + f.getName()).toPath());
}
// Copy all files into the temporary folder espFile.setWorkingDirectory(tempDir);
for (File f : sourceFolder.listFiles()) { espNode.setWorkingDirectory(tempDir);
Files.copy(f.toPath(), new File(tempDir.getAbsolutePath() + File.separator + f.getName()).toPath()); doFile.setWorkingDirectory(tempDir);
} } catch (IOException e) {
System.err.println("[Nodemcu] " + e.getMessage());
espFile = null;
espNode = null;
}
}
espFile.setWorkingDirectory(tempDir); public static void main(String[] args) {
espNode.setWorkingDirectory(tempDir); SwingUtilities.invokeLater(new Runnable() {
doFile.setWorkingDirectory(tempDir); @Override
} catch (IOException e) { public void run() {
System.err.println("[Nodemcu] " + e.getMessage()); if (args.length == 0) {
espFile = null; printUsage();
espNode = null; return;
} }
}
public static void main(String[] args) { if (args.length >= 1) {
File f = new File(args[0]);
if (f.exists()) {
WS2812Simulation simu = new WS2812Simulation(f.getParentFile());
System.out.println("File : " + f.getAbsolutePath());
if (args.length == 0) { if (args.length >= 2) {
printUsage(); simu.setWS2812Layout(new File(args[1]));
return; }
} try {
if (args.length >= 3) {
File additionalFile = new File(args[2]);
if (additionalFile.exists() && (simu.doFile != null)) {
if (args.length >= 1) { Files.copy(additionalFile.toPath(), new File(simu.doFile.getWorkingDirectory()
File f = new File(args[0]); + File.separator + additionalFile.getName()).toPath());
if (f.exists()) { System.out.println("Integrate " + additionalFile.getName() + " into simulation");
WS2812Simulation simu = new WS2812Simulation(f.getParentFile()); } else {
System.out.println("File : " + f.getAbsolutePath()); System.err.println("Script " + args[2] + " cannot be found");
System.exit(1);
}
}
if (args.length >= 2) { simu.callScript(f.getName());
simu.setWS2812Layout(new File(args[1])); } catch (IOException e) {
} System.err.println("[Nodemcu] " + e.getMessage());
try { }
if (args.length >= 3) { }
File additionalFile = new File(args[2]); } else {
if (additionalFile.exists() && (simu.doFile != null)) { printUsage();
}
Files.copy(additionalFile.toPath(), }
new File(simu.doFile.getWorkingDirectory() + File.separator + additionalFile.getName()).toPath()); });
System.out.println("Integrate " + additionalFile.getName() + " into simulation"); }
} else {
System.err.println("Script " + args[2] + " cannot be found");
System.exit(1);
}
}
simu.callScript(f.getName()); private void setWS2812Layout(File file) {
} catch (IOException e) { if (file.exists()) {
System.err.println("[Nodemcu] " + e.getMessage()); ws2812.setLayout(file);
} } else {
} throw new RuntimeException("WS2812 Layout: " + file.getAbsolutePath() + " does not exists");
} else { }
printUsage(); }
}
} private static void printUsage() {
System.out.println("Usage:");
System.out.println("one argument required: file to execute.");
System.out.println(".e.g: init.lua");
}
private void setWS2812Layout(File file) { @Override
if (file.exists()) { public void rebootTriggered() {
ws2812.setLayout(file); System.out.println("=================== Reboot in Simulation -> call it again =================");
} else { this.espTmr.stopAllTimer();
throw new RuntimeException("WS2812 Layout: " + file.getAbsolutePath() + " does not exists"); try {
} Thread.sleep(200);
} if (this.scriptName != null) {
System.out.println("Reexecuting...");
callScript(this.scriptName);
}
} catch (InterruptedException e) {
private static void printUsage() { }
System.out.println("Usage:");
System.out.println("one argument required: file to execute.");
System.out.println(".e.g: init.lua");
}
@Override }
public void reboottriggered() {
System.out.println("=================== Reboot in Simulation -> call it again =================");
this.espTmr.stopAllTimer();
try {
Thread.sleep(200);
if (this.scriptName != null) {
System.out.println("Reexecuting...");
callScript(this.scriptName);
}
} catch (InterruptedException e) {
} private void callScript(String filename) {
this.scriptName = filename;
} if ((espFile != null) && (espFile.getFileInWorkingDir(filename) != null)) {
LuaValue chunk = globals.loadfile(espFile.getFileInWorkingDir(filename).getAbsolutePath());
private void callScript(String filename) { chunk.call();
this.scriptName=filename; } else {
throw new RuntimeException("Copy into temporary folder failed; script not available");
if ((espFile != null) && (espFile.getFileInWorkingDir(filename) != null)) { }
LuaValue chunk = globals.loadfile(espFile.getFileInWorkingDir(filename).getAbsolutePath()); }
chunk.call();
} else {
throw new RuntimeException("Copy into temporary folder failed; script not available");
}
}
} }

View File

@ -67,7 +67,7 @@ public class ESP8266Node extends TwoArgFunction {
@Override @Override
public LuaValue call() { public LuaValue call() {
System.out.println("[Node] Restart"); System.out.println("[Node] Restart");
nodemcuSimu.reboottriggered(); nodemcuSimu.rebootTriggered();
return LuaValue.valueOf(false); return LuaValue.valueOf(false);
} }

View File

@ -2,6 +2,9 @@ package de.c3ma.ollo.mockup;
import java.io.File; import java.io.File;
import javax.swing.SwingUtilities;
import org.luaj.vm2.LuaString;
import org.luaj.vm2.LuaTable; import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue; import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.OneArgFunction; import org.luaj.vm2.lib.OneArgFunction;
@ -15,62 +18,70 @@ import de.c3ma.ollo.mockup.ui.WS2812Layout;
* creator: ollo<br /> * creator: ollo<br />
* project: WS2812Emulation<br /> * project: WS2812Emulation<br />
* $Id: $<br /> * $Id: $<br />
*
* @author ollo<br /> * @author ollo<br />
*/ */
public class ESP8266Ws2812 extends TwoArgFunction { public class ESP8266Ws2812 extends TwoArgFunction {
private static WS2812Layout layout = null; private static WS2812Layout layout = null;
@Override @Override
public LuaValue call(LuaValue modname, LuaValue env) { public LuaValue call(LuaValue modname, LuaValue env) {
env.checkglobals(); env.checkglobals();
final LuaTable ws2812 = new LuaTable(); final LuaTable ws2812 = new LuaTable();
ws2812.set("init", new init()); ws2812.set("init", new init());
ws2812.set("write", new write()); ws2812.set("write", new write());
env.set("ws2812", ws2812); env.set("ws2812", ws2812);
env.get("package").get("loaded").set("ws2812", ws2812); env.get("package").get("loaded").set("ws2812", ws2812);
return ws2812; return ws2812;
} }
private class init extends ZeroArgFunction { private class init extends ZeroArgFunction {
@Override @Override
public LuaValue call() { public LuaValue call() {
System.out.println("[WS2812] init"); System.out.println("[WS2812] init");
return LuaValue.valueOf(true); return LuaValue.valueOf(true);
} }
} }
private class write extends OneArgFunction { private class write extends OneArgFunction {
@Override @Override
public LuaValue call(LuaValue arg) { public LuaValue call(LuaValue arg) {
if (arg.isstring()) { if (arg.isstring()) {
int length = arg.checkstring().rawlen(); LuaString jstring = arg.checkstring();
if ((length % 3) == 0) { int length = jstring.rawlen();
byte[] array = arg.toString().getBytes(); if ((length % 3) == 0) {
for (int i = 0; i < length; i+=3) { byte[] array = jstring.m_bytes;
if (ESP8266Ws2812.layout != null) { SwingUtilities.invokeLater(new Runnable() {
ESP8266Ws2812.layout.updateLED(i/3, array[i+0], array[i+1], array[i+2]); @Override
} public void run() {
} for (int i = 0; i < length; i += 3) {
} if (ESP8266Ws2812.layout != null) {
int r = array[i + 0]+(Byte.MIN_VALUE*-1);
int g = array[i + 1]+(Byte.MIN_VALUE*-1);
int b = array[i + 2]+(Byte.MIN_VALUE*-1);
ESP8266Ws2812.layout.updateLED(i / 3, r, g, b);
}
}
}
});
}
if (ESP8266Ws2812.layout == null) { if (ESP8266Ws2812.layout == null) {
System.out.println("[WS2812] write length:" + length); System.out.println("[WS2812] write length:" + length);
} else { } else {
/*ESP8266Ws2812.layout.update(ESP8266Ws2812.layout.getGraphics());*/ }
ESP8266Ws2812.layout.repaint(); }
} return LuaValue.valueOf(true);
} }
return LuaValue.valueOf(true); }
}
}
public void setLayout(File file) { public void setLayout(File file) {
if (ESP8266Ws2812.layout == null) { if (ESP8266Ws2812.layout == null) {
ESP8266Ws2812.layout = WS2812Layout.parse(file); ESP8266Ws2812.layout = WS2812Layout.parse(file);
} }
} }
} }

View File

@ -10,6 +10,7 @@ import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList; import java.util.ArrayList;
import javax.swing.BorderFactory; import javax.swing.BorderFactory;
@ -17,180 +18,177 @@ import javax.swing.JButton;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.SwingUtilities;
/** /**
* created at 02.01.2018 - 12:57:02<br /> * created at 02.01.2018 - 12:57:02<br />
* creator: ollo<br /> * creator: ollo<br />
* project: WS2812Emulation<br /> * project: WS2812Emulation<br />
* $Id: $<br /> * $Id: $<br />
*
* @author ollo<br /> * @author ollo<br />
*/ */
public class WS2812Layout extends JFrame { public class WS2812Layout extends JFrame {
/** /**
* *
*/ */
private static final long serialVersionUID = -6815557232118826140L; private static final long serialVersionUID = -6815557232118826140L;
private ArrayList<String> mLines = new ArrayList<String>(); private ArrayList<String> mLines = new ArrayList<String>();
private int mColumn = 0; private int mColumn = 0;
private int mRow = 0; private int mRow = 0;
private Element[][] mElements; private Element[][] mElements;
public static WS2812Layout parse(File file) { public static WS2812Layout parse(File file) {
WS2812Layout layout = null; WS2812Layout layout = null;
try { try {
BufferedReader br = new BufferedReader(new FileReader(file)); BufferedReader br = new BufferedReader(new FileReader(file));
try { try {
String line = br.readLine(); String line = br.readLine();
if (line != null) { if (line != null) {
layout = new WS2812Layout(); layout = new WS2812Layout();
} }
while (line != null) { while (line != null) {
if (!line.startsWith("#")) { if (!line.startsWith("#")) {
layout.mLines.add(line); layout.mLines.add(line);
layout.mRow++; layout.mRow++;
layout.mColumn = Math.max(layout.mColumn, line.length()); layout.mColumn = Math.max(layout.mColumn, line.length());
} }
/* get the next line */ /* get the next line */
line = br.readLine(); line = br.readLine();
} }
/* parse each line */ /* parse each line */
layout.parse(); layout.parse();
layout.createAndDisplayGUI(); layout.createAndDisplayGUI();
} finally { } finally {
if (br != null) { if (br != null) {
br.close(); br.close();
} }
} }
} catch (IOException ioe) { } catch (IOException ioe) {
} }
return layout; return layout;
} }
private void createAndDisplayGUI() { private void createAndDisplayGUI() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel(); JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout()); contentPane.setLayout(new BorderLayout());
contentPane.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 2)); contentPane.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 2));
JPanel ledPanel = new JPanel(); JPanel ledPanel = new JPanel();
ledPanel.setLayout(new GridLayout(this.mRow, this.mColumn, 10, 10)); ledPanel.setLayout(new GridLayout(this.mRow, this.mColumn, 10, 10));
for (int i = 0; i < this.mRow; i++) for (int i = 0; i < this.mRow; i++) {
{ for (int j = 0; j < this.mColumn; j++) {
for (int j = 0; j < this.mColumn; j++) if (this.mElements[i][j] != null) {
{ ledPanel.add(this.mElements[i][j]);
if (this.mElements[i][j] != null) { }
ledPanel.add(this.mElements[i][j]); }
} }
} contentPane.add(ledPanel, BorderLayout.CENTER);
} JButton button = new JButton("Do something");
contentPane.add(ledPanel, BorderLayout.CENTER); button.setActionCommand("Do something");
JButton button = new JButton("Do something"); button.addActionListener(new ActionListener() {
button.setActionCommand("Do something"); public void actionPerformed(ActionEvent ae) {
button.addActionListener(new ActionListener() JButton but = (JButton) ae.getSource();
{ // FIXME some clever logic
public void actionPerformed(ActionEvent ae) }
{ });
JButton but = (JButton) ae.getSource(); contentPane.add(button, BorderLayout.SOUTH);
//FIXME some clever logic
}
});
contentPane.add(button, BorderLayout.SOUTH);
setContentPane(contentPane);
pack();
setLocationByPlatform(true);
setVisible(true);
}
setContentPane(contentPane); private void parse() {
pack(); this.mElements = new Element[this.mRow][this.mColumn];
setLocationByPlatform(true); int row = 0;
setVisible(true); for (String line : this.mLines) {
} for (int i = 0; i < line.length(); i++) {
char c = line.charAt(i);
if ((('A' <= c) && (c <= 'Z')) || (('0' <= c) && (c <= '9')) || (c == 'Ä') || (c == 'Ö')
|| (c == 'Ü')) {
this.mElements[row][i] = new Element(c);
} else {
this.mElements[row][i] = new Element();
}
this.mElements[row][i].setColor(0, 0, 0);
}
row++;
}
}
private void parse() { public class Element extends JLabel {
this.mElements = new Element[this.mRow][this.mColumn];
int row=0;
for (String line : this.mLines) {
for (int i = 0; i < line.length(); i++) {
char c = line.charAt(i);
if ((('A' <= c) && (c <= 'Z')) ||
(('0' <= c) && (c <= '9')) ||
(c == 'Ä') || (c == 'Ö') || (c == 'Ü')) {
this.mElements[row][i] = new Element(c);
} else {
this.mElements[row][i] = new Element();
}
this.mElements[row][i].setColor(0, 0, 0);
}
row++;
}
}
public class Element extends JLabel { /**
*
*/
private static final long serialVersionUID = -3933903441113933637L;
/** private boolean noText = false;
*
*/
private static final long serialVersionUID = -3933903441113933637L;
private boolean noText=false; /**
* Draw a simple rect
*/
public Element() {
super();
this.noText = true;
this.setBackground(Color.BLACK);
}
/** /**
* Draw a simple rect * Draw a character
*/ *
public Element() { * @param character
super(); * to show
this.noText = true; */
this.setBackground(Color.BLACK); public Element(char character) {
} super("" + character);
setFont(new Font("Dialog", Font.BOLD, 24));
setHorizontalAlignment(CENTER);
// FIXME: Background color is not updated:
this.setBackground(Color.BLACK);
}
/** public void setColor(int red, int green, int blue) {
* Draw a character this.setForeground(new Color(red, green, blue));
* @param character to show // FIXME changing the color is not working
*/ this.repaint();
public Element(char character) { System.out.println(this.toString());
super(""+character); }
setFont(new Font("Dialog", Font.BOLD, 24));
setHorizontalAlignment(CENTER);
//FIXME: Background color is not updated:
this.setBackground(Color.BLACK);
}
public void setColor(int red, int green, int blue) { @Override
this.setForeground(new Color(red, green, blue)); public String toString() {
//FIXME changing the color is not working StringBuilder sb = new StringBuilder();
this.repaint(); if (noText) {
System.out.println( this.toString()); sb.append(" ");
} } else {
sb.append("" + this.getText());
}
sb.append("|" + Integer.toHexString(this.getForeground().getRed()) + " "
+ Integer.toHexString(this.getForeground().getGreen()) + " "
+ Integer.toHexString(this.getForeground().getBlue()));
@Override return sb.toString();
public String toString() { }
StringBuilder sb = new StringBuilder(); }
if (noText) {
sb.append(" ");
} else {
sb.append("" + this.getText());
}
sb.append("|" + Integer.toHexString(this.getForeground().getRed()) +
" " + Integer.toHexString(this.getForeground().getGreen()) +
" " + Integer.toHexString(this.getForeground().getBlue()));
return sb.toString(); public void updateLED(int index, int r, int g, int b) {
} if (mElements != null) {
} int i = (index / mColumn);
int j = (index % mColumn);
public void updateLED(int index, byte red, byte green, byte blue) { if ((i < mElements.length) && (j < mElements[i].length) && (mElements[i][j] != null)) {
if (this.mElements != null) { Element curlbl = mElements[i][j];
int i = (index / this.mColumn); curlbl.setColor(r, g, b);
int j = (index % this.mColumn); }
if ((i < this.mElements.length) && }
(j < this.mElements[i].length) && }
(this.mElements[i][j] != null)) {
this.mElements[i][j].setColor(red, green, blue);
}
}
}
} }