-- ############################################
-- radio_ctrl (controller side)
-- version 0.1
-- http://hevohevo.hatenablog.com/
-- 1. Refer to turtle side program (http://pastebin.com/0ian5ggx).
-- 2. Input to the following.
-- > pastebin get uJfWnku6 radio_ctrl
-- 3. Edit config parameter, and execute this program.
-- "TurtleIDs" is important!
-- ################## config ##################
CtrlMonSide = "top"
ModemSide = "left"
TurtleIDs = {1,2,3}
----------------------------
-- btns[1] btns[2] btns[3]
-- btns[4] btns[5] btns[6]
-- btns[7] btns[8] btns[9]
-- btns[10] btns[11] btns[12]
----------------------------
Btns = {}
Btns[1] = {name="up", cmd="turtle.up()"}
Btns[2] = {name="forw", cmd="turtle.forward()"}
Btns[3] = {name="down", cmd="turtle.down()"}
Btns[4] = {name="<=", cmd="turtle.turnLeft()"}
Btns[5] = {name="back", cmd="turtle.back()"}
Btns[6] = {name="=>", cmd="turtle.turnRight()"}
Btns[7] = {name="refu", cmd="turtle.refuel()"}
Btns[8] = {name="dig", cmd="turtle.dig()"}
Btns[9] = {name="place", cmd="turtle.place()"}
Btns[10] = {name="sel1", cmd="turtle.select(1)"}
Btns[11] = {name="sel2", cmd="turtle.select(2)"}
Btns[12] = {name="sel16", cmd="turtle.select(16)"}
-- ################## functions ###############
-- send a message-string to turtles
function send_msg(s, turtle_ids)
print("send:",s)
for _,id in pairs(turtle_ids) do
rednet.send(id,s)
end
end
-- return btns-table
function makeButtons(ctrl_mon, Btns)
local mon_w, mon_h = ctrl_mon.getSize()
local btn_w = math.floor(mon_w/3)
local btn_h = math.floor(mon_h/4)
local btns = {}
local i=1 -- table index
for row=1,4 do
for col=1,3 do
btns[i] = {
name=Btns[i]["name"], cmd=Btns[i]["cmd"],
min_x = 1+(btn_w)*(col-1), min_y = 1+(btn_h)*(row-1),
max_x = btn_w*col, max_y = btn_h*row}
i = i+1
end
end
return btns
end
function drowButtons(mon, buttons)
for i,b in pairs(buttons) do
local center_x = math.floor((b.min_x + b.max_x)/2)
local center_y = math.floor((b.min_y + b.max_y)/2)
local center_label = math.floor(string.len(b.name)/2)
mon.setCursorPos(center_x - center_label, center_y)
mon.write(b.name)
end
end
-- whichButton(buttons, 1, 1) => btn (btn-table)
-- => false (don\'t pushed)
function whichButton(buttons, x, y)
local function within(min_num, max_num, num)
return (min_num <= num and max_num >= num)
end
local pushed_btn = false
for i,v in pairs(buttons) do
if within(v.min_x, v.max_x, x) and within(v.min_y, v.max_y, y) then
pushed_btn = v
break
end
end
return pushed_btn
end
-- event_name, pushed_btn = pullPushButtonEvent(buttons_table, "top")
function pullPushButtonEvent(buttons, mon_dir)
local pushed_btn = false
repeat
local event, dir, x, y = os.pullEvent("monitor_touch")
if mon_dir == dir then
pushed_btn = whichButton(buttons, x, y)
end
until pushed_btn
return "push_button", pushed_btn
end
-- ################## main #####################
-- init ctrl monitor
local CtrlMon = peripheral.wrap(CtrlMonSide)
CtrlMon.setTextScale(0.5)
CtrlMon.clear()
-- make buttons and drow on ctrl monitor
local mon_btns = makeButtons(CtrlMon, Btns)
drowButtons(CtrlMon, mon_btns)
-- init modem
rednet.open(ModemSide)
-- main loop
while true do
local event_name, pushed_btn = pullPushButtonEvent(mon_btns, CtrlMonSide)
if event_name == "push_button" then
print("pushed: ",pushed_btn.name)
send_msg(pushed_btn.cmd, TurtleIDs)
end
sleep(0) -- "monitor_touch" event is too sensitive.
end