Toggle Navigation
Hatchery
Eggs
fahrplan test
fahrplan.py
Users
Badges
Login
Register
MCH2022 badge?
go to mch2022.badge.team
fahrplan.py
raw
Content
### Fahrplan Egg ### Author: f0x ### License: GPLv3 ### Screen is divided in three columns, each representing a track ### each column represents a time period of 2 hours import ugfx, badge, wifi import json, time import urequests as requests display_width = 295 display_height = 128 selected = 0 # number of leftmost track currently displayed day = 1 # day to display data for TODO: get from user input offset = 500 #room_list = ["No", "Pa", "Re"] room_list = [] column_width = int((display_width - 4)/3) wifi.init() # Fetch schedule, UPDATE URL IN FUTURE string = "Getting schedule data for day "+str(day) text_len = ugfx.get_string_width(string,"Roboto_Regular18") position = int((display_width-text_len)/2) ugfx.string(position, 3, string, "Roboto_Regular18", ugfx.BLACK) # Wait for WiFi connection while not wifi.sta_if.isconnected(): time.sleep(0.1) pass badge.init() ugfx.init() ugfx.input_init() ugfx.clear(ugfx.WHITE) ugfx.flush() #url = "https://f.0x52.eu/dl/day" + str(day) + ".json" url = "https://badge.sha2017.org/schedule/day/"+str(day) r = requests.get(url) schedule = r.json() r.close() for room in schedule["rooms"]: room_list.append(room) def draw_gui(): draw_blocks() ugfx.area(0, 0, display_width, 18, ugfx.WHITE) ugfx.box(0, 0, display_width+1, display_height, ugfx.BLACK) ugfx.line(1+column_width, 0, 1+column_width, display_height, ugfx.BLACK) ugfx.line(2+2*column_width, 0, 2+2*column_width, display_height, ugfx.BLACK) ugfx.line(0, 18, 295, 18, ugfx.BLACK) draw_headers() def draw_headers(): draw_header(room_list[selected], 0) draw_header(room_list[selected+1], 1) draw_header(room_list[selected+2], 2) def draw_header(string, pos): ## Write a stage header [string], where pos is column 0, 1 or 2 stage_len = ugfx.get_string_width(string,"Roboto_Regular12") position = int((column_width-stage_len)/2) ugfx.string(position + pos*column_width, 3, string, "Roboto_Regular12", ugfx.BLACK) def draw_blocks(): #draw_block("Talk 1", 0, 0, 90) #draw_block("Another Talk", 0, 30, 60) #draw_block("Talk", 1, 0, 30) for x in range(0,3): for event in schedule["rooms"][room_list[selected+x]]: #convert time from hh:mm to minutes event_start = event["start"].split(":") event_start_mins = 60*int(event_start[0])+int(event_start[1]) if event_start[0] == "00": #assume it's late at night event_start_mins = 24*60 event_duration = event["duration"].split(":") event_dur_mins = 60*int(event_duration[0])+int(event_duration[1]) event_stop_mins = event_start_mins + event_dur_mins event_end_hour, event_end_min = divmod(event_stop_mins, 60) event_time = event["start"] + " - " + str(event_end_hour) + ":" + str(event_end_min) if event_end_min == 0: event_time = event_time + "0" print("Event: "+room_list[selected+x]+" start: "+str(event_start_mins) + " stop: "+str(event_stop_mins)) draw_block(event["title"], event_time, x, event_start_mins, event_stop_mins) print("done") def draw_block(string, event_time, col, start, stop, selected = False): #convert start and stop times in minutes to lengths in pixels # whole column is 2 hours, and 128 - 18 = 110 pixels high, minus 2*2px padding = 106 pixels start = int(start * 106 / 120)-offset stop = int(stop * 106 / 120)-offset #box position x = 1 + col * column_width + col + 1 y = 2 + 18 + start x_len = column_width - 2 y_len = stop-start #max amounts of chars is 16, so shorten if more string = (string[:14] + "..") if len(string) > 16 else string text_len = ugfx.get_string_width(string, "Roboto_Regular12") text_x = x + int(( x_len - text_len ) / 2) text_y = y + int((y_len - 12) / 2) if selected: ugfx.area(x, y, x_len, y_len, ugfx.BLACK) ugfx.string(text_x, text_y, string, "Roboto_Regular12", ugfx.WHITE) ugfx.string(x+ 2, y+2, event_time, "Roboto_Regular12", ugfx.WHITE) else: ugfx.box(x, y, x_len, y_len, ugfx.BLACK) ugfx.string(text_x, text_y, string, "Roboto_Regular12", ugfx.BLACK) ugfx.string(x+ 2, y+2, event_time, "Roboto_Regular12", ugfx.BLACK) def scroll_right(pressed): global selected if pressed: selected = selected + 1 scroll(selected, offset) print("JOY_RIGHT") def scroll_left(pressed): global selected if pressed: selected = selected - 1 scroll(selected, offset) print("JOY_LEFT") def scroll_up(pressed): global offset if pressed: offset = offset - 20 scroll(selected, offset) print("JOY_UP") def scroll_down(pressed): global offset if pressed: offset = offset + 20 scroll(selected, offset) print("JOY_DOWN") def scroll(selected, offset = 500): ugfx.clear(ugfx.WHITE) draw_gui() ugfx.flush() scroll(selected, offset) ugfx.flush() ugfx.input_attach(ugfx.JOY_RIGHT, scroll_right) ugfx.input_attach(ugfx.JOY_LEFT, scroll_left) ugfx.input_attach(ugfx.JOY_UP, scroll_up) ugfx.input_attach(ugfx.JOY_DOWN, scroll_down)