Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ ccTuner ]]--
- --[[ for OpenFM ]]--
- --[[ by ]]--
- --[[ HydrantHunter ]]--
- --[[ aka Dog ]]--
- --[[ Pastebin: ]]--
- --[[ hKuKNEcM ]]--
- local ccTunerVer = "1.0.04"
- --[[
- fm.setScreenColor(0xFF0000) --Sets the screen color to red
- fm.getScreenColor()
- ]]--
- local termX, termY = term.getSize()
- local menu, mute = false, false
- local currentStation, nowPlaying, radioVolume = 0, 0, 0
- local pageNum, numPages, numPerPage = 1, 1, 36
- local screen, stationList = "tuner", "/data/FMstations"
- local fm
- local sources = {
- ["Radionomy"] = "http://listen.radionomy.com/",
- ["Other "] = "",
- }
- local radioStations = {
- { name = "Deep House", site = "Radionomy", channel = "DeepHouse" };
- { name = "Urban Hotspot", site = "Radionomy", channel = "AllUrbanHotSpot" };
- { name = "Flavor Remix", site = "Radionomy", channel = "McDonald-sFlavorBattleRadio" };
- { name = "One Hip Hop", site = "Radionomy", channel = "One-love-Hip-Hop-Radio" };
- { name = "Boss Boss", site = "Radionomy", channel = "BossBossRadio" };
- { name = "Classic Rock", site = "Radionomy", channel = "-1ClassicRock" };
- { name = "Underground", site = "Radionomy", channel = "3WKCOMClassicUndergroundradio" };
- { name = "Smooth Jazz", site = "Radionomy", channel = "all-smooth-jazz" };
- { name = "Smooth Jazz 2", site = "Other ", channel = "http://173.244.199.249:80" };
- }
- local function saveData()
- if not fs.exists("/data") then fs.makeDir("/data") end
- local stationData = fs.open(stationList, "w")
- stationData.write(textutils.serialize(radioStations))
- stationData.close()
- end
- local function clearLowerScreen(bgColor)
- paintutils.drawFilledBox(1, 4, termX, termY, bgColor or colors.black)
- end
- local function drawRadioVolume()
- term.setBackgroundColor(colors.blue)
- term.setTextColor(mute and colors.lightGray or colors.white)
- term.setCursorPos(6, 2)
- term.write("0" .. tostring(radioVolume))
- end
- local function drawHeader()
- paintutils.drawBox(1, 1, 3, 3, colors.white) --# STOP button body
- paintutils.drawFilledBox(4, 1, 48, 3, colors.blue) --# Header body
- paintutils.drawBox(49, 1, termX, 3, colors.red) --# Close button body
- term.setTextColor(colors.white)
- term.setCursorPos(2, 2)
- term.write(" ") --# STOP button center
- term.setCursorPos(termX - 1, 2)
- term.write("X") --# Close button 'X'
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- term.setCursorPos(5, 1)
- term.write(" /\\ ") --# VOL UP
- term.setCursorPos(5, 3)
- term.write(" \\/ ") --# VOL DN
- drawRadioVolume() --# volume level
- term.setTextColor(colors.lightBlue)
- term.setCursorPos(23, 2)
- term.write("ccTuner") --# title
- end
- local function drawSourceMenu()
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- local y = 7
- for source in pairs(sources) do
- y = y + 1
- term.setCursorPos(8, y)
- term.write(" " .. source .. " ")
- end
- end
- local function drawRadioStationInfo()
- clearLowerScreen()
- term.setTextColor(colors.white)
- term.setCursorPos(2, 5)
- term.write("Name: ")
- term.setTextColor(colors.lightGray)
- term.write(radioStations[currentStation].name) --# Name
- term.setTextColor(colors.white)
- term.setCursorPos(2, 7)
- term.write("Site: ")
- term.setBackgroundColor(colors.lightGray)
- term.setTextColor(colors.black)
- term.write(" " .. radioStations[currentStation].site .. " ") --# Site / Source
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- term.write("v")
- term.setBackgroundColor(colors.black)
- term.setCursorPos(2, 9)
- term.write("Channel: ")
- term.setTextColor(colors.lightGray)
- term.write(radioStations[currentStation].channel) --# Channel
- term.setTextColor(colors.white)
- term.setCursorPos(2, 11)
- term.write("Station #: ")
- term.setTextColor(colors.lightGray)
- term.write(tostring(currentStation)) --# Number in list
- term.setCursorPos(2, 13)
- if nowPlaying == currentStation then
- term.setTextColor(colors.green)
- term.write("This station is now playing")
- else
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.green)
- term.write(" PLAY ")
- end
- end
- local function drawTunerScreen()
- clearLowerScreen()
- local magicNumber = ((pageNum - 1) * numPerPage) + 1
- local xPos, yPos = 2, 5
- for i = magicNumber, math.min(pageNum * numPerPage, #radioStations) do --# start station entries loop
- term.setCursorPos(xPos, yPos)
- term.setTextColor(colors.white)
- term.setBackgroundColor(i == nowPlaying and colors.green or colors.black)
- term.write(radioStations[i].name:sub(1, 15)) --# write the entry to screen
- if i >= #radioStations then break end --# break the loop if at the end of the list
- yPos = yPos + 1
- if yPos >= 17 then xPos = xPos + 17 yPos = 5 end
- end
- if numPages > 1 then --# show page navigation controls if more than one page
- term.setCursorPos(16, termY - 1)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.lightGray)
- term.write("<< < > >>") --# page navigation controls
- local pages = tostring(pageNum) .. " of " .. tostring(numPages)
- term.setCursorPos(math.ceil(termX / 2) - math.floor(#pages / 2), termY - 1)
- term.write(pages) --# page number
- end
- term.setCursorPos(1, termY)
- term.setBackgroundColor(colors.green)
- term.setTextColor(colors.white)
- term.write(" Add new station ")
- end
- local function drawHelpScreen()
- clearLowerScreen(colors.white)
- term.setTextColor(colors.gray)
- term.setCursorPos(17, 5)
- term.write("an entry to play")
- term.setCursorPos(17, 7)
- term.write("an entry to view/edit")
- term.setCursorPos(17, 9)
- term.write("an entry to delete")
- term.setCursorPos(17, 12)
- term.write("For Radionomy sources, the channel")
- term.setCursorPos(17, 13)
- term.write("is the last part of the listen URL")
- term.setCursorPos(17, 15)
- term.write("For Other sources, the channel")
- term.setCursorPos(17, 16)
- term.write("is the entire stream URL")
- paintutils.drawFilledBox(1, 4, 15, termY, colors.lightGray)
- term.setTextColor(colors.black)
- term.setCursorPos(2, 5)
- term.write("Left Click")
- term.setCursorPos(2, 7)
- term.write("Right Click")
- term.setCursorPos(2, 9)
- term.write("Middle Click")
- term.setCursorPos(2, 12)
- term.write("Sources")
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.lightGray)
- term.setCursorPos(1, termY)
- term.write(" ccTuner ver. " .. ccTunerVer .. " ")
- end
- local function highlightButton(x, y, word)
- term.setCursorPos(x, y)
- term.setBackgroundColor(colors.orange)
- term.setTextColor(colors.white)
- term.write(word)
- end
- local function userInputPreview(x, y, word)
- term.setCursorPos(x, y)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.gray)
- term.write(word)
- term.setTextColor(colors.white)
- term.setCursorPos(x, y)
- end
- local function userInput(x, y, word)
- userInputPreview(x, y, word)
- local newWord = read()
- return newWord ~= "" and newWord or word
- end
- local function queueMusic()
- pcall(fm.stop)
- pcall(fm.setURL, sources[radioStations[nowPlaying].site] .. radioStations[nowPlaying].channel)
- pcall(fm.start)
- pcall(fm.setScreenText, radioStations[nowPlaying].name) --# Text will scroll if larger than the display.
- end
- local function userInput()
- local event, data, mX, mY, xPos, yPos, magicNumber, tmpName, tmpSite, tmpChannel, newNum
- while true do
- event, data, mX, mY = os.pullEvent()
- if event == "mouse_click" then
- if mY < 4 and data == 1 then
- if mX < 4 then --# STOP
- pcall(fm.stop)
- pcall(fm.setScreenText, "OpenFM") --# Text will scroll if larger than the display.
- nowPlaying = 0
- if screen == "tuner" then
- drawTunerScreen()
- elseif screen == "info" then
- drawRadioStationInfo()
- end
- elseif mX > 4 and mX < 9 then
- if mY == 1 then --# Volume UP
- radioVolume = math.min(radioVolume + 1, 9)
- if not mute then pcall(fm.volUp) end
- elseif mY == 2 then --# Mute / Unmute
- mute = not mute
- for i = 1, radioVolume do
- pcall(mute and fm.volDown or fm.volUp)
- end
- elseif mY == 3 then --# Volume DOWN
- radioVolume = math.max(radioVolume - 1, 0)
- if not mute then pcall(fm.volDown) end
- end
- drawRadioVolume()
- elseif mX > termX - 3 then
- if screen == "tuner" then --# exit program
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1, 1)
- return
- else --# return to tuner screen
- screen = "tuner"
- currentStation = 0
- drawTunerScreen()
- end
- end
- end
- if screen == "tuner" then
- if mY == termY and data == 1 then
- radioStations[#radioStations + 1] = { name = "Deep House", site = "Radionomy", channel = "DeepHouse" }
- saveData()
- numPages = math.max(1, math.ceil(#radioStations / numPerPage)) --# paginate radio stations
- currentStation = #radioStations
- screen = "info"
- drawRadioStationInfo()
- elseif mY == termY - 1 and ((mX > 15 and mX < 20) or (mX > 31 and mX < 36)) and numPages > 1 and data == 1 then --# page navigation
- if (mX > 15 and mX < 18) or (mX > 33 and mX < 36) then --# Home / End
- pageNum = (mX > 15 and mX < 18) and 1 or numPages
- elseif mX == 19 or mX == 32 then --# Page - / +
- pageNum = mX == 19 and math.max(1, pageNum - 1) or math.min(pageNum + 1, numPages)
- end
- drawTunerScreen()
- else
- magicNumber = ((pageNum - 1) * numPerPage) + 1
- xPos, yPos = 2, 5
- for i = magicNumber, math.min(pageNum * numPerPage, #radioStations) do --# start click detection loop
- if mX >= xPos and mX < xPos + #radioStations[i].name and mY == yPos then
- if data == 1 and nowPlaying ~= i then --# Button 1 / play
- nowPlaying = 0
- drawTunerScreen()
- nowPlaying = i
- highlightButton(xPos, yPos, radioStations[i].name)
- queueMusic()
- drawTunerScreen()
- elseif data == 2 then --# Button 2 / edit
- screen = "info"
- currentStation = i
- drawRadioStationInfo()
- elseif data == 3 and #radioStations > 1 then --# Button 3 / delete
- table.remove(radioStations, i)
- saveData()
- numPages = math.max(1, math.ceil(#radioStations / numPerPage)) --# paginate radio stations
- pageNum = math.min(pageNum, numPages) --# ensure pageNum is valid
- if nowPlaying == i then nowPlaying = 0 end
- if nowPlaying > i then nowPlaying = nowPlaying - 1 end
- drawTunerScreen()
- end
- break
- end
- yPos = yPos + 1
- if yPos >= 17 then xPos = xPos + 17 yPos = 5 end
- end
- end
- elseif screen == "info" and data == 1 and not menu then
- if mY == 5 and mX > 7 and mX < 8 + #radioStations[currentStation].name then --# name
- tmpName = userInput(8, 5, radioStations[currentStation].name)
- if tmpName ~= radioStations[currentStation].name then
- radioStations[currentStation].name = tmpName
- saveData()
- end
- drawRadioStationInfo()
- elseif mY == 7 and mX > 7 and mX < 11 + #radioStations[currentStation].site then --# site menu
- menu = true
- drawSourceMenu()
- elseif mY == 9 and mX > 10 and mX < 11 + #radioStations[currentStation].channel then --# channel
- tmpChannel = userInput(11, 9, radioStations[currentStation].channel)
- if tmpChannel ~= radioStations[currentStation].channel then
- radioStations[currentStation].channel = tmpChannel
- if nowPlaying == currentStation then nowPlaying = 0 end
- saveData()
- end
- drawRadioStationInfo()
- elseif mY == 11 and mX > 12 and mX < 13 + #tostring(currentStation) then --# number in list
- newNum = tonumber(userInput(13, 11, tostring(currentStation)))
- if newNum then newNum = math.floor(newNum) end
- if newNum and newNum > 0 and newNum <= #radioStations and newNum ~= currentStation then
- tmpName = radioStations[currentStation].name
- tmpSite = radioStations[currentStation].site
- tmpChannel = radioStations[currentStation].channel
- table.remove(radioStations, currentStation)
- table.insert(radioStations, newNum, { name = tmpName, site = tmpSite, channel = tmpChannel })
- if nowPlaying == currentStation then
- nowPlaying = newNum
- elseif newNum <= nowPlaying and currentStation > nowPlaying then
- nowPlaying = nowPlaying + 1
- elseif newNum >= nowPlaying and currentStation < nowPlaying then
- nowPlaying = nowPlaying - 1
- end
- currentStation = newNum
- saveData()
- end
- drawRadioStationInfo()
- elseif mY == 13 and mX > 1 and mX < 7 and currentStation ~= nowPlaying then --# play
- nowPlaying = currentStation
- highlightButton(2, 13, "Tuning")
- queueMusic()
- drawRadioStationInfo()
- end
- elseif screen == "info" and data == 1 and menu then --# Source selection menu
- if mX > 7 and mX < 20 and mY > 7 and mY < 10 then
- if mY == 8 and radioStations[currentStation].site ~= "Other " then --# Select 'Other'
- radioStations[currentStation].site = "Other "
- if nowPlaying == currentStation then nowPlaying = 0 end
- saveData()
- elseif mY == 9 and radioStations[currentStation].site ~= "Radionomy" then --# Select 'Radionomy'
- radioStations[currentStation].site = "Radionomy"
- if nowPlaying == currentStation then nowPlaying = 0 end
- saveData()
- end
- end
- menu = false
- drawRadioStationInfo()
- end
- elseif event == "mouse_scroll" then
- if screen == "tuner" then
- pageNum = data == 1 and math.min(pageNum + 1, numPages) or math.max(pageNum - 1, 1)
- drawTunerScreen()
- elseif screen == "info" and not menu then
- currentStation = data == 1 and math.min(currentStation + 1, #radioStations) or math.max(currentStation - 1, 1)
- drawRadioStationInfo()
- end
- elseif event == "key" then
- if data == keys.f1 then --# F1
- if screen == "help" or screen == "tuner" then
- screen = screen == "help" and "tuner" or "help"
- if screen == "help" then drawHelpScreen() else drawTunerScreen() end
- end
- elseif data == keys.pageDown or data == keys.pageUp then --# PageDN / PageUP
- if screen == "tuner" then
- pageNum = data == keys.pageDown and math.max(pageNum - 1, 1) or math.min(pageNum + 1, numPages)
- drawTunerScreen()
- elseif screen == "info" and not menu then
- currentStation = data == keys.pageDown and math.max(currentStation - 1, 1) or math.min(currentStation + 1, #radioStations)
- drawRadioStationInfo()
- end
- elseif data == keys.home or data == keys["end"] then --# Home / End
- if screen == "tuner" then
- pageNum = data == keys.home and 1 or numPages
- drawTunerScreen()
- elseif screen == "info" and not menu then
- currentStation = data == keys.home and 1 or #radioStations
- drawRadioStationInfo()
- end
- end
- end
- end
- end
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- if not os.getComputerLabel() then os.setComputerLabel("ccTuner") end
- if not term.isColor() or pocket or turtle then error("Advanced Computer Required!", 0) end
- fm = peripheral.find("openfm_radio")
- if not fm then error("No OpenFM tuner found!", 0) end
- radioVolume = math.floor(fm.getVol() * 10)
- if fs.exists(stationList) then
- for i = #radioStations, 1, -1 do
- radioStations[i] = nil
- end
- local stationData = fs.open(stationList, "r")
- radioStations = textutils.unserialize(stationData.readAll())
- stationData.close()
- end
- numPages = math.max(1, math.ceil(#radioStations / numPerPage)) --# paginate radio stations
- pageNum = math.min(pageNum, numPages) --# ensure pageNum is valid
- clearLowerScreen()
- drawHeader()
- drawTunerScreen()
- userInput()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement