Skip to content

Commit 0aea7e6

Browse files
authored
Merge pull request #83 from UnicycleDumpTruck/add-type-annotations
Added type annotations.
2 parents 2926b90 + 7db37ae commit 0aea7e6

File tree

4 files changed

+77
-41
lines changed

4 files changed

+77
-41
lines changed

adafruit_magtag/graphics.py

+23-5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
import board
3333
from adafruit_portalbase.graphics import GraphicsBase
3434

35+
try:
36+
from typing import Union, Optional, Tuple
37+
except ImportError:
38+
pass
39+
3540
__version__ = "0.0.0-auto.0"
3641
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MagTag.git"
3742

@@ -50,8 +55,13 @@ class Graphics(GraphicsBase):
5055

5156
# pylint: disable=too-many-instance-attributes, too-many-locals, too-many-branches, too-many-statements
5257
def __init__(
53-
self, *, default_bg=0xFFFFFF, auto_refresh=True, rotation=270, debug=False
54-
):
58+
self,
59+
*,
60+
default_bg: Union[str, int] = 0xFFFFFF,
61+
auto_refresh: bool = True,
62+
rotation: int = 270,
63+
debug: bool = False
64+
) -> None:
5565
self._debug = debug
5666
self.display = board.DISPLAY
5767
self.display.rotation = rotation
@@ -60,7 +70,9 @@ def __init__(
6070

6171
super().__init__(board.DISPLAY, default_bg=default_bg, debug=debug)
6272

63-
def set_background(self, file_or_color, position=None):
73+
def set_background(
74+
self, file_or_color: Union[str, int], position: Optional[Tuple[int, int]] = None
75+
) -> None:
6476
"""The background image to a bitmap file.
6577
6678
:param file_or_color: The filename of the chosen background image, or a hex color.
@@ -73,8 +85,14 @@ def set_background(self, file_or_color, position=None):
7385
gc.collect()
7486

7587
def qrcode(
76-
self, qr_data, *, qr_size=1, x=0, y=0, qr_color=0x000000
77-
): # pylint: disable=invalid-name
88+
self,
89+
qr_data: Union[bytes, str],
90+
*,
91+
qr_size: int = 1,
92+
x: int = 0,
93+
y: int = 0,
94+
qr_color: int = 0x000000
95+
) -> None: # pylint: disable=invalid-name
7896
"""Display a QR code on the eInk
7997
8098
:param qr_data: The data for the QR code.

adafruit_magtag/magtag.py

+28-16
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@
3535
from adafruit_magtag.graphics import Graphics
3636
from adafruit_magtag.peripherals import Peripherals
3737

38+
try:
39+
from typing import Optional, Union, Sequence, Dict, Callable, Any
40+
import microcontroller
41+
import neopixel
42+
except ImportError:
43+
pass
44+
3845
__version__ = "0.0.0-auto.0"
3946
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MagTag.git"
4047

@@ -65,16 +72,16 @@ class MagTag(PortalBase):
6572
def __init__(
6673
self,
6774
*,
68-
url=None,
69-
headers=None,
70-
json_path=None,
71-
regexp_path=None,
72-
default_bg=0xFFFFFF,
73-
status_neopixel=None,
74-
json_transform=None,
75-
rotation=270,
76-
debug=False,
77-
):
75+
url: Optional[str] = None,
76+
headers: Optional[Dict[str, str]] = None,
77+
json_path: Optional[Sequence[Any]] = None,
78+
regexp_path: Optional[Sequence[str]] = None,
79+
default_bg: Union[str, int] = 0xFFFFFF,
80+
status_neopixel: Optional[Union[microcontroller.Pin, neopixel.NeoPixel]] = None,
81+
json_transform: Union[Sequence[Callable], Callable] = None,
82+
rotation: int = 270,
83+
debug: bool = False,
84+
) -> None:
7885

7986
self.peripherals = Peripherals()
8087

@@ -107,7 +114,7 @@ def __init__(
107114

108115
gc.collect()
109116

110-
def exit_and_deep_sleep(self, sleep_time):
117+
def exit_and_deep_sleep(self, sleep_time: float) -> None:
111118
"""
112119
Stops the current program and enters deep sleep. The program is restarted from the beginning
113120
after a certain period of time.
@@ -123,7 +130,7 @@ def exit_and_deep_sleep(self, sleep_time):
123130
self.peripherals.speaker_disable = True
124131
super().exit_and_deep_sleep(sleep_time)
125132

126-
def enter_light_sleep(self, sleep_time):
133+
def enter_light_sleep(self, sleep_time: float) -> None:
127134
"""
128135
Enter light sleep and resume the program after a certain period of time.
129136
@@ -147,7 +154,7 @@ def enter_light_sleep(self, sleep_time):
147154
gc.collect()
148155

149156
# pylint: disable=arguments-differ
150-
def set_text(self, val, index=0, auto_refresh=True):
157+
def set_text(self, val: str, index: int = 0, auto_refresh: bool = True) -> None:
151158
"""Display text, with indexing into our list of text boxes.
152159
153160
:param str val: The text to be displayed
@@ -162,11 +169,16 @@ def set_text(self, val, index=0, auto_refresh=True):
162169

163170
# pylint: enable=arguments-differ
164171

165-
def _fetch_set_text(self, val, index=0):
172+
def _fetch_set_text(self, val: str, index: int = 0) -> None:
166173
self.set_text(val, index=index, auto_refresh=False)
167174

168175
# pylint: disable=arguments-differ
169-
def fetch(self, refresh_url=None, timeout=10, auto_refresh=True):
176+
def fetch(
177+
self,
178+
refresh_url: Optional[str] = None,
179+
timeout: int = 10,
180+
auto_refresh: bool = True,
181+
) -> Any:
170182
"""Fetch data from the url we initialized with, perfom any parsing,
171183
and display text or graphics. This function does pretty much everything
172184
Optionally update the URL
@@ -183,7 +195,7 @@ def fetch(self, refresh_url=None, timeout=10, auto_refresh=True):
183195

184196
# pylint: enable=arguments-differ
185197

186-
def refresh(self):
198+
def refresh(self) -> None:
187199
"""
188200
Refresh the display
189201
"""

adafruit_magtag/network.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@
3131
from adafruit_portalbase.network import NetworkBase
3232
from adafruit_portalbase.wifi_esp32s2 import WiFi
3333

34+
try:
35+
from typing import Optional, Union
36+
import microcontroller
37+
except ImportError:
38+
pass
39+
3440
__version__ = "0.0.0-auto.0"
3541
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MagTag.git"
3642

@@ -50,10 +56,10 @@ class Network(NetworkBase):
5056
def __init__(
5157
self,
5258
*,
53-
status_neopixel=None,
54-
extract_values=True,
55-
debug=False,
56-
):
59+
status_neopixel: Optional[Union[microcontroller.Pin, neopixel.NeoPixel]] = None,
60+
extract_values: bool = True,
61+
debug: bool = False,
62+
) -> None:
5763
if status_neopixel:
5864
if isinstance(status_neopixel, neopixel.NeoPixel):
5965
status_led = status_neopixel
@@ -68,13 +74,13 @@ def __init__(
6874
)
6975

7076
@property
71-
def enabled(self):
77+
def enabled(self) -> bool:
7278
"""
7379
Get or Set whether the WiFi is enabled
7480
7581
"""
7682
return self._wifi.enabled
7783

7884
@enabled.setter
79-
def enabled(self, value):
85+
def enabled(self, value: bool) -> None:
8086
self._wifi.enabled = bool(value)

adafruit_magtag/peripherals.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class Peripherals:
4141
"""Peripherals Helper Class for the MagTag Library"""
4242

4343
# pylint: disable=too-many-instance-attributes, too-many-locals, too-many-branches, too-many-statements
44-
def __init__(self):
44+
def __init__(self) -> None:
4545
# Neopixel power
4646
try:
4747
self._neopixel_disable = DigitalInOut(board.NEOPIXEL_POWER)
@@ -71,7 +71,7 @@ def __init__(self):
7171
switch.pull = Pull.UP
7272
self.buttons.append(switch)
7373

74-
def play_tone(self, frequency, duration):
74+
def play_tone(self, frequency: float, duration: float) -> None:
7575
"""Automatically Enable/Disable the speaker and play
7676
a tone at the specified frequency for the specified duration
7777
It will attempt to play the sound up to 3 times in the case of
@@ -91,7 +91,7 @@ def play_tone(self, frequency, duration):
9191
attempt += 1
9292
self._speaker_enable.value = False
9393

94-
def deinit(self):
94+
def deinit(self) -> None:
9595
"""Call deinit on all resources to free them"""
9696
self.neopixels.deinit()
9797
if self._neopixel_disable is not None:
@@ -103,12 +103,12 @@ def deinit(self):
103103
self._light.deinit()
104104

105105
@property
106-
def battery(self):
106+
def battery(self) -> float:
107107
"""Return the voltage of the battery"""
108108
return (self._batt_monitor.value / 65535.0) * 3.3 * 2
109109

110110
@property
111-
def neopixel_disable(self):
111+
def neopixel_disable(self) -> bool:
112112
"""
113113
Enable or disable the neopixels for power savings
114114
"""
@@ -117,58 +117,58 @@ def neopixel_disable(self):
117117
return False
118118

119119
@neopixel_disable.setter
120-
def neopixel_disable(self, value):
120+
def neopixel_disable(self, value: bool) -> None:
121121
if self._neopixel_disable is not None:
122122
self._neopixel_disable.value = value
123123

124124
@property
125-
def speaker_disable(self):
125+
def speaker_disable(self) -> bool:
126126
"""
127127
Enable or disable the speaker for power savings
128128
"""
129129
return not self._speaker_enable.value
130130

131131
@speaker_disable.setter
132-
def speaker_disable(self, value):
132+
def speaker_disable(self, value: bool) -> None:
133133
self._speaker_enable.value = not value
134134

135135
@property
136-
def button_a_pressed(self):
136+
def button_a_pressed(self) -> bool:
137137
"""
138138
Return whether Button A is pressed
139139
"""
140140
return not self.buttons[0].value
141141

142142
@property
143-
def button_b_pressed(self):
143+
def button_b_pressed(self) -> bool:
144144
"""
145145
Return whether Button B is pressed
146146
"""
147147
return not self.buttons[1].value
148148

149149
@property
150-
def button_c_pressed(self):
150+
def button_c_pressed(self) -> bool:
151151
"""
152152
Return whether Button C is pressed
153153
"""
154154
return not self.buttons[2].value
155155

156156
@property
157-
def button_d_pressed(self):
157+
def button_d_pressed(self) -> bool:
158158
"""
159159
Return whether Button D is pressed
160160
"""
161161
return not self.buttons[3].value
162162

163163
@property
164-
def any_button_pressed(self):
164+
def any_button_pressed(self) -> bool:
165165
"""
166166
Return whether any button is pressed
167167
"""
168168
return False in [self.buttons[i].value for i in range(0, 4)]
169169

170170
@property
171-
def light(self):
171+
def light(self) -> int:
172172
"""
173173
Return the value of the light sensor. The neopixel_disable property
174174
must be false to get a value.

0 commit comments

Comments
 (0)