From 32641893e75f11903489a38a201bd661c8f99b50 Mon Sep 17 00:00:00 2001 From: cheapie Date: Fri, 7 Jan 2022 16:51:04 -0600 Subject: Add examples for a few devices --- init.lua | 1 + lc_examples/button.lua | 18 +++++++++++ lc_examples/ioexpander.lua | 14 ++++++++ lc_examples/light.lua | 24 ++++++++++++++ lc_examples/memory.lua | 81 ++++++++++++++++++++++++++++++++++++++++++++++ lc_examples/piston.lua | 19 +++++++++++ lcdocs.lua | 15 +++++++++ mod.conf | 2 +- 8 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 lc_examples/button.lua create mode 100644 lc_examples/ioexpander.lua create mode 100644 lc_examples/light.lua create mode 100644 lc_examples/memory.lua create mode 100644 lc_examples/piston.lua create mode 100644 lcdocs.lua diff --git a/init.lua b/init.lua index ee719ab..821ab57 100644 --- a/init.lua +++ b/init.lua @@ -21,6 +21,7 @@ local components = { "sillystuff", "movestone", "lclibraries", + "lcdocs", } if minetest.get_modpath("mesecons_luacontroller") then table.insert(components,"ioexpander") end diff --git a/lc_examples/button.lua b/lc_examples/button.lua new file mode 100644 index 0000000..951d45a --- /dev/null +++ b/lc_examples/button.lua @@ -0,0 +1,18 @@ +--Digilines Button Example + +--Connect a button on the channel "button" with any message. +--When the button is pressed, pin A will toggle. +--If manual light control is selected on the button, the button light will also be flashing. +--If the button has a message set, it will be sent to an LCD on channel "lcd" + +if event.type == "program" then + mem.flash = false + interrupt(0,"flash") +elseif event.iid == "flash" then + mem.flash = not mem.flash + digiline_send("button","light_"..(mem.flash and "on" or "off")) + interrupt(1,"flash",true) +elseif event.channel == "button" then + port.a = not port.a + digiline_send("lcd",event.msg) +end diff --git a/lc_examples/ioexpander.lua b/lc_examples/ioexpander.lua new file mode 100644 index 0000000..02a8fee --- /dev/null +++ b/lc_examples/ioexpander.lua @@ -0,0 +1,14 @@ +--Digilines I/O Expander Example + +--Connect two I/O expanders, one on channel "expander1" and one on "expander2" +--The pins on the second expander will follow the input states on the first one. +--In addition, whenever an input on the first expander changes, a line will be logged to the terminal with the new state. + +local function iostr(pin) + return (pin and "1" or "0") +end + +if event.channel == "expander1" then + digiline_send("expander2",event.msg) + print(string.format("A: %s B: %s C: %s D: %s",iostr(event.msg.a),iostr(event.msg.b),iostr(event.msg.c),iostr(event.msg.d))) +end diff --git a/lc_examples/light.lua b/lc_examples/light.lua new file mode 100644 index 0000000..6914167 --- /dev/null +++ b/lc_examples/light.lua @@ -0,0 +1,24 @@ +--Digilines Dimmable Light Demo + +--Connect one or more lights on the channel "light" +--Send pulses on: +----Pin A to make the light brighter +----Pin B to make the light dimmer +----Pin C to set the light to full brightness +----Pin D to turn the light off + +if event.type == "program" then + mem.light = 0 +elseif event.type == "on" then + if event.pin.name == "A" then + mem.light = math.min(14,mem.light+1) + elseif event.pin.name == "B" then + mem.light = math.max(0,mem.light-1) + elseif event.pin.name == "C" then + mem.light = 14 + elseif event.pin.name == "D" then + mem.light = 0 + end +end + +digiline_send("light",mem.light) diff --git a/lc_examples/memory.lua b/lc_examples/memory.lua new file mode 100644 index 0000000..cf38121 --- /dev/null +++ b/lc_examples/memory.lua @@ -0,0 +1,81 @@ +--Digilines Memory Example + +--Connect an EEPROM or SRAM chip on channel "memory" +--Enter "help" in the terminal for a command list + +local function runcmd(command) + if command ~= "" then + print(" "..command,true) + print() + end + if string.sub(command,1,4) == "read" then + local address = string.sub(command,6,7) + if string.sub(address,2,2) == " " then address = string.sub(address,1,1) end + address = tonumber(address) + if (not address) or address > 31 or address < 0 or math.floor(address) ~= address then + print("Invalid address - address must be an integer from 0 to 31") + else + digiline_send(mem.channel,{command = "read",address = address}) + mem.readsuccess = false + interrupt(1,"readtimeout") + return --Suppress printing a new prompt + end + elseif string.sub(command,1,5) == "write" then + local address = string.sub(command,7,8) + local data = string.sub(command,10,-1) + if string.sub(address,2,2) == " " then + address = string.sub(command,7,7) + data = string.sub(command,9,-1) + end + address = tonumber(address) + if (not address) or address > 31 or address <0 or math.floor(address) ~= address then + print("Invalid address - address must be an integer from 0 to 31") + elseif data == "" then + print("No data specified") + else + print(string.format("Wrote to address %d",address)) + digiline_send(mem.channel,{command = "write",address = address,data = data}) + end + elseif string.sub(command,1,7) == "channel" then + local channel = string.sub(command,9,-1) + if channel and channel ~= "" then + mem.channel = channel + print("Channel changed") + else + print("No channel specified") + end + elseif command == "clear" then + clearterm() + print(">") + return --Suppress printing a new prompt - it was done already to omit the blank line + elseif command == "help" then + print("Available commands:") + print("channel : Changes the digilines channel the memory device is attached to, default is \"memory\"") + print("clear: Clears the screen") + print("read
: Reads from the specified address and displays the received data") + print("write
: Writes the specified data to the specified address") + print("help: Shows this help message") + elseif command == "" then + --Do nothing + else + print("Unknown command - use \"help\" to see a list of valid commands.") + end + print() + print(">") +end + +if event.type == "program" then + mem.channel = "memory" + runcmd("clear") +elseif event.iid == "readtimeout" and not mem.readsuccess then + print("No response received. Is there a memory device connected on the channel \""..mem.channel.."\"?") + print() + print(">") +elseif event.channel == mem.channel then + mem.readsuccess = true + print(event.msg) + print() + print(">") +elseif event.type == "terminal" then + runcmd(event.text) +end diff --git a/lc_examples/piston.lua b/lc_examples/piston.lua new file mode 100644 index 0000000..938acbb --- /dev/null +++ b/lc_examples/piston.lua @@ -0,0 +1,19 @@ +--Digilines Piston Example + +--Connect the piston on the channel "piston" +--Pulse pin A to extend the piston +--Pulse pin B to retract the piston +--Pulse pin C to retract the piston, pulling one node back +--Pulse pin D to silently retract the piston, pulling up to 5 nodes back + +if event.type == "on" then + if event.pin.name == "A" then + digiline_send("piston","extend") + elseif event.pin.name == "B" then + digiline_send("piston","retract") + elseif event.pin.name == "C" then + digiline_send("piston","retract_sticky") + elseif event.pin.name == "D" then + digiline_send("piston",{action = "retract",allsticky = true,max = 5,sound = "none"}) + end +end diff --git a/lcdocs.lua b/lcdocs.lua new file mode 100644 index 0000000..6661af4 --- /dev/null +++ b/lcdocs.lua @@ -0,0 +1,15 @@ +if (not minetest.global_exists("mesecon")) or type(mesecon.lc_docs) ~= "table" then return end + +local examples = { + ["Digilines Piston"] = "piston.lua", + ["Digilines Button"] = "button.lua", + ["Digilines I/O Expander"] = "ioexpander.lua", + ["Digilines EEPROM/SRAM"] = "memory.lua", + ["Digilines Dimmable Light"] = "light.lua", +} + +for k,v in pairs(examples) do + local f = io.open(minetest.get_modpath("digistuff")..DIR_DELIM.."lc_examples"..DIR_DELIM..v,"r") + mesecon.lc_docs.examples[k] = f:read("*all") + f:close() +end diff --git a/mod.conf b/mod.conf index 15edfcf..dc49b4a 100644 --- a/mod.conf +++ b/mod.conf @@ -2,4 +2,4 @@ name = digistuff title = digistuff description = Random digilines devices for Minetest depends = digilines -optional_depends = default,mesecons,mesecons_mvps,screwdriver,pipeworks +optional_depends = default,mesecons,mesecons_mvps,screwdriver,pipeworks,mesecons_luacontroller -- cgit v1.2.3