|
| 1 | +# Sample code using the textMap library and the "textBox" wrapper class |
| 2 | +# Creates four textBox instances |
| 3 | +# Inserts each textBox into a tileGrid group |
| 4 | +# Writes text into the box one character at a time |
| 5 | +# Moves the position of the textBox around the display |
| 6 | +# Clears each textBox after the full string is written (even if the text is outside of the box) |
| 7 | + |
| 8 | +#import textmap |
| 9 | +#from textmap import textBox |
| 10 | + |
| 11 | +import board |
| 12 | +import displayio |
| 13 | +import time |
| 14 | +import terminalio |
| 15 | +import fontio |
| 16 | +import sys |
| 17 | +import busio |
| 18 | +#from adafruit_st7789 import ST7789 |
| 19 | +from adafruit_ili9341 import ILI9341 |
| 20 | + |
| 21 | + |
| 22 | +# Use these two options to decide which system and font to use |
| 23 | + |
| 24 | +########## |
| 25 | +use_bitmap_label=True |
| 26 | +########## |
| 27 | +use_builtin_font=False |
| 28 | +########## |
| 29 | + |
| 30 | +my_scale=1 |
| 31 | + |
| 32 | +if use_bitmap_label: # use bitmap_label.py library (Bitmap) |
| 33 | + from adafruit_display_text import bitmap_label as label |
| 34 | + myString6='bitmap_label -->' |
| 35 | + label6_anchor_point=(1,0.5) |
| 36 | + label6_anchored_position=(200,200) |
| 37 | + |
| 38 | + version='bitmap_label.py' |
| 39 | + |
| 40 | +else: # use label.py library (TileGrid) |
| 41 | + from adafruit_display_text import label as label |
| 42 | + myString6='<-- label' |
| 43 | + label6_anchor_point=(0,0.5) |
| 44 | + label6_anchored_position=(50,200) |
| 45 | + |
| 46 | + version='label.py' |
| 47 | + |
| 48 | + |
| 49 | +# Setup the SPI display |
| 50 | + |
| 51 | +print('Starting the display...') # goes to serial only |
| 52 | +displayio.release_displays() |
| 53 | + |
| 54 | +spi = board.SPI() |
| 55 | +tft_cs = board.D9 # arbitrary, pin not used |
| 56 | +tft_dc = board.D10 |
| 57 | +tft_backlight = board.D12 |
| 58 | +tft_reset=board.D11 |
| 59 | + |
| 60 | +while not spi.try_lock(): |
| 61 | + spi.configure(baudrate=32000000) |
| 62 | + pass |
| 63 | +spi.unlock() |
| 64 | + |
| 65 | +display_bus = displayio.FourWire( |
| 66 | + spi, |
| 67 | + command=tft_dc, |
| 68 | + chip_select=tft_cs, |
| 69 | + reset=tft_reset, |
| 70 | + baudrate=32000000, |
| 71 | + polarity=1, |
| 72 | + phase=1, |
| 73 | +) |
| 74 | + |
| 75 | +print('spi.frequency: {}'.format(spi.frequency)) |
| 76 | + |
| 77 | +DISPLAY_WIDTH=320 |
| 78 | +DISPLAY_HEIGHT=240 |
| 79 | + |
| 80 | +#display = ST7789(display_bus, width=240, height=240, rotation=0, rowstart=80, colstart=0) |
| 81 | +display = ILI9341(display_bus, width=DISPLAY_WIDTH, height=DISPLAY_HEIGHT, rotation=180, auto_refresh=True) |
| 82 | + |
| 83 | +display.show(None) |
| 84 | + |
| 85 | +print ('Display is started') |
| 86 | + |
| 87 | + |
| 88 | +# load all the fonts |
| 89 | +print('loading fonts...') |
| 90 | + |
| 91 | +import terminalio |
| 92 | + |
| 93 | + |
| 94 | +fontList = [] |
| 95 | +fontHeight = [] |
| 96 | + |
| 97 | +##### the BuiltinFont terminalio.FONT has a different return strategy for get_glyphs and |
| 98 | +# is currently not handled by these functions. |
| 99 | +#fontList.append(terminalio.FONT) |
| 100 | +#fontHeight = [10] # somehow the terminalio.FONT needs to be adjusted to 10 |
| 101 | + |
| 102 | +# Load some proportional fonts |
| 103 | +fontFiles = [ |
| 104 | + 'fonts/Helvetica-Bold-16.bdf', |
| 105 | +# 'fonts/BitstreamVeraSans-Roman-24.bdf', # Header2 |
| 106 | +# 'fonts/BitstreamVeraSans-Roman-16.bdf', # mainText |
| 107 | +# 'fonts/Fayette-HandwrittenScript-64.bdf', |
| 108 | + ] |
| 109 | + |
| 110 | +from adafruit_bitmap_font import bitmap_font |
| 111 | + |
| 112 | +for i, fontFile in enumerate(fontFiles): |
| 113 | + thisFont = bitmap_font.load_font(fontFile) |
| 114 | + |
| 115 | + if use_builtin_font: |
| 116 | + thisFont=terminalio.FONT # comment this out to switch back to BDF loaded fonts |
| 117 | + |
| 118 | + fontList.append(thisFont) |
| 119 | + fontHeight.append( thisFont.get_glyph(ord("M")).height ) |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | +preloadTheGlyphs= True # set this to True if you want to preload the font glyphs into memory |
| 124 | + # preloading the glyphs will help speed up the rendering of text but will use more RAM |
| 125 | + |
| 126 | +if preloadTheGlyphs: |
| 127 | + |
| 128 | + # identify the glyphs to load into memory -> increases rendering speed |
| 129 | + glyphs = b'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,.:?! ' |
| 130 | + |
| 131 | + print('loading glyphs...') |
| 132 | + for font in fontList: |
| 133 | + if font is not terminalio.FONT: |
| 134 | + font.load_glyphs(glyphs) |
| 135 | + |
| 136 | + print('Glyphs are loaded.') |
| 137 | + |
| 138 | + |
| 139 | +#for char in glyphs: |
| 140 | +# my_glyph=font.get_glyph(char) |
| 141 | +# print('char: {}, size x,y ({},{}) offset x,y ({},{})'.format(chr(char), my_glyph.width, my_glyph.height, my_glyph.dx, my_glyph.dy)) |
| 142 | + |
| 143 | +print('Fonts completed loading.') |
| 144 | + |
| 145 | +# create group |
| 146 | +import gc |
| 147 | + |
| 148 | +#tileGridList=[] # list of tileGrids |
| 149 | +#print( 'After creating Group, Memory free: {}'.format(gc.mem_free()) ) |
| 150 | + |
| 151 | + |
| 152 | +myString12=('Bug Juice ({[]}) \'Monsters!\"\'\npuppy jump ({[]})') |
| 153 | +myString34='\n none ' |
| 154 | +myString_bitmap_label='bitmap_label' |
| 155 | +myString_label='label bitmap_label' |
| 156 | + |
| 157 | + |
| 158 | +gc.collect() |
| 159 | +label_start_memory=gc.mem_free() |
| 160 | +bmap_label = label.Label(font=fontList[0], text=myString12, color=0xFFFFFF, max_glyphs=len(myString12), |
| 161 | + background_color=0xFF0000, |
| 162 | + padding_bottom=0, |
| 163 | + padding_left=0, |
| 164 | + padding_right=0, |
| 165 | + padding_top=0, |
| 166 | + background_tight=False, |
| 167 | + x=10, |
| 168 | + y=30, |
| 169 | + line_spacing=1.25, |
| 170 | + scale=my_scale, |
| 171 | + ) |
| 172 | + |
| 173 | + |
| 174 | +bmap_label2 = label.Label(font=fontList[0], text=myString12, color=0xFFFFFF, max_glyphs=len(myString12), |
| 175 | + background_color=0xFF0000, |
| 176 | + padding_bottom=0, |
| 177 | + padding_left=0, |
| 178 | + padding_right=0, |
| 179 | + padding_top=0, |
| 180 | + background_tight=True, |
| 181 | + x=10, |
| 182 | + y=90, |
| 183 | + line_spacing=1.25, |
| 184 | + scale=my_scale, |
| 185 | + #anchored_position=(10,60), |
| 186 | + ) |
| 187 | +label3_padding=0 |
| 188 | +bmap_label3 = label.Label(font=fontList[0], text=myString34, |
| 189 | + color=0x000000, |
| 190 | + #color=0xFF00FF, |
| 191 | + max_glyphs=len(myString34), |
| 192 | + background_color=0xFFFF00, |
| 193 | + #background_color=None, |
| 194 | + padding_bottom=label3_padding, |
| 195 | + padding_left=label3_padding, |
| 196 | + padding_right=label3_padding, |
| 197 | + padding_top=label3_padding, |
| 198 | + background_tight=False, |
| 199 | + x=10, |
| 200 | + y=150, |
| 201 | + line_spacing=1.25, |
| 202 | + scale=my_scale, |
| 203 | + anchor_point=(0,0), |
| 204 | + anchored_position=(200,150), |
| 205 | + ) |
| 206 | + |
| 207 | +bmap_label4 = label.Label(font=fontList[0], text=myString34, |
| 208 | + color=0x000000, |
| 209 | + #color=0xFF00FF, |
| 210 | + max_glyphs=len(myString34), |
| 211 | + background_color=0xFFFF00, |
| 212 | + #background_color=None, |
| 213 | + padding_bottom=0, |
| 214 | + padding_left=0, |
| 215 | + padding_right=0, |
| 216 | + padding_top=0, |
| 217 | + background_tight=True, |
| 218 | + x=10, |
| 219 | + y=150, |
| 220 | + line_spacing=1.25, |
| 221 | + scale=my_scale, |
| 222 | + #anchored_position=(10,150), |
| 223 | + ) |
| 224 | + |
| 225 | +bmap_label5 = label.Label(font=fontList[0], text=myString_label, color=0x000000, max_glyphs=len(myString_label), |
| 226 | + background_color=0xFFFF00, |
| 227 | + padding_bottom=0, |
| 228 | + padding_left=0, |
| 229 | + padding_right=0, |
| 230 | + padding_top=0, |
| 231 | + background_tight=False, |
| 232 | + x=10, |
| 233 | + y=200, |
| 234 | + line_spacing=1.25, |
| 235 | + scale=my_scale, |
| 236 | + #anchored_position=(10,200), |
| 237 | + ) |
| 238 | + |
| 239 | + |
| 240 | +bmap_label6 = label.Label(font=fontList[0], text=myString6, color=0xFFFFFF, max_glyphs=len(myString6), |
| 241 | + background_color=0x000000, |
| 242 | + padding_bottom=0, |
| 243 | + padding_left=0, |
| 244 | + padding_right=0, |
| 245 | + padding_top=0, |
| 246 | + background_tight=True, |
| 247 | + x=10, |
| 248 | + y=200, |
| 249 | + line_spacing=1.25, |
| 250 | + anchor_point=label6_anchor_point, |
| 251 | + anchored_position=label6_anchored_position, |
| 252 | + scale=my_scale, |
| 253 | + ) |
| 254 | + |
| 255 | + |
| 256 | + |
| 257 | +gc.collect() |
| 258 | +label_end_memory=gc.mem_free() |
| 259 | + |
| 260 | +bmap_group = displayio.Group( max_size=6 ) # Create a group for displaying |
| 261 | +#bmap_group.append(bmap_label) |
| 262 | +#bmap_group.append(bmap_label2) |
| 263 | +#bmap_group.append(bmap_label3) |
| 264 | +bmap_group.append(bmap_label4) |
| 265 | +#bmap_group.append(bmap_label5) |
| 266 | +#bmap_group.append(bmap_label6) |
| 267 | + |
| 268 | + |
| 269 | +print('***') |
| 270 | +print('{} memory used: {}'.format(version, label_start_memory-label_end_memory)) |
| 271 | +display.auto_refresh=True |
| 272 | + |
| 273 | +#time.sleep(0.1) |
| 274 | +display.show(bmap_group) |
| 275 | + |
| 276 | +while True: |
| 277 | + pass |
| 278 | + |
| 279 | + |
| 280 | + |
0 commit comments