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 blockSize = 32768
- local control = {1200,1500,1800}
- local state = {0,0}
- for k,v in pairs(control) do
- modem.open(v)
- end
- local function backSeek()
- print("Seeking to start")
- tape.seek(-tape.getPosition())
- end
- local function write(url)
- state[2] = 1
- backSeek()
- print("Writing: "..url)
- 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()
- print("End: "..endPos)
- backSeek()
- print("End of Write")
- state[2] = 0
- end
- local function stop()
- print("Stopping")
- tape.stop()
- end
- local function play()
- print("Playing..." )
- 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")
- if rChan == control[1] and string.find(msg,".dfpwm") then
- backSeek()
- write(msg)
- play()
- elseif rChan == control[2] and msg == "stop" then
- stop()
- end
- end
- end
- local function secondary()
- while true do
- if tape.getPosition() >= endPos and state[1] == 1 and state[2] ~= 1 then
- print("End Of Song")
- fin()
- backSeek()
- end
- sleep(0.5)
- end
- end
- parallel.waitForAny(mainLoop,secondary)
Add Comment
Please, Sign In to add comment