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

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

View File

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

View File

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