Created
December 21, 2021 23:36
-
-
Save cheapie/cb67fb6656a09a0438f5b9929e88412d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--libdetector Demo | |
--A product of Advanced Mesecons Devices, a Cheapie Systems company | |
--This is free and unencumbered software released into the public domain. | |
--See http://unlicense.org/ for more information | |
local libdetector = { | |
pre_hook = function() | |
if event.type == "program" then | |
mem.__libdetector_registered = {} | |
end | |
if event.type == "digiline" then | |
local channel = event.channel | |
if mem.__libdetector_registered[channel] then | |
if not mem.__libdetector_registered[channel].active then | |
mem.__libdetector_registered[channel].active = true | |
if not port[string.lower(mem.__libdetector_registered[channel].virtualpin)] then | |
event = { | |
type = "on", | |
virtual = true, | |
pin = { | |
name = string.upper(mem.__libdetector_registered[channel].virtualpin), | |
channel = channel, | |
}, | |
} | |
end | |
end | |
interrupt(mem.__libdetector_registered[channel].timeout,"__libdetector"..channel) | |
end | |
end | |
if event.type == "interrupt" and string.sub(event.iid,1,13) == "__libdetector" then | |
local channel = string.sub(event.iid,14,-1) | |
if mem.__libdetector_registered[channel] then | |
mem.__libdetector_registered[channel].active = false | |
if not port[string.lower(mem.__libdetector_registered[channel].virtualpin)] then | |
event = { | |
type = "off", | |
virtual = true, | |
pin = { | |
name = string.upper(mem.__libdetector_registered[channel].virtualpin), | |
channel = channel, | |
}, | |
} | |
end | |
end | |
end | |
for k,v in pairs(mem.__libdetector_registered) do | |
pin[string.lower(v.virtualpin)] = v.active or port[string.lower(v.virtualpin)] | |
end | |
end, | |
new = function(channel,virtualpin,timeout) | |
if not tonumber(timeout) then timeout = 1.5 end | |
timeout = math.max(0.5,timeout) | |
assert(type(channel) == "string","channel must be a string") | |
assert(type(virtualpin) == "string","virtualpin must be a string") | |
mem.__libdetector_registered[channel] = { | |
virtualpin = virtualpin, | |
timeout = timeout, | |
active = false, | |
} | |
end, | |
delete = function(channel) | |
if not mem.__libdetector_registered[channel] then return false end | |
mem.__libdetector_registered[channel] = nil | |
return true | |
end, | |
} | |
libdetector.pre_hook() | |
--CUT HERE-- | |
if event.type == "program" then | |
libdetector.new("detector","e") | |
elseif event.type == "on" and event.pin.name == "E" then | |
port.a = true | |
interrupt(0.5,"aoff") | |
elseif event.type == "off" and event.pin.name == "E" then | |
port.b = true | |
interrupt(0.5,"boff") | |
elseif event.iid == "aoff" then | |
port.a = false | |
elseif event.iid == "boff" then | |
port.b = false | |
end | |
port.c = pin.e |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment