Skip to content

Added type annotations. #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions adafruit_magtag/graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
import board
from adafruit_portalbase.graphics import GraphicsBase

try:
from typing import Union, Optional, Tuple
except ImportError:
pass

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MagTag.git"

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

# pylint: disable=too-many-instance-attributes, too-many-locals, too-many-branches, too-many-statements
def __init__(
self, *, default_bg=0xFFFFFF, auto_refresh=True, rotation=270, debug=False
):
self,
*,
default_bg: Union[str, int] = 0xFFFFFF,
auto_refresh: bool = True,
rotation: int = 270,
debug: bool = False
) -> None:
self._debug = debug
self.display = board.DISPLAY
self.display.rotation = rotation
Expand All @@ -60,7 +70,9 @@ def __init__(

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

def set_background(self, file_or_color, position=None):
def set_background(
self, file_or_color: Union[str, int], position: Optional[Tuple[int, int]] = None
) -> None:
"""The background image to a bitmap file.

:param file_or_color: The filename of the chosen background image, or a hex color.
Expand All @@ -73,8 +85,14 @@ def set_background(self, file_or_color, position=None):
gc.collect()

def qrcode(
self, qr_data, *, qr_size=1, x=0, y=0, qr_color=0x000000
): # pylint: disable=invalid-name
self,
qr_data: Union[bytes, str],
*,
qr_size: int = 1,
x: int = 0,
y: int = 0,
qr_color: int = 0x000000
) -> None: # pylint: disable=invalid-name
"""Display a QR code on the eInk

:param qr_data: The data for the QR code.
Expand Down
44 changes: 28 additions & 16 deletions adafruit_magtag/magtag.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@
from adafruit_magtag.graphics import Graphics
from adafruit_magtag.peripherals import Peripherals

try:
from typing import Optional, Union, Sequence, Dict, Callable, Any
import microcontroller
import neopixel
except ImportError:
pass

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MagTag.git"

Expand Down Expand Up @@ -65,16 +72,16 @@ class MagTag(PortalBase):
def __init__(
self,
*,
url=None,
headers=None,
json_path=None,
regexp_path=None,
default_bg=0xFFFFFF,
status_neopixel=None,
json_transform=None,
rotation=270,
debug=False,
):
url: Optional[str] = None,
headers: Optional[Dict[str, str]] = None,
json_path: Optional[Sequence[Any]] = None,
regexp_path: Optional[Sequence[str]] = None,
default_bg: Union[str, int] = 0xFFFFFF,
status_neopixel: Optional[Union[microcontroller.Pin, neopixel.NeoPixel]] = None,
json_transform: Union[Sequence[Callable], Callable] = None,
rotation: int = 270,
debug: bool = False,
) -> None:

self.peripherals = Peripherals()

Expand Down Expand Up @@ -107,7 +114,7 @@ def __init__(

gc.collect()

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

def enter_light_sleep(self, sleep_time):
def enter_light_sleep(self, sleep_time: float) -> None:
"""
Enter light sleep and resume the program after a certain period of time.

Expand All @@ -147,7 +154,7 @@ def enter_light_sleep(self, sleep_time):
gc.collect()

# pylint: disable=arguments-differ
def set_text(self, val, index=0, auto_refresh=True):
def set_text(self, val: str, index: int = 0, auto_refresh: bool = True) -> None:
"""Display text, with indexing into our list of text boxes.

:param str val: The text to be displayed
Expand All @@ -162,11 +169,16 @@ def set_text(self, val, index=0, auto_refresh=True):

# pylint: enable=arguments-differ

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

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

# pylint: enable=arguments-differ

def refresh(self):
def refresh(self) -> None:
"""
Refresh the display
"""
Expand Down
18 changes: 12 additions & 6 deletions adafruit_magtag/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
from adafruit_portalbase.network import NetworkBase
from adafruit_portalbase.wifi_esp32s2 import WiFi

try:
from typing import Optional, Union
import microcontroller
except ImportError:
pass

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MagTag.git"

Expand All @@ -50,10 +56,10 @@ class Network(NetworkBase):
def __init__(
self,
*,
status_neopixel=None,
extract_values=True,
debug=False,
):
status_neopixel: Optional[Union[microcontroller.Pin, neopixel.NeoPixel]] = None,
extract_values: bool = True,
debug: bool = False,
) -> None:
if status_neopixel:
if isinstance(status_neopixel, neopixel.NeoPixel):
status_led = status_neopixel
Expand All @@ -68,13 +74,13 @@ def __init__(
)

@property
def enabled(self):
def enabled(self) -> bool:
"""
Get or Set whether the WiFi is enabled

"""
return self._wifi.enabled

@enabled.setter
def enabled(self, value):
def enabled(self, value: bool) -> None:
self._wifi.enabled = bool(value)
28 changes: 14 additions & 14 deletions adafruit_magtag/peripherals.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Peripherals:
"""Peripherals Helper Class for the MagTag Library"""

# pylint: disable=too-many-instance-attributes, too-many-locals, too-many-branches, too-many-statements
def __init__(self):
def __init__(self) -> None:
# Neopixel power
try:
self._neopixel_disable = DigitalInOut(board.NEOPIXEL_POWER)
Expand Down Expand Up @@ -71,7 +71,7 @@ def __init__(self):
switch.pull = Pull.UP
self.buttons.append(switch)

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

def deinit(self):
def deinit(self) -> None:
"""Call deinit on all resources to free them"""
self.neopixels.deinit()
if self._neopixel_disable is not None:
Expand All @@ -103,12 +103,12 @@ def deinit(self):
self._light.deinit()

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

@property
def neopixel_disable(self):
def neopixel_disable(self) -> bool:
"""
Enable or disable the neopixels for power savings
"""
Expand All @@ -117,58 +117,58 @@ def neopixel_disable(self):
return False

@neopixel_disable.setter
def neopixel_disable(self, value):
def neopixel_disable(self, value: bool) -> None:
if self._neopixel_disable is not None:
self._neopixel_disable.value = value

@property
def speaker_disable(self):
def speaker_disable(self) -> bool:
"""
Enable or disable the speaker for power savings
"""
return not self._speaker_enable.value

@speaker_disable.setter
def speaker_disable(self, value):
def speaker_disable(self, value: bool) -> None:
self._speaker_enable.value = not value

@property
def button_a_pressed(self):
def button_a_pressed(self) -> bool:
"""
Return whether Button A is pressed
"""
return not self.buttons[0].value

@property
def button_b_pressed(self):
def button_b_pressed(self) -> bool:
"""
Return whether Button B is pressed
"""
return not self.buttons[1].value

@property
def button_c_pressed(self):
def button_c_pressed(self) -> bool:
"""
Return whether Button C is pressed
"""
return not self.buttons[2].value

@property
def button_d_pressed(self):
def button_d_pressed(self) -> bool:
"""
Return whether Button D is pressed
"""
return not self.buttons[3].value

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

@property
def light(self):
def light(self) -> int:
"""
Return the value of the light sensor. The neopixel_disable property
must be false to get a value.
Expand Down