Last active
April 30, 2024 22:16
-
-
Save spotlightishere/09a73708ea99feec56e6539fdd5ce520 to your computer and use it in GitHub Desktop.
OwO Beta status for Badger 2040 W
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import badger2040 | |
from badger2040 import WIDTH | |
import badger_os | |
import jpegdec | |
import urequests | |
import utime | |
# Display Setup | |
display = badger2040.Badger2040() | |
display.led(128) | |
display.set_update_speed(2) | |
jpeg = jpegdec.JPEG(display.display) | |
# Pagination | |
state = { | |
"current_domain": 0 | |
} | |
badger_os.state_load("owo_beta", state) | |
button_down = machine.Pin(badger2040.BUTTON_DOWN, machine.Pin.IN, machine.Pin.PULL_DOWN) | |
button_up = machine.Pin(badger2040.BUTTON_UP, machine.Pin.IN, machine.Pin.PULL_DOWN) | |
button_a = machine.Pin(badger2040.BUTTON_A, machine.Pin.IN, machine.Pin.PULL_DOWN) | |
button_c = machine.Pin(badger2040.BUTTON_C, machine.Pin.IN, machine.Pin.PULL_DOWN) | |
# Connects to the wireless network. Ensure you have entered your details in WIFI_CONFIG.py :). | |
display.connect() | |
def get_status_data(): | |
global domains, domain_count | |
status_endpoint = "https://status.owo.cloud/api/v1/status.json" | |
print(f"Requesting URL: {status_endpoint}") | |
result = urequests.get(status_endpoint) | |
status_data = result.json() | |
print("Data obtained!") | |
print(status_data) | |
# parse relevant data from JSON | |
domains = status_data["domains"] | |
domain_count = len(domains) | |
result.close() | |
def draw_page(): | |
# Reset display | |
display.set_pen(15) | |
display.clear() | |
display.set_pen(0) | |
# Draw app title bar | |
display.set_font("bitmap6") | |
display.set_pen(0) | |
display.rectangle(0, 0, WIDTH, 20) | |
display.set_pen(15) | |
display.text("OwO Beta Status", 3, 4) | |
# e.g. "Domain 1/20" | |
domain_page = state["current_domain"] + 1 | |
page_text = f"#{domain_page}/{domain_count}" | |
display.text(page_text, WIDTH - display.measure_text(page_text) - 4, 4) | |
display.set_pen(0) | |
# Draw the OwO Beta logo | |
display.set_font("bitmap8") | |
jpeg.open_file("/examples/icon-owo.jpg") | |
jpeg.decode(13, 40, jpegdec.JPEG_SCALE_FULL) | |
display.set_pen(0) | |
display.text(f"Domain count: {domain_count}", int(WIDTH / 3), 28, WIDTH - 105, 2) | |
# Now, move on to drawing the app's contents. | |
# Let's make sure we have a valid domain index. | |
current_domain = state["current_domain"] | |
if current_domain < 0 or current_domain >= domain_count: | |
# Reset to the first one - why not? | |
state["current_domain"] = 0 | |
draw_page() | |
current_status = domains[current_domain] | |
domain_name = current_status["name"] | |
# https://docs.micropython.org/en/v1.8.7/esp8266/library/utime.html | |
domain_uptime = utime.localtime(int(current_status["since"])) | |
uptime_year = domain_uptime[0] | |
uptime_month = domain_uptime[1] | |
uptime_day = domain_uptime[2] | |
display.text(f"{domain_name}", int(WIDTH / 3), 48, WIDTH - 105, 2) | |
display.text(f"Up since: {uptime_year}-{uptime_month}-{uptime_day}", int(WIDTH / 3), 68, WIDTH - 105, 2) | |
status_text = current_status["status"][0].upper() + current_status["status"][1:] | |
display.text(f"Status: {status_text}", int(WIDTH / 3), 88, WIDTH - 105, 2) | |
display.update() | |
get_status_data() | |
draw_page() | |
# Listen for button changes. | |
while True: | |
changed = False | |
# Next domain | |
if button_down.value(): | |
if state["current_domain"] < domain_count: | |
state["current_domain"] += 1 | |
changed = True | |
# Previous domain | |
if button_up.value(): | |
if state["current_domain"] > 0: | |
state["current_domain"] -= 1 | |
changed = True | |
# Jump back to first domain | |
if button_a.value(): | |
state["current_domain"] = 0 | |
changed = True | |
# Reload domain status data | |
if button_c.value(): | |
state["current_domain"] = 0 | |
get_status_data() | |
changed = True | |
if changed: | |
badger_os.state_save("owo_beta", state) | |
draw_page() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment