24
24
25
25
"""
26
26
27
+ import gc
27
28
import board
28
29
from digitalio import DigitalInOut
30
+ import pulseio
29
31
import audioio
30
32
import audiocore
31
33
import storage
@@ -47,11 +49,13 @@ class Peripherals:
47
49
"""Peripherals Helper Class for the PyPortal Library"""
48
50
49
51
# pylint: disable=too-many-instance-attributes, too-many-locals, too-many-branches, too-many-statements
50
- def __init__ (self , spi , debug = False ):
52
+ def __init__ (self , spi , display , debug = False ):
51
53
# Speaker Enable
52
54
self ._speaker_enable = DigitalInOut (board .SPEAKER_ENABLE )
53
55
self ._speaker_enable .switch_to_output (False )
54
56
57
+ self ._display = display
58
+
55
59
if hasattr (board , "AUDIO_OUT" ):
56
60
self .audio = audioio .AudioOut (board .AUDIO_OUT )
57
61
elif hasattr (board , "SPEAKER" ):
@@ -73,6 +77,68 @@ def __init__(self, spi, debug=False):
73
77
except OSError as error :
74
78
print ("No SD card found:" , error )
75
79
80
+ try :
81
+ if hasattr (board , "TFT_BACKLIGHT" ):
82
+ self ._backlight = pulseio .PWMOut (
83
+ board .TFT_BACKLIGHT
84
+ ) # pylint: disable=no-member
85
+ elif hasattr (board , "TFT_LITE" ):
86
+ self ._backlight = pulseio .PWMOut (
87
+ board .TFT_LITE
88
+ ) # pylint: disable=no-member
89
+ except ValueError :
90
+ self ._backlight = None
91
+ self .set_backlight (1.0 ) # turn on backlight
92
+
93
+ if hasattr (board , "TOUCH_XL" ):
94
+ import adafruit_touchscreen
95
+
96
+ if debug :
97
+ print ("Init touchscreen" )
98
+ # pylint: disable=no-member
99
+ self .touchscreen = adafruit_touchscreen .Touchscreen (
100
+ board .TOUCH_XL ,
101
+ board .TOUCH_XR ,
102
+ board .TOUCH_YD ,
103
+ board .TOUCH_YU ,
104
+ calibration = ((5200 , 59000 ), (5800 , 57000 )),
105
+ size = (board .DISPLAY .width , board .DISPLAY .height ),
106
+ )
107
+ # pylint: enable=no-member
108
+
109
+ self .set_backlight (1.0 ) # turn on backlight
110
+ elif hasattr (board , "BUTTON_CLOCK" ):
111
+ from adafruit_cursorcontrol .cursorcontrol import Cursor
112
+ from adafruit_cursorcontrol .cursorcontrol_cursormanager import CursorManager
113
+
114
+ if debug :
115
+ print ("Init cursor" )
116
+ self .mouse_cursor = Cursor (
117
+ board .DISPLAY , display_group = self .splash , cursor_speed = 8
118
+ )
119
+ self .mouse_cursor .hide ()
120
+ self .cursor = CursorManager (self .mouse_cursor )
121
+ else :
122
+ raise AttributeError (
123
+ "PyPortal module requires either a touchscreen or gamepad."
124
+ )
125
+
126
+ gc .collect ()
127
+
128
+ def set_backlight (self , val ):
129
+ """Adjust the TFT backlight.
130
+
131
+ :param val: The backlight brightness. Use a value between ``0`` and ``1``, where ``0`` is
132
+ off, and ``1`` is 100% brightness.
133
+
134
+ """
135
+ val = max (0 , min (1.0 , val ))
136
+ if self ._backlight :
137
+ self ._backlight .duty_cycle = int (val * 65535 )
138
+ else :
139
+ self ._display .auto_brightness = False
140
+ self ._display .brightness = val
141
+
76
142
def play_file (self , file_name , wait_to_finish = True ):
77
143
"""Play a wav file.
78
144
0 commit comments