Toggle Navigation
Hatchery
Eggs
Radioknop
__init__.py
Users
Badges
Login
Register
MCH2022 badge?
go to mch2022.badge.team
__init__.py
raw
Content
import system, ugfx, wifi, urequests, easydraw, time, virtualtimers, orientation, sndmixer orientation.default() player = None initialised = False easydraw.msg("Loading...","Radioknop",True) wifi.connect() if not wifi.wait(30, True): easydraw.msg("WiFi failed.") time.sleep(2) system.home() easydraw.msg("Downloading list...") try: req = urequests.get("https://www.radioknop.nl/api.php") except: easydraw.msg("API failed.") time.sleep(2) system.home() easydraw.msg("Reading list...") try: data = req.json() except: easydraw.msg("JSON failed.") time.sleep(2) system.home() easydraw.msg("Cleaning up...") data = [station for station in data if station['url'].endswith('.mp3')] easydraw.msg("Number of stations: "+str(len(data))) easydraw.msg("Loading menu... (this takes some time)") options = ugfx.List(0,0,ugfx.width(),ugfx.height()) for station in data: options.add_item(station["name"]) def btn_unhandled(pressed): ugfx.flush() def btn_home(pressed): if pressed: system.home() def btn_play(pressed): global player global initialised if pressed: url = data[options.selected_index()]["url"] print("URL",url) if not initialised: sndmixer.begin(1) initialised = True if player: sndmixer.stop(player) easydraw.msg(data[options.selected_index()]["name"], "Play", True) easydraw.msg("Loading...") player = sndmixer.mp3_stream(urequests.get(url).raw) easydraw.msg("Playing...") time.sleep(0.5) options.visible(False) ugfx.flush() options.visible(True) ugfx.flush() def btn_stop(pressed): if pressed: if player: sndmixer.stop(player) easydraw.msg(" ", "Stop", True) time.sleep(0.5) options.visible(False) ugfx.flush() options.visible(True) ugfx.flush() def btn_volume_down(pressed): if pressed: audio.volume(audio.volume()-1) easydraw.msg("Value: "+str(audio.volume()), "Volume", True) time.sleep(0.5) options.visible(False) ugfx.flush() options.visible(True) ugfx.flush() def btn_volume_up(pressed): if pressed: audio.volume(audio.volume()+1) easydraw.msg("Value: "+str(audio.volume()), "Volume", True) time.sleep(0.5) options.visible(False) ugfx.flush() options.visible(True) ugfx.flush() ugfx.input_attach(ugfx.BTN_START, btn_home) ugfx.input_attach(ugfx.BTN_SELECT, btn_unhandled) ugfx.input_attach(ugfx.BTN_A, btn_play) ugfx.input_attach(ugfx.BTN_B, btn_stop) ugfx.input_attach(ugfx.JOY_UP, btn_unhandled) ugfx.input_attach(ugfx.JOY_DOWN, btn_unhandled) ugfx.input_attach(ugfx.JOY_LEFT, btn_volume_down) ugfx.input_attach(ugfx.JOY_RIGHT, btn_volume_up) ugfx.flush() def wifiTask(): if not wifi.status(): wifi.connect() return 5000 virtualtimers.activate(25) virtualtimers.new(0,wifiTask)