Badger2040 display bug (?)

I have a very simple function to draw some text, which should be triggered when a button is pressed. The first time the button fires, everything works; but after that, every new button press will result in a black screen. This is on 1.18.3

Here’s the code:

import os
import sys
import time
import machine
import badger2040
from badger2040 import WIDTH, HEIGHT

display = badger2040.Badger2040()
display.update_speed(badger2040.UPDATE_MEDIUM)
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)

#### this is the code that triggers bug on second execution
def draw_email(email, scale_dec=None):
    scale = scale_dec if scale_dec else 0.6
    email_user, email_domain = email.split("@")
    email_user = email_user + "@"
    display.clear()
    display.pen(15)
    x_pos = 20
    display.text(email_user , x_pos, 20, scale + 0.2)
    display.text(email_domain, x_pos, 40, scale)
    display.update()
# -- end bug routine

while True:
    if button_up.value():
        draw_email("myemail@mydomain.com", 0.55)
    if button_down.value():
        draw_email("myemail@mydomain.com", 0.55)
    if button_a.value():
        draw_email("myemail@mydomain.com", 0.55)
    if button_b.value():
        draw_email("myemail@mydomain.com", 0.55)
 
    time.sleep(0.01)

This appears to work with 1.18.2 and 1.18.3

# Modified by Tony Goodhew  6 March 2022
import os
import sys
import time
import machine
import badger2040
from badger2040 import WIDTH, HEIGHT

display = badger2040.Badger2040()
display.update_speed(badger2040.UPDATE_NORMAL)
display.pen(15)
display.clear()    
display.pen(0)
display.text("Press a button" , 30, 30, 0.8)
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)

#### this is the code that triggers bug on second execution
def draw_email(email, scale_dec):
    scale = 0.6
    email_user, email_domain = email.split("@")
    email_user = email_user + "@"
    display.pen(15)
    display.clear()    
    x_pos = 20
    display.pen(0)
    display.text(email_user , x_pos, 20, scale + 0.2)
    display.text(email_domain, x_pos, 40, scale)
    display.update()
# -- end bug routine
running = True
while running:
    if button_up.value():
        draw_email("myemail@mydomain.com - U", 0.55)
        print("U")
    if button_down.value():
        draw_email("myemail@mydomain.com - D", 0.55)
        print("D")
    if button_a.value():
        draw_email("myemail@mydomain.com - A", 0.55)
        print("A")
    if button_b.value():
        draw_email("myemail@mydomain.com - B", 0.55)
        print("B")
    if button_c.value():
        display.pen(15)
        display.clear()    
        display.pen(0)
        display.text("HALTED",140,70)
        display.update()
        running = False
    time.sleep(0.01)

Hope this helps

it looks like the core of the matter is the sequence in which one calls pen() and clear().

My original code had:

display.clear() 
display.pen(15)   

Yours has:

display.pen(15)
display.clear()    
display.pen(0)

It doesn’t make much sense but okay, i can live with that.

To clear the screen I believe that the code writes the current pen colour to every pixel on the screen with the clear command so you have to set the pen to white first with pen = 15. [This is often called fill() with other screen drivers.]

You then set the pen to 0 = black to write on the now white screen.

A very easy and common mistake is to leave out one of these pen changes and write invisibly on the screen in the same colour. You then spend ages wondering why a screen write is not appearing. - I’ve wasted hours!

1 Like