Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local trackList = {}
- local modemSide = "top"
- local musicTurtleID = 5
- local termWidth, termHeight = term.getSize()
- local menuFocus = 1
- local AudioIsPlay = false
- local PlayTimer = 0
- local startTime = 0
- local DiskDriveIsPresent = false
- local needReDraw = true
- local installedDisk = "no disk"
- local diskDriveStateChange = false
- ---------------------------------------------------------------
- local DefaultDiskDriveSide = "right"
- -- local Tracks = {"C418 - 13","C418 - cat","C418 - blocks","C418 - chirp","C418 - far","C418 - mall","C418 - mellohi","C418 - stal","C418 - strad","C418 - ward","C418 - 11","C418 - wait"}
- local Tracks = {
- ["C418 - 13"] = 178,
- ["C418 - cat"] = 188,
- ["C418 - blocks"] = 345,
- ["C418 - chirp"] = 188,
- ["C418 - far"] = 174,
- ["C418 - mall"] = 197,
- ["C418 - mellohi"] = 97,
- ["C418 - stal"] = 150,
- ["C418 - strad"] = 189,
- ["C418 - ward"] = 251,
- ["C418 - 11"] = 71,
- ["C418 - wait"] = 238,
- ["Lena Raine - otherside"] = 196,
- ["Lena Raine - Pigstep"] = 149,
- ["Samuel Åberg - 5"] = 179,
- ["no disk"] = 0,
- ["disk has no audio!"] = 0
- }
- -- local TrackDurability = {178,188,345,188,174,197,97,150,189,251,71,238}
- local TotalTrackCount = 12
- local exitFromProgram = false
- local diskID = 0
- local AutoPlayMode = false
- local UserState = false
- local DiskList = {}
- local Debug = false
- ------------------------SIMPLE GRAPHIC FUNCTIONS-------------------------------
- function drawLine(x1,y1,x2,y2,...)
- local function setPixel(x,y,monitorOrTerm)
- monitorOrTerm.setCursorPos(x,y)
- monitorOrTerm.write("-")
- end
- local args = {...}
- local monitorOrTerm = term
- if args[1] ~= nil then monitorOrTerm = peripheral.wrap(args[1]) end
- local dx = x2 - x1
- local dy = y2 - y1
- local sign_x = (dx > 0 and 1 or -1)
- local sign_y = (dy > 0 and 1 or -1)
- if dx < 0 then dx = dx * -1 end
- if dy < 0 then dy = dy * -1 end
- local pdx, pdy
- local es, el
- if dx > dy then
- pdx, pdy = sign_x, 0
- es, el = dy, dx
- else
- pdx, pdy = 0, sign_y
- es, el = dx, dy
- end
- local x, y = x1, y1
- local err, t = el/2, 0
- setPixel(x,y,monitorOrTerm)
- while t < el do
- err = err - es
- if err < 0 then
- err = err + el
- x = x + sign_x
- y = y + sign_y
- else
- x = x + pdx
- y = y + pdy
- end
- t = t + 1
- setPixel(x,y,monitorOrTerm)
- end
- end
- ---------------------------------------------------------------
- function getTrackList()
- rednet.send(musicTurtleID,"getList")
- local from, msg = rednet.receive()
- local tracks = {}
- if from == musicTurtleID then
- for i=1, tonumber(msg) do
- from, trackName = rednet.receive()
- table.insert(tracks,trackName)
- end
- from, msg = rednet.receive()
- if msg == "OK" then
- term.write("Get "..#tracks.." tracks!")
- end
- end
- return tracks
- end
- function next()
- rednet.send(musicTurtleID,"next")
- local from, msg = rednet.receive()
- if msg == "OK" and from == musicTurtleID then
- AudioIsPlay = true
- startTime = os.clock()
- disk.playAudio(DefaultDiskDriveSide)
- end
- end
- function set(trackName)
- rednet.send(musicTurtleID,"set")
- rednet.send(musicTurtleID,trackName)
- local from, msg = rednet.receive()
- if msg == "OK" and from == musicTurtleID then
- --term.write("Set!")
- end
- end
- function writeTextCenter(y,text)
- term.setCursorPos(termWidth/2-(string.len(text)/2),y)
- term.write(text)
- end
- function writeText(x,y,text)
- term.setCursorPos(x,y)
- term.write(text)
- end
- function setToNormalTime(durability)
- local minutes = 0
- durability = math.floor(durability)
- while durability >= 60 do
- minutes = minutes + 1
- durability = durability - 60
- end
- if string.len(durability)==1 then
- return "0"..minutes..":0"..durability
- else
- return "0"..minutes..":"..durability
- end
- end
- function progressBar(durability, timer)
- local maxWidth = termWidth
- local percent = timer/durability*100
- local progress = math.floor(maxWidth * percent / 100)
- local progressBar = ""
- for i=1, progress do
- progressBar = progressBar.."#"
- end
- return progressBar
- end
- function newMenu(menu)
- table.insert(menu,"back")
- local menuFocus = 1
- local exitMenu = false
- local PressedKey = 0
- while exitMenu==false do
- term.clear()
- for key, value in pairs(menu) do
- if menuFocus == key then
- local xPos = (termWidth/2)-(string.len(menu[key])/2)-2
- local yPos = key+(termHeight/2)-(#menu/2)
- term.setCursorPos(xPos,yPos)
- term.write("->"..value)
- else
- local xPos = (termWidth/2)-(string.len(menu[key])/2)
- local yPos = key+(termHeight/2)-(#menu/2)
- term.setCursorPos(xPos,yPos)
- term.write(value)
- end
- end
- ----------------------KeyPress Event and Timeout-----------------
- local timeout = os.startTimer(.2)
- while true do
- local event = {os.pullEvent()}
- if event[1] == "key" then
- PressedKey = event[2]
- elseif event[1] == "timer" and event[2] == timeout then
- break
- end
- end
- ----------------KeyPress and menuFocus-----------------
- if PressedKey ~= 0 then
- if PressedKey == 265 then
- menuFocus=menuFocus-1
- PressedKey=0
- elseif PressedKey == 264 then
- menuFocus=menuFocus+1
- PressedKey=0
- elseif PressedKey == 257 then
- if menu[menuFocus] == "back" then
- exitMenu = true
- else
- return menu[menuFocus]
- end
- PressedKey=0
- end
- else
- if menuFocus < 1 then
- menuFocus = #menu
- elseif menuFocus > #menu then
- menuFocus = 1
- end
- end
- end
- end
- -----------------------------------------------------------------------------
- --search of a disk drive
- local devices = peripheral.getNames()
- for i=1, #devices do
- if peripheral.getType(devices[i]) == "drive" then
- DiskDriveIsPresent = true
- DefaultDiskDriveSide = devices[i]
- break
- end
- end
- rednet.open(modemSide)
- while rednet.isOpen() do
- local timeout = os.startTimer(.2)
- local event = {os.pullEvent()}
- if event[1] == "key" and event[2] == 257 then --play/stop
- if AudioIsPlay then
- --stop playing
- AudioIsPlay = false
- startTime = 0
- disk.stopAudio(DefaultDiskDriveSide)
- PlayTimer = 0
- else
- --playing
- AudioIsPlay = true
- startTime = os.clock()
- disk.playAudio(DefaultDiskDriveSide)
- end
- needReDraw = true
- elseif event[1] == "key" and event[2] == 76 then --list menu L
- term.clear()
- writeTextCenter(termHeight/2,"Waiting for list downloading...")
- trackList = getTrackList()
- if #trackList > 0 then
- local selectedTrack = newMenu(trackList)
- term.clear()
- writeTextCenter(termHeight/2,"Changing disk to "..selectedTrack)
- set(selectedTrack)
- AudioIsPlay = true
- startTime = os.clock()
- disk.playAudio(DefaultDiskDriveSide)
- end
- needReDraw = true
- elseif event[1] == "key" and event[2] == 65 then --automode On/Off A
- AutoPlayMode = not AutoPlayMode
- elseif event[1] == "key" and event[2] == 78 then --next disk N
- next()
- elseif event[1] == "key" and event[2] == 81 then --QUIT Q
- term.clear()
- term.setCursorPos(1,1)
- rednet.close()
- end
- ---------------------------------checking disk is disk drive----------------------------------------
- if disk.isPresent(DefaultDiskDriveSide) ~= diskDrivePresentState then
- if disk.hasAudio(DefaultDiskDriveSide) then
- installedDisk = disk.getAudioTitle(DefaultDiskDriveSide)
- else
- installedDisk = "no disk"
- AudioIsPlay = false
- startTime = 0
- PlayTimer = 0
- end
- else
- installedDisk = "no disk"
- --------------------------
- AudioIsPlay = false
- startTime = 0
- PlayTimer = 0
- -------------------------
- end
- if diskDriveStateChange then needReDraw=true end --- ??????????????????????? REALY ?
- --------------------------------------------------------------------------------------------------
- if AudioIsPlay then
- if os.clock()-startTime >= Tracks[installedDisk] then --music is end play
- AudioIsPlay = false
- startTime = 0
- PlayTimer = 0
- ----------------
- if AutoPlayMode then next() end
- end
- end
- ------------------------------drawing menu-------------------------------------
- if needReDraw then
- local centerByY = termHeight/2
- local centerByX = termWidth/2
- local yShift = -3
- term.clear()
- drawLine(1,1,termWidth,1)
- drawLine(1,centerByY+yShift+2,termWidth,centerByY+yShift+2)
- writeTextCenter((termHeight/2)-3+yShift,installedDisk)
- if AudioIsPlay then
- local writeString = " Play -> Stop"
- writeTextCenter(centerByY+yShift,writeString)
- PlayTimer = setToNormalTime(os.clock()-startTime)
- writeText(1,centerByY+yShift+7,PlayTimer)
- else
- local writeString = "-> Play Stop"
- writeTextCenter(centerByY+yShift,writeString)
- writeText(1,centerByY+yShift+7,"00:00")
- end
- writeText(termWidth-string.len(setToNormalTime(Tracks[installedDisk]))+1,centerByY+yShift+7,setToNormalTime(Tracks[installedDisk]))
- writeText(1,centerByY+yShift+5,"[")
- if disk.isPresent(DefaultDiskDriveSide) and AudioIsPlay then writeText(2,centerByY+yShift+5,progressBar(Tracks[installedDisk],os.clock()-startTime)) end
- writeText(termWidth-string.len("]")+1,centerByY+yShift+5,"]")
- writeText(1,termHeight,"A-APM:"..(AutoPlayMode and "on" or "off"))
- writeTextCenter(termHeight,"N-Next disk")
- writeText(termWidth-string.len("L-Track list")+1,termHeight,"L-Track list")
- --needReDraw=false
- end
- end
- rednet.close(modemSide)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement