Skip to content

Commit df98457

Browse files
committed
Add type hints for cursorcontrol_cursormanager.py
1 parent e11fd78 commit df98457

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

adafruit_cursorcontrol/cursorcontrol_cursormanager.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
from keypad import ShiftRegisterKeys, Event
1515
from adafruit_debouncer import Debouncer
1616

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+
1724
__version__ = "0.0.0-auto.0"
1825
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_CursorControl.git"
1926

@@ -33,36 +40,36 @@ class CursorManager:
3340
:param Cursor cursor: The cursor object we are using.
3441
"""
3542

36-
def __init__(self, cursor):
43+
def __init__(self, cursor: Cursor) -> None:
3744
self._cursor = cursor
3845
self._is_clicked = False
3946
self._pad_states = 0
4047
self._event = Event()
4148
self._init_hardware()
4249

43-
def __enter__(self):
50+
def __enter__(self) -> 'CursorManager':
4451
return self
4552

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:
4754
self.deinit()
4855

49-
def deinit(self):
56+
def deinit(self) -> None:
5057
"""Deinitializes a CursorManager object."""
5158
self._is_deinited()
5259
self._pad.deinit()
5360
self._cursor.deinit()
5461
self._cursor = None
5562
self._event = None
5663

57-
def _is_deinited(self):
64+
def _is_deinited(self) -> None:
5865
"""Checks if CursorManager object has been deinitd."""
5966
if self._cursor is None:
6067
raise ValueError(
6168
"CursorManager object has been deinitialized and can no longer "
6269
"be used. Create a new CursorManager object."
6370
)
6471

65-
def _init_hardware(self):
72+
def _init_hardware(self) -> None:
6673
"""Initializes PyBadge or PyGamer hardware."""
6774
if hasattr(board, "BUTTON_CLOCK") and not hasattr(board, "JOYSTICK_X"):
6875
self._pad_btns = {
@@ -93,13 +100,13 @@ def _init_hardware(self):
93100
)
94101

95102
@property
96-
def is_clicked(self):
103+
def is_clicked(self) -> bool:
97104
"""Returns True if the cursor button was pressed
98105
during previous call to update()
99106
"""
100107
return self._is_clicked
101108

102-
def update(self):
109+
def update(self) -> None:
103110
"""Updates the cursor object."""
104111
if self._pad.events.get_into(self._event):
105112
self._store_button_states()
@@ -109,7 +116,7 @@ def update(self):
109116
elif self._pad_states & (1 << self._pad_btns["btn_a"]):
110117
self._is_clicked = True
111118

112-
def _read_joystick_x(self, samples=3):
119+
def _read_joystick_x(self, samples: int = 3) -> float:
113120
"""Read the X analog joystick on the PyGamer.
114121
:param int samples: How many samples to read and average.
115122
"""
@@ -121,7 +128,7 @@ def _read_joystick_x(self, samples=3):
121128
reading /= samples
122129
return reading
123130

124-
def _read_joystick_y(self, samples=3):
131+
def _read_joystick_y(self, samples: int = 3) -> float:
125132
"""Read the Y analog joystick on the PyGamer.
126133
:param int samples: How many samples to read and average.
127134
"""
@@ -133,7 +140,7 @@ def _read_joystick_y(self, samples=3):
133140
reading /= samples
134141
return reading
135142

136-
def _store_button_states(self):
143+
def _store_button_states(self) -> None:
137144
"""Stores the state of the PyBadge's D-Pad or the PyGamer's Joystick
138145
into a byte
139146
"""
@@ -142,7 +149,7 @@ def _store_button_states(self):
142149
if current_state != self._event.pressed:
143150
self._pad_states = (1 << bit_index) ^ self._pad_states
144151

145-
def _check_cursor_movement(self):
152+
def _check_cursor_movement(self) -> None:
146153
"""Checks the PyBadge D-Pad or the PyGamer's Joystick for movement."""
147154
if hasattr(board, "BUTTON_CLOCK") and not hasattr(board, "JOYSTICK_X"):
148155
if self._pad_states & (1 << self._pad_btns["btn_right"]):
@@ -180,15 +187,15 @@ class DebouncedCursorManager(CursorManager):
180187
:param Cursor cursor: The cursor object we are using.
181188
"""
182189

183-
def __init__(self, cursor, debounce_interval=0.01):
190+
def __init__(self, cursor: Cursor, debounce_interval: float = 0.01) -> None:
184191
CursorManager.__init__(self, cursor)
185192
self._debouncer = Debouncer(
186193
lambda: bool(self._pad_states & (1 << self._pad_btns["btn_a"])),
187194
interval=debounce_interval,
188195
)
189196

190197
@property
191-
def is_clicked(self):
198+
def is_clicked(self) -> bool:
192199
"""Returns True if the cursor button was pressed
193200
during previous call to update()
194201
"""
@@ -197,18 +204,18 @@ def is_clicked(self):
197204
pressed = is_clicked
198205

199206
@property
200-
def released(self):
207+
def released(self) -> bool:
201208
"""Returns True if the cursor button was released
202209
during previous call to update()
203210
"""
204211
return self._debouncer.fell
205212

206213
@property
207-
def held(self):
214+
def held(self) -> bool:
208215
"""Returns True if the cursor button is currently being held"""
209216
return self._debouncer.value
210217

211-
def update(self):
218+
def update(self) -> None:
212219
"""Updates the cursor object."""
213220
if self._pad.events.get_into(self._event):
214221
self._store_button_states()

0 commit comments

Comments
 (0)