diff --git a/micropython/examples/breakout_colourlcd240x240/colourlcd240x240_demo.py b/micropython/examples/breakout_colourlcd160x80/balls_demo.py similarity index 85% rename from micropython/examples/breakout_colourlcd240x240/colourlcd240x240_demo.py rename to micropython/examples/breakout_colourlcd160x80/balls_demo.py index b614cef9..1f90c1c3 100644 --- a/micropython/examples/breakout_colourlcd240x240/colourlcd240x240_demo.py +++ b/micropython/examples/breakout_colourlcd160x80/balls_demo.py @@ -1,16 +1,14 @@ import time import random -from st7789 import ST7789, PALETTE_USER, DISPLAY_LCD_240X240 +from picographics import PicoGraphics, DISPLAY_LCD_160X80, PEN_P8 -display = ST7789(DISPLAY_LCD_240X240, rotate=0) +display = PicoGraphics(display=DISPLAY_LCD_160X80, pen_type=PEN_P8) +display.set_backlight(1.0) WIDTH, HEIGHT = display.get_bounds() # We're creating 100 balls with their own individual colour and 1 BG colour -# for a total of 101 colours, which will all fit in the 256 entry palette! -display.set_palette_mode(PALETTE_USER) - -display.set_backlight(1.0) +# for a total of 101 colours, which will all fit in the custom 256 entry palette! class Ball: diff --git a/micropython/examples/pico_display/demo.py b/micropython/examples/breakout_colourlcd240x240/balls_demo.py similarity index 76% rename from micropython/examples/pico_display/demo.py rename to micropython/examples/breakout_colourlcd240x240/balls_demo.py index 305e5d4b..ab3b5028 100644 --- a/micropython/examples/pico_display/demo.py +++ b/micropython/examples/breakout_colourlcd240x240/balls_demo.py @@ -1,12 +1,14 @@ import time import random -import st7789 +from picographics import PicoGraphics, DISPLAY_LCD_240X240, PEN_P8 -display = st7789.ST7789(st7789.DISPLAY_PICO_DISPLAY, rotate=0) +display = PicoGraphics(display=DISPLAY_LCD_240X240, pen_type=PEN_P8) +display.set_backlight(1.0) WIDTH, HEIGHT = display.get_bounds() -display.set_backlight(1.0) +# We're creating 100 balls with their own individual colour and 1 BG colour +# for a total of 101 colours, which will all fit in the custom 256 entry palette! class Ball: @@ -34,8 +36,10 @@ for i in range(0, 100): ) ) +BG = display.create_pen(40, 40, 40) + while True: - display.set_pen(40, 40, 40) + display.set_pen(BG) display.clear() for ball in balls: diff --git a/micropython/examples/breakout_roundlcd/demo.py b/micropython/examples/breakout_roundlcd/demo.py index 23a16dd3..2c169ebd 100644 --- a/micropython/examples/breakout_roundlcd/demo.py +++ b/micropython/examples/breakout_roundlcd/demo.py @@ -1,15 +1,15 @@ import time import math -from st7789 import ST7789 - -WIDTH, HEIGHT = 240, 240 - -display = ST7789(WIDTH, HEIGHT, round=True) +from picographics import PicoGraphics, DISPLAY_ROUND_LCD_240X240 +display = PicoGraphics(display=DISPLAY_ROUND_LCD_240X240) display.set_backlight(1.0) +WIDTH, HEIGHT = display.get_bounds() RADIUS = WIDTH // 2 +BLACK = display.create_pen(0, 0, 0) + def hsv_to_rgb(h, s, v): if s == 0.0: @@ -37,7 +37,7 @@ def hsv_to_rgb(h, s, v): t = 0 while True: - display.set_pen(0, 0, 0) + display.set_pen(BLACK) display.clear() angle = t % (math.pi * 2) @@ -60,7 +60,8 @@ while True: y = RADIUS + int(distance * math.sin(angle)) radius = ((math.sin(t + angle) + 1) / 2.0) * 10 - display.set_pen(r, g, b) + dot_colour = display.create_pen(r, g, b) + display.set_pen(dot_colour) display.circle(int(x), int(y), int(radius)) prev_x = x diff --git a/micropython/examples/breakout_roundlcd/drawing_primitives_demo.py b/micropython/examples/breakout_roundlcd/drawing_primitives_demo.py index 86bec1bc..650cb9ed 100644 --- a/micropython/examples/breakout_roundlcd/drawing_primitives_demo.py +++ b/micropython/examples/breakout_roundlcd/drawing_primitives_demo.py @@ -1,20 +1,25 @@ -from breakout_roundlcd import BreakoutRoundLCD +from picographics import PicoGraphics, DISPLAY_ROUND_LCD_240X240 +display = PicoGraphics(display=DISPLAY_ROUND_LCD_240X240) +display.set_backlight(1.0) -width = BreakoutRoundLCD.WIDTH -height = BreakoutRoundLCD.HEIGHT +width, height = display.get_bounds() -display_buffer = bytearray(width * height * 2) # 2-bytes per pixel (RGB565) -display = BreakoutRoundLCD(display_buffer) - -display.set_backlight(1) +BLACK = display.create_pen(0, 0, 0) +RED = display.create_pen(255, 0, 0) +YELLOW = display.create_pen(255, 255, 0) +GREEN = display.create_pen(0, 255, 0) +CYAN = display.create_pen(0, 255, 255) +WHITE = display.create_pen(255, 255, 255) +BLUE = display.create_pen(0, 0, 255) +MAGENTA = display.create_pen(255, 0, 255) while True: - display.set_pen(0, 0, 0) + display.set_pen(BLACK) display.clear() # circle - display.set_pen(255, 0, 0) + display.set_pen(RED) display.circle( width // 5, # center point x height // 3, # center point y @@ -22,7 +27,7 @@ while True: ) # rectangle - display.set_pen(255, 255, 0) + display.set_pen(YELLOW) display.rectangle( int((width * 2 / 5) - 16), # starting point x int(height // 3) - 8, # starting point y @@ -31,7 +36,7 @@ while True: ) # triangle - display.set_pen(0, 255, 0) + display.set_pen(GREEN) display.triangle( int(width * 3 / 5), int(height // 3) - 16, # point 1 x, y int(width * 3 / 5) - 16, int(height // 3) + 16, # point 2 x, y @@ -39,7 +44,7 @@ while True: ) # character - display.set_pen(0, 255, 255) + display.set_pen(CYAN) display.character( 64, # int character code int(width * 4 / 5 - 16), # box starting point x @@ -48,7 +53,7 @@ while True: ) # pixel span - display.set_pen(255, 255, 255) + display.set_pen(WHITE) display.pixel_span( int(width * 1 / 5), # starting point x int(height * 2.5 / 5), # starting point y @@ -56,7 +61,7 @@ while True: ) # text - display.set_pen(0, 0, 255) + display.set_pen(BLUE) display.text( 'test text', # text int(width // 5), # box starting point x @@ -66,7 +71,7 @@ while True: ) # lines - display.set_pen(255, 0, 255) + display.set_pen(MAGENTA) display.line( 0, # staring point x int(height / 2), # staring point y diff --git a/micropython/examples/breakout_colourlcd160x80/demo.py b/micropython/examples/pico_display/balls_demo.py similarity index 63% rename from micropython/examples/breakout_colourlcd160x80/demo.py rename to micropython/examples/pico_display/balls_demo.py index ed9b66fa..71c79a50 100644 --- a/micropython/examples/breakout_colourlcd160x80/demo.py +++ b/micropython/examples/pico_display/balls_demo.py @@ -1,15 +1,15 @@ import time import random -from breakout_colourlcd160x80 import BreakoutColourLCD160x80 - -width = BreakoutColourLCD160x80.WIDTH -height = BreakoutColourLCD160x80.HEIGHT - -display_buffer = bytearray(width * height * 2) # 2-bytes per pixel (RGB565) -display = BreakoutColourLCD160x80(display_buffer) +from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY, PEN_P8 +display = PicoGraphics(display=DISPLAY_PICO_DISPLAY, pen_type=PEN_P8) display.set_backlight(1.0) +WIDTH, HEIGHT = display.get_bounds() + +# We're creating 100 balls with their own individual colour and 1 BG colour +# for a total of 101 colours, which will all fit in the custom 256 entry palette! + class Ball: def __init__(self, x, y, r, dx, dy, pen): @@ -27,8 +27,8 @@ for i in range(0, 100): r = random.randint(0, 10) + 3 balls.append( Ball( - random.randint(r, r + (width - 2 * r)), - random.randint(r, r + (height - 2 * r)), + random.randint(r, r + (WIDTH - 2 * r)), + random.randint(r, r + (HEIGHT - 2 * r)), r, (14 - r) / 2, (14 - r) / 2, @@ -36,17 +36,19 @@ for i in range(0, 100): ) ) +BG = display.create_pen(40, 40, 40) + while True: - display.set_pen(40, 40, 40) + display.set_pen(BG) display.clear() for ball in balls: ball.x += ball.dx ball.y += ball.dy - xmax = width - ball.r + xmax = WIDTH - ball.r xmin = ball.r - ymax = height - ball.r + ymax = HEIGHT - ball.r ymin = ball.r if ball.x < xmin or ball.x > xmax: diff --git a/micropython/examples/pico_display/basic_qrcode.py b/micropython/examples/pico_display/basic_qrcode.py index a689b764..01025987 100644 --- a/micropython/examples/pico_display/basic_qrcode.py +++ b/micropython/examples/pico_display/basic_qrcode.py @@ -1,12 +1,13 @@ -import st7789 +from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY import qrcode -display = st7789.ST7789(st7789.DISPLAY_PICO_DISPLAY, rotate=0) +display = PicoGraphics(display=DISPLAY_PICO_DISPLAY, rotate=0) +display.set_backlight(1.0) WIDTH, HEIGHT = display.get_bounds() BG = display.create_pen(0, 0, 0) -FG = display.create_pen(128, 128, 128) +FG = display.create_pen(255, 255, 255) def measure_qr_code(size, code): diff --git a/micropython/examples/pico_display/buttons.py b/micropython/examples/pico_display/button_test.py similarity index 58% rename from micropython/examples/pico_display/buttons.py rename to micropython/examples/pico_display/button_test.py index 0d350f8d..d55d8efb 100644 --- a/micropython/examples/pico_display/buttons.py +++ b/micropython/examples/pico_display/button_test.py @@ -1,59 +1,69 @@ # This example shows you a simple, non-interrupt way of reading Pico Display's buttons with a loop that checks to see if buttons are pressed. -import st7789 -import utime +import time from pimoroni import Button +from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY, PEN_P4 +# We're only using a few colours so we can use a 4 bit/16 colour palette and save RAM! +display = PicoGraphics(display=DISPLAY_PICO_DISPLAY, pen_type=PEN_P4, rotate=0) -display = st7789.ST7789(st7789.DISPLAY_PICO_DISPLAY, rotate=0) display.set_backlight(0.5) - -WIDTH, HEIGHT = display.get_bounds() +display.set_font("bitmap8") button_a = Button(12) button_b = Button(13) button_x = Button(14) button_y = Button(15) +WHITE = display.create_pen(255, 255, 255) +BLACK = display.create_pen(0, 0, 0) +CYAN = display.create_pen(0, 255, 255) +MAGENTA = display.create_pen(255, 0, 255) +YELLOW = display.create_pen(255, 255, 0) +GREEN = display.create_pen(0, 255, 0) + # sets up a handy function we can call to clear the screen def clear(): - display.set_pen(0, 0, 0) + display.set_pen(BLACK) display.clear() display.update() +# set up +clear() + while True: if button_a.read(): # if a button press is detected then... clear() # clear to black - display.set_pen(255, 255, 255) # change the pen colour + display.set_pen(WHITE) # change the pen colour display.text("Button A pressed", 10, 10, 240, 4) # display some text on the screen display.update() # update the display - utime.sleep(1) # pause for a sec + time.sleep(1) # pause for a sec clear() # clear to black again elif button_b.read(): clear() - display.set_pen(0, 255, 255) + display.set_pen(CYAN) display.text("Button B pressed", 10, 10, 240, 4) display.update() - utime.sleep(1) + time.sleep(1) clear() elif button_x.read(): clear() - display.set_pen(255, 0, 255) + display.set_pen(MAGENTA) display.text("Button X pressed", 10, 10, 240, 4) display.update() - utime.sleep(1) + time.sleep(1) clear() elif button_y.read(): clear() - display.set_pen(255, 255, 0) + display.set_pen(YELLOW) display.text("Button Y pressed", 10, 10, 240, 4) display.update() - utime.sleep(1) + time.sleep(1) clear() else: - display.set_pen(255, 0, 0) + display.set_pen(GREEN) display.text("Press any button!", 10, 10, 240, 4) display.update() - utime.sleep(0.1) # this number is how frequently the Pico checks for button presses + time.sleep(0.1) # this number is how frequently the Pico checks for button presses diff --git a/micropython/examples/pico_display/pride_stripes.py b/micropython/examples/pico_display/pride_stripes.py new file mode 100644 index 00000000..e6846fd5 --- /dev/null +++ b/micropython/examples/pico_display/pride_stripes.py @@ -0,0 +1,48 @@ +# A customisable Pride flag. (Look in the Tufty 2040 examples for a name badge version!) + +from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY + +display = PicoGraphics(display=DISPLAY_PICO_DISPLAY, rotate=0) + +WIDTH, HEIGHT = display.get_bounds() + +# List of available pen colours, add more if necessary +RED = display.create_pen(209, 34, 41) +ORANGE = display.create_pen(246, 138, 30) +YELLOW = display.create_pen(255, 216, 0) +GREEN = display.create_pen(0, 121, 64) +INDIGO = display.create_pen(36, 64, 142) +VIOLET = display.create_pen(115, 41, 130) +WHITE = display.create_pen(255, 255, 255) +PINK = display.create_pen(255, 175, 200) +BLUE = display.create_pen(116, 215, 238) +BROWN = display.create_pen(97, 57, 21) +BLACK = display.create_pen(0, 0, 0) +MAGENTA = display.create_pen(255, 33, 140) +CYAN = display.create_pen(33, 177, 255) + +# Uncomment one of these to change flag +# If adding your own, colour order is left to right (or top to bottom) +COLOUR_ORDER = [RED, ORANGE, YELLOW, GREEN, INDIGO, VIOLET] # traditional pride flag +# COLOUR_ORDER = [BLACK, BROWN, RED, ORANGE, YELLOW, GREEN, INDIGO, VIOLET] # Philadelphia pride flag +# COLOUR_ORDER = [BLUE, PINK, WHITE, PINK, BLUE] # trans flag +# COLOUR_ORDER = [MAGENTA, YELLOW, CYAN] # pan flag +# COLOUR_ORDER = [MAGENTA, VIOLET, INDIGO] # bi flag + +# Change this for vertical stripes +STRIPES_DIRECTION = "horizontal" + +# Draw the flag +if STRIPES_DIRECTION == "horizontal": + stripe_width = round(HEIGHT / len(COLOUR_ORDER)) + for x in range(len(COLOUR_ORDER)): + display.set_pen(COLOUR_ORDER[x]) + display.rectangle(0, stripe_width * x, WIDTH, stripe_width) + +if STRIPES_DIRECTION == "vertical": + stripe_width = round(WIDTH / len(COLOUR_ORDER)) + for x in range(len(COLOUR_ORDER)): + display.set_pen(COLOUR_ORDER[x]) + display.rectangle(stripe_width * x, 0, stripe_width, HEIGHT) + +display.update() diff --git a/micropython/examples/pico_display/rainbow.py b/micropython/examples/pico_display/rainbow.py index d2af8d85..38305fbf 100644 --- a/micropython/examples/pico_display/rainbow.py +++ b/micropython/examples/pico_display/rainbow.py @@ -1,16 +1,18 @@ # This example borrows a CircuitPython hsv_to_rgb function to cycle through some rainbows on Pico Display's screen and RGB LED . If you're into rainbows, HSV (Hue, Saturation, Value) is very useful! +# We're using a RAM intensive 64K colour palette here to get a nice smooth colour transition. -import utime -import st7789 +import time +from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY, PEN_RGB565 from pimoroni import RGBLED -display = st7789.ST7789(st7789.DISPLAY_PICO_DISPLAY, rotate=0) +display = PicoGraphics(display=DISPLAY_PICO_DISPLAY, pen_type=PEN_RGB565, rotate=0) display.set_backlight(0.8) -display.set_palette_mode(st7789.PALETTE_USER) + +led = RGBLED(6, 7, 8) WIDTH, HEIGHT = display.get_bounds() -led = RGBLED(6, 7, 8) +BLACK = display.create_pen(0, 0, 0) # From CPython Lib/colorsys.py @@ -39,18 +41,14 @@ def hsv_to_rgb(h, s, v): h = 0 -BLACK = display.create_pen(0, 0, 0) -RAINBOW = BLACK + 1 # Put RAINBOW right after BLACK in the palette - - while True: h += 1 r, g, b = [int(255 * c) for c in hsv_to_rgb(h / 360.0, 1.0, 1.0)] # rainbow magic led.set_rgb(r, g, b) # Set LED to a converted HSV value - display.set_palette(RAINBOW, st7789.RGB565(r, g, b)) # Create pen with converted HSV value + RAINBOW = display.create_pen(r, g, b) # Create pen with converted HSV value display.set_pen(RAINBOW) # Set pen display.clear() # Fill the screen with the colour display.set_pen(BLACK) # Set pen to black display.text("pico disco!", 10, 10, 240, 6) # Add some text display.update() # Update the display - utime.sleep(1.0 / 60) + time.sleep(1.0 / 60) diff --git a/micropython/examples/pico_display/ram_test.py b/micropython/examples/pico_display/ram_test.py new file mode 100644 index 00000000..98e4beae --- /dev/null +++ b/micropython/examples/pico_display/ram_test.py @@ -0,0 +1,64 @@ +import gc +import time +from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY, PEN_RGB332 + +# PEN_RGB332 is an 8 bit, fixed 256 colour palette which conserves your RAM. +# Try switching the pen_type to PEN_RGB565 (16 bit, 65K colour) and see the difference! + +display = PicoGraphics(DISPLAY_PICO_DISPLAY, pen_type=PEN_RGB332, rotate=0) + +# set up constants for drawing +WIDTH, HEIGHT = display.get_bounds() + +BLACK = display.create_pen(0, 0, 0) + + +def free(full=False): + # Calculates RAM usage + gc.collect() + F = gc.mem_free() + A = gc.mem_alloc() + T = F + A + P = '{0:.2f}%'.format(F / T * 100) + if not full: + return P + else: + return (f"Total RAM \n{T} KB \nUnused RAM \n{F} KB \n({P} free)") + + +def hsv_to_rgb(h, s, v): + # From CPython Lib/colorsys.py + if s == 0.0: + return v, v, v + i = int(h * 6.0) + f = (h * 6.0) - i + p = v * (1.0 - s) + q = v * (1.0 - s * f) + t = v * (1.0 - s * (1.0 - f)) + i = i % 6 + if i == 0: + return v, t, p + if i == 1: + return q, v, p + if i == 2: + return p, v, t + if i == 3: + return p, q, v + if i == 4: + return t, p, v + if i == 5: + return v, p, q + + +h = 0 + +while True: + h += 1 + r, g, b = [int(255 * c) for c in hsv_to_rgb(h / 360.0, 1.0, 1.0)] # rainbow magic + display.set_pen(BLACK) + RAINBOW = display.create_pen(r, g, b) # Create pen with converted HSV value + display.set_pen(RAINBOW) + display.set_font("bitmap8") + display.text(free(full=True), 0, 0, WIDTH, 3) + display.update() + time.sleep(1.0 / 60) diff --git a/micropython/examples/pico_display/thermometer.py b/micropython/examples/pico_display/thermometer.py index ea680779..b3ffa0e5 100644 --- a/micropython/examples/pico_display/thermometer.py +++ b/micropython/examples/pico_display/thermometer.py @@ -2,28 +2,26 @@ # It's based on the thermometer example in the "Getting Started with MicroPython on the Raspberry Pi Pico" book, which is a great read if you're a beginner! import machine -import utime - -import st7789 +import time from pimoroni import RGBLED +from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY -display = st7789.ST7789(st7789.DISPLAY_PICO_DISPLAY, rotate=0) -display.set_palette_mode(st7789.PALETTE_USER) +# set up the hardware +display = PicoGraphics(display=DISPLAY_PICO_DISPLAY, rotate=0) +sensor_temp = machine.ADC(4) +led = RGBLED(6, 7, 8) -# Set the display backlight to 50% +# set the display backlight to 50% display.set_backlight(0.5) +# set up constants for drawing WIDTH, HEIGHT = display.get_bounds() BLACK = display.create_pen(0, 0, 0) WHITE = display.create_pen(255, 255, 255) -TEMPERATURE = WHITE + 1 -led = RGBLED(6, 7, 8) +conversion_factor = 3.3 / (65535) # used for calculating a temperature from the raw sensor reading -# reads from Pico's temp sensor and converts it into a more manageable number -sensor_temp = machine.ADC(4) -conversion_factor = 3.3 / (65535) temp_min = 10 temp_max = 30 bar_width = 5 @@ -69,10 +67,11 @@ while True: temperatures.pop(0) i = 0 + for t in temperatures: - # chooses a pen colour based on the temperature and update the palette entry - display.set_palette(TEMPERATURE, st7789.RGB565(*temperature_to_color(t))) - display.set_pen(TEMPERATURE) + # chooses a pen colour based on the temperature + TEMPERATURE_COLOUR = display.create_pen(*temperature_to_color(t)) + display.set_pen(TEMPERATURE_COLOUR) # draws the reading as a tall, thin rectangle display.rectangle(i, HEIGHT - (round(t) * 4), bar_width, HEIGHT) @@ -96,4 +95,4 @@ while True: display.update() # waits for 5 seconds - utime.sleep(5) + time.sleep(5) diff --git a/micropython/examples/pico_explorer/balls_demo.py b/micropython/examples/pico_explorer/balls_demo.py index e87725fe..40cc9de4 100644 --- a/micropython/examples/pico_explorer/balls_demo.py +++ b/micropython/examples/pico_explorer/balls_demo.py @@ -1,12 +1,14 @@ import time import random -import st7789 +from picographics import PicoGraphics, DISPLAY_PICO_EXPLORER, PEN_P8 -display = st7789.ST7789(st7789.DISPLAY_PICO_EXPLORER, rotate=0) -display.set_backlight(1.0) +display = PicoGraphics(display=DISPLAY_PICO_EXPLORER, pen_type=PEN_P8) WIDTH, HEIGHT = display.get_bounds() +# We're creating 100 balls with their own individual colour and 1 BG colour +# for a total of 101 colours, which will all fit in the custom 256 entry palette! + class Ball: def __init__(self, x, y, r, dx, dy, pen): @@ -33,7 +35,7 @@ for i in range(0, 100): ) ) -BG = display.create_pen(32, 32, 64) +BG = display.create_pen(40, 40, 40) while True: display.set_pen(BG) @@ -58,5 +60,4 @@ while True: display.circle(int(ball.x), int(ball.y), int(ball.r)) display.update() - -time.sleep(0.01) + time.sleep(0.01) diff --git a/micropython/examples/pico_explorer/button_test.py b/micropython/examples/pico_explorer/button_test.py new file mode 100644 index 00000000..c1066049 --- /dev/null +++ b/micropython/examples/pico_explorer/button_test.py @@ -0,0 +1,74 @@ +# This example shows you a simple, non-interrupt way of reading Pico Explorer's buttons with a loop that checks to see if buttons are pressed. + +import time +from pimoroni import Button +from picographics import PicoGraphics, DISPLAY_PICO_EXPLORER, PEN_P4 +import picoexplorer + +# We're only using a few colours so we can use a 4 bit/16 colour palette and save RAM! +display = PicoGraphics(display=DISPLAY_PICO_EXPLORER, pen_type=PEN_P4) + +button_a = Button(picoexplorer.BUTTON_A) +button_b = Button(picoexplorer.BUTTON_B) +button_x = Button(picoexplorer.BUTTON_X) +button_y = Button(picoexplorer.BUTTON_Y) + +# alternatively, you could set up the buttons using pin number if you prefer +# button_a = Button(12) +# button_b = Button(13) +# button_x = Button(14) +# button_y = Button(15) + +WHITE = display.create_pen(255, 255, 255) +BLACK = display.create_pen(0, 0, 0) +CYAN = display.create_pen(0, 255, 255) +MAGENTA = display.create_pen(255, 0, 255) +YELLOW = display.create_pen(255, 255, 0) +GREEN = display.create_pen(0, 255, 0) + + +# sets up a handy function we can call to clear the screen +def clear(): + display.set_pen(BLACK) + display.clear() + display.update() + + +# set up +clear() +display.set_font("bitmap8") + +while True: + if button_a.read(): # if a button press is detected then... + clear() # clear to black + display.set_pen(WHITE) # change the pen colour + display.text("Button A pressed", 10, 10, 240, 4) # display some text on the screen + display.update() # update the display + time.sleep(1) # pause for a sec + clear() # clear to black again + elif button_b.read(): + clear() + display.set_pen(CYAN) + display.text("Button B pressed", 10, 10, 240, 4) + display.update() + time.sleep(1) + clear() + elif button_x.read(): + clear() + display.set_pen(MAGENTA) + display.text("Button X pressed", 10, 10, 240, 4) + display.update() + time.sleep(1) + clear() + elif button_y.read(): + clear() + display.set_pen(YELLOW) + display.text("Button Y pressed", 10, 10, 240, 4) + display.update() + time.sleep(1) + clear() + else: + display.set_pen(GREEN) + display.text("Press any button!", 10, 10, 240, 4) + display.update() + time.sleep(0.1) # this number is how frequently the Pico checks for button presses diff --git a/micropython/examples/pico_explorer/buttons.py b/micropython/examples/pico_explorer/buttons.py deleted file mode 100644 index 8e49d70a..00000000 --- a/micropython/examples/pico_explorer/buttons.py +++ /dev/null @@ -1,66 +0,0 @@ -# This example shows you a simple, non-interrupt way of reading Pico Explorer's buttons with a loop that checks to see if buttons are pressed. - -import utime -import st7789 -import picoexplorer -from pimoroni import Button - - -display = st7789.ST7789(st7789.DISPLAY_PICO_EXPLORER, rotate=0) -display.set_palette_mode(st7789.PALETTE_USER) -display.set_backlight(1.0) - -button_a = Button(picoexplorer.BUTTON_A) -button_b = Button(picoexplorer.BUTTON_B) -button_x = Button(picoexplorer.BUTTON_X) -button_y = Button(picoexplorer.BUTTON_Y) - -WHITE = display.create_pen(255, 255, 255) -BLACK = display.create_pen(0, 0, 0) -TEAL = display.create_pen(0, 255, 255) -MAGENTA = display.create_pen(255, 0, 255) -YELLOW = display.create_pen(255, 255, 0) -RED = display.create_pen(255, 0, 0) - - -while True: - if button_a.is_pressed: # if a button press is detected then... - display.set_pen(BLACK) # set pen to black - display.clear() # clear display to the pen colour - display.set_pen(WHITE) # change the pen colour - display.text("Button A pressed", 10, 10, 240, 4) # display some text on the screen - display.update() # update the display - utime.sleep(1) # pause for a sec - - elif button_b.is_pressed: - display.set_pen(BLACK) - display.clear() - display.set_pen(TEAL) - display.text("Button B pressed", 10, 10, 240, 4) - display.update() - utime.sleep(1) - - elif button_x.is_pressed: - display.set_pen(BLACK) - display.clear() - display.set_pen(MAGENTA) - display.text("Button X pressed", 10, 10, 240, 4) - display.update() - utime.sleep(1) - - elif button_y.is_pressed: - display.set_pen(BLACK) - display.clear() - display.set_pen(YELLOW) - display.text("Button Y pressed", 10, 10, 240, 4) - display.update() - utime.sleep(1) - - else: - display.set_pen(BLACK) - display.clear() - display.set_pen(RED) - display.text("Press any button!", 10, 10, 240, 4) - display.update() - - utime.sleep(0.1) # this number is how frequently the Pico checks for button presses diff --git a/micropython/examples/pico_explorer/demo.py b/micropython/examples/pico_explorer/demo.py index 3156091f..32f06129 100644 --- a/micropython/examples/pico_explorer/demo.py +++ b/micropython/examples/pico_explorer/demo.py @@ -1,13 +1,10 @@ import time -import st7789 +from picographics import PicoGraphics, DISPLAY_PICO_EXPLORER from motor import Motor import picoexplorer from pimoroni import Button, Analog, Buzzer -display = st7789.ST7789(st7789.DISPLAY_PICO_EXPLORER, rotate=0) -display.set_palette_mode(st7789.PALETTE_USER) -display.set_backlight(1.0) - +display = PicoGraphics(display=DISPLAY_PICO_EXPLORER) adc0 = Analog(picoexplorer.ADC0) adc1 = Analog(picoexplorer.ADC1) @@ -21,9 +18,9 @@ button_y = Button(picoexplorer.BUTTON_Y) BG = display.create_pen(32, 32, 64) WHITE = display.create_pen(255, 255, 255) -ADC0_PEN = display.reserve_palette() -ADC1_PEN = display.reserve_palette() -ADC2_PEN = display.reserve_palette() +ADC0_PEN = display.create_pen(255, 0, 0) +ADC1_PEN = display.create_pen(0, 255, 0) +ADC2_PEN = display.create_pen(0, 0, 255) MOTOR1 = Motor(picoexplorer.MOTOR_1) MOTOR2 = Motor(picoexplorer.MOTOR_2) @@ -41,11 +38,6 @@ while True: adc1v = int(adc1.read_voltage() / 3.3 * 120) adc2v = int(adc2.read_voltage() / 3.3 * 120) - # Update our ADC channel palette colours - display.set_palette(ADC0_PEN, st7789.RGB565(adc0v * 2, 10, 10)) - display.set_palette(ADC1_PEN, st7789.RGB565(10, adc1v * 2, 10)) - display.set_palette(ADC2_PEN, st7789.RGB565(10, 10, adc2v * 2)) - # ADC labels display.set_pen(WHITE) display.text("ADC0:", 20, 20, 100) diff --git a/micropython/examples/pico_explorer/noise.py b/micropython/examples/pico_explorer/noise.py index 5db940dc..ba467afd 100644 --- a/micropython/examples/pico_explorer/noise.py +++ b/micropython/examples/pico_explorer/noise.py @@ -2,17 +2,14 @@ # It uses code written by Avram Piltch - check out his Tom's Hardware article! https://www.tomshardware.com/uk/how-to/buzzer-music-raspberry-pi-pico # You'll need to connect a jumper wire between GPO and AUDIO on the Explorer Base to hear noise. -import utime -import st7789 +import time +from picographics import PicoGraphics, DISPLAY_PICO_EXPLORER from pimoroni import Buzzer +display = PicoGraphics(display=DISPLAY_PICO_EXPLORER) -display = st7789.ST7789(st7789.DISPLAY_PICO_EXPLORER, rotate=0) -display.set_palette_mode(st7789.PALETTE_USER) -display.set_backlight(1.0) - -# tCreate a buzzer on pin 0 -# Don't forget t write GP0 to AUDIO! +# Create a buzzer on pin 0 +# Don't forget to wire GP0 to AUDIO! BUZZER = Buzzer(0) BLACK = display.create_pen(0, 0, 0) @@ -143,7 +140,7 @@ def playsong(song): # this function plays your song clear() a = 0 display.update() - utime.sleep(0.15) # change this number if you want to alter how long the notes play for + time.sleep(0.15) # change this number if you want to alter how long the notes play for bequiet() diff --git a/micropython/examples/pico_explorer/rainbow.py b/micropython/examples/pico_explorer/rainbow.py index dee58743..e058e9b0 100644 --- a/micropython/examples/pico_explorer/rainbow.py +++ b/micropython/examples/pico_explorer/rainbow.py @@ -1,19 +1,14 @@ -# This example borrows a CircuitPython hsv_to_rgb function to cycle through some rainbows on Pico Explorer's screen. -# If you're into rainbows, HSV (Hue, Saturation, Value) is very useful! +# This example borrows a CircuitPython hsv_to_rgb function to cycle through some rainbows on Pico Explorer's screen. If you're into rainbows, HSV (Hue, Saturation, Value) is very useful! +# We're using a RAM intensive 64K colour palette here to get a nice smooth colour transition. -import utime -import st7789 +import time +from picographics import PicoGraphics, DISPLAY_PICO_EXPLORER, PEN_RGB565 +display = PicoGraphics(display=DISPLAY_PICO_EXPLORER, pen_type=PEN_RGB565) -display = st7789.ST7789(st7789.DISPLAY_PICO_EXPLORER, rotate=0) -display.set_palette_mode(st7789.PALETTE_USER) -display.set_backlight(1.0) +WIDTH, HEIGHT = display.get_bounds() -# Create a text colour -TEXT_COLOR = display.create_pen(0, 0, 0) - -# Reserve a palette entry for our rainbow background colour -RAINBOW = display.reserve_palette() +BLACK = display.create_pen(0, 0, 0) # From CPython Lib/colorsys.py @@ -45,12 +40,12 @@ h = 0 while True: h += 1 r, g, b = [int(255 * c) for c in hsv_to_rgb(h / 360.0, 1.0, 1.0)] # rainbow magic - display.set_palette(RAINBOW, st7789.RGB565(r, g, b)) # Set pen to a converted HSV value - display.set_pen(RAINBOW) + RAINBOW = display.create_pen(r, g, b) # Create pen with converted HSV value + display.set_pen(RAINBOW) # Set pen display.clear() # Fill the screen with the colour - display.set_pen(TEXT_COLOR) - display.text("pico disco!", 25, 20, 240, 6) # Add some text + display.set_pen(BLACK) # Set pen to black + display.text("pico disco!", 10, 10, 240, 6) # Add some text display.text("\\o/ \\o/ \\o/ \\o/ \\o/ \\o/ \\o/ \\o/ \\o/", 25, 120, 240, 4) # and some more text display.text("oontz oontz oontz", 25, 220, 240, 2) # and a bit more tiny text display.update() # Update the display - utime.sleep(1.0 / 60) + time.sleep(1.0 / 60) diff --git a/micropython/examples/pico_explorer/thermometer.py b/micropython/examples/pico_explorer/thermometer.py index 3b7b5b7c..05602d70 100644 --- a/micropython/examples/pico_explorer/thermometer.py +++ b/micropython/examples/pico_explorer/thermometer.py @@ -1,63 +1,90 @@ # This example takes the temperature from the Pico's onboard temperature sensor, and displays it on Pico Explorer, along with a little pixelly graph. # It's based on the thermometer example in the "Getting Started with MicroPython on the Raspberry Pi Pico" book, which is a great read if you're a beginner! -import st7789 import machine -import utime +import time +from picographics import PicoGraphics, DISPLAY_PICO_EXPLORER -# Pico Explorer boilerplate -display = st7789.ST7789(st7789.DISPLAY_PICO_EXPLORER, rotate=0) -display.set_palette_mode(st7789.PALETTE_USER) -display.set_backlight(1.0) - -WIDTH, HEIGHT = display.get_bounds() - -# reads from Pico's temp sensor and converts it into a more manageable number +# set up the hardware +display = PicoGraphics(display=DISPLAY_PICO_EXPLORER) sensor_temp = machine.ADC(4) -conversion_factor = 3.3 / (65535) + +# set up constants for drawing +WIDTH, HEIGHT = display.get_bounds() BLACK = display.create_pen(0, 0, 0) WHITE = display.create_pen(255, 255, 255) -RED = display.create_pen(255, 0, 0) -GREEN = display.create_pen(0, 255, 0) -BLUE = display.create_pen(0, 0, 255) -i = 0 +conversion_factor = 3.3 / (65535) # used for calculating a temperature from the raw sensor reading + +temp_min = 10 +temp_max = 30 +bar_width = 5 + +temperatures = [] + +colors = [(0, 0, 255), (0, 255, 0), (255, 255, 0), (255, 0, 0)] + + +def temperature_to_color(temp): + temp = min(temp, temp_max) + temp = max(temp, temp_min) + + f_index = float(temp - temp_min) / float(temp_max - temp_min) + f_index *= len(colors) - 1 + index = int(f_index) + + if index == len(colors) - 1: + return colors[index] + + blend_b = f_index - index + blend_a = 1.0 - blend_b + + a = colors[index] + b = colors[index + 1] + + return [int((a[i] * blend_a) + (b[i] * blend_b)) for i in range(3)] + while True: + # fills the screen with black + display.set_pen(BLACK) + display.clear() + # the following two lines do some maths to convert the number from the temp sensor into celsius reading = sensor_temp.read_u16() * conversion_factor - temperature = round(27 - (reading - 0.706) / 0.001721) + temperature = 27 - (reading - 0.706) / 0.001721 - # this if statement clears the display once the graph reaches the right hand side of the display - if i >= WIDTH + 1: - i = 0 - display.set_pen(BLACK) - display.clear() + temperatures.append(temperature) - # chooses a pen colour based on the temperature - display.set_pen(GREEN) - if temperature > 20: - display.set_pen(RED) - if temperature < 13: - display.set_pen(BLUE) + # shifts the temperatures history to the left by one sample + if len(temperatures) > WIDTH // bar_width: + temperatures.pop(0) - # draws the reading as a tall, thin rectangle - display.rectangle(i, HEIGHT - (temperature * 6), 6, HEIGHT) + i = 0 + + for t in temperatures: + # chooses a pen colour based on the temperature + TEMPERATURE_COLOUR = display.create_pen(*temperature_to_color(t)) + display.set_pen(TEMPERATURE_COLOUR) + + # draws the reading as a tall, thin rectangle + display.rectangle(i, HEIGHT - (round(t) * 4), bar_width, HEIGHT) + + # the next tall thin rectangle needs to be drawn + # "bar_width" (default: 5) pixels to the right of the last one + i += bar_width # draws a white background for the text display.set_pen(WHITE) - display.rectangle(1, 1, 65, 33) + display.rectangle(1, 1, 100, 25) # writes the reading as text in the white rectangle display.set_pen(BLACK) - display.text("{:.0f}".format(temperature) + "c", 3, 3, 0, 4) + display.text("{:.2f}".format(temperature) + "c", 3, 3, 0, 3) # time to update the display display.update() # waits for 5 seconds - utime.sleep(5) - - # the next tall thin rectangle needs to be drawn 6 pixels to the right of the last one - i += 6 + time.sleep(5) diff --git a/micropython/examples/pico_explorer/weatherstation_BME280.py b/micropython/examples/pico_explorer/weatherstation_BME280.py index 3e4d67dc..8e915704 100644 --- a/micropython/examples/pico_explorer/weatherstation_BME280.py +++ b/micropython/examples/pico_explorer/weatherstation_BME280.py @@ -1,22 +1,18 @@ # This example lets you plug a BME280 breakout into your Pico Explorer and make a little indoor weather station, with barometer style descriptions. -import utime - +import time from breakout_bme280 import BreakoutBME280 from pimoroni_i2c import PimoroniI2C from pimoroni import PICO_EXPLORER_I2C_PINS +from picographics import PicoGraphics, DISPLAY_PICO_EXPLORER -# Pico Explorer boilerplate -import st7789 -display = st7789.ST7789(st7789.DISPLAY_PICO_EXPLORER, rotate=0) -display.set_palette_mode(st7789.PALETTE_USER) -display.set_backlight(1.0) - +# set up the hardware +display = PicoGraphics(display=DISPLAY_PICO_EXPLORER) i2c = PimoroniI2C(**PICO_EXPLORER_I2C_PINS) -bme = BreakoutBME280(i2c) +bme = BreakoutBME280(i2c, address=0x76) # lets set up some pen colours to make drawing easier -TEMPCOLOUR = display.reserve_palette() # this colour will get changed in a bit +TEMPCOLOUR = display.create_pen(0, 0, 0) # this colour will get changed in a bit WHITE = display.create_pen(255, 255, 255) BLACK = display.create_pen(0, 0, 0) RED = display.create_pen(255, 0, 0) @@ -28,22 +24,22 @@ def describe_temperature(temperature): global TEMPCOLOUR if temperature < 10: description = "very cold" - display.set_palette(TEMPCOLOUR, st7789.RGB565(0, 255, 255)) + TEMPCOLOUR = display.create_pen(0, 255, 255) elif 10 <= temperature < 20: description = "cold" - display.set_palette(TEMPCOLOUR, st7789.RGB565(0, 0, 255)) + TEMPCOLOUR = display.create_pen(0, 0, 255) elif 20 <= temperature < 25: description = "temperate" - display.set_palette(TEMPCOLOUR, st7789.RGB565(0, 255, 0)) + TEMPCOLOUR = display.create_pen(0, 255, 0) elif 25 <= temperature < 30: description = "warm" - display.set_palette(TEMPCOLOUR, st7789.RGB565(255, 255, 0)) + TEMPCOLOUR = display.create_pen(255, 255, 0) elif temperature >= 30: description = "very warm" - display.set_palette(TEMPCOLOUR, st7789.RGB565(255, 0, 0)) + TEMPCOLOUR = display.create_pen(255, 0, 0) else: description = "" - display.set_palette(TEMPCOLOUR, st7789.RGB565(0, 0, 0)) + TEMPCOLOUR = display.create_pen(0, 0, 0) return description @@ -53,22 +49,22 @@ def describe_temperature(temperature): global TEMPCOLOUR if temperature < 10: description = "frozzed" - display.set_palette(TEMPCOLOUR, st7789.RGB565(0, 255, 255)) + TEMPCOLOUR = display.create_pen(0, 255, 255) elif 10 <= temperature < 20: description = "nithering" - display.set_palette(TEMPCOLOUR, st7789.RGB565(0, 0, 255)) + TEMPCOLOUR = display.create_pen(0, 0, 255) elif 20 <= temperature < 25: description = "fair t' middlin" - display.set_palette(TEMPCOLOUR, st7789.RGB565(0, 255, 0)) + TEMPCOLOUR = display.create_pen(0, 255, 0) elif 25 <= temperature < 30: description = "chuffing warm" - display.set_palette(TEMPCOLOUR, st7789.RGB565(255, 255, 0)) + TEMPCOLOUR = display.create_pen(255, 255, 0) elif temperature >= 30: description = "crackin t' flags" - display.set_palette(TEMPCOLOUR, st7789.RGB565(255, 0, 0)) + TEMPCOLOUR = display.create_pen(255, 0, 0) else: description = "" - display.set_palette(TEMPCOLOUR, st7789.RGB565(0, 0, 0)) + TEMPCOLOUR = display.create_pen(0, 0, 0) return description """ @@ -145,4 +141,4 @@ while True: display.update() # waits for 1 second and clears to BLACK - utime.sleep(1) + time.sleep(1) diff --git a/micropython/examples/pico_explorer/weatherstation_BME68X.py b/micropython/examples/pico_explorer/weatherstation_BME68X.py index 19535746..4b025dea 100644 --- a/micropython/examples/pico_explorer/weatherstation_BME68X.py +++ b/micropython/examples/pico_explorer/weatherstation_BME68X.py @@ -1,22 +1,18 @@ # This example lets you plug a BME680 or BME688 breakout into your Pico Explorer to make a little indoor weather station, with barometer style descriptions. -import utime - +import time from breakout_bme68x import BreakoutBME68X from pimoroni_i2c import PimoroniI2C from pimoroni import PICO_EXPLORER_I2C_PINS +from picographics import PicoGraphics, DISPLAY_PICO_EXPLORER -# Pico Explorer boilerplate -import st7789 -display = st7789.ST7789(st7789.DISPLAY_PICO_EXPLORER, rotate=0) -display.set_palette_mode(st7789.PALETTE_USER) -display.set_backlight(1.0) - +# set up the hardware +display = PicoGraphics(display=DISPLAY_PICO_EXPLORER) i2c = PimoroniI2C(**PICO_EXPLORER_I2C_PINS) -bme = BreakoutBME68X(i2c) +bme = BreakoutBME68X(i2c, address=0x76) # lets set up some pen colours to make drawing easier -TEMPCOLOUR = display.reserve_palette() # this colour will get changed in a bit +TEMPCOLOUR = display.create_pen(0, 0, 0) # this colour will get changed in a bit WHITE = display.create_pen(255, 255, 255) BLACK = display.create_pen(0, 0, 0) RED = display.create_pen(255, 0, 0) @@ -28,22 +24,22 @@ def describe_temperature(temperature): global TEMPCOLOUR if temperature < 10: description = "very cold" - display.set_palette(TEMPCOLOUR, st7789.RGB565(0, 255, 255)) + TEMPCOLOUR = display.create_pen(0, 255, 255) elif 10 <= temperature < 20: description = "cold" - display.set_palette(TEMPCOLOUR, st7789.RGB565(0, 0, 255)) + TEMPCOLOUR = display.create_pen(0, 0, 255) elif 20 <= temperature < 25: description = "temperate" - display.set_palette(TEMPCOLOUR, st7789.RGB565(0, 255, 0)) + TEMPCOLOUR = display.create_pen(0, 255, 0) elif 25 <= temperature < 30: description = "warm" - display.set_palette(TEMPCOLOUR, st7789.RGB565(255, 255, 0)) + TEMPCOLOUR = display.create_pen(255, 255, 0) elif temperature >= 30: description = "very warm" - display.set_palette(TEMPCOLOUR, st7789.RGB565(255, 0, 0)) + TEMPCOLOUR = display.create_pen(255, 0, 0) else: description = "" - display.set_palette(TEMPCOLOUR, st7789.RGB565(0, 0, 0)) + TEMPCOLOUR = display.create_pen(0, 0, 0) return description @@ -53,22 +49,22 @@ def describe_temperature(temperature): global TEMPCOLOUR if temperature < 10: description = "frozzed" - display.set_palette(TEMPCOLOUR, st7789.RGB565(0, 255, 255)) + TEMPCOLOUR = display.create_pen(0, 255, 255) elif 10 <= temperature < 20: description = "nithering" - display.set_palette(TEMPCOLOUR, st7789.RGB565(0, 0, 255)) + TEMPCOLOUR = display.create_pen(0, 0, 255) elif 20 <= temperature < 25: description = "fair t' middlin" - display.set_palette(TEMPCOLOUR, st7789.RGB565(0, 255, 0)) + TEMPCOLOUR = display.create_pen(0, 255, 0) elif 25 <= temperature < 30: description = "chuffing warm" - display.set_palette(TEMPCOLOUR, st7789.RGB565(255, 255, 0)) + TEMPCOLOUR = display.create_pen(255, 255, 0) elif temperature >= 30: description = "crackin t' flags" - display.set_palette(TEMPCOLOUR, st7789.RGB565(255, 0, 0)) + TEMPCOLOUR = display.create_pen(255, 0, 0) else: description = "" - display.set_palette(TEMPCOLOUR, st7789.RGB565(0, 0, 0)) + TEMPCOLOUR = display.create_pen(0, 0, 0) return description """ @@ -146,4 +142,4 @@ while True: display.update() # waits for 1 second and clears to BLACK - utime.sleep(1) + time.sleep(1) diff --git a/micropython/examples/pico_inky/button_test.py b/micropython/examples/pico_inky/button_test.py new file mode 100644 index 00000000..156276fb --- /dev/null +++ b/micropython/examples/pico_inky/button_test.py @@ -0,0 +1,53 @@ +# This example shows you a simple, non-interrupt way of reading Pico Inky Pack's buttons with a loop that checks to see if buttons are pressed. + +import time +from pimoroni import Button +from picographics import PicoGraphics, DISPLAY_INKY_PACK + +display = PicoGraphics(display=DISPLAY_INKY_PACK) + +# you can change the update speed here! +# it goes from 0 (slowest) to 3 (fastest) +display.set_update_speed(2) + +display.set_font("bitmap8") + +button_a = Button(12) +button_b = Button(13) +button_c = Button(14) + + +# a handy function we can call to clear the screen +# display.set_pen(15) is white and display.set_pen(0) is black +def clear(): + display.set_pen(15) + display.clear() + + +# set up +clear() +display.set_pen(0) +display.text("Press any button!", 10, 10, 240, 3) +display.update() +time.sleep(0.5) + +while True: + if button_a.read(): # if a button press is detected then... + clear() # clear to white + display.set_pen(0) # change the pen colour + display.text("Button A pressed", 10, 10, 240, 3) # display some text on the screen + display.update() # update the display + time.sleep(0.5) + elif button_b.read(): + clear() + display.set_pen(0) + display.text("Button B pressed", 10, 50, 240, 3) + display.update() + time.sleep(0.5) + elif button_c.read(): + clear() + display.set_pen(0) + display.text("Button C pressed", 10, 90, 240, 3) + display.update() + time.sleep(0.5) + time.sleep(0.1) # this number is how frequently the Pico checks for button presses diff --git a/micropython/examples/tufty2040/name_badge.py b/micropython/examples/tufty2040/name_badge.py index cc0c655f..9b84b4f8 100644 --- a/micropython/examples/tufty2040/name_badge.py +++ b/micropython/examples/tufty2040/name_badge.py @@ -19,11 +19,14 @@ RED = display.create_pen(200, 0, 0) # Read name from file try: file = open("badge.txt", "r") - name = file.readline() - file.close() except OSError: - name = "open name.txt in thonny to edit badge :)" + with open("badge.txt", "w") as f: + f.write("open badge.txt in thonny to edit badge :)") + f.flush() + file = open("badge.txt", "r") +name = file.readline() +file.close() text_size = 12 text_x = 0