SHARE
TWEET

ComputerCraft Tutorial: radio_ctrl(controller)0_1

hevohevo Jan 11th, 2014 966 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- ############################################
  2. -- radio_ctrl  (controller side)
  3. -- version 0.1
  4. -- http://hevohevo.hatenablog.com/
  5. --  1. Refer to turtle side program (http://pastebin.com/0ian5ggx).
  6. --  2. Input to the following.
  7. --    > pastebin get uJfWnku6 radio_ctrl
  8. --  3. Edit config parameter, and execute this program.
  9. --    "TurtleIDs" is important!
  10.  
  11. -- ################## config ##################
  12. CtrlMonSide = "top"
  13. ModemSide = "left"
  14. TurtleIDs = {1,2,3}
  15.  
  16. ----------------------------
  17. -- btns[1]   btns[2]   btns[3]
  18. -- btns[4]   btns[5]   btns[6]
  19. -- btns[7]   btns[8]   btns[9]
  20. -- btns[10]  btns[11]  btns[12]
  21. ----------------------------
  22. Btns = {}
  23. Btns[1] = {name="up",   cmd="turtle.up()"}
  24. Btns[2] = {name="forw",   cmd="turtle.forward()"}
  25. Btns[3] = {name="down", cmd="turtle.down()"}
  26. Btns[4] = {name="<=",    cmd="turtle.turnLeft()"}
  27. Btns[5] = {name="back",  cmd="turtle.back()"}
  28. Btns[6] = {name="=>",    cmd="turtle.turnRight()"}
  29. Btns[7] = {name="refu",  cmd="turtle.refuel()"}
  30. Btns[8] = {name="dig",  cmd="turtle.dig()"}
  31. Btns[9] = {name="place", cmd="turtle.place()"}
  32. Btns[10] = {name="sel1",  cmd="turtle.select(1)"}
  33. Btns[11] = {name="sel2",  cmd="turtle.select(2)"}
  34. Btns[12] = {name="sel16", cmd="turtle.select(16)"}
  35.  
  36. -- ################## functions ###############
  37. -- send a message-string to turtles
  38. function send_msg(s, turtle_ids)
  39.   print("send:",s)
  40.   for _,id in pairs(turtle_ids) do
  41.     rednet.send(id,s)
  42.   end
  43. end
  44.  
  45. -- return btns-table
  46. function makeButtons(ctrl_mon, Btns)
  47.   local mon_w, mon_h = ctrl_mon.getSize()
  48.   local btn_w = math.floor(mon_w/3)
  49.   local btn_h = math.floor(mon_h/4)
  50.   local btns = {}
  51.   local i=1 -- table index
  52.   for row=1,4 do
  53.     for col=1,3 do
  54.       btns[i] = {
  55.         name=Btns[i]["name"], cmd=Btns[i]["cmd"],
  56.         min_x = 1+(btn_w)*(col-1), min_y = 1+(btn_h)*(row-1),
  57.         max_x = btn_w*col, max_y = btn_h*row}
  58.       i = i+1
  59.     end
  60.   end
  61.   return btns
  62. end
  63.  
  64. function drowButtons(mon, buttons)
  65.   for i,b in pairs(buttons) do
  66.     local center_x = math.floor((b.min_x + b.max_x)/2)
  67.     local center_y = math.floor((b.min_y + b.max_y)/2)
  68.     local center_label = math.floor(string.len(b.name)/2)
  69.     mon.setCursorPos(center_x - center_label, center_y)
  70.     mon.write(b.name)
  71.   end
  72. end
  73.  
  74. -- whichButton(buttons, 1, 1) => btn     (btn-table)
  75. --                            => false   (don't pushed)
  76. function whichButton(buttons, x, y)
  77.   local function within(min_num, max_num, num)
  78.           return (min_num <= num and max_num >= num)
  79.         end
  80.   local pushed_btn = false
  81.  
  82.   for i,v in pairs(buttons) do
  83.     if within(v.min_x, v.max_x, x) and within(v.min_y, v.max_y, y) then
  84.       pushed_btn = v
  85.       break
  86.     end
  87.   end
  88.  
  89.   return pushed_btn
  90. end
  91.  
  92. -- event_name, pushed_btn = pullPushButtonEvent(buttons_table, "top")
  93. function pullPushButtonEvent(buttons, mon_dir)
  94.   local pushed_btn = false
  95.   repeat
  96.     local event, dir, x, y = os.pullEvent("monitor_touch")
  97.     if mon_dir == dir  then
  98.       pushed_btn = whichButton(buttons, x, y)
  99.     end
  100.   until  pushed_btn
  101.  
  102.   return "push_button", pushed_btn
  103. end
  104.  
  105. -- ################## main #####################
  106. -- init ctrl monitor
  107. local CtrlMon = peripheral.wrap(CtrlMonSide)
  108. CtrlMon.setTextScale(0.5)
  109. CtrlMon.clear()
  110.  
  111. -- make buttons and drow on ctrl monitor
  112. local mon_btns = makeButtons(CtrlMon, Btns)
  113. drowButtons(CtrlMon, mon_btns)
  114.  
  115. -- init modem
  116. rednet.open(ModemSide)
  117.  
  118. -- main loop
  119. while true do
  120.   local event_name, pushed_btn = pullPushButtonEvent(mon_btns, CtrlMonSide)
  121.   if event_name == "push_button" then
  122.     print("pushed: ",pushed_btn.name)
  123.     send_msg(pushed_btn.cmd, TurtleIDs)
  124.   end
  125.  
  126.   sleep(0) -- "monitor_touch" event is too sensitive.
  127. end
RAW Paste Data
Top