Advertisement
Guest User

reciever

a guest
Feb 24th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. -- NoteBlock Player
  2. -- by MysticT
  3.  
  4. -- Config
  5.  
  6. -- id of the controller computer
  7. local nControllerID = 8
  8. -- sides corresponding to each instrument
  9. -- order: low-high
  10. local tSides = {"front","left","back","right","top"}
  11. -- pulse sleep time
  12. local nPulse = 0.01
  13.  
  14. -- Functions
  15.  
  16. -- clear the screen
  17. local function clear()
  18. term.clear()
  19. term.setCursorPos(1, 1)
  20. end
  21.  
  22. -- detect modem and connect
  23. local function connect()
  24. for _,s in ipairs(rs.getSides()) do
  25. if peripheral.isPresent(s) and peripheral.getType(s) == "modem" then
  26. rednet.open(s)
  27. return true
  28. end
  29. end
  30. return false
  31. end
  32.  
  33. -- send a pulse
  34. --[[
  35. table format:
  36. t = {
  37. [side1] = colors,
  38. [side2] = colors,
  39. ...
  40. [sideN] = colors,
  41. }
  42. --]]
  43. local function pulse(t)
  44. --print(textutils.serialize(t))
  45. if t[1] then
  46. rs.setOutput("front",true)
  47. --print("f")
  48. end
  49. if t[2] then
  50. rs.setOutput("left",true)
  51. --print("l")
  52. end
  53. if t[3] then
  54. rs.setOutput("back",true)
  55. --print("b")
  56. end
  57. if t[4] then
  58. rs.setOutput("right",true)
  59. --print("r")
  60. end
  61. if t[5] then
  62. rs.setOutput("top",true)
  63. --print("t")
  64. end
  65.  
  66. sleep(nPulse)
  67. rs.setOutput("front",false)
  68. rs.setOutput("left",false)
  69. rs.setOutput("back",false)
  70. rs.setOutput("right",false)
  71. rs.setOutput("top",false)
  72. sleep(nPulse)
  73. end
  74.  
  75. -- play the given notes on the given instruments
  76. --[[
  77. table format:
  78. t = {
  79. [i1] = { n1, n2, ..., nN },
  80. [i2] = { n1, n2, ..., nN },
  81. ...
  82. [iN] = { n1, n2, ..., nN }
  83. }
  84.  
  85. i: instrument number (range defined by the sides table)
  86. n: notes (range defined by the colors table)
  87. --]]
  88. --[[
  89. local function play(t)
  90. print("Playing...")
  91. local tPulse = {}
  92. for i, notes in pairs(t) do
  93. print("Instrument: ", i)
  94. local side = tSides[i]
  95. if side then
  96. if type(notes) == "table" then
  97. print("Notes: ", unpack(notes))
  98. local c = 0
  99. for _,n in ipairs(notes) do
  100. local k = tColors[n]
  101. if k then
  102. c = colors.combine(c, k)
  103. else
  104. print("Unknown note: ", n)
  105. end
  106. end
  107. tPulse[side] = c
  108. else
  109. print("Wrong format. table expected, got ", type(notes))
  110. end
  111. else
  112. print("Unknown Instrument")
  113. end
  114. end
  115. pulse(tPulse)
  116. end
  117. --]]
  118. local function play(t)
  119. print("Playing...")
  120. local c={}
  121. --local c[i]
  122. for i,notes in pairs(t) do
  123. --if type(notes) == "table" then
  124. --table.insert(c,i{})
  125.  
  126. local r = notes%5
  127. if r==0 then
  128.  
  129. r=r+5
  130. end
  131. c[r]=true
  132. end
  133. --print("c",textutils.serialize(c))
  134. --else
  135. -- print("Wrong format. table expected, got ", type(notes))
  136. --end
  137. pulse(c)
  138.  
  139. end
  140.  
  141. -- Start Program
  142.  
  143. -- try to connect to rednet
  144. if not connect() then
  145. print("No modem found")
  146. return
  147. end
  148.  
  149. clear()
  150. print("Waiting for messages...")
  151.  
  152. -- start receiving
  153. while true do
  154. local id, msg = rednet.receive()
  155. if msg then
  156. --print ("recieved")
  157. --print(id)
  158. end
  159. if id == nControllerID then
  160. --print("matched")
  161. local t = string.match(msg, "<PLAY> (.+)")
  162. if t then
  163. local notes = textutils.unserialize(t)
  164. if notes then
  165. play(notes)
  166. --print(";;",unpack(notes),";;")
  167. end
  168. end
  169.  
  170. end
  171. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement