Created
February 16, 2016 17:29
-
-
Save xingrz/17a6793dab532f6ee6dc to your computer and use it in GitHub Desktop.
Minecraft Computer Train Controlling System 1.0 (Standard)
This file contains hidden or 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
local rs = require("component").redstone | |
local digital = {} | |
function digital.on(side, port) | |
return rs.getBundledInput(side, port) > 0x00 | |
end | |
function digital.set(side, port, on) | |
if (on) then | |
rs.setBundledOutput(side, port, 0xFF) | |
else | |
rs.setBundledOutput(side, port, 0x00) | |
end | |
end | |
return digital |
This file contains hidden or 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
local event = require("event") | |
local component = require("component") | |
local sides = require("sides") | |
local colors = require("colors") | |
local rs = component.redstone | |
local segment = require("segment") | |
local digital = require("digital") | |
local revt = require("revt") | |
-- io configs | |
local DURATION = 10 | |
local SIDE_DISPLAY = sides.right | |
local SIDE_CONTROL = sides.left | |
local PORT_DETECT = colors.yellow | |
local PORT_SIGNAL = colors.lime | |
local PORT_LOCK = colors.pink | |
-- controls | |
function on(port) | |
return digital.on(SIDE_CONTROL, port) | |
end | |
function set(port, on) | |
digital.set(SIDE_CONTROL, port, on) | |
end | |
-- countdown | |
local countdown = { number = 0, _timer = nil } | |
function countdown.start(callback) | |
countdown.number = DURATION | |
countdown._update() | |
countdown._timer = event.timer(1, function() | |
countdown._tick() | |
countdown._update() | |
countdown.go(callback) | |
end, 115) | |
end | |
function countdown.stop() | |
if (countdown._timer ~= nil) then | |
event.cancel(countdown._timer) | |
countdown._timer = nil | |
end | |
segment.clear(SIDE_DISPLAY) | |
end | |
function countdown.go(callback) | |
if (countdown.number <= 0) then | |
callback(-countdown.number) | |
end | |
end | |
function countdown._tick() | |
countdown.number = countdown.number - 1 | |
end | |
function countdown._update() | |
segment.put(SIDE_DISPLAY, math.abs(countdown.number)) | |
end | |
------------ | |
print("Minecraft 计算机列控系统 1.0 (标准版)") | |
print("===========================================\n") | |
print(string.format("%s 数码管自检中…\n", os.date())) | |
segment.put(SIDE_DISPLAY, 11) | |
os.sleep(0.5) | |
segment.put(SIDE_DISPLAY, 25) | |
os.sleep(0.5) | |
segment.put(SIDE_DISPLAY, 88) | |
os.sleep(0.5) | |
segment.clear(SIDE_DISPLAY) | |
print(string.format("%s 系统启动完成\n", os.date())) | |
local tookoff = true | |
function takeoff(delayed) | |
if (tookoff) then | |
return | |
end | |
if (on(PORT_SIGNAL)) then | |
tookoff = true | |
set(PORT_LOCK, true) | |
countdown.stop() | |
if (delayed > 0) then | |
print(string.format("%s 列车发出,延误 %d 秒\n", os.date(), delayed)) | |
else | |
print(string.format("%s 列出发出,准点\n", os.date())) | |
end | |
end | |
end | |
revt.listen(SIDE_CONTROL, PORT_DETECT, function(detected) | |
if (detected) then | |
tookoff = false | |
set(PORT_LOCK, false) | |
countdown.start(takeoff) | |
print(string.format("%s 列车停靠", os.date())) | |
else | |
countdown.stop() | |
end | |
end) | |
revt.listen(SIDE_CONTROL, PORT_SIGNAL, function(signal) | |
countdown.go(takeoff) | |
end) | |
-- run | |
function handle(evt, ...) | |
if (evt == "redstone_changed") then | |
revt.handle(SIDE_CONTROL)(...) | |
end | |
end | |
while true do | |
handle(event.pull()) | |
end |
This file contains hidden or 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
local digital = require("digital") | |
local revt = { listeners = {}, last = {} } | |
function revt.handle(side) | |
return function(address, which) | |
if (side == which) then | |
for port, listener in pairs(revt.listeners) do | |
compare(side, port, listener) | |
end | |
end | |
end | |
end | |
function revt.listen(side, port, listener) | |
revt.listeners[port] = listener | |
compare(side, port, listener) | |
end | |
function compare(side, port, listener) | |
local on = digital.on(side, port) | |
if (revt.last[port] ~= on) then | |
revt.last[port] = on | |
listener(on) | |
end | |
end | |
return revt |
This file contains hidden or 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
local rs = require("component").redstone | |
local segment = {} | |
local numbers = { | |
[0] = 0xFC, [1] = 0x60, [2] = 0xDA, [3] = 0xF2, [4] = 0x66, | |
[5] = 0xB6, [6] = 0xBE, [7] = 0xE0, [8] = 0xFE, [9] = 0xF6 | |
} | |
local off = { [0] = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } | |
function segment.put(display, number) | |
local l = number % 10 | |
local h = (number - l) / 10 | |
local result = {} | |
local ls = numbers[l] | |
for i = 7, 0, -1 do | |
result[i] = bit32.band(ls, 1) * 0xF | |
ls = bit32.rshift(ls, 1) | |
end | |
local hs = numbers[h] | |
for i = 15, 8, -1 do | |
result[i] = bit32.band(hs, 1) * 0xF | |
hs = bit32.rshift(hs, 1) | |
end | |
rs.setBundledOutput(display, result) | |
end | |
local countdown = 0 | |
function segment.clear(display) | |
rs.setBundledOutput(display, off) | |
end | |
return segment |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment