2022-02-24 11:41:37 +00:00
import os
import sys
import time
import badger2040
from badger2040 import WIDTH , HEIGHT
2022-02-24 13:08:01 +00:00
REAMDE = """
2022-02-24 11:41:37 +00:00
Images must be 296 x128 pixel with 1 bit colour depth .
2022-02-24 14:30:42 +00:00
You can use examples / badger2040 / image_converter / convert . py to convert them :
2022-02-24 11:41:37 +00:00
python3 convert . py - - binary - - resize image_file_1 . png image_file_2 . png image_file_3 . png
Create a new " images " directory via Thonny , and upload the . bin files there .
"""
OVERLAY_BORDER = 40
OVERLAY_SPACING = 20
OVERLAY_TEXT_SIZE = 0.5
TOTAL_IMAGES = 0
2022-02-24 14:30:42 +00:00
# Try to preload BadgerPunk image
2022-02-24 13:08:01 +00:00
try :
os . mkdir ( " images " )
except OSError :
pass
try :
2022-02-24 14:30:42 +00:00
import badgerpunk
with open ( " images/badgerpunk.bin " , " wb " ) as f :
f . write ( badgerpunk . data ( ) )
2022-02-24 13:08:01 +00:00
f . flush ( )
with open ( " images/readme.txt " , " w " ) as f :
f . write ( REAMDE )
f . flush ( )
2022-02-24 14:30:42 +00:00
del badgerpunk
2022-02-24 13:08:01 +00:00
except ( OSError , ImportError ) :
pass
2022-02-24 11:41:37 +00:00
try :
IMAGES = [ f for f in os . listdir ( " /images " ) if f . endswith ( " .bin " ) ]
TOTAL_IMAGES = len ( IMAGES )
except OSError :
pass
display = badger2040 . Badger2040 ( )
image = bytearray ( int ( 296 * 128 / 8 ) )
current_image = 0
2022-03-04 14:58:52 +00:00
show_info = True
2022-02-24 11:41:37 +00:00
2022-03-23 21:42:18 +00:00
try :
with open ( " appstate.txt " , " r " ) as f :
f . readline ( )
current_image = int ( f . readline ( ) . strip ( ' \n ' ) )
show_info = f . readline ( ) . strip ( ' \n ' ) == " True "
except OSError :
pass
2022-02-24 11:41:37 +00:00
2022-03-23 22:42:46 +00:00
2022-02-24 11:41:37 +00:00
# Draw an overlay box with a given message within it
def draw_overlay ( message , width , height , line_spacing , text_size ) :
# Draw a light grey background
display . pen ( 12 )
display . rectangle ( ( WIDTH - width ) / / 2 , ( HEIGHT - height ) / / 2 , width , height )
# Take the provided message and split it up into
# lines that fit within the specified width
words = message . split ( " " )
2022-02-24 13:08:01 +00:00
2022-02-24 11:41:37 +00:00
lines = [ ]
2022-02-24 13:08:01 +00:00
current_line = " "
2022-02-24 11:41:37 +00:00
for word in words :
2022-02-24 13:08:01 +00:00
if display . measure_text ( current_line + word + " " , text_size ) < width :
current_line + = word + " "
2022-02-24 11:41:37 +00:00
else :
2022-02-24 13:08:01 +00:00
lines . append ( current_line . strip ( ) )
current_line = word + " "
lines . append ( current_line . strip ( ) )
2022-02-24 11:41:37 +00:00
display . pen ( 0 )
display . thickness ( 2 )
# Display each line of text from the message, centre-aligned
num_lines = len ( lines )
for i in range ( num_lines ) :
length = display . measure_text ( lines [ i ] , text_size )
current_line = ( i * line_spacing ) - ( ( num_lines - 1 ) * line_spacing ) / / 2
display . text ( lines [ i ] , ( WIDTH - length ) / / 2 , ( HEIGHT / / 2 ) + current_line , text_size )
def show_image ( n ) :
file = IMAGES [ n ]
name = file . split ( " . " ) [ 0 ]
open ( " images/ {} " . format ( file ) , " r " ) . readinto ( image )
display . image ( image )
2022-03-04 14:58:52 +00:00
if show_info :
name_length = display . measure_text ( name , 0.5 )
2022-02-24 11:41:37 +00:00
display . pen ( 0 )
2022-03-04 14:58:52 +00:00
display . rectangle ( 0 , HEIGHT - 21 , name_length + 11 , 21 )
display . pen ( 15 )
display . rectangle ( 0 , HEIGHT - 20 , name_length + 10 , 20 )
display . pen ( 0 )
display . text ( name , 5 , HEIGHT - 10 , 0.5 )
for i in range ( TOTAL_IMAGES ) :
x = 286
y = int ( ( 128 / 2 ) - ( TOTAL_IMAGES * 10 / 2 ) + ( i * 10 ) )
display . pen ( 0 )
display . rectangle ( x , y , 8 , 8 )
if current_image != i :
display . pen ( 15 )
display . rectangle ( x + 1 , y + 1 , 6 , 6 )
2022-02-24 11:41:37 +00:00
display . update ( )
if TOTAL_IMAGES == 0 :
display . pen ( 15 )
display . clear ( )
draw_overlay ( " To run this demo, create an /images directory on your device and upload some 1bit 296x128 pixel images. " , WIDTH - OVERLAY_BORDER , HEIGHT - OVERLAY_BORDER , OVERLAY_SPACING , OVERLAY_TEXT_SIZE )
display . update ( )
sys . exit ( )
2022-03-23 21:42:18 +00:00
if display . pressed_to_wake ( badger2040 . BUTTON_UP ) :
if current_image > 0 :
current_image - = 1
if display . pressed_to_wake ( badger2040 . BUTTON_DOWN ) :
if current_image < TOTAL_IMAGES - 1 :
current_image + = 1
if display . pressed_to_wake ( badger2040 . BUTTON_A ) :
show_info = not show_info
if display . pressed_to_wake ( badger2040 . BUTTON_B ) or display . pressed_to_wake ( badger2040 . BUTTON_C ) :
display . pen ( 15 )
display . clear ( )
draw_overlay ( " To add images connect Badger2040 to a PC, load up Thonny, and see readme.txt in images/ " , WIDTH - OVERLAY_BORDER , HEIGHT - OVERLAY_BORDER , OVERLAY_SPACING , 0.5 )
display . update ( )
time . sleep ( 4 )
2022-02-24 11:41:37 +00:00
show_image ( current_image )
2022-03-23 22:42:46 +00:00
# Tell launcher to relaunch this app on wake and record state
2022-03-23 21:42:18 +00:00
with open ( " appstate.txt " , " w " ) as f :
f . write ( " image \n " )
2022-03-23 22:42:46 +00:00
f . write ( " {} \n {} \n " . format ( current_image , show_info ) )
2022-03-23 21:42:18 +00:00
2022-03-23 22:42:46 +00:00
# Halt the Badger to save power, it will wake up if any of the front buttons are pressed
2022-03-23 21:42:18 +00:00
display . halt ( )