forked from Mirrorlandia_minetest/digistuff
Add examples for a few devices
This commit is contained in:
parent
ee316f2325
commit
32641893e7
1
init.lua
1
init.lua
@ -21,6 +21,7 @@ local components = {
|
|||||||
"sillystuff",
|
"sillystuff",
|
||||||
"movestone",
|
"movestone",
|
||||||
"lclibraries",
|
"lclibraries",
|
||||||
|
"lcdocs",
|
||||||
}
|
}
|
||||||
|
|
||||||
if minetest.get_modpath("mesecons_luacontroller") then table.insert(components,"ioexpander") end
|
if minetest.get_modpath("mesecons_luacontroller") then table.insert(components,"ioexpander") end
|
||||||
|
18
lc_examples/button.lua
Normal file
18
lc_examples/button.lua
Normal file
@ -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
|
14
lc_examples/ioexpander.lua
Normal file
14
lc_examples/ioexpander.lua
Normal file
@ -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
|
24
lc_examples/light.lua
Normal file
24
lc_examples/light.lua
Normal file
@ -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)
|
81
lc_examples/memory.lua
Normal file
81
lc_examples/memory.lua
Normal file
@ -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 <channel>: Changes the digilines channel the memory device is attached to, default is \"memory\"")
|
||||||
|
print("clear: Clears the screen")
|
||||||
|
print("read <address>: Reads from the specified address and displays the received data")
|
||||||
|
print("write <address> <data>: 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
|
19
lc_examples/piston.lua
Normal file
19
lc_examples/piston.lua
Normal file
@ -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
|
15
lcdocs.lua
Normal file
15
lcdocs.lua
Normal file
@ -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
|
2
mod.conf
2
mod.conf
@ -2,4 +2,4 @@ name = digistuff
|
|||||||
title = digistuff
|
title = digistuff
|
||||||
description = Random digilines devices for Minetest
|
description = Random digilines devices for Minetest
|
||||||
depends = digilines
|
depends = digilines
|
||||||
optional_depends = default,mesecons,mesecons_mvps,screwdriver,pipeworks
|
optional_depends = default,mesecons,mesecons_mvps,screwdriver,pipeworks,mesecons_luacontroller
|
||||||
|
Loading…
Reference in New Issue
Block a user