diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1b9fadc..43d1385 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -24,7 +24,7 @@ repos: name: pylint (library code) types: [python] args: - - --disable=consider-using-f-string + - --disable=consider-using-f-string,duplicate-code exclude: "^(docs/|examples/|tests/|setup.py$)" - id: pylint name: pylint (example code) diff --git a/adafruit_featherwing/alphanum_featherwing.py b/adafruit_featherwing/alphanum_featherwing.py index c458641..8928c9f 100755 --- a/adafruit_featherwing/alphanum_featherwing.py +++ b/adafruit_featherwing/alphanum_featherwing.py @@ -15,9 +15,15 @@ __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FeatherWing.git" import board -import adafruit_ht16k33.segments as segments +from adafruit_ht16k33 import segments from adafruit_featherwing.led_segments import Segments +try: + from typing import Optional + from busio import I2C +except ImportError: + pass + class AlphaNumFeatherWing(Segments): """Class representing an `Adafruit 14-segment AlphaNumeric FeatherWing @@ -25,7 +31,7 @@ class AlphaNumFeatherWing(Segments): Automatically uses the feather's I2C bus.""" - def __init__(self, address=0x70, i2c=None): + def __init__(self, address: int = 0x70, i2c: Optional[I2C] = None): super().__init__() if i2c is None: i2c = board.I2C() diff --git a/adafruit_featherwing/auto_writeable.py b/adafruit_featherwing/auto_writeable.py index c108aa8..221dd65 100644 --- a/adafruit_featherwing/auto_writeable.py +++ b/adafruit_featherwing/auto_writeable.py @@ -27,6 +27,6 @@ def auto_write(self): return self._auto_write @auto_write.setter - def auto_write(self, write): + def auto_write(self, write: bool): if isinstance(write, bool): self._auto_write = write diff --git a/adafruit_featherwing/dotstar_featherwing.py b/adafruit_featherwing/dotstar_featherwing.py index d161f47..853ee1b 100755 --- a/adafruit_featherwing/dotstar_featherwing.py +++ b/adafruit_featherwing/dotstar_featherwing.py @@ -18,6 +18,12 @@ import adafruit_dotstar as dotstar from adafruit_featherwing.pixelmatrix import PixelMatrix +try: + import typing # pylint: disable=unused-import + from microcontroller import Pin +except ImportError: + pass + class DotStarFeatherWing(PixelMatrix): """Class representing a `DotStar FeatherWing @@ -25,7 +31,9 @@ class DotStarFeatherWing(PixelMatrix): The feather uses pins D13 and D11""" - def __init__(self, clock=board.D13, data=board.D11, brightness=0.2): + def __init__( + self, clock: Pin = board.D13, data: Pin = board.D11, brightness: float = 0.2 + ): """ :param pin clock: The clock pin for the featherwing :param pin data: The data pin for the featherwing diff --git a/adafruit_featherwing/gps_featherwing.py b/adafruit_featherwing/gps_featherwing.py index 1b43153..3fac735 100644 --- a/adafruit_featherwing/gps_featherwing.py +++ b/adafruit_featherwing/gps_featherwing.py @@ -18,6 +18,11 @@ import busio import adafruit_gps +try: + from typing import Optional +except ImportError: + pass + class GPSFeatherWing: """Class representing an `Ultimate GPS FeatherWing @@ -25,7 +30,7 @@ class GPSFeatherWing: Automatically uses the feather's UART bus.""" - def __init__(self, update_period=1000, baudrate=9600): + def __init__(self, update_period: int = 1000, baudrate: int = 9600): """ :param int update_period: (Optional) The amount of time in milliseconds between updates (default=1000) @@ -36,8 +41,7 @@ def __init__(self, update_period=1000, baudrate=9600): if update_period < 250: raise ValueError("Update Frequency be at least 250 milliseconds") timeout = update_period // 1000 + 2 - if timeout < 3: - timeout = 3 + timeout = max(timeout, 3) self._uart = busio.UART(board.TX, board.RX, baudrate=baudrate, timeout=timeout) self._gps = adafruit_gps.GPS(self._uart, debug=False) @@ -47,7 +51,7 @@ def __init__(self, update_period=1000, baudrate=9600): ) self._gps.send_command(bytes("PMTK220,{}".format(update_period), "utf-8")) - def update(self): + def update(self) -> bool: """ Make sure to call ``gps.update()`` every loop iteration and at least twice as fast as data comes from the GPS unit (usually every second). @@ -57,7 +61,7 @@ def update(self): """ return self._gps.update() - def read(self, size): + def read(self, size: int) -> Optional[bytearray]: """ Read the UART for any information that may be on it @@ -69,7 +73,7 @@ def read(self, size): return self._uart.read(size) return None - def send_command(self, command): + def send_command(self, command: bytearray): """ Send a bytearray command to the GPS module diff --git a/adafruit_featherwing/ina219_featherwing.py b/adafruit_featherwing/ina219_featherwing.py index b43414d..7b8e9bf 100644 --- a/adafruit_featherwing/ina219_featherwing.py +++ b/adafruit_featherwing/ina219_featherwing.py @@ -17,6 +17,12 @@ import board import adafruit_ina219 +try: + from typing import Optional + from busio import I2C +except ImportError: + pass + class INA219FeatherWing: """Class representing an `Adafruit INA219 FeatherWing @@ -24,7 +30,7 @@ class INA219FeatherWing: Automatically uses the feather's I2C bus.""" - def __init__(self, i2c=None): + def __init__(self, i2c: Optional[I2C] = None): if i2c is None: i2c = board.I2C() self._ina219 = adafruit_ina219.INA219(i2c) diff --git a/adafruit_featherwing/joy_featherwing.py b/adafruit_featherwing/joy_featherwing.py index faf9cb8..807288b 100644 --- a/adafruit_featherwing/joy_featherwing.py +++ b/adafruit_featherwing/joy_featherwing.py @@ -18,6 +18,13 @@ from micropython import const import adafruit_seesaw.seesaw +try: + from typing import Optional, Tuple + from busio import I2C +except ImportError: + pass + + BUTTON_A = const(1 << 6) BUTTON_B = const(1 << 7) BUTTON_Y = const(1 << 9) @@ -30,7 +37,7 @@ class JoyFeatherWing: Automatically uses the feather's I2C bus.""" - def __init__(self, i2c=None): + def __init__(self, i2c: Optional[I2C] = None): if i2c is None: i2c = board.I2C() self._seesaw = adafruit_seesaw.seesaw.Seesaw(i2c) @@ -157,7 +164,7 @@ def button_select(self): """ return self._check_button(BUTTON_SELECT) - def _check_button(self, button): + def _check_button(self, button: int) -> bool: """Utilises the seesaw to determine which button is being pressed.""" buttons = self._seesaw.digital_read_bulk(button) return not buttons != 0 @@ -200,7 +207,7 @@ def joystick_offset(self): return self._joystick_offset @joystick_offset.setter - def joystick_offset(self, offset): + def joystick_offset(self, offset: Tuple[int, int]): self._joystick_offset = offset def zero_joystick(self): diff --git a/adafruit_featherwing/keyboard_featherwing.py b/adafruit_featherwing/keyboard_featherwing.py index fc14303..3553b24 100644 --- a/adafruit_featherwing/keyboard_featherwing.py +++ b/adafruit_featherwing/keyboard_featherwing.py @@ -29,6 +29,13 @@ # pylint: disable-msg=too-many-arguments from adafruit_featherwing.tft_featherwing import TFTFeatherWing +try: + from typing import Optional + from busio import SPI, I2C + from microcontroller import Pin +except ImportError: + pass + class KeyboardFeatherwing(TFTFeatherWing): """Class representing a `Keyboard Featherwing` @@ -38,13 +45,13 @@ class KeyboardFeatherwing(TFTFeatherWing): def __init__( self, - spi=None, - cs=None, - dc=None, - i2c=None, - ts_cs=None, - sd_cs=None, - neopixel_pin=None, + spi: Optional[SPI] = None, + cs: Optional[Pin] = None, + dc: Optional[Pin] = None, + i2c: Optional[I2C] = None, + ts_cs: Optional[Pin] = None, + sd_cs: Optional[Pin] = None, + neopixel_pin: Optional[Pin] = None, ): super().__init__(spi, cs, dc, ts_cs, sd_cs) diff --git a/adafruit_featherwing/led_segments.py b/adafruit_featherwing/led_segments.py index 2ab6bac..67930b8 100755 --- a/adafruit_featherwing/led_segments.py +++ b/adafruit_featherwing/led_segments.py @@ -16,6 +16,11 @@ # pylint: disable-msg=unsubscriptable-object, unsupported-assignment-operation +try: + from typing import Union +except ImportError: + pass + class Segments: """Class representing an `Adafruit 14-segment AlphaNumeric FeatherWing @@ -26,7 +31,7 @@ class Segments: def __init__(self): self._segments = None - def print(self, value): + def print(self, value: Union[str, int]): """ Print a number or text to the display @@ -41,7 +46,7 @@ def print(self, value): self._segments.print(value) self._segments.show() - def marquee(self, text, delay=0.25, loop=True): + def marquee(self, text: str, delay: float = 0.25, loop: bool = True): """ Automatically scroll the text at the specified delay between characters @@ -53,7 +58,7 @@ def marquee(self, text, delay=0.25, loop=True): """ self._segments.marquee(text, delay, loop) - def fill(self, fill): + def fill(self, fill: bool): """Change all Segments on or off :param bool fill: True turns all segments on, False turns all segments off @@ -75,7 +80,7 @@ def blink_rate(self): return self._segments.blink_rate @blink_rate.setter - def blink_rate(self, rate): + def blink_rate(self, rate: int): self._segments.blink_rate = rate @property @@ -87,7 +92,7 @@ def brightness(self): return round(self._segments.brightness * 15) @brightness.setter - def brightness(self, brightness): + def brightness(self, brightness: int): if not 0 <= brightness <= 15: raise ValueError("Brightness must be a value between 0 and 15") self._segments.brightness = brightness / 15 diff --git a/adafruit_featherwing/matrix_featherwing.py b/adafruit_featherwing/matrix_featherwing.py index 066525e..cdf95bf 100755 --- a/adafruit_featherwing/matrix_featherwing.py +++ b/adafruit_featherwing/matrix_featherwing.py @@ -16,10 +16,16 @@ __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FeatherWing.git" import board -import adafruit_ht16k33.matrix as matrix +from adafruit_ht16k33 import matrix from adafruit_featherwing.auto_writeable import AutoWriteable +try: + from typing import Optional, Tuple, Union + from busio import I2C +except ImportError: + pass + class MatrixFeatherWing(AutoWriteable): """Class representing an `Adafruit 8x16 LED Matrix FeatherWing @@ -27,7 +33,7 @@ class MatrixFeatherWing(AutoWriteable): Automatically uses the feather's I2C bus.""" - def __init__(self, address=0x70, i2c=None): + def __init__(self, address: int = 0x70, i2c: Optional[I2C] = None): if i2c is None: i2c = board.I2C() @@ -37,14 +43,14 @@ def __init__(self, address=0x70, i2c=None): self.rows = 8 super().__init__() - def __getitem__(self, key): + def __getitem__(self, key: Tuple[int, int]) -> bool: """ Get the current value of a pixel """ x, y = key return self.pixel(x, y) - def __setitem__(self, key, value): + def __setitem__(self, key: Tuple[int, int], value: Union[int, bool]): """ Turn a pixel off or on """ @@ -59,7 +65,7 @@ def _update(self): if self._auto_write: self._matrix.show() - def pixel(self, x, y, color=None): + def pixel(self, x: int, y: int, color: Optional[bool] = None) -> Optional[bool]: """ Turn a pixel on or off or retrieve a pixel value @@ -79,7 +85,7 @@ def show(self): """ self._matrix.show() - def fill(self, fill): + def fill(self, fill: bool): """ Turn all pixels on or off @@ -92,7 +98,7 @@ def fill(self, fill): else: raise ValueError("Must set to either True or False.") - def shift_right(self, rotate=False): + def shift_right(self, rotate: bool = False): """ Shift all pixels right @@ -101,7 +107,7 @@ def shift_right(self, rotate=False): self._matrix.shift_right(rotate) self._update() - def shift_left(self, rotate=False): + def shift_left(self, rotate: bool = False): """ Shift all pixels left @@ -110,7 +116,7 @@ def shift_left(self, rotate=False): self._matrix.shift_left(rotate) self._update() - def shift_up(self, rotate=False): + def shift_up(self, rotate: bool = False): """ Shift all pixels up @@ -119,7 +125,7 @@ def shift_up(self, rotate=False): self._matrix.shift_up(rotate) self._update() - def shift_down(self, rotate=False): + def shift_down(self, rotate: bool = False): """ Shift all pixels down @@ -138,7 +144,7 @@ def blink_rate(self): return self._matrix.blink_rate @blink_rate.setter - def blink_rate(self, rate): + def blink_rate(self, rate: int): self._matrix.blink_rate = rate @property @@ -150,7 +156,7 @@ def brightness(self): return round(self._matrix.brightness * 15) @brightness.setter - def brightness(self, brightness): + def brightness(self, brightness: int): if not 0 <= brightness <= 15: raise ValueError("Brightness must be a value between 0 and 15") self._matrix.brightness = brightness / 15 diff --git a/adafruit_featherwing/minitft_featherwing.py b/adafruit_featherwing/minitft_featherwing.py index 68a495e..321f16c 100755 --- a/adafruit_featherwing/minitft_featherwing.py +++ b/adafruit_featherwing/minitft_featherwing.py @@ -23,6 +23,14 @@ import displayio from adafruit_st7735r import ST7735R +try: + from typing import Optional + from busio import I2C, SPI + from microcontroller import Pin +except ImportError: + pass + + BUTTON_RIGHT = const(7) BUTTON_DOWN = const(4) BUTTON_LEFT = const(3) @@ -50,7 +58,14 @@ class MiniTFTFeatherWing: | (1 << BUTTON_B) ) # pylint: disable-msg=too-many-arguments - def __init__(self, address=0x5E, i2c=None, spi=None, cs=None, dc=None): + def __init__( + self, + address: int = 0x5E, + i2c: Optional[I2C] = None, + spi: Optional[SPI] = None, + cs: Optional[Pin] = None, + dc: Optional[Pin] = None, + ): displayio.release_displays() if i2c is None: i2c = board.I2C() @@ -81,7 +96,7 @@ def backlight(self): return self._backlight.duty_cycle / 255 @backlight.setter - def backlight(self, brightness): + def backlight(self, brightness: float): """ Set the backlight duty cycle """ diff --git a/adafruit_featherwing/neopixel_featherwing.py b/adafruit_featherwing/neopixel_featherwing.py index d28e2aa..c3f56a3 100755 --- a/adafruit_featherwing/neopixel_featherwing.py +++ b/adafruit_featherwing/neopixel_featherwing.py @@ -18,6 +18,12 @@ import neopixel from adafruit_featherwing.pixelmatrix import PixelMatrix +try: + import typing # pylint: disable=unused-import + from microcontroller import Pin +except ImportError: + pass + class NeoPixelFeatherWing(PixelMatrix): """Class representing a `NeoPixel FeatherWing @@ -25,7 +31,7 @@ class NeoPixelFeatherWing(PixelMatrix): The feather uses pins D6 by default""" - def __init__(self, pixel_pin=board.D6, brightness=0.1): + def __init__(self, pixel_pin: Pin = board.D6, brightness: float = 0.1): """ :param pin pixel_pin: The pin for the featherwing :param float brightness: Optional brightness (0.0-1.0) that defaults to 1.0 @@ -41,7 +47,7 @@ def __init__(self, pixel_pin=board.D6, brightness=0.1): pixel_order=neopixel.GRB, ) - def shift_up(self, rotate=False): + def shift_up(self, rotate: bool = False): """ Shift all pixels up @@ -74,7 +80,7 @@ def shift_up(self, rotate=False): """ super().shift_down(rotate) # Up and down are reversed - def shift_down(self, rotate=False): + def shift_down(self, rotate: bool = False): """ Shift all pixels down. diff --git a/adafruit_featherwing/pixelmatrix.py b/adafruit_featherwing/pixelmatrix.py index 9658ad3..f38ff69 100755 --- a/adafruit_featherwing/pixelmatrix.py +++ b/adafruit_featherwing/pixelmatrix.py @@ -16,8 +16,19 @@ __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FeatherWing.git" # pylint: disable-msg=unsubscriptable-object, unsupported-assignment-operation +from typing import Sequence from adafruit_featherwing.auto_writeable import AutoWriteable +try: + from typing import Union, Tuple, List + + IndexUnion = Union[Tuple[int, int], slice, int] + RGBSequence = Union[List[int], Tuple[int, int, int]] + RGBBSequence = Union[List[int], Tuple[int, int, int, int]] + ValueUnion = Union[RGBBSequence, RGBBSequence, int] +except ImportError: + pass + class PixelMatrix(AutoWriteable): """Base Class for DotStar and NeoPixel FeatherWings @@ -30,7 +41,7 @@ def __init__(self): self._matrix = None super().__init__() - def __setitem__(self, indices, value): + def __setitem__(self, indices: IndexUnion, value: ValueUnion): """ indices can be one of three things: x and y ints that are calculated to the DotStar index @@ -45,7 +56,7 @@ def __setitem__(self, indices, value): self._matrix[self._get_index(indices)] = value self._update() - def __getitem__(self, indices): + def __getitem__(self, indices: IndexUnion) -> Tuple[int, int, int]: """ indices can be one of three things: x and y ints that are calculated to the DotStar index @@ -54,7 +65,7 @@ def __getitem__(self, indices): """ return self._matrix[self._get_index(indices)] - def _get_index(self, indices): + def _get_index(self, indices: IndexUnion) -> Union[int, slice]: """ Figure out which DotStar to address based on what was passed in """ @@ -80,7 +91,7 @@ def _update(self): if self._auto_write: self._matrix.show() - def fill(self, color=0): + def fill(self, color: Sequence[int] = 0): """ Fills all of the Pixels with a color or unlit if empty. @@ -97,7 +108,7 @@ def show(self): """ self._matrix.show() - def shift_right(self, rotate=False): + def shift_right(self, rotate: bool = False): """ Shift all pixels right @@ -112,7 +123,7 @@ def shift_right(self, rotate=False): self._matrix[y * self.columns] = last_pixel self._update() - def shift_left(self, rotate=False): + def shift_left(self, rotate: bool = False): """ Shift all pixels left @@ -127,7 +138,7 @@ def shift_left(self, rotate=False): self._matrix[(y + 1) * self.columns - 1] = last_pixel self._update() - def shift_up(self, rotate=False): + def shift_up(self, rotate: bool = False): """ Shift all pixels up @@ -144,7 +155,7 @@ def shift_up(self, rotate=False): self._matrix[x] = last_pixel self._update() - def shift_down(self, rotate=False): + def shift_down(self, rotate: bool = False): """ Shift all pixels down @@ -167,6 +178,6 @@ def brightness(self): return self._matrix.brightness @brightness.setter - def brightness(self, brightness): + def brightness(self, brightness: float): self._matrix.brightness = min(max(brightness, 0.0), 1.0) self._update() diff --git a/adafruit_featherwing/rtc_featherwing.py b/adafruit_featherwing/rtc_featherwing.py index 8baa215..e439bba 100755 --- a/adafruit_featherwing/rtc_featherwing.py +++ b/adafruit_featherwing/rtc_featherwing.py @@ -20,6 +20,12 @@ import board import adafruit_ds3231 +try: + from typing import Optional, Dict + from busio import I2C +except ImportError: + pass + class RTCFeatherWing: """Class representing an `DS3231 Precision RTC FeatherWing @@ -27,24 +33,24 @@ class RTCFeatherWing: Automatically uses the feather's I2C bus.""" - def __init__(self, i2c=None): + def __init__(self, i2c: Optional[I2C] = None): if i2c is None: i2c = board.I2C() self._rtc = adafruit_ds3231.DS3231(i2c) - def __setitem__(self, index, value): + def __setitem__(self, index: str, value: int): """ Allow updates using setitem if that makes it easier """ self._set_time_value(index, value) - def __getitem__(self, index): + def __getitem__(self, index: str): """ Allow retrievals using getitem if that makes it easier """ return self._get_time_value(index) - def _set_time_value(self, unit, value): + def _set_time_value(self, unit: str, value: int): """ Set just the specific unit of time """ @@ -56,7 +62,7 @@ def _set_time_value(self, unit, value): self._rtc.datetime = self._encode(now) - def _get_time_value(self, unit): + def _get_time_value(self, unit: str): """ Get just the specific unit of time """ @@ -65,7 +71,7 @@ def _get_time_value(self, unit): return now[unit] raise ValueError("The specified unit of time is invalid") - def _get_now(self): + def _get_now(self) -> Dict[str, int]: """ Return the current date and time in a nice updatable dictionary """ @@ -80,7 +86,7 @@ def _get_now(self): "weekday": now.tm_wday, } - def _encode(self, date): + def _encode(self, date: Dict[str, int]) -> Dict[str, int]: """ Encode the updatable dictionary back into a time struct """ @@ -99,7 +105,7 @@ def _encode(self, date): ) ) - def is_leap_year(self, year=None): + def is_leap_year(self, year: Optional[int] = None) -> bool: """ Check if the year is a leap year @@ -109,7 +115,9 @@ def is_leap_year(self, year=None): year = self._get_time_value("year") return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0) - def get_month_days(self, month=None, year=None): + def get_month_days( + self, month: Optional[int] = None, year: Optional[int] = None + ) -> int: """ Return the number of days for the month of the given year @@ -122,7 +130,7 @@ def get_month_days(self, month=None, year=None): max_days = (31, 29 if leap_year else 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) return max_days[month - 1] - def set_time(self, hour, minute, second=0): + def set_time(self, hour: int, minute: int, second: int = 0): """ Set the time only @@ -145,7 +153,7 @@ def set_time(self, hour, minute, second=0): now["second"] = second self._rtc.datetime = self._encode(now) - def set_date(self, day, month, year): + def set_date(self, day: int, month: int, year: int): """ Set the date only @@ -179,7 +187,7 @@ def datetime(self): return self._rtc.datetime @datetime.setter - def datetime(self, datetime): + def datetime(self, datetime: time.struct_time): self._rtc.datetime = datetime @property @@ -190,7 +198,7 @@ def year(self): return self._get_time_value("year") @year.setter - def year(self, year): + def year(self, year: int): if isinstance(year, int): self._set_time_value("year", year) else: @@ -204,7 +212,7 @@ def month(self): return self._get_time_value("month") @month.setter - def month(self, month): + def month(self, month: int): if isinstance(month, int) and 1 <= month <= 12: self._set_time_value("month", month) else: @@ -218,7 +226,7 @@ def day(self): return self._get_time_value("day") @day.setter - def day(self, day): + def day(self, day: int): month_days = self.get_month_days() if isinstance(day, int) and 1 <= day <= month_days: self._set_time_value("day", day) @@ -235,7 +243,7 @@ def hour(self): return self._get_time_value("hour") @hour.setter - def hour(self, hour): + def hour(self, hour: int): if isinstance(hour, int) and 0 <= hour < 24: self._set_time_value("hour", hour) else: @@ -249,7 +257,7 @@ def minute(self): return self._get_time_value("minute") @minute.setter - def minute(self, minute): + def minute(self, minute: int): if isinstance(minute, int) and 0 <= minute < 60: self._set_time_value("minute", minute) else: @@ -263,7 +271,7 @@ def second(self): return self._get_time_value("second") @second.setter - def second(self, second): + def second(self, second: int): if isinstance(second, int) and 0 <= second < 60: self._set_time_value("second", second) else: @@ -277,7 +285,7 @@ def weekday(self): return self._get_time_value("weekday") @weekday.setter - def weekday(self, weekday): + def weekday(self, weekday: int): if isinstance(weekday, int) and 0 <= weekday < 7: self._set_time_value("weekday", weekday) else: @@ -303,7 +311,7 @@ def unixtime(self): return None @unixtime.setter - def unixtime(self, unixtime): + def unixtime(self, unixtime: int): if isinstance(unixtime, int): try: self._rtc.datetime = time.localtime(unixtime) diff --git a/adafruit_featherwing/sevensegment_featherwing.py b/adafruit_featherwing/sevensegment_featherwing.py index c685e8d..9e03303 100755 --- a/adafruit_featherwing/sevensegment_featherwing.py +++ b/adafruit_featherwing/sevensegment_featherwing.py @@ -15,9 +15,15 @@ __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FeatherWing.git" import board -import adafruit_ht16k33.segments as segments +from adafruit_ht16k33 import segments from adafruit_featherwing.led_segments import Segments +try: + from typing import Optional + from busio import I2C +except ImportError: + pass + class SevenSegmentFeatherWing(Segments): """Class representing an `Adafruit 7-Segment LED HT16K33 FeatherWing @@ -25,7 +31,7 @@ class SevenSegmentFeatherWing(Segments): Automatically uses the feather's I2C bus.""" - def __init__(self, address=0x70, i2c=None): + def __init__(self, address: int = 0x70, i2c: Optional[I2C] = None): super().__init__() if i2c is None: i2c = board.I2C() diff --git a/adafruit_featherwing/tempmotion_featherwing.py b/adafruit_featherwing/tempmotion_featherwing.py index f369df2..ae37e95 100755 --- a/adafruit_featherwing/tempmotion_featherwing.py +++ b/adafruit_featherwing/tempmotion_featherwing.py @@ -19,6 +19,11 @@ import adafruit_adxl34x import adafruit_adt7410 +try: + from typing import Optional +except ImportError: + pass + class TempMotionFeatherWing: """Class helper representing an `Adafruit ADXL343 + ADT7410 Sensor FeatherWing @@ -26,7 +31,12 @@ class TempMotionFeatherWing: Automatically uses the feather's I2C bus.""" - def __init__(self, adxl343_address=0x53, adt7410_address=0x48, i2c=None): + def __init__( + self, + adxl343_address: int = 0x53, + adt7410_address: int = 0x48, + i2c: Optional[int] = None, + ): if i2c is None: i2c = board.I2C() self._adxl343 = adafruit_adxl34x.ADXL345(i2c, address=adxl343_address) @@ -48,7 +58,7 @@ def configuration(self): return self._adt7410.configuration @configuration.setter - def configuration(self, val): + def configuration(self, val: int): self._adt7410.configuration = val @property @@ -61,7 +71,7 @@ def events(self): """Returns the ADXL343 Enabled Events""" return self._adxl343.events - def enable_motion_detection(self, **kwargs): + def enable_motion_detection(self, **kwargs: int): """Enable motion detection""" self._adxl343.enable_motion_detection(**kwargs) @@ -69,7 +79,7 @@ def disable_motion_detection(self): """Disable motion detection""" self._adxl343.disable_motion_detection() - def enable_freefall_detection(self, **kwargs): + def enable_freefall_detection(self, **kwargs: int): """Enable freefall detection""" self._adxl343.enable_freefall_detection(**kwargs) @@ -77,7 +87,7 @@ def disable_freefall_detection(self): """Disable freefall detection""" self._adxl343.disable_freefall_detection() - def enable_tap_detection(self, **kwargs): + def enable_tap_detection(self, **kwargs: int): """Enable freefall detection""" self._adxl343.enable_tap_detection(**kwargs) @@ -91,7 +101,7 @@ def data_rate(self): return self._adxl343.data_rate @data_rate.setter - def data_rate(self, val): + def data_rate(self, val: int): self._adxl343.data_rate = val @property @@ -100,5 +110,5 @@ def range(self): return self._adxl343.range @range.setter - def range(self, val): + def range(self, val: int): self._adxl343.range = val diff --git a/adafruit_featherwing/tft_featherwing.py b/adafruit_featherwing/tft_featherwing.py index 415f1ad..0579739 100644 --- a/adafruit_featherwing/tft_featherwing.py +++ b/adafruit_featherwing/tft_featherwing.py @@ -23,6 +23,13 @@ import sdcardio import storage +try: + from typing import Optional + from busio import SPI + from microcontroller import Pin +except ImportError: + pass + # pylint: disable-msg=too-few-public-methods, too-many-arguments class TFTFeatherWing: @@ -31,7 +38,14 @@ class TFTFeatherWing: """ - def __init__(self, spi=None, cs=None, dc=None, ts_cs=None, sd_cs=None): + def __init__( + self, + spi: Optional[SPI] = None, + cs: Optional[Pin] = None, + dc: Optional[Pin] = None, + ts_cs: Optional[Pin] = None, + sd_cs: Optional[Pin] = None, + ): displayio.release_displays() if spi is None: spi = board.SPI() diff --git a/adafruit_featherwing/tft_featherwing_24.py b/adafruit_featherwing/tft_featherwing_24.py index 8d56163..9fc2a41 100644 --- a/adafruit_featherwing/tft_featherwing_24.py +++ b/adafruit_featherwing/tft_featherwing_24.py @@ -23,6 +23,14 @@ import adafruit_ili9341 from adafruit_featherwing.tft_featherwing import TFTFeatherWing +try: + from typing import Optional + from busio import SPI + from microcontroller import Pin +except ImportError: + pass + + # pylint: disable-msg=too-few-public-methods, too-many-arguments class TFTFeatherWing24(TFTFeatherWing): """Class representing an `TFT FeatherWing 2.4 @@ -30,7 +38,14 @@ class TFTFeatherWing24(TFTFeatherWing): """ - def __init__(self, spi=None, cs=None, dc=None, ts_cs=None, sd_cs=None): + def __init__( + self, + spi: Optional[SPI] = None, + cs: Optional[Pin] = None, + dc: Optional[Pin] = None, + ts_cs: Optional[Pin] = None, + sd_cs: Optional[Pin] = None, + ): super().__init__(spi, cs, dc, ts_cs, sd_cs) self.display = adafruit_ili9341.ILI9341( self._display_bus, width=320, height=240 diff --git a/adafruit_featherwing/tft_featherwing_35.py b/adafruit_featherwing/tft_featherwing_35.py index 3546951..99a0709 100644 --- a/adafruit_featherwing/tft_featherwing_35.py +++ b/adafruit_featherwing/tft_featherwing_35.py @@ -23,6 +23,14 @@ from adafruit_hx8357 import HX8357 from adafruit_featherwing.tft_featherwing import TFTFeatherWing +try: + from typing import Optional + from busio import SPI + from microcontroller import Pin +except ImportError: + pass + + # pylint: disable-msg=too-few-public-methods, too-many-arguments class TFTFeatherWing35(TFTFeatherWing): """Class representing an `TFT FeatherWing 3.5 @@ -30,6 +38,13 @@ class TFTFeatherWing35(TFTFeatherWing): """ - def __init__(self, spi=None, cs=None, dc=None, ts_cs=None, sd_cs=None): + def __init__( + self, + spi: Optional[SPI] = None, + cs: Optional[Pin] = None, + dc: Optional[Pin] = None, + ts_cs: Optional[Pin] = None, + sd_cs: Optional[Pin] = None, + ): super().__init__(spi, cs, dc, ts_cs, sd_cs) self.display = HX8357(self._display_bus, width=480, height=320) diff --git a/examples/featherwing_keyboard_featherwing.py b/examples/featherwing_keyboard_featherwing.py index 643f443..5e0b217 100644 --- a/examples/featherwing_keyboard_featherwing.py +++ b/examples/featherwing_keyboard_featherwing.py @@ -20,13 +20,12 @@ kbd_featherwing.neopixel[0] = 0x002244 try: - f = open("/sd/tft_featherwing.txt", "w") - f.write("Blinka\nBlackberry Q10 Keyboard") - f.close() + with open("/sd/tft_featherwing.txt", "w") as f: + f.write("Blinka\nBlackberry Q10 Keyboard") + + with open("/sd/tft_featherwing.txt", "r") as f: + print(f.read()) - f = open("/sd/tft_featherwing.txt", "r") - print(f.read()) - f.close() except OSError as error: print("Unable to write to SD Card.") diff --git a/examples/featherwing_tft24_simpletest.py b/examples/featherwing_tft24_simpletest.py index 7f11eac..2aee653 100644 --- a/examples/featherwing_tft24_simpletest.py +++ b/examples/featherwing_tft24_simpletest.py @@ -13,13 +13,12 @@ tft_featherwing = tft_featherwing_24.TFTFeatherWing24() try: - f = open("/sd/tft_featherwing.txt", "w") - f.write("Blinka\nBlackberry Q10 Keyboard") - f.close() + with open("/sd/tft_featherwing.txt", "w") as f: + f.write("Blinka\nBlackberry Q10 Keyboard") + + with open("/sd/tft_featherwing.txt", "r") as f: + print(f.read()) - f = open("/sd/tft_featherwing.txt", "r") - print(f.read()) - f.close() except OSError as error: print("Unable to write to SD Card.") diff --git a/examples/featherwing_tft35_simpletest.py b/examples/featherwing_tft35_simpletest.py index 475073a..26a6720 100644 --- a/examples/featherwing_tft35_simpletest.py +++ b/examples/featherwing_tft35_simpletest.py @@ -13,13 +13,11 @@ tft_featherwing = tft_featherwing_35.TFTFeatherWing35() try: - f = open("/sd/tft_featherwing.txt", "w") - f.write("Blinka\nBlackberry Q10 Keyboard") - f.close() + with open("/sd/tft_featherwing.txt", "w") as f: + f.write("Blinka\nBlackberry Q10 Keyboard") - f = open("/sd/tft_featherwing.txt", "r") - print(f.read()) - f.close() + with open("/sd/tft_featherwing.txt", "r") as f: + print(f.read()) except OSError as error: print("Unable to write to SD Card.")