GPIO module added to simulation

This commit is contained in:
Ollo 2021-03-18 21:32:03 +01:00
parent efee1dab1f
commit 890c84dfbd
2 changed files with 93 additions and 0 deletions

View File

@ -13,6 +13,7 @@ import org.luaj.vm2.lib.jse.JsePlatform;
import de.c3ma.ollo.mockup.DoFileFunction; import de.c3ma.ollo.mockup.DoFileFunction;
import de.c3ma.ollo.mockup.ESP8266Adc; import de.c3ma.ollo.mockup.ESP8266Adc;
import de.c3ma.ollo.mockup.ESP8266File; import de.c3ma.ollo.mockup.ESP8266File;
import de.c3ma.ollo.mockup.ESP8266Gpio;
import de.c3ma.ollo.mockup.ESP8266Net; import de.c3ma.ollo.mockup.ESP8266Net;
import de.c3ma.ollo.mockup.ESP8266Node; import de.c3ma.ollo.mockup.ESP8266Node;
import de.c3ma.ollo.mockup.ESP8266Time; import de.c3ma.ollo.mockup.ESP8266Time;
@ -37,6 +38,7 @@ public class WS2812Simulation implements LuaSimulation {
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 ESP8266Gpio gpio = new ESP8266Gpio();
private ESP8266Adc adc = new ESP8266Adc(); private ESP8266Adc adc = new ESP8266Adc();
private String scriptName; private String scriptName;
@ -47,6 +49,7 @@ public class WS2812Simulation implements LuaSimulation {
globals.load(espFile); globals.load(espFile);
globals.load(espNode); globals.load(espNode);
globals.load(adc); globals.load(adc);
globals.load(gpio);
globals.load(new ESP8266Wifi()); globals.load(new ESP8266Wifi());
globals.load(new ESP8266Net()); globals.load(new ESP8266Net());
globals.load(new ESP8266Time()); globals.load(new ESP8266Time());

View File

@ -0,0 +1,90 @@
package de.c3ma.ollo.mockup;
import java.util.HashMap;
import javax.swing.SwingUtilities;
import org.luaj.vm2.LuaString;
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.Varargs;
import org.luaj.vm2.lib.OneArgFunction;
import org.luaj.vm2.lib.TwoArgFunction;
import org.luaj.vm2.lib.VarArgFunction;
/**
* created at 18.03.2021 - 21:09:03<br />
* creator: ollo<br />
* project: Esp8266 GPIO Emulation<br />
* $Id: $<br />
* @author ollo<br />
*/
public class ESP8266Gpio extends TwoArgFunction {
private static final String DIRECTION_INPUT = "input";
private HashMap<Integer, Integer> mInputs = new HashMap<Integer, Integer>();
@Override
public LuaValue call(LuaValue modname, LuaValue env) {
env.checkglobals();
final LuaTable gpio = new LuaTable();
gpio.set("mode", new Mode(this));
gpio.set("read", new Read(this));
gpio.set("INPUT", DIRECTION_INPUT);
env.set("gpio", gpio);
env.get("package").get("loaded").set("gpio", gpio);
return gpio;
}
private class Mode extends VarArgFunction {
private ESP8266Gpio gpio;
public Mode(ESP8266Gpio a) {
this.gpio = a;
}
public Varargs invoke(Varargs varargs) {
if (varargs.narg() == 2) {
final int pin = varargs.arg(1).toint();
final LuaString lsDirection = varargs.arg(2).checkstring();
String direction = lsDirection.toString();
if (direction.equals(DIRECTION_INPUT)) {
gpio.mInputs.put(pin, -1);
}
System.out.println("[GPIO] PIN" + pin +" as " + direction);
return LuaValue.valueOf(true);
} else {
return LuaValue.NIL;
}
}
}
private class Read extends OneArgFunction {
private ESP8266Gpio gpio;
public Read(ESP8266Gpio a) {
this.gpio = a;
}
@Override
public LuaValue call(LuaValue arg) {
int pin = arg.toint();
if (mInputs.containsKey(pin)) {
return LuaValue.valueOf(mInputs.get(pin));
} else {
System.out.println("[GPIO] pin" + pin + " not defined (gpio.mode missing)");
return LuaValue.NIL;
}
}
}
public void setPin(int pin, int newValue) {
if (mInputs.containsKey(pin)) {
mInputs.put(pin, newValue);
} else {
System.out.println("[GPIO] PIN" + pin +" not defined (missing gpio.mode)");
}
}
}