Badger 2040 Tutorial

Here is a program using the buttons, thickness, size and fonts to try out while waiting for the tutorial.

# Pimoroni Badger 2040 Tutorial example
# How to use buttons, fonts and thickness
# Tony Goodhew (tonygo2) 3 March 2022

import badger2040
import machine
import time
fonts = ["sans","gothic","cursive","serif","serif_italic"]
display = badger2040.Badger2040()
display.update_speed(badger2040.UPDATE_TURBO)
display.pen(15)
display.clear()
display.update()

button_a = machine.Pin(badger2040.BUTTON_A, machine.Pin.IN, machine.Pin.PULL_DOWN)
button_b = machine.Pin(badger2040.BUTTON_B, machine.Pin.IN, machine.Pin.PULL_DOWN)
button_c = machine.Pin(badger2040.BUTTON_C, machine.Pin.IN, machine.Pin.PULL_DOWN)
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)

message = None
message_y = 60
f = 2
t = 1
running = True

# Instruction page
display.pen(0)
display.font(fonts[3])
display.thickness(3)
display.text("Text & Buttons", 10, 30, 1.0)
display.thickness(2)
display.text("Press U & D = Font", 35, 67, 0.7)
display.text("Press B & C = Thickness", 3, 87, 0.7)
display.text("Press A to halt", 55, 110, 0.7)
for _ in range(2):
    display.update() 

def button(pin):      # This is the interrupt handle
    
    global message, f,t,running
    if message is not None:    
        return
        
    if pin == button_a:
        message = "A"
        display.pen(15)
        display.clear()
        display.update()
        display.font(fonts[3])
        display.thickness(2)
        display.text("Done", 100, 50, 1.0)
        for _ in range(2):
            display.update() 
        display.pen(15)
        display.clear()
        display.font(fonts[3])
        display.thickness(2)
        display.pen(0)
        display.text("Done", 100, 50, 1.0)
        for _ in range(2):
            display.update() 
        time.sleep(3)
        running = False
        display.pen(15)
        display.clear()
        for _ in range(2):
            display.update() 
        return
    if pin == button_b:
        message ="B"
        t = t - 1
        if t < 1:
            t = 1       
        return
    if pin == button_c:
        message = "C"
        t = t + 1
        if t > 6:
            t = 6        
        return
    if pin == button_up:
        message = "U"        
        f = f + 1
        if f > 4:
            f = 4
        return
    if pin == button_down:
        message = "D"
        f = f - 1
        if f < 0:
            f = 0
        return

button_a.irq(trigger=machine.Pin.IRQ_RISING, handler=button)
button_b.irq(trigger=machine.Pin.IRQ_RISING, handler=button)
button_c.irq(trigger=machine.Pin.IRQ_RISING, handler=button)
button_up.irq(trigger=machine.Pin.IRQ_RISING, handler=button)
button_down.irq(trigger=machine.Pin.IRQ_RISING, handler=button)

while running:
    if message is not None:
        display.pen(15)
        display.clear()
        display.pen(0)
        # Print top line in serif and thickness 2
        display.font(fonts[3])
        display.thickness(2)
        display.text(fonts[f], 10, 20, 1.0)
        display.text(" T: "+str(t), 210,20,1.0)
        # Update font and thickness
        display.font(fonts[f])
        display.thickness(t)
        display.text("Tony Goodhew", 5, 80, 1.1)
        for _ in range(2):
            display.update() 
        message = None
    time.sleep(0.1)

Badger 2040 coding documentation is here:

Have fun
Tony

1 Like