Skip to content

Commit 500170b

Browse files
committed
Added Type Annotations + Corrected Docstrings
1 parent 2be4664 commit 500170b

File tree

3 files changed

+67
-36
lines changed

3 files changed

+67
-36
lines changed

adafruit_max7219/bcddigits.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99
from micropython import const
1010
from adafruit_max7219 import max7219
1111

12+
try:
13+
import digitalio
14+
import busio
15+
from typing import List
16+
except ImportError:
17+
pass
18+
1219
__version__ = "0.0.0-auto.0"
1320
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MAX7219.git"
1421

@@ -23,16 +30,16 @@ class BCDDigits(max7219.MAX7219):
2330
Basic support for display on a 7-Segment BCD display controlled
2431
by a Max7219 chip using SPI.
2532
26-
:param object spi: an spi busio or spi bitbangio object
33+
:param ~busio.SPI spi: an spi busio or spi bitbangio object
2734
:param ~digitalio.DigitalInOut cs: digital in/out to use as chip select signal
2835
:param int nDigits: number of led 7-segment digits; default 1; max 8
2936
"""
3037

31-
def __init__(self, spi, cs, nDigits=1):
38+
def __init__(self, spi: busio.SPI, cs: digitalio.DigitalInOut, nDigits: int = 1):
3239
self._ndigits = nDigits
3340
super().__init__(self._ndigits, 8, spi, cs)
3441

35-
def init_display(self):
42+
def init_display(self) -> None:
3643

3744
for cmd, data in (
3845
(_SHUTDOWN, 0),
@@ -46,7 +53,7 @@ def init_display(self):
4653
self.clear_all()
4754
self.show()
4855

49-
def set_digit(self, dpos, value):
56+
def set_digit(self, dpos: int, value: int) -> None:
5057
"""
5158
Display one digit.
5259
@@ -59,43 +66,43 @@ def set_digit(self, dpos, value):
5966
self.pixel(dpos, i, value & 0x01)
6067
value >>= 1
6168

62-
def set_digits(self, start, values):
69+
def set_digits(self, start: int, values: List[int]) -> None:
6370
"""
6471
Display digits from a list.
6572
66-
:param int s: digit to start display zero-based
67-
:param list ds: list of integer values ranging from 0->15
73+
:param int start: digit to start display zero-based
74+
:param list[int] values: list of integer values ranging from 0->15
6875
"""
6976
for value in values:
7077
# print('set digit {} start {}'.format(d,start))
7178
self.set_digit(start, value)
7279
start += 1
7380

74-
def show_dot(self, dpos, bit_value=None):
81+
def show_dot(self, dpos: int, bit_value: int = None) -> None:
7582
"""
7683
The decimal point for a digit.
7784
7885
:param int dpos: the digit to set the decimal point zero-based
79-
:param int value: value > zero lights the decimal point, else unlights the point
86+
:param int bit_value: value > zero lights the decimal point, else unlights the point
8087
"""
8188
if 0 <= dpos < self._ndigits:
8289
# print('set dot {} = {}'.format((self._ndigits - d -1),col))
8390
self.pixel(self._ndigits - dpos - 1, 7, bit_value)
8491

85-
def clear_all(self):
92+
def clear_all(self) -> None:
8693
"""
8794
Clear all digits and decimal points.
8895
"""
8996
self.fill(1)
9097
for i in range(self._ndigits):
9198
self.show_dot(i)
9299

93-
def show_str(self, start, strg):
100+
def show_str(self, start: int, strg: str) -> None:
94101
"""
95102
Displays a numeric str in the display. Shows digits ``0-9``, ``-``, and ``.``.
96103
97104
:param int start: start position to show the numeric string
98-
:param string str: the numeric string
105+
:param str strg: the numeric string
99106
"""
100107
cpos = start
101108
for char in strg:
@@ -111,7 +118,7 @@ def show_str(self, start, strg):
111118
self.set_digit(cpos, value)
112119
cpos += 1
113120

114-
def show_help(self, start):
121+
def show_help(self, start: int) -> None:
115122
"""
116123
Display the word HELP in the display.
117124

adafruit_max7219/matrices.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
from micropython import const
1010
from adafruit_max7219 import max7219
1111

12+
try:
13+
import digitalio
14+
import busio
15+
except ImportError:
16+
pass
17+
1218
__version__ = "0.0.0-auto.0"
1319
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MAX7219.git"
1420

@@ -22,14 +28,14 @@ class Matrix8x8(max7219.MAX7219):
2228
"""
2329
Driver for a 8x8 LED matrix based on the MAX7219 chip.
2430
25-
:param object spi: an spi busio or spi bitbangio object
31+
:param ~busio.SPI spi: an spi busio or spi bitbangio object
2632
:param ~digitalio.DigitalInOut cs: digital in/out to use as chip select signal
2733
"""
2834

29-
def __init__(self, spi, cs):
35+
def __init__(self, spi: busio.SPI, cs: digitalio.DigitalInOut):
3036
super().__init__(8, 8, spi, cs)
3137

32-
def init_display(self):
38+
def init_display(self) -> None:
3339
for cmd, data in (
3440
(_SHUTDOWN, 0),
3541
(_DISPLAYTEST, 0),
@@ -42,18 +48,18 @@ def init_display(self):
4248
self.fill(0)
4349
self.show()
4450

45-
def text(self, strg, xpos, ypos, bit_value=1):
51+
def text(self, strg: str, xpos: int, ypos: int, bit_value: int = 1) -> None:
4652
"""
4753
Draw text in the 8x8 matrix.
4854
55+
:param str strg: string to place in to display
4956
:param int xpos: x position of LED in matrix
5057
:param int ypos: y position of LED in matrix
51-
:param string strg: string to place in to display
52-
:param bit_value: > 1 sets the text, otherwise resets
58+
:param int bit_value: > 1 sets the text, otherwise resets
5359
"""
5460
self.framebuf.text(strg, xpos, ypos, bit_value)
5561

56-
def clear_all(self):
62+
def clear_all(self) -> None:
5763
"""
5864
Clears all matrix leds.
5965
"""

adafruit_max7219/max7219.py

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,17 @@
3737
**Notes:**
3838
#. Datasheet: https://cdn-shop.adafruit.com/datasheets/MAX7219.pdf
3939
"""
40-
# MicroPython SSD1306 OLED driver, I2C and SPI interfaces
40+
# MicroPython MAX7219 driver, SPI interfaces
4141
import digitalio
4242
from adafruit_bus_device import spi_device
4343
from micropython import const
4444
import adafruit_framebuf as framebuf
4545

46+
try:
47+
import busio
48+
except ImportError:
49+
pass
50+
4651
__version__ = "0.0.0-auto.0"
4752
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MAX7219.git"
4853

@@ -57,15 +62,23 @@ class MAX7219:
5762
5863
:param int width: the number of pixels wide
5964
:param int height: the number of pixels high
60-
:param object spi: an spi busio or spi bitbangio object
65+
:param ~busio.SPI spi: an spi busio or spi bitbangio object
6166
:param ~digitalio.DigitalInOut chip_select: digital in/out to use as chip select signal
62-
:param baudrate: for SPIDevice baudrate (default 8000000)
63-
:param polarity: for SPIDevice polarity (default 0)
64-
:param phase: for SPIDevice phase (default 0)
67+
:param int baudrate: for SPIDevice baudrate (default 8000000)
68+
:param int polarity: for SPIDevice polarity (default 0)
69+
:param int phase: for SPIDevice phase (default 0)
6570
"""
6671

6772
def __init__(
68-
self, width, height, spi, cs, *, baudrate=8000000, polarity=0, phase=0
73+
self,
74+
width: int,
75+
height: int,
76+
spi: busio.SPI,
77+
cs: digitalio.DigitalInOut,
78+
*,
79+
baudrate: int = 8000000,
80+
polarity: int = 0,
81+
phase: int = 0
6982
):
7083

7184
self._chip_select = cs
@@ -83,10 +96,10 @@ def __init__(
8396

8497
self.init_display()
8598

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

89-
def brightness(self, value):
102+
def brightness(self, value: int) -> None:
90103
"""
91104
Controls the brightness of the display.
92105
@@ -96,37 +109,42 @@ def brightness(self, value):
96109
raise ValueError("Brightness out of range")
97110
self.write_cmd(_INTENSITY, value)
98111

99-
def show(self):
112+
def show(self) -> None:
100113
"""
101114
Updates the display.
102115
"""
103116
for ypos in range(8):
104117
self.write_cmd(_DIGIT0 + ypos, self._buffer[ypos])
105118

106-
def fill(self, bit_value):
119+
def fill(self, bit_value: int) -> None:
107120
"""
108121
Fill the display buffer.
109122
110123
:param int bit_value: value > 0 set the buffer bit, else clears the buffer bit
111124
"""
112125
self.framebuf.fill(bit_value)
113126

114-
def pixel(self, xpos, ypos, bit_value=None):
127+
def pixel(self, xpos: int, ypos: int, bit_value: int = None) -> None:
115128
"""
116129
Set one buffer bit
117130
118-
:param xpos: x position to set bit
119-
:param ypos: y position to set bit
131+
:param int xpos: x position to set bit
132+
:param int ypos: y position to set bit
120133
:param int bit_value: value > 0 sets the buffer bit, else clears the buffer bit
121134
"""
122135
bit_value = 0x01 if bit_value else 0x00
123136
self.framebuf.pixel(xpos, ypos, bit_value)
124137

125-
def scroll(self, delta_x, delta_y):
126-
"""Srcolls the display using delta_x,delta_y."""
138+
def scroll(self, delta_x: int, delta_y: int) -> None:
139+
"""
140+
Srcolls the display using delta_x,delta_y.
141+
142+
:param int delta_x: positions to scroll in the x direction
143+
:param int delta_y: positions to scroll in the y direction
144+
"""
127145
self.framebuf.scroll(delta_x, delta_y)
128146

129-
def write_cmd(self, cmd, data):
147+
def write_cmd(self, cmd, data) -> None:
130148
# pylint: disable=no-member
131149
"""Writes a command to spi device."""
132150
# print('cmd {} data {}'.format(cmd,data))

0 commit comments

Comments
 (0)