2727
2828"""
2929
30+ from __future__ import annotations
31+
32+
3033import time
3134import array
3235import math
3639from adafruit_bitmap_font import bitmap_font
3740import displayio
3841from adafruit_display_shapes .rect import Rect
39- from adafruit_display_text import label
42+ from adafruit_display_text import bitmap_label as label
4043import terminalio
4144import adafruit_miniqr
4245
5558 pass
5659
5760try :
61+ from typing import TYPE_CHECKING
62+ except ImportError :
63+ TYPE_CHECKING = const (0 )
64+
65+ if TYPE_CHECKING :
5866 from typing import Union , Tuple , Optional , Generator
5967 from adafruit_bitmap_font .bdf import BDF # pylint: disable=ungrouped-imports
6068 from adafruit_bitmap_font .pcf import PCF # pylint: disable=ungrouped-imports
6169 from fontio import BuiltinFont
6270 from keypad import Keys , ShiftRegisterKeys
6371 from neopixel import NeoPixel
64- from adafruit_lsm6ds .lsm6ds33 import LSM6DS33
65- from adafruit_lis3dh import LIS3DH_I2C
66- except ImportError :
67- pass
6872
6973
7074__version__ = "0.0.0-auto.0"
@@ -161,27 +165,20 @@ def _create_badge_background(self) -> None:
161165 if self ._background_group is None :
162166 self ._background_group = displayio .Group ()
163167
164- self .display . show (self ._background_group )
168+ self .show (self ._background_group )
165169
166170 if self ._background_image_filename :
167- with open (self ._background_image_filename , "rb" ) as file_handle :
168- on_disk_bitmap = displayio .OnDiskBitmap (file_handle )
169- background_image = displayio .TileGrid (
170- on_disk_bitmap ,
171- pixel_shader = getattr (
172- on_disk_bitmap , "pixel_shader" , displayio .ColorConverter ()
173- ),
174- # TODO: Once CP6 is no longer supported, replace the above line with below
175- # pixel_shader=on_disk_background.pixel_shader,
176- )
177- self ._background_group .append (background_image )
178- for image_label in self ._lines :
179- self ._background_group .append (image_label )
180-
181- self .display .refresh ()
182- else :
183- for background_label in self ._lines :
184- self ._background_group .append (background_label )
171+ file_handle = open ( # pylint: disable=consider-using-with
172+ self ._background_image_filename , "rb"
173+ )
174+ on_disk_bitmap = displayio .OnDiskBitmap (file_handle )
175+ background_image = displayio .TileGrid (
176+ on_disk_bitmap ,
177+ pixel_shader = on_disk_bitmap .pixel_shader ,
178+ )
179+ self ._background_group .append (background_image )
180+ for image_label in self ._lines :
181+ self ._background_group .append (image_label )
185182
186183 def badge_background (
187184 self ,
@@ -274,7 +271,7 @@ def badge_line(
274271 scale : int = 1 ,
275272 font : Union [BuiltinFont , BDF , PCF ] = terminalio .FONT ,
276273 left_justify : bool = False ,
277- padding_above : int = 0 ,
274+ padding_above : float = 0 ,
278275 ) -> None :
279276 """Add a line of text to the display. Designed to work with ``badge_background`` for a
280277 color-block style badge, or with ``image_background`` for a badge with a background image.
@@ -331,7 +328,7 @@ def badge_line(
331328 trim_padding = 0
332329 if font is terminalio .FONT :
333330 trim_y = 4 * scale
334- trim_padding = 4 * padding_above
331+ trim_padding = round ( 4 * padding_above )
335332
336333 if not padding_above :
337334 text_label .y = self ._y_position + ((height // 2 ) * scale ) - trim_y
@@ -342,14 +339,14 @@ def badge_line(
342339 self ._y_position += height * scale + 4
343340
344341 else :
345- text_label .y = (
342+ text_label .y = round (
346343 self ._y_position
347344 + (((height // 2 ) * scale ) - trim_y )
348345 + ((height * padding_above ) - trim_padding )
349346 )
350347
351348 if font is terminalio .FONT :
352- self ._y_position += (height * scale - trim_y ) + (
349+ self ._y_position += (height * scale - trim_y ) + round (
353350 (height * padding_above ) - trim_padding
354351 )
355352 else :
@@ -362,7 +359,7 @@ def show_custom_badge(self) -> None:
362359 if not self ._created_background :
363360 self ._create_badge_background ()
364361
365- self .display . show (self ._background_group )
362+ self .show (self ._background_group )
366363
367364 # pylint: disable=too-many-arguments
368365 def _create_label_group (
@@ -424,14 +421,23 @@ def auto_dim_display(self, delay: float = 5.0, movement_threshold: int = 10):
424421 while True:
425422 pybadger.auto_dim_display(delay=10)
426423 """
427- if not self ._check_for_movement (movement_threshold = movement_threshold ):
428- current_time = time .monotonic ()
429- if current_time - self ._start_time > delay :
430- self .display .brightness = 0.1
431- self ._start_time = current_time
424+ if not hasattr (self .display , "brightness" ):
425+ return
426+ current_time = time .monotonic ()
427+ if self ._check_for_movement (movement_threshold = movement_threshold ):
428+ self .activity (current_time )
429+ if current_time - self ._start_time > delay :
430+ self .display .brightness = 0.1
432431 else :
433432 self .display .brightness = self ._display_brightness
434433
434+ def activity (self , current_time = None ):
435+ """Turn postpone dimming of the screen"""
436+ if not hasattr (self .display , "brightness" ):
437+ return
438+ self .display .brightness = self ._display_brightness
439+ self ._start_time = current_time or time .monotonic ()
440+
435441 @property
436442 def pixels (self ) -> NeoPixel :
437443 """Sequence like object representing the NeoPixels on the board."""
@@ -443,12 +449,12 @@ def light(self) -> bool:
443449 return self ._light_sensor .value
444450
445451 @property
446- def acceleration (self ) -> Union [ LSM6DS33 , LIS3DH_I2C ]:
452+ def acceleration (self ) -> Tuple [ int , int , int ]:
447453 """Accelerometer data, +/- 2G sensitivity."""
448454 return (
449455 self ._accelerometer .acceleration
450456 if self ._accelerometer is not None
451- else None
457+ else ( 0 , 0 , 0 )
452458 )
453459
454460 @property
@@ -476,7 +482,7 @@ def show_business_card(
476482 email_font_one : Union [BuiltinFont , BDF , PCF ] = terminalio .FONT ,
477483 email_string_two : Optional [str ] = None ,
478484 email_scale_two : int = 1 ,
479- email_font_two : Union [BuiltinFont , BDF , PCF ] = terminalio .FONT
485+ email_font_two : Union [BuiltinFont , BDF , PCF ] = terminalio .FONT ,
480486 ) -> None :
481487 """Display a bitmap image and a text string, such as a personal image and email address.
482488
@@ -547,22 +553,15 @@ def show_business_card(
547553 business_card_label_groups .append (email_two_group )
548554
549555 business_card_splash = displayio .Group ()
550- self .display .show (business_card_splash )
551- with open (image_name , "rb" ) as file_name :
552- on_disk_bitmap = displayio .OnDiskBitmap (file_name )
553- face_image = displayio .TileGrid (
554- on_disk_bitmap ,
555- pixel_shader = getattr (
556- on_disk_bitmap , "pixel_shader" , displayio .ColorConverter ()
557- ),
558- # TODO: Once CP6 is no longer supported, replace the above line with below
559- # pixel_shader=on_disk_bitmap.pixel_shader,
560- )
561- business_card_splash .append (face_image )
562- for group in business_card_label_groups :
563- business_card_splash .append (group )
564-
565- self .display .refresh ()
556+ image_file = open (image_name , "rb" ) # pylint: disable=consider-using-with
557+ on_disk_bitmap = displayio .OnDiskBitmap (image_file )
558+ face_image = displayio .TileGrid (
559+ on_disk_bitmap , pixel_shader = on_disk_bitmap .pixel_shader
560+ )
561+ business_card_splash .append (face_image )
562+ for group in business_card_label_groups :
563+ business_card_splash .append (group )
564+ self .show (business_card_splash )
566565
567566 # pylint: disable=too-many-locals
568567 def show_badge (
@@ -580,7 +579,7 @@ def show_badge(
580579 my_name_is_string : str = "MY NAME IS" ,
581580 name_font : Union [BuiltinFont , BDF , PCF ] = terminalio .FONT ,
582581 name_scale : int = 1 ,
583- name_string : str = "Blinka"
582+ name_string : str = "Blinka" ,
584583 ) -> None :
585584 """Create a "Hello My Name is"-style badge.
586585
@@ -647,11 +646,19 @@ def show_badge(
647646 group .append (hello_group )
648647 group .append (my_name_is_group )
649648 group .append (name_group )
649+ self .show (group )
650+
651+ def show (self , group ) -> None :
652+ """Show the given group, refreshing the screen immediately"""
653+ self .activity ()
654+ self .display .auto_refresh = False
650655 self .display .show (group )
656+ self .display .refresh ()
657+ self .display .auto_refresh = True
651658
652659 def show_terminal (self ) -> None :
653660 """Revert to terminalio screen."""
654- self .display . show (None )
661+ self .show (None )
655662
656663 @staticmethod
657664 def bitmap_qr (matrix : adafruit_miniqr .QRBitMatrix ) -> displayio .Bitmap :
@@ -703,7 +710,7 @@ def show_qr_code(self, data: str = "https://circuitpython.org") -> None:
703710 )
704711 qr_code = displayio .Group (scale = qr_code_scale )
705712 qr_code .append (qr_img )
706- self .display . show (qr_code )
713+ self .show (qr_code )
707714
708715 @staticmethod
709716 def _sine_sample (length : int ) -> Generator [int , None , None ]:
0 commit comments