diff --git a/multibadge/README.md b/multibadge/README.md new file mode 100644 index 0000000..d7e78bc --- /dev/null +++ b/multibadge/README.md @@ -0,0 +1,21 @@ +# Multibadge + +This is like the original Badge app that comes with the Badger, but with +some additional things: + +* You can have multiple badges that you switch between with the up + and down arrow buttons. +* Each badge can have a rotating display of messages (think URLs, + contact info, cute messages, etc). +* The config file is in JSON to better support multiple badge data. + +I removed the company field, because I had no use for it. I'd rather have more +space for the name shown on the badge. The image size is the same, so it's +easy to upgrade from `badge` to `multibadge`. + +## Installation + +* Customize `multibadge.json` +* Copy `multibadge.py` to `examples` on the Badger 2040 +* Copy `multibadge.json` to `badges` +* Start `multibadge` from BadgerOS diff --git a/multibadge/icon-multibadge.jpg b/multibadge/icon-multibadge.jpg new file mode 100644 index 0000000..0b86070 Binary files /dev/null and b/multibadge/icon-multibadge.jpg differ diff --git a/multibadge/multibadge.json b/multibadge/multibadge.json new file mode 100644 index 0000000..2c64306 --- /dev/null +++ b/multibadge/multibadge.json @@ -0,0 +1,14 @@ +[ + { + "name": [ + "Badger", + "Mushroom" + ], + "image": "/badges/badge.jpg", + "messages": [ + ["first part", "badger badger", "badger badger"], + ["second part", "mushroom", "mushroom"], + ["third part", "SNAAAAKE", ""] + ] + } +] diff --git a/multibadge/multibadge.py b/multibadge/multibadge.py new file mode 100644 index 0000000..0a98017 --- /dev/null +++ b/multibadge/multibadge.py @@ -0,0 +1,131 @@ +import badger2040 +import jpegdec +import json +import time +import machine + +# Global Constants +WIDTH = badger2040.WIDTH +HEIGHT = badger2040.HEIGHT + +IMAGE_WIDTH = 104 +CONFIG_PATH = "/badges/multibadge.json" + +display = badger2040.Badger2040() + +config = None +with open(CONFIG_PATH, "r") as file: + config = json.loads(file.read()) + +# [ ] Show an error if the config is missing +# [ ] Verify all of the images are available and show an error if they're not + +current_badge_index = 0 +current_messages_index = 0 +sleep_count = 0 + +# Create a new Badger and set it to update NORMAL +display = badger2040.Badger2040() +display.led(128) +display.set_update_speed(badger2040.UPDATE_NORMAL) +display.set_thickness(2) + +jpeg = jpegdec.JPEG(display.display) + +def render_basics(): + display.set_pen(15) + display.clear() + + current_badge = config[current_badge_index] + badge_image = current_badge["image"] + + jpeg.open_file(badge_image) + jpeg.decode(WIDTH - IMAGE_WIDTH, 0) + + display.set_pen(0) + + # border next to image + display.rectangle(WIDTH - IMAGE_WIDTH - 2, 0, 2, HEIGHT) + + # your name + display.set_font("sans") + display.text(current_badge["name"][0], 10, 20, WIDTH, 1) + display.text(current_badge["name"][1], 10, 50, WIDTH, 1) + + # border under name + display.rectangle(0, 70, WIDTH - IMAGE_WIDTH, 2) + + display.set_update_speed(badger2040.UPDATE_NORMAL) + display.update() + +def render_details(): + current_badge = config[current_badge_index] + + # details area + display.set_pen(14) + display.rectangle(0, 72, WIDTH - IMAGE_WIDTH - 2, HEIGHT - 72) + + # details + current_message = current_badge["messages"][current_messages_index] + + display.set_pen(0) + display.text(current_message[0], 10, 80, WIDTH, 0.5) + display.line(0, 88, WIDTH - IMAGE_WIDTH, 88) + display.text(current_message[1], 10, 100, WIDTH, 0.65) + display.text(current_message[2], 10, 116, WIDTH, 0.65) + + display.set_update_speed(badger2040.UPDATE_FAST) + display.partial_update(0, HEIGHT - 56, WIDTH - IMAGE_WIDTH, 56) + +def advance_details(): + global current_messages_index + + current_badge = config[current_badge_index] + current_messages_index += 1 + current_messages_index %= len(current_badge["messages"]) + +def decrement_badge(): + global current_badge_index, current_messages_index + + current_badge_index -= 1 + current_badge_index += len(config) + current_badge_index %= len(config) + current_messages_index = 0 + +def increment_badge(): + global current_badge_index, current_messages_index + + current_badge_index += 1 + current_badge_index %= len(config) + current_messages_index = 0 + +MESSAGE_TIME_SEC = 10 +MESSAGE_TIME_COUNT = MESSAGE_TIME_SEC * 100 + +def render_badge_start(): + global sleep_count + + render_basics() + render_details() + advance_details() + sleep_count = 0 + +render_badge_start() + +while True: + if sleep_count >= MESSAGE_TIME_COUNT: + render_details() + advance_details() + sleep_count = 0 + + sleep_count += 1 + + if display.pressed(badger2040.BUTTON_UP): + decrement_badge() + render_badge_start() + + if display.pressed(badger2040.BUTTON_DOWN): + increment_badge() + render_badge_start() + + time.sleep(0.01)