Skip to content

Commit 9e55281

Browse files
authored
Merge pull request #35 from FlantasticDan/type-annotations
Type Annotations
2 parents 2be4664 + 43c42c2 commit 9e55281

File tree

3 files changed

+78
-38
lines changed

3 files changed

+78
-38
lines changed

adafruit_max7219/bcddigits.py

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

12+
try:
13+
# Used only for typing
14+
from typing import List
15+
import digitalio
16+
import busio
17+
except ImportError:
18+
pass
19+
1220
__version__ = "0.0.0-auto.0"
1321
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MAX7219.git"
1422

@@ -23,16 +31,16 @@ class BCDDigits(max7219.MAX7219):
2331
Basic support for display on a 7-Segment BCD display controlled
2432
by a Max7219 chip using SPI.
2533
26-
:param object spi: an spi busio or spi bitbangio object
34+
:param ~busio.SPI spi: an spi busio or spi bitbangio object
2735
:param ~digitalio.DigitalInOut cs: digital in/out to use as chip select signal
2836
:param int nDigits: number of led 7-segment digits; default 1; max 8
2937
"""
3038

31-
def __init__(self, spi, cs, nDigits=1):
39+
def __init__(self, spi: busio.SPI, cs: digitalio.DigitalInOut, nDigits: int = 1):
3240
self._ndigits = nDigits
3341
super().__init__(self._ndigits, 8, spi, cs)
3442

35-
def init_display(self):
43+
def init_display(self) -> None:
3644

3745
for cmd, data in (
3846
(_SHUTDOWN, 0),
@@ -46,7 +54,7 @@ def init_display(self):
4654
self.clear_all()
4755
self.show()
4856

49-
def set_digit(self, dpos, value):
57+
def set_digit(self, dpos: int, value: int) -> None:
5058
"""
5159
Display one digit.
5260
@@ -59,43 +67,43 @@ def set_digit(self, dpos, value):
5967
self.pixel(dpos, i, value & 0x01)
6068
value >>= 1
6169

62-
def set_digits(self, start, values):
70+
def set_digits(self, start: int, values: List[int]) -> None:
6371
"""
6472
Display digits from a list.
6573
66-
:param int s: digit to start display zero-based
67-
:param list ds: list of integer values ranging from 0->15
74+
:param int start: digit to start display zero-based
75+
:param list[int] values: list of integer values ranging from 0->15
6876
"""
6977
for value in values:
7078
# print('set digit {} start {}'.format(d,start))
7179
self.set_digit(start, value)
7280
start += 1
7381

74-
def show_dot(self, dpos, bit_value=None):
82+
def show_dot(self, dpos: int, bit_value: int = None) -> None:
7583
"""
7684
The decimal point for a digit.
7785
7886
: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
87+
:param int bit_value: value > zero lights the decimal point, else unlights the point
8088
"""
8189
if 0 <= dpos < self._ndigits:
8290
# print('set dot {} = {}'.format((self._ndigits - d -1),col))
8391
self.pixel(self._ndigits - dpos - 1, 7, bit_value)
8492

85-
def clear_all(self):
93+
def clear_all(self) -> None:
8694
"""
8795
Clear all digits and decimal points.
8896
"""
8997
self.fill(1)
9098
for i in range(self._ndigits):
9199
self.show_dot(i)
92100

93-
def show_str(self, start, strg):
101+
def show_str(self, start: int, strg: str) -> None:
94102
"""
95103
Displays a numeric str in the display. Shows digits ``0-9``, ``-``, and ``.``.
96104
97105
:param int start: start position to show the numeric string
98-
:param string str: the numeric string
106+
:param str strg: the numeric string
99107
"""
100108
cpos = start
101109
for char in strg:
@@ -111,7 +119,7 @@ def show_str(self, start, strg):
111119
self.set_digit(cpos, value)
112120
cpos += 1
113121

114-
def show_help(self, start):
122+
def show_help(self, start: int) -> None:
115123
"""
116124
Display the word HELP in the display.
117125

adafruit_max7219/matrices.py

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

12+
try:
13+
# Used only for typing
14+
import typing # pylint: disable=unused-import
15+
import digitalio
16+
import busio
17+
except ImportError:
18+
pass
19+
1220
__version__ = "0.0.0-auto.0"
1321
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MAX7219.git"
1422

@@ -22,14 +30,14 @@ class Matrix8x8(max7219.MAX7219):
2230
"""
2331
Driver for a 8x8 LED matrix based on the MAX7219 chip.
2432
25-
:param object spi: an spi busio or spi bitbangio object
33+
:param ~busio.SPI spi: an spi busio or spi bitbangio object
2634
:param ~digitalio.DigitalInOut cs: digital in/out to use as chip select signal
2735
"""
2836

29-
def __init__(self, spi, cs):
37+
def __init__(self, spi: busio.SPI, cs: digitalio.DigitalInOut):
3038
super().__init__(8, 8, spi, cs)
3139

32-
def init_display(self):
40+
def init_display(self) -> None:
3341
for cmd, data in (
3442
(_SHUTDOWN, 0),
3543
(_DISPLAYTEST, 0),
@@ -42,18 +50,18 @@ def init_display(self):
4250
self.fill(0)
4351
self.show()
4452

45-
def text(self, strg, xpos, ypos, bit_value=1):
53+
def text(self, strg: str, xpos: int, ypos: int, bit_value: int = 1) -> None:
4654
"""
4755
Draw text in the 8x8 matrix.
4856
57+
:param str strg: string to place in to display
4958
:param int xpos: x position of LED in matrix
5059
: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
60+
:param int bit_value: > 1 sets the text, otherwise resets
5361
"""
5462
self.framebuf.text(strg, xpos, ypos, bit_value)
5563

56-
def clear_all(self):
64+
def clear_all(self) -> None:
5765
"""
5866
Clears all matrix leds.
5967
"""

adafruit_max7219/max7219.py

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,19 @@
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+
# Used only for typing
48+
import typing # pylint: disable=unused-import
49+
import busio
50+
except ImportError:
51+
pass
52+
4653
__version__ = "0.0.0-auto.0"
4754
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MAX7219.git"
4855

@@ -57,15 +64,23 @@ class MAX7219:
5764
5865
:param int width: the number of pixels wide
5966
:param int height: the number of pixels high
60-
:param object spi: an spi busio or spi bitbangio object
67+
:param ~busio.SPI spi: an spi busio or spi bitbangio object
6168
: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)
69+
:param int baudrate: for SPIDevice baudrate (default 8000000)
70+
:param int polarity: for SPIDevice polarity (default 0)
71+
:param int phase: for SPIDevice phase (default 0)
6572
"""
6673

6774
def __init__(
68-
self, width, height, spi, cs, *, baudrate=8000000, polarity=0, phase=0
75+
self,
76+
width: int,
77+
height: int,
78+
spi: busio.SPI,
79+
cs: digitalio.DigitalInOut,
80+
*,
81+
baudrate: int = 8000000,
82+
polarity: int = 0,
83+
phase: int = 0
6984
):
7085

7186
self._chip_select = cs
@@ -83,10 +98,10 @@ def __init__(
8398

8499
self.init_display()
85100

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

89-
def brightness(self, value):
104+
def brightness(self, value: int) -> None:
90105
"""
91106
Controls the brightness of the display.
92107
@@ -96,39 +111,48 @@ def brightness(self, value):
96111
raise ValueError("Brightness out of range")
97112
self.write_cmd(_INTENSITY, value)
98113

99-
def show(self):
114+
def show(self) -> None:
100115
"""
101116
Updates the display.
102117
"""
103118
for ypos in range(8):
104119
self.write_cmd(_DIGIT0 + ypos, self._buffer[ypos])
105120

106-
def fill(self, bit_value):
121+
def fill(self, bit_value: int) -> None:
107122
"""
108123
Fill the display buffer.
109124
110125
:param int bit_value: value > 0 set the buffer bit, else clears the buffer bit
111126
"""
112127
self.framebuf.fill(bit_value)
113128

114-
def pixel(self, xpos, ypos, bit_value=None):
129+
def pixel(self, xpos: int, ypos: int, bit_value: int = None) -> None:
115130
"""
116131
Set one buffer bit
117132
118-
:param xpos: x position to set bit
119-
:param ypos: y position to set bit
133+
:param int xpos: x position to set bit
134+
:param int ypos: y position to set bit
120135
:param int bit_value: value > 0 sets the buffer bit, else clears the buffer bit
121136
"""
122137
bit_value = 0x01 if bit_value else 0x00
123138
self.framebuf.pixel(xpos, ypos, bit_value)
124139

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

129-
def write_cmd(self, cmd, data):
130-
# pylint: disable=no-member
131-
"""Writes a command to spi device."""
149+
def write_cmd(self, cmd: int, data: int) -> None:
150+
"""
151+
Writes a command to spi device.
152+
153+
:param int cmd: register address to write data to
154+
:param int data: data to be written to commanded register
155+
"""
132156
# print('cmd {} data {}'.format(cmd,data))
133157
self._chip_select.value = False
134158
with self._spi_device as my_spi_device:

0 commit comments

Comments
 (0)