27
27
28
28
"""
29
29
30
+ from __future__ import annotations
31
+
32
+
30
33
import time
31
34
import array
32
35
import math
36
39
from adafruit_bitmap_font import bitmap_font
37
40
import displayio
38
41
from adafruit_display_shapes .rect import Rect
39
- from adafruit_display_text import label
42
+ from adafruit_display_text import bitmap_label as label
40
43
import terminalio
41
44
import adafruit_miniqr
42
45
55
58
pass
56
59
57
60
try :
61
+ from typing import TYPE_CHECKING
62
+ except ImportError :
63
+ TYPE_CHECKING = const (0 )
64
+
65
+ if TYPE_CHECKING :
58
66
from typing import Union , Tuple , Optional , Generator
59
67
from adafruit_bitmap_font .bdf import BDF # pylint: disable=ungrouped-imports
60
68
from adafruit_bitmap_font .pcf import PCF # pylint: disable=ungrouped-imports
61
69
from fontio import BuiltinFont
62
70
from keypad import Keys , ShiftRegisterKeys
63
71
from neopixel import NeoPixel
64
- from adafruit_lsm6ds .lsm6ds33 import LSM6DS33
65
- from adafruit_lis3dh import LIS3DH_I2C
66
- except ImportError :
67
- pass
68
72
69
73
70
74
__version__ = "0.0.0-auto.0"
@@ -161,27 +165,20 @@ def _create_badge_background(self) -> None:
161
165
if self ._background_group is None :
162
166
self ._background_group = displayio .Group ()
163
167
164
- self .display . show (self ._background_group )
168
+ self .show (self ._background_group )
165
169
166
170
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 )
185
182
186
183
def badge_background (
187
184
self ,
@@ -274,7 +271,7 @@ def badge_line(
274
271
scale : int = 1 ,
275
272
font : Union [BuiltinFont , BDF , PCF ] = terminalio .FONT ,
276
273
left_justify : bool = False ,
277
- padding_above : int = 0 ,
274
+ padding_above : float = 0 ,
278
275
) -> None :
279
276
"""Add a line of text to the display. Designed to work with ``badge_background`` for a
280
277
color-block style badge, or with ``image_background`` for a badge with a background image.
@@ -331,7 +328,7 @@ def badge_line(
331
328
trim_padding = 0
332
329
if font is terminalio .FONT :
333
330
trim_y = 4 * scale
334
- trim_padding = 4 * padding_above
331
+ trim_padding = round ( 4 * padding_above )
335
332
336
333
if not padding_above :
337
334
text_label .y = self ._y_position + ((height // 2 ) * scale ) - trim_y
@@ -342,14 +339,14 @@ def badge_line(
342
339
self ._y_position += height * scale + 4
343
340
344
341
else :
345
- text_label .y = (
342
+ text_label .y = round (
346
343
self ._y_position
347
344
+ (((height // 2 ) * scale ) - trim_y )
348
345
+ ((height * padding_above ) - trim_padding )
349
346
)
350
347
351
348
if font is terminalio .FONT :
352
- self ._y_position += (height * scale - trim_y ) + (
349
+ self ._y_position += (height * scale - trim_y ) + round (
353
350
(height * padding_above ) - trim_padding
354
351
)
355
352
else :
@@ -362,7 +359,7 @@ def show_custom_badge(self) -> None:
362
359
if not self ._created_background :
363
360
self ._create_badge_background ()
364
361
365
- self .display . show (self ._background_group )
362
+ self .show (self ._background_group )
366
363
367
364
# pylint: disable=too-many-arguments
368
365
def _create_label_group (
@@ -424,14 +421,23 @@ def auto_dim_display(self, delay: float = 5.0, movement_threshold: int = 10):
424
421
while True:
425
422
pybadger.auto_dim_display(delay=10)
426
423
"""
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
432
431
else :
433
432
self .display .brightness = self ._display_brightness
434
433
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
+
435
441
@property
436
442
def pixels (self ) -> NeoPixel :
437
443
"""Sequence like object representing the NeoPixels on the board."""
@@ -443,12 +449,12 @@ def light(self) -> bool:
443
449
return self ._light_sensor .value
444
450
445
451
@property
446
- def acceleration (self ) -> Union [ LSM6DS33 , LIS3DH_I2C ]:
452
+ def acceleration (self ) -> Tuple [ int , int , int ]:
447
453
"""Accelerometer data, +/- 2G sensitivity."""
448
454
return (
449
455
self ._accelerometer .acceleration
450
456
if self ._accelerometer is not None
451
- else None
457
+ else ( 0 , 0 , 0 )
452
458
)
453
459
454
460
@property
@@ -476,7 +482,7 @@ def show_business_card(
476
482
email_font_one : Union [BuiltinFont , BDF , PCF ] = terminalio .FONT ,
477
483
email_string_two : Optional [str ] = None ,
478
484
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 ,
480
486
) -> None :
481
487
"""Display a bitmap image and a text string, such as a personal image and email address.
482
488
@@ -547,22 +553,15 @@ def show_business_card(
547
553
business_card_label_groups .append (email_two_group )
548
554
549
555
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 )
566
565
567
566
# pylint: disable=too-many-locals
568
567
def show_badge (
@@ -580,7 +579,7 @@ def show_badge(
580
579
my_name_is_string : str = "MY NAME IS" ,
581
580
name_font : Union [BuiltinFont , BDF , PCF ] = terminalio .FONT ,
582
581
name_scale : int = 1 ,
583
- name_string : str = "Blinka"
582
+ name_string : str = "Blinka" ,
584
583
) -> None :
585
584
"""Create a "Hello My Name is"-style badge.
586
585
@@ -647,11 +646,19 @@ def show_badge(
647
646
group .append (hello_group )
648
647
group .append (my_name_is_group )
649
648
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
650
655
self .display .show (group )
656
+ self .display .refresh ()
657
+ self .display .auto_refresh = True
651
658
652
659
def show_terminal (self ) -> None :
653
660
"""Revert to terminalio screen."""
654
- self .display . show (None )
661
+ self .show (None )
655
662
656
663
@staticmethod
657
664
def bitmap_qr (matrix : adafruit_miniqr .QRBitMatrix ) -> displayio .Bitmap :
@@ -703,7 +710,7 @@ def show_qr_code(self, data: str = "https://circuitpython.org") -> None:
703
710
)
704
711
qr_code = displayio .Group (scale = qr_code_scale )
705
712
qr_code .append (qr_img )
706
- self .display . show (qr_code )
713
+ self .show (qr_code )
707
714
708
715
@staticmethod
709
716
def _sine_sample (length : int ) -> Generator [int , None , None ]:
0 commit comments