Advertisement
rithrin

wireless music

Mar 29th, 2013
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.05 KB | None | 0 0
  1. rednet.open("top")
  2. while true do
  3.  
  4. local timeout = 120 -- The time between tracks
  5. -- Can be set in the program
  6. local width = 3 -- The width of the rectangle of disk drives
  7. local height = 4 -- The height of the rectangle of disk drives
  8. local filledDrives = 12 -- The number of drives in the rectangle to check
  9. -- Drives count left-to-right from the bottom
  10. -- Drives after this number will be ignored
  11. local random = true -- Whether the system shoud start off shuffleing
  12. -- Initial setup, modify if you know what you're doing -- edit need an extra ' to prevent comenting on site
  13. local side = "left"
  14. local location = 0
  15. -- Variables, Do not modify
  16. local state = "play"
  17. local quit = false
  18. local startTime = 0
  19. -- Check for a disk
  20. function checkDisk()
  21. if disk.isPresent(side) then
  22. return true
  23. else
  24. return false
  25. end
  26. end
  27. -- Clears the screen
  28. function clear()
  29. term.clear()
  30. term.setCursorPos(1,1)
  31. end
  32. -- Prints the gui
  33. function show()
  34. clear()
  35. if (state == "change") then
  36. print("Changing Disk")
  37. else
  38. if (state == "stop") then
  39. print("Currently stopped")
  40. print()
  41. else
  42. print("Currently playing, ")
  43. print("Time Remaining: " .. math.floor(timeout - (os.time()*60 - startTime)) .. " seconds.")
  44. end
  45. print("Time between tracks: " .. timeout)
  46. if (checkDisk()) then
  47. print("Disk Inserted: " .. disk.getAudioTitle(side))
  48. else
  49. print("No audio disk inserted")
  50. end
  51. if (random) then
  52. print("Random order")
  53. else
  54. print("Sequential order")
  55. end
  56. print("---------------------")
  57. if (state == "stop") then
  58. print("1: Play")
  59. else
  60. print("1: Stop")
  61. end
  62. if (random) then
  63. print("2: Sequential Order")
  64. else
  65. print("2: Random Order")
  66. end
  67. print("3: Change disk")
  68. print("4: Change timeout")
  69. print("0: Exit")
  70. end
  71. end
  72. -- Change Disk
  73. function change()
  74. disk.stopAudio(side)
  75. local oldstate = state
  76. state = "change"
  77. show()
  78. if (random) then
  79. move(math.random(0,filledDrives-1))
  80. else
  81. move((location+1)%filledDrives)
  82. end
  83. while (checkDisk() == false) do
  84. if (random) then
  85. move(math.random(0,filledDrives-1))
  86. else
  87. move((location+1)%filledDrives)
  88. end
  89. end
  90. state = oldstate
  91. if (state == "play") then
  92. startTime = os.time()*60
  93. disk.playAudio(side)
  94. end
  95. end
  96. -- Move to disk
  97. function move(tarDisk)
  98. local x, y, xtar, ytar = location%width, math.floor((location+0.01)/width), tarDisk%width, math.floor((tarDisk+0.01)/width)
  99. while (x < xtar) do
  100. turtle.forward()
  101. x = x+1
  102. end
  103. while (y > ytar) do
  104. turtle.down()
  105. y = y-1
  106. end
  107. while (x > xtar) do
  108. turtle.back()
  109. x = x-1
  110. end
  111. while (y < ytar) do
  112. turtle.up()
  113. y = y+1
  114. end
  115. location = tarDisk
  116. end
  117. -- Change Timeout, messy implementation
  118. function changeTimeout()
  119. if (timeout == 10) then
  120. timeout = 30
  121. elseif (timeout == 30) then
  122. timeout = 60
  123. elseif (timeout == 60) then
  124. timeout = 90
  125. elseif (timeout == 90) then
  126. timeout = 120
  127. elseif (timeout == 120) then
  128. timeout = 150
  129. elseif (timeout == 150) then
  130. timeout = 180
  131. elseif (timeout == 180) then
  132. timeout = 240
  133. elseif (timeout == 240) then
  134. timeout = 300
  135. elseif (timeout == 300) then
  136. timeout = 420
  137. elseif (timeout == 420) then
  138. timeout = 540
  139. elseif (timeout == 540) then
  140. timeout = 900
  141. else
  142. timeout = 10
  143. end
  144. end
  145. -- Logical step
  146. function step()
  147. if (state == "play") then
  148. local curTime = os.time()*60
  149. if (startTime > curTime) then --day clocked over
  150. startTime = startTime - 24*60
  151. end
  152. if (curTime - startTime >= timeout) then
  153. change()
  154. end
  155. end
  156. end
  157. -- Main function
  158. function main()
  159. while (quit == false) do
  160. step()
  161. show()
  162. if (state ~= "change") then
  163. os.startTimer(1)
  164. local event, p1, p2 = os.pullEvent()
  165. if (event == "key") then
  166. if (p1 == 2) then
  167. if (state == "stop") then
  168. state = "play"
  169. startTime = os.time()*60
  170. disk.playAudio(side)
  171. else
  172. state = "stop"
  173. disk.stopAudio(side)
  174. end
  175. end
  176. if (p1 == 3) then
  177. if (random) then
  178. random = false
  179. else
  180. random = true
  181. end
  182. end
  183. if (p1 == 4) then
  184. change()
  185. end
  186. if (p1 == 5) then
  187. changeTimeout()
  188. end
  189. if (p1 == 11) then
  190. quit = true
  191. end
  192. end
  193. end
  194. end
  195. disk.stopAudio(side)
  196. move(0)
  197. end
  198. -- Moves the turtle into position (or resets it)
  199. function moveIn()
  200. clear()
  201. print("Moving into position")
  202. while(disk.isPresent("right") == false) do
  203. turtle.turnRight()
  204. end
  205. while (disk.isPresent("right") and turtle.detect() == false) do
  206. turtle.forward()
  207. end
  208. turtle.turnRight()
  209. turtle.turnRight()
  210. if (disk.isPresent(side) == false) then
  211. turtle.forward()
  212. end
  213. while (disk.isPresent(side) and turtle.detectDown() == false) do
  214. turtle.down()
  215. end
  216. if (disk.isPresent(side) == false) then
  217. turtle.up()
  218. end
  219. end
  220. -- Start Program
  221. moveIn()
  222. main()
  223.  
  224. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement