Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ############################################
- -- radio_ctrl (controller side)
- -- version 0.2
- -- http://hevohevo.hatenablog.com/
- -- 1. Refer to turtle side program (http://pastebin.com/z6QVrviC).
- -- 2. Input to the following.
- -- > pastebin get uJfWnku6 radio_ctrl
- -- 3. Edit config parameter, and execute this program.
- os.loadAPI('h2peripheral')
- -- ################## config ##################
- TurtleIDs = {65535} -- 65535: BROADCAST Channel
- ----------------------------
- -- 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[13] btns[14] btns[15]
- ----------------------------
- Btns = {}
- Btns[1] = {name="ShifL", cmd="turtle.turnLeft() turtle.forward() turtle.turnRight()"}
- Btns[2] = {name="GoF", cmd="turtle.forward()"}
- Btns[3] = {name="ShifR", cmd="turtle.turnRight() turtle.forward() turtle.turnLeft()"}
- Btns[4] = {name="TurnL", cmd="turtle.turnLeft()"}
- Btns[5] = {name="GoB", cmd="turtle.back()"}
- Btns[6] = {name="TurnR", cmd="turtle.turnRight()"}
- Btns[7] = {name="place", cmd="turtle.place()"}
- Btns[8] = {name="UP", cmd="turtle.up()"}
- Btns[9] = {name="dig", cmd="turtle.dig()"}
- Btns[10] = {name="???", cmd="os.run({},'Logfile')"}
- Btns[11] = {name="Down", cmd="turtle.down()"}
- Btns[12] = {name="fuel", cmd="turtle.refuel()"}
- Btns[13] = {name="slot1", cmd="turtle.select(1)"}
- Btns[14] = {name="slot2", cmd="hoge()"}
- Btns[15] = {name="clLog", cmd="clearLogfile()"}
- -- ################## functions ###############
- -- evaluate a function-string
- function sendMsg(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)
- ctrl_mon.setTextScale(0.5)
- local mon_w, mon_h = ctrl_mon.getSize()
- local btn_w = math.floor(mon_w/3)
- local btn_h = math.floor(mon_h/5)
- local btns = {}
- local bgc_list = {colors.lightBlue, colors.blue}
- local i=1 -- table index
- for row=1,5 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,
- bgc = Btns[i]["bgc"] or bgc_list[(i % #bgc_list)+1]
- }
- i = i+1
- end
- end
- return btns
- end
- function drowRectangle(mon, start_x, start_y, goal_x, goal_y, color)
- print(string.format("(%d,%d)-(%d,%d): %d",start_x, start_y, goal_x, goal_y, color))
- term.redirect(mon)
- for y=start_y,goal_y do
- paintutils.drawLine(start_x, y, goal_x, y, color)
- end
- term.restore()
- 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)
- local btn_bgcolor = b.bgc
- drowRectangle(mon, b.min_x, b.min_y, b.max_x, b.max_y, btn_bgcolor)
- mon.setCursorPos(center_x - center_label, center_y)
- mon.setBackgroundColor(btn_bgcolor)
- 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(num, min_num, max_num)
- return (min_num <= num and max_num >= num)
- end
- local pushed_btn = false
- for i,v in pairs(buttons) do
- if within(x, v.min_x, v.max_x) and within(y, v.min_y, v.max_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")
- print(string.format("%s: %s, (%s,%s)",event,dir,x,y))
- if mon_dir == dir then
- pushed_btn = whichButton(buttons, x, y)
- end
- until pushed_btn
- return "push_button", pushed_btn
- end
- function pushButtonEffect(mon, btns, pushed_btn)
- print("pushed: ",pushed_btn.name)
- mon.setBackgroundColor(colors.black)
- mon.clear()
- drowButtons(mon, {pushed_btn})
- sleep(0.3)
- drowButtons(mon, btns)
- end
- -- ################## main #####################
- -- init ctrl monitor
- local mon = h2peripheral.waitForDetect("advanced_monitor")
- mon.setTextScale(0.5)
- mon.clear()
- -- make buttons and drow on ctrl monitor
- local btns = makeButtons(mon, Btns)
- drowButtons(mon, btns)
- -- init modem
- modem = h2peripheral.waitForDetect("wireless_modem")
- rednet.open(modem.side)
- -- main loop
- while true do
- local event_name, pushed_btn = pullPushButtonEvent(btns, mon.side)
- if event_name == "push_button" then
- pushButtonEffect(mon, btns, pushed_btn)
- sendMsg(pushed_btn.cmd, TurtleIDs)
- end
- sleep(0) -- "monitor_touch" event is too sensitive.
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement