Skip to content

Type Annotations #35

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 4 commits into from
Oct 2, 2021
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
34 changes: 21 additions & 13 deletions adafruit_max7219/bcddigits.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
from micropython import const
from adafruit_max7219 import max7219

try:
# Used only for typing
from typing import List
import digitalio
import busio
except ImportError:
pass

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

Expand All @@ -23,16 +31,16 @@ class BCDDigits(max7219.MAX7219):
Basic support for display on a 7-Segment BCD display controlled
by a Max7219 chip using SPI.

:param object spi: an spi busio or spi bitbangio object
:param ~busio.SPI spi: an spi busio or spi bitbangio object
:param ~digitalio.DigitalInOut cs: digital in/out to use as chip select signal
:param int nDigits: number of led 7-segment digits; default 1; max 8
"""

def __init__(self, spi, cs, nDigits=1):
def __init__(self, spi: busio.SPI, cs: digitalio.DigitalInOut, nDigits: int = 1):
self._ndigits = nDigits
super().__init__(self._ndigits, 8, spi, cs)

def init_display(self):
def init_display(self) -> None:

for cmd, data in (
(_SHUTDOWN, 0),
Expand All @@ -46,7 +54,7 @@ def init_display(self):
self.clear_all()
self.show()

def set_digit(self, dpos, value):
def set_digit(self, dpos: int, value: int) -> None:
"""
Display one digit.

Expand All @@ -59,43 +67,43 @@ def set_digit(self, dpos, value):
self.pixel(dpos, i, value & 0x01)
value >>= 1

def set_digits(self, start, values):
def set_digits(self, start: int, values: List[int]) -> None:
"""
Display digits from a list.

:param int s: digit to start display zero-based
:param list ds: list of integer values ranging from 0->15
:param int start: digit to start display zero-based
:param list[int] values: list of integer values ranging from 0->15
"""
for value in values:
# print('set digit {} start {}'.format(d,start))
self.set_digit(start, value)
start += 1

def show_dot(self, dpos, bit_value=None):
def show_dot(self, dpos: int, bit_value: int = None) -> None:
"""
The decimal point for a digit.

:param int dpos: the digit to set the decimal point zero-based
:param int value: value > zero lights the decimal point, else unlights the point
:param int bit_value: value > zero lights the decimal point, else unlights the point
"""
if 0 <= dpos < self._ndigits:
# print('set dot {} = {}'.format((self._ndigits - d -1),col))
self.pixel(self._ndigits - dpos - 1, 7, bit_value)

def clear_all(self):
def clear_all(self) -> None:
"""
Clear all digits and decimal points.
"""
self.fill(1)
for i in range(self._ndigits):
self.show_dot(i)

def show_str(self, start, strg):
def show_str(self, start: int, strg: str) -> None:
"""
Displays a numeric str in the display. Shows digits ``0-9``, ``-``, and ``.``.

:param int start: start position to show the numeric string
:param string str: the numeric string
:param str strg: the numeric string
"""
cpos = start
for char in strg:
Expand All @@ -111,7 +119,7 @@ def show_str(self, start, strg):
self.set_digit(cpos, value)
cpos += 1

def show_help(self, start):
def show_help(self, start: int) -> None:
"""
Display the word HELP in the display.

Expand Down
22 changes: 15 additions & 7 deletions adafruit_max7219/matrices.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
from micropython import const
from adafruit_max7219 import max7219

try:
# Used only for typing
import typing # pylint: disable=unused-import
import digitalio
import busio
except ImportError:
pass

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

Expand All @@ -22,14 +30,14 @@ class Matrix8x8(max7219.MAX7219):
"""
Driver for a 8x8 LED matrix based on the MAX7219 chip.

:param object spi: an spi busio or spi bitbangio object
:param ~busio.SPI spi: an spi busio or spi bitbangio object
:param ~digitalio.DigitalInOut cs: digital in/out to use as chip select signal
"""

def __init__(self, spi, cs):
def __init__(self, spi: busio.SPI, cs: digitalio.DigitalInOut):
super().__init__(8, 8, spi, cs)

def init_display(self):
def init_display(self) -> None:
for cmd, data in (
(_SHUTDOWN, 0),
(_DISPLAYTEST, 0),
Expand All @@ -42,18 +50,18 @@ def init_display(self):
self.fill(0)
self.show()

def text(self, strg, xpos, ypos, bit_value=1):
def text(self, strg: str, xpos: int, ypos: int, bit_value: int = 1) -> None:
"""
Draw text in the 8x8 matrix.

:param str strg: string to place in to display
:param int xpos: x position of LED in matrix
:param int ypos: y position of LED in matrix
:param string strg: string to place in to display
:param bit_value: > 1 sets the text, otherwise resets
:param int bit_value: > 1 sets the text, otherwise resets
"""
self.framebuf.text(strg, xpos, ypos, bit_value)

def clear_all(self):
def clear_all(self) -> None:
"""
Clears all matrix leds.
"""
Expand Down
60 changes: 42 additions & 18 deletions adafruit_max7219/max7219.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,19 @@
**Notes:**
#. Datasheet: https://cdn-shop.adafruit.com/datasheets/MAX7219.pdf
"""
# MicroPython SSD1306 OLED driver, I2C and SPI interfaces
# MicroPython MAX7219 driver, SPI interfaces
import digitalio
from adafruit_bus_device import spi_device
from micropython import const
import adafruit_framebuf as framebuf

try:
# Used only for typing
import typing # pylint: disable=unused-import
import busio
except ImportError:
pass

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

Expand All @@ -57,15 +64,23 @@ class MAX7219:

:param int width: the number of pixels wide
:param int height: the number of pixels high
:param object spi: an spi busio or spi bitbangio object
:param ~busio.SPI spi: an spi busio or spi bitbangio object
:param ~digitalio.DigitalInOut chip_select: digital in/out to use as chip select signal
:param baudrate: for SPIDevice baudrate (default 8000000)
:param polarity: for SPIDevice polarity (default 0)
:param phase: for SPIDevice phase (default 0)
:param int baudrate: for SPIDevice baudrate (default 8000000)
:param int polarity: for SPIDevice polarity (default 0)
:param int phase: for SPIDevice phase (default 0)
"""

def __init__(
self, width, height, spi, cs, *, baudrate=8000000, polarity=0, phase=0
self,
width: int,
height: int,
spi: busio.SPI,
cs: digitalio.DigitalInOut,
*,
baudrate: int = 8000000,
polarity: int = 0,
phase: int = 0
):

self._chip_select = cs
Expand All @@ -83,10 +98,10 @@ def __init__(

self.init_display()

def init_display(self):
def init_display(self) -> None:
"""Must be implemented by derived class (``matrices``, ``bcddigits``)"""

def brightness(self, value):
def brightness(self, value: int) -> None:
"""
Controls the brightness of the display.

Expand All @@ -96,39 +111,48 @@ def brightness(self, value):
raise ValueError("Brightness out of range")
self.write_cmd(_INTENSITY, value)

def show(self):
def show(self) -> None:
"""
Updates the display.
"""
for ypos in range(8):
self.write_cmd(_DIGIT0 + ypos, self._buffer[ypos])

def fill(self, bit_value):
def fill(self, bit_value: int) -> None:
"""
Fill the display buffer.

:param int bit_value: value > 0 set the buffer bit, else clears the buffer bit
"""
self.framebuf.fill(bit_value)

def pixel(self, xpos, ypos, bit_value=None):
def pixel(self, xpos: int, ypos: int, bit_value: int = None) -> None:
"""
Set one buffer bit

:param xpos: x position to set bit
:param ypos: y position to set bit
:param int xpos: x position to set bit
:param int ypos: y position to set bit
:param int bit_value: value > 0 sets the buffer bit, else clears the buffer bit
"""
bit_value = 0x01 if bit_value else 0x00
self.framebuf.pixel(xpos, ypos, bit_value)

def scroll(self, delta_x, delta_y):
"""Srcolls the display using delta_x,delta_y."""
def scroll(self, delta_x: int, delta_y: int) -> None:
"""
Srcolls the display using delta_x,delta_y.

:param int delta_x: positions to scroll in the x direction
:param int delta_y: positions to scroll in the y direction
"""
self.framebuf.scroll(delta_x, delta_y)

def write_cmd(self, cmd, data):
# pylint: disable=no-member
"""Writes a command to spi device."""
def write_cmd(self, cmd: int, data: int) -> None:
"""
Writes a command to spi device.

:param int cmd: register address to write data to
:param int data: data to be written to commanded register
"""
# print('cmd {} data {}'.format(cmd,data))
self._chip_select.value = False
with self._spi_device as my_spi_device:
Expand Down