diff --git a/simulation/src/de/c3ma/ollo/WS2812Simulation.java b/simulation/src/de/c3ma/ollo/WS2812Simulation.java index 1c4cff9..4c15f8d 100644 --- a/simulation/src/de/c3ma/ollo/WS2812Simulation.java +++ b/simulation/src/de/c3ma/ollo/WS2812Simulation.java @@ -13,6 +13,7 @@ import org.luaj.vm2.lib.jse.JsePlatform; import de.c3ma.ollo.mockup.DoFileFunction; import de.c3ma.ollo.mockup.ESP8266Adc; import de.c3ma.ollo.mockup.ESP8266File; +import de.c3ma.ollo.mockup.ESP8266Gpio; import de.c3ma.ollo.mockup.ESP8266Net; import de.c3ma.ollo.mockup.ESP8266Node; import de.c3ma.ollo.mockup.ESP8266Time; @@ -37,6 +38,7 @@ public class WS2812Simulation implements LuaSimulation { private ESP8266Node espNode = new ESP8266Node(this); private DoFileFunction doFile = new DoFileFunction(globals); private ESP8266Ws2812 ws2812 = new ESP8266Ws2812(); + private ESP8266Gpio gpio = new ESP8266Gpio(); private ESP8266Adc adc = new ESP8266Adc(); private String scriptName; @@ -47,6 +49,7 @@ public class WS2812Simulation implements LuaSimulation { globals.load(espFile); globals.load(espNode); globals.load(adc); + globals.load(gpio); globals.load(new ESP8266Wifi()); globals.load(new ESP8266Net()); globals.load(new ESP8266Time()); diff --git a/simulation/src/de/c3ma/ollo/mockup/ESP8266Gpio.java b/simulation/src/de/c3ma/ollo/mockup/ESP8266Gpio.java new file mode 100644 index 0000000..93ee00c --- /dev/null +++ b/simulation/src/de/c3ma/ollo/mockup/ESP8266Gpio.java @@ -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
+ * creator: ollo
+ * project: Esp8266 GPIO Emulation
+ * $Id: $
+ * @author ollo
+ */ +public class ESP8266Gpio extends TwoArgFunction { + + private static final String DIRECTION_INPUT = "input"; + private HashMap mInputs = new HashMap(); + + @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)"); + } + } +}