multibadge

This commit is contained in:
John Bintz 2023-06-22 12:30:25 -04:00
parent 99f961cec0
commit dada03d770
4 changed files with 166 additions and 0 deletions

21
multibadge/README.md Normal file
View File

@ -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

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

View File

@ -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", ""]
]
}
]

131
multibadge/multibadge.py Normal file
View File

@ -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)