Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local modem = peripheral.find("modem")
- local tape = peripheral.find("tape_drive")
- local endPos = 1
- local control = {1200,1500,1800}
- local blockSize = 32768
- local state = {0,0}
- for k,v in pairs(control) do
- modem.open(v)
- end
- local function backSeek()
- tape.seek(-tape.getPosition())
- end
- local function Write(url)
- state[2] = 1
- backSeek()
- local reply = http.get(url, nil, true)
- repeat
- local bytes = {}
- for i=1, blockSize do
- local byte = reply.read()
- if not byte then
- break
- else
- bytes[#bytes+1] = byte
- end
- end
- if #bytes > 0 then
- for i=1, #bytes do
- tape.write(bytes[i])
- end
- sleep(0)
- end
- until not bytes or #bytes <= 0
- endPos = tape.getPosition()
- backSeek()
- state[2] = 0
- end
- local function stop()
- print("Stopping")
- tape.stop()
- end
- local function play(A,T)
- print(T.." - "..A)
- tape.play()
- state[1] = 1
- end
- local function fin()
- print("Awaiting song..")
- tape.stop()
- modem.transmit(control[3],control[3],"ready")
- end
- local function main()
- fin()
- while true do
- local evnt, side, sChan, rChan, msg, dist = os.pullEvent("modem_message")
- local tbl = textutils.unserialize(msg)
- if tbl ~= nil and tbl["URL"] ~= nil then
- local URL = tbl["URL"]
- local Artist = tbl["Artist"]
- local Title = tbl["Title"]
- if rChan == control[1] and string.find(URL,".dfpwm") then
- backSeek()
- Write(URL)
- play(Artist,Title)
- elseif rChan == control[2] and msg == "stop" then
- stop()
- end
- end
- end
- end
- local function secondary()
- while true do
- if tape.getPosition() >= endPos and state[1] == 1 and state[2] ~= 1 then
- fin()
- backSeek()
- end
- sleep(0.5)
- end
- end
- parallel.waitForAny(main,secondary)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement