Skip to content

Instantly share code, notes, and snippets.

@niutech
Last active September 2, 2023 09:18
Show Gist options
  • Save niutech/07f8f659c4a972af88e24470df0ed2ff to your computer and use it in GitHub Desktop.
Save niutech/07f8f659c4a972af88e24470df0ed2ff to your computer and use it in GitHub Desktop.
Display text from stdin on Pimoroni Badger 2040
#!/usr/bin/env python3
import pyboard
import sys
from getopt import getopt
from unidecode import unidecode
device = '/dev/ttyACM0'
font = 'bitmap8'
monospace = False
scale = '1'
thickness = '1'
opts, _ = getopt(sys.argv[1:], 'd:hf:ms:t:', ['device=', 'help', 'font=', 'monospace', 'scale=', 'thickness='])
for opt, val in opts:
if opt in ('-d', '--device'):
device = val
elif opt in ('-f', '--font'):
font = val
elif opt in ('-m', '--monospace'):
monospace = True
elif opt in ('-s', '--scale'):
scale = val
elif opt in ('-t', '--thickness'):
thickness = val
elif opt in ('-h', '--help'):
print('Usage: ', sys.argv[0], '[-d|--device=/dev/ttyACM0] [-h|--help] [-f|--font=bitmap8] [-m|--monospace] [-s|--scale=1] [-t|--thickness=1] < inputfile')
sys.exit()
text = unidecode(sys.stdin.read().rstrip(), errors='replace')
pyb = pyboard.Pyboard(device)
pyb.enter_raw_repl()
pyb.exec_raw_no_follow('''
import badger2040
import machine
badger = badger2040.Badger2040()
badger.set_update_speed(badger2040.UPDATE_FAST)
badger.set_font("''' + font + '''")
badger.set_thickness(''' + thickness + ''')
lines = \'\'\'''' + text + '''\'\'\'.split('\\n')
scale = ''' + scale + '''
monospace = ''' + str(monospace) + '''
curr_page = 0
all_pages = len(lines) // 12 + 1
def refresh():
badger.set_pen(15)
badger.clear()
badger.set_pen(0)
for i, line in enumerate(lines[curr_page * 12 : (curr_page + 1) * 12]):
badger.text(line, 0, i * 10, scale=scale, fixed_width=monospace)
badger.update()
def button(pin):
global curr_page
if pin == button_down and curr_page < all_pages - 1:
curr_page += 1
refresh()
elif pin == button_up and curr_page > 0:
curr_page -= 1
refresh()
badger.halt()
button_up = machine.Pin(badger2040.BUTTON_UP, machine.Pin.IN, machine.Pin.PULL_DOWN)
button_down = machine.Pin(badger2040.BUTTON_DOWN, machine.Pin.IN, machine.Pin.PULL_DOWN)
button_up.irq(trigger=machine.Pin.IRQ_RISING, handler=button)
button_down.irq(trigger=machine.Pin.IRQ_RISING, handler=button)
refresh()
badger.halt()
''')
pyb.exit_raw_repl()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment