14
14
from keypad import ShiftRegisterKeys , Event
15
15
from adafruit_debouncer import Debouncer
16
16
17
+ try :
18
+ from typing import Optional , Type
19
+ from types import TracebackType
20
+ from adafruit_cursorcontrol .cursorcontrol import Cursor
21
+ except ImportError :
22
+ pass
23
+
17
24
__version__ = "0.0.0-auto.0"
18
25
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_CursorControl.git"
19
26
@@ -33,36 +40,36 @@ class CursorManager:
33
40
:param Cursor cursor: The cursor object we are using.
34
41
"""
35
42
36
- def __init__ (self , cursor ) :
43
+ def __init__ (self , cursor : Cursor ) -> None :
37
44
self ._cursor = cursor
38
45
self ._is_clicked = False
39
46
self ._pad_states = 0
40
47
self ._event = Event ()
41
48
self ._init_hardware ()
42
49
43
- def __enter__ (self ):
50
+ def __enter__ (self ) -> 'CursorManager' :
44
51
return self
45
52
46
- def __exit__ (self , exception_type , exception_value , traceback ) :
53
+ def __exit__ (self , exception_type : Optional [ Type [ type ]] , exception_value : Optional [ BaseException ] , traceback : Optional [ TracebackType ]) -> None :
47
54
self .deinit ()
48
55
49
- def deinit (self ):
56
+ def deinit (self ) -> None :
50
57
"""Deinitializes a CursorManager object."""
51
58
self ._is_deinited ()
52
59
self ._pad .deinit ()
53
60
self ._cursor .deinit ()
54
61
self ._cursor = None
55
62
self ._event = None
56
63
57
- def _is_deinited (self ):
64
+ def _is_deinited (self ) -> None :
58
65
"""Checks if CursorManager object has been deinitd."""
59
66
if self ._cursor is None :
60
67
raise ValueError (
61
68
"CursorManager object has been deinitialized and can no longer "
62
69
"be used. Create a new CursorManager object."
63
70
)
64
71
65
- def _init_hardware (self ):
72
+ def _init_hardware (self ) -> None :
66
73
"""Initializes PyBadge or PyGamer hardware."""
67
74
if hasattr (board , "BUTTON_CLOCK" ) and not hasattr (board , "JOYSTICK_X" ):
68
75
self ._pad_btns = {
@@ -93,13 +100,13 @@ def _init_hardware(self):
93
100
)
94
101
95
102
@property
96
- def is_clicked (self ):
103
+ def is_clicked (self ) -> bool :
97
104
"""Returns True if the cursor button was pressed
98
105
during previous call to update()
99
106
"""
100
107
return self ._is_clicked
101
108
102
- def update (self ):
109
+ def update (self ) -> None :
103
110
"""Updates the cursor object."""
104
111
if self ._pad .events .get_into (self ._event ):
105
112
self ._store_button_states ()
@@ -109,7 +116,7 @@ def update(self):
109
116
elif self ._pad_states & (1 << self ._pad_btns ["btn_a" ]):
110
117
self ._is_clicked = True
111
118
112
- def _read_joystick_x (self , samples = 3 ) :
119
+ def _read_joystick_x (self , samples : int = 3 ) -> float :
113
120
"""Read the X analog joystick on the PyGamer.
114
121
:param int samples: How many samples to read and average.
115
122
"""
@@ -121,7 +128,7 @@ def _read_joystick_x(self, samples=3):
121
128
reading /= samples
122
129
return reading
123
130
124
- def _read_joystick_y (self , samples = 3 ) :
131
+ def _read_joystick_y (self , samples : int = 3 ) -> float :
125
132
"""Read the Y analog joystick on the PyGamer.
126
133
:param int samples: How many samples to read and average.
127
134
"""
@@ -133,7 +140,7 @@ def _read_joystick_y(self, samples=3):
133
140
reading /= samples
134
141
return reading
135
142
136
- def _store_button_states (self ):
143
+ def _store_button_states (self ) -> None :
137
144
"""Stores the state of the PyBadge's D-Pad or the PyGamer's Joystick
138
145
into a byte
139
146
"""
@@ -142,7 +149,7 @@ def _store_button_states(self):
142
149
if current_state != self ._event .pressed :
143
150
self ._pad_states = (1 << bit_index ) ^ self ._pad_states
144
151
145
- def _check_cursor_movement (self ):
152
+ def _check_cursor_movement (self ) -> None :
146
153
"""Checks the PyBadge D-Pad or the PyGamer's Joystick for movement."""
147
154
if hasattr (board , "BUTTON_CLOCK" ) and not hasattr (board , "JOYSTICK_X" ):
148
155
if self ._pad_states & (1 << self ._pad_btns ["btn_right" ]):
@@ -180,15 +187,15 @@ class DebouncedCursorManager(CursorManager):
180
187
:param Cursor cursor: The cursor object we are using.
181
188
"""
182
189
183
- def __init__ (self , cursor , debounce_interval = 0.01 ):
190
+ def __init__ (self , cursor : Cursor , debounce_interval : float = 0.01 ) -> None :
184
191
CursorManager .__init__ (self , cursor )
185
192
self ._debouncer = Debouncer (
186
193
lambda : bool (self ._pad_states & (1 << self ._pad_btns ["btn_a" ])),
187
194
interval = debounce_interval ,
188
195
)
189
196
190
197
@property
191
- def is_clicked (self ):
198
+ def is_clicked (self ) -> bool :
192
199
"""Returns True if the cursor button was pressed
193
200
during previous call to update()
194
201
"""
@@ -197,18 +204,18 @@ def is_clicked(self):
197
204
pressed = is_clicked
198
205
199
206
@property
200
- def released (self ):
207
+ def released (self ) -> bool :
201
208
"""Returns True if the cursor button was released
202
209
during previous call to update()
203
210
"""
204
211
return self ._debouncer .fell
205
212
206
213
@property
207
- def held (self ):
214
+ def held (self ) -> bool :
208
215
"""Returns True if the cursor button is currently being held"""
209
216
return self ._debouncer .value
210
217
211
- def update (self ):
218
+ def update (self ) -> None :
212
219
"""Updates the cursor object."""
213
220
if self ._pad .events .get_into (self ._event ):
214
221
self ._store_button_states ()
0 commit comments