EMF_Camp_Badge/beer/main.py

89 lines
2.6 KiB
Python
Raw Normal View History

2018-09-01 09:53:14 -04:00
"""What's on tap?!
2018-09-01 16:50:12 -04:00
Get up to date information on what's in stock at The Robot Arms!
2018-09-01 09:53:14 -04:00
"""
2018-09-03 09:59:37 -04:00
___title___ = "beer"
2018-09-01 09:53:14 -04:00
___license___ = "MIT"
___dependencies___ = ["app", "sleep", "wifi", "http", "ugfx_helper"]
___categories___ = ["EMF"]
2018-09-01 11:53:59 -04:00
import wifi, ugfx, http, ujson, app, sleep
from tilda import Buttons, LED
2018-09-01 09:53:14 -04:00
2018-09-01 13:28:59 -04:00
orientation = 270
2018-09-01 11:53:59 -04:00
def get_beer():
2018-09-01 16:45:33 -04:00
global bar, stock
2018-09-01 11:53:59 -04:00
LED(LED.RED).on()
2018-09-01 13:28:59 -04:00
try:
bar_json = http.get("https://bar.emf.camp/location/Bar.json").raise_for_status().content
2018-09-01 16:45:33 -04:00
stock_json = http.get("https://bar.emf.camp/stock.json").raise_for_status().content
2018-09-01 13:28:59 -04:00
bar = ujson.loads(bar_json)
2018-09-01 16:45:33 -04:00
stock = ujson.loads(stock_json)
2018-09-01 13:28:59 -04:00
except:
print('oh poop')
2018-09-01 16:45:33 -04:00
2018-09-01 11:53:59 -04:00
LED(LED.RED).off()
2018-09-01 13:28:59 -04:00
draw_screen()
2018-09-01 09:53:14 -04:00
2018-09-01 11:53:59 -04:00
def draw_screen():
2018-09-01 16:45:33 -04:00
global bar, stock
2018-09-01 13:28:59 -04:00
2018-09-01 11:53:59 -04:00
ugfx.clear(ugfx.BLACK)
2018-09-01 16:45:33 -04:00
ugfx.text(65, 5, "what's on tap?", ugfx.RED)
2018-09-01 11:53:59 -04:00
ugfx.line(5, 20, ugfx.width(), 20, ugfx.GREY)
2018-09-01 16:45:33 -04:00
2018-09-01 09:53:14 -04:00
for idx, beer in enumerate(bar['location']):
2018-09-01 16:45:33 -04:00
remaining = 0
for item in stock['stock']:
if item['description'] == beer['description']:
remaining = float(item['remaining'])
ugfx.text(5, 22 + idx*15, beer['description'][:28], ugfx.WHITE)
ugfx.text(202, 22 + idx*15, '!' if (remaining < 30) else ' ', ugfx.RED)
ugfx.text(210, 22 + idx*15, "{:>4}".format(beer['price']), ugfx.WHITE)
2018-09-01 11:53:59 -04:00
2018-09-01 13:28:59 -04:00
def toggle_orientation():
2018-09-01 16:45:33 -04:00
2018-09-01 13:28:59 -04:00
global orientation
if orientation == 90:
ugfx.orientation(270)
orientation = 270
draw_screen()
else:
ugfx.orientation(90)
orientation = 90
draw_screen()
2018-09-01 11:53:59 -04:00
ugfx.init()
2018-09-01 11:59:56 -04:00
ugfx.clear(ugfx.BLACK)
2018-09-01 16:45:33 -04:00
ugfx.set_default_font(ugfx.FONT_FIXED)
s=ugfx.Style()
s.set_enabled([ugfx.WHITE, ugfx.BLACK, ugfx.BLACK, ugfx.GREY])
s.set_background(ugfx.BLACK)
ugfx.set_default_style(s)
2018-09-01 11:53:59 -04:00
2018-09-01 13:28:59 -04:00
Buttons.enable_interrupt(Buttons.BTN_A, lambda button_id:get_beer(), on_press=True, on_release=False)
Buttons.enable_interrupt(Buttons.BTN_B, lambda button_id:toggle_orientation(), on_press=True, on_release=False)
Buttons.enable_interrupt(Buttons.BTN_Menu, lambda button_id:app.restart_to_default(), on_press=True, on_release=False)
2018-09-01 11:59:56 -04:00
ugfx.text(5, 10, "Instructions:", ugfx.WHITE)
ugfx.text(5, 30, "Press the A button to refresh", ugfx.WHITE)
2018-09-01 13:28:59 -04:00
ugfx.text(5, 45, "Press the B button to rotate", ugfx.WHITE)
ugfx.text(5, 60, "Press the Menu button to exit", ugfx.WHITE)
2018-09-01 16:45:33 -04:00
ugfx.text(5, 90, "!", ugfx.RED)
ugfx.text(15, 90, "means the stock is low", ugfx.WHITE)
ugfx.text(5, 120, "Loading data from the bar...", ugfx.WHITE)
2018-09-01 09:53:14 -04:00
2018-09-01 13:28:59 -04:00
get_beer()
2018-09-01 09:53:14 -04:00
2018-09-01 11:53:59 -04:00
while True:
sleep.wfi()
ugfx.clear()
2018-09-01 09:53:14 -04:00
app.restart_to_default()