Advertisement
hevohevo

ComputerCraft Tutorial: radio_ctrl(controller)0_2

Jan 22nd, 2014
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.82 KB | None | 0 0
  1. -- ############################################
  2. -- radio_ctrl  (controller side)
  3. -- version 0.2
  4. -- http://hevohevo.hatenablog.com/
  5. --  1. Refer to turtle side program (http://pastebin.com/z6QVrviC).
  6. --  2. Input to the following.
  7. --    > pastebin get uJfWnku6 radio_ctrl
  8. --  3. Edit config parameter, and execute this program.
  9.  
  10. os.loadAPI('h2peripheral')
  11.  
  12. -- ################## config ##################
  13. TurtleIDs = {65535} -- 65535: BROADCAST Channel
  14.  
  15. ----------------------------
  16. -- btns[1]   btns[2]   btns[3]
  17. -- btns[4]   btns[5]   btns[6]
  18. -- btns[7]   btns[8]   btns[9]
  19. -- btns[10]  btns[11]  btns[12]
  20. -- btns[13]  btns[14]  btns[15]
  21. ----------------------------
  22. Btns = {}
  23. Btns[1] = {name="ShifL", cmd="turtle.turnLeft() turtle.forward() turtle.turnRight()"}
  24. Btns[2] = {name="GoF",   cmd="turtle.forward()"}
  25. Btns[3] = {name="ShifR", cmd="turtle.turnRight() turtle.forward() turtle.turnLeft()"}
  26. Btns[4] = {name="TurnL", cmd="turtle.turnLeft()"}
  27. Btns[5] = {name="GoB",   cmd="turtle.back()"}
  28. Btns[6] = {name="TurnR", cmd="turtle.turnRight()"}
  29. Btns[7] = {name="place", cmd="turtle.place()"}
  30. Btns[8] = {name="UP",    cmd="turtle.up()"}
  31. Btns[9] = {name="dig",   cmd="turtle.dig()"}
  32. Btns[10] = {name="???",   cmd="os.run({},'Logfile')"}
  33. Btns[11] = {name="Down",  cmd="turtle.down()"}
  34. Btns[12] = {name="fuel",  cmd="turtle.refuel()"}
  35. Btns[13] = {name="slot1",  cmd="turtle.select(1)"}
  36. Btns[14] = {name="slot2",  cmd="hoge()"}
  37. Btns[15] = {name="clLog", cmd="clearLogfile()"}
  38.  
  39. -- ################## functions ###############
  40. -- evaluate a function-string
  41. function sendMsg(s, turtle_ids)
  42.   print("send:",s)
  43.   for _,id in pairs(turtle_ids) do
  44.     rednet.send(id,s)
  45.   end
  46. end
  47.  
  48. -- return btns-table
  49. function makeButtons(ctrl_mon, Btns)
  50.   ctrl_mon.setTextScale(0.5)
  51.   local mon_w, mon_h = ctrl_mon.getSize()
  52.   local btn_w = math.floor(mon_w/3)
  53.   local btn_h = math.floor(mon_h/5)
  54.   local btns = {}
  55.   local bgc_list = {colors.lightBlue, colors.blue}
  56.   local i=1 -- table index
  57.  
  58.   for row=1,5 do
  59.     for col=1,3 do
  60.       btns[i] = {
  61.         name=Btns[i]["name"], cmd=Btns[i]["cmd"],
  62.         min_x = 1+(btn_w)*(col-1), min_y = 1+(btn_h)*(row-1),
  63.         max_x = btn_w*col, max_y = btn_h*row,
  64.         bgc = Btns[i]["bgc"] or bgc_list[(i % #bgc_list)+1]
  65.       }
  66.       i = i+1
  67.     end
  68.   end
  69.   return btns
  70. end
  71.  
  72. function drowRectangle(mon, start_x, start_y, goal_x, goal_y, color)
  73.   print(string.format("(%d,%d)-(%d,%d): %d",start_x, start_y, goal_x, goal_y, color))
  74.   term.redirect(mon)
  75.   for y=start_y,goal_y do
  76.     paintutils.drawLine(start_x, y, goal_x, y, color)
  77.   end
  78.  
  79.   term.restore()  
  80. end
  81.  
  82. function drowButtons(mon, buttons)
  83.   for i,b in pairs(buttons) do
  84.     local center_x = math.floor((b.min_x + b.max_x)/2)
  85.     local center_y = math.floor((b.min_y + b.max_y)/2)
  86.     local center_label = math.floor(string.len(b.name)/2)
  87.     local btn_bgcolor = b.bgc
  88.     drowRectangle(mon, b.min_x, b.min_y, b.max_x, b.max_y, btn_bgcolor)
  89.     mon.setCursorPos(center_x - center_label, center_y)
  90.     mon.setBackgroundColor(btn_bgcolor)
  91.     mon.write(b.name)
  92.   end
  93. end
  94.  
  95. -- whichButton(buttons, 1, 1) => btn     (btn-table)
  96. --                            => false   (don't pushed)
  97. function whichButton(buttons, x, y)
  98.   local function within(num, min_num, max_num)
  99.           return (min_num <= num and max_num >= num)
  100.         end
  101.   local pushed_btn = false
  102.  
  103.   for i,v in pairs(buttons) do
  104.     if within(x, v.min_x, v.max_x) and within(y, v.min_y, v.max_y) then
  105.       pushed_btn = v
  106.       break
  107.     end
  108.   end
  109.   return pushed_btn
  110. end
  111.  
  112. -- event_name, pushed_btn = pullPushButtonEvent(buttons_table, "top")
  113. function pullPushButtonEvent(buttons, mon_dir)
  114.   local pushed_btn = false
  115.   repeat
  116.     local event, dir, x, y = os.pullEvent("monitor_touch")
  117.     print(string.format("%s: %s, (%s,%s)",event,dir,x,y))
  118.     if mon_dir == dir  then
  119.       pushed_btn = whichButton(buttons, x, y)
  120.     end
  121.   until  pushed_btn
  122.  
  123.   return "push_button", pushed_btn
  124. end
  125.  
  126. function pushButtonEffect(mon, btns, pushed_btn)
  127.   print("pushed: ",pushed_btn.name)
  128.   mon.setBackgroundColor(colors.black)
  129.   mon.clear()
  130.   drowButtons(mon, {pushed_btn})
  131.   sleep(0.3)
  132.   drowButtons(mon, btns)
  133. end
  134.  
  135.  
  136. -- ################## main #####################
  137. -- init ctrl monitor
  138. local mon = h2peripheral.waitForDetect("advanced_monitor")
  139. mon.setTextScale(0.5)
  140. mon.clear()
  141.  
  142. -- make buttons and drow on ctrl monitor
  143. local btns = makeButtons(mon, Btns)
  144. drowButtons(mon, btns)
  145.  
  146. -- init modem
  147. modem = h2peripheral.waitForDetect("wireless_modem")
  148. rednet.open(modem.side)
  149.  
  150. -- main loop
  151. while true do
  152.   local event_name, pushed_btn = pullPushButtonEvent(btns, mon.side)
  153.   if event_name == "push_button" then
  154.     pushButtonEffect(mon, btns, pushed_btn)
  155.     sendMsg(pushed_btn.cmd, TurtleIDs)
  156.   end
  157.  
  158.   sleep(0) -- "monitor_touch" event is too sensitive.
  159. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement