-
Notifications
You must be signed in to change notification settings - Fork 10
/
adafruit_74hc595.py
175 lines (141 loc) · 5.49 KB
/
adafruit_74hc595.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
`adafruit_74hc595`
====================================================
CircuitPython driver for 74HC595 shift register.
* Author(s): Kattni Rembor, Tony DiCola
Implementation Notes
--------------------
**Hardware:**
"* `74HC595 Shift Register - 3 pack <https://www.adafruit.com/product/450>`_"
**Software and Dependencies:**
* Adafruit CircuitPython firmware for the supported boards:
https://github.com/adafruit/circuitpython/releases
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
"""
import digitalio
from adafruit_bus_device import spi_device
try:
import typing # pylint: disable=unused-import
from microcontroller import Pin
import busio
from circuitpython_typing import ReadableBuffer
except ImportError:
pass
__version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_74HC595.git"
class DigitalInOut:
"""Digital input/output of the 74HC595. The interface is exactly the
same as the ``digitalio.DigitalInOut`` class, however note that by design
this device is OUTPUT ONLY! Attempting to read inputs or set
direction as input will raise an exception.
"""
_pin: Pin
_byte_pos: int
_byte_pin: int
_shift_register: "ShiftRegister74HC595"
def __init__(
self,
pin_number: Pin,
shift_register_74hc595: "ShiftRegister74HC595",
) -> None:
"""Specify the pin number of the shift register (0...7) and
ShiftRegister74HC595 instance.
"""
self._pin = pin_number
self._byte_pos = self._pin // 8
self._byte_pin = self._pin % 8
self._shift_register = shift_register_74hc595
# kwargs in switch functions below are _necessary_ for compatibility
# with DigitalInout class (which allows specifying pull, etc. which
# is unused by this class). Do not remove them, instead turn off pylint
# in this case.
# pylint: disable=unused-argument
def switch_to_output(self, value: bool = False, **kwargs) -> None:
"""``DigitalInOut switch_to_output``"""
self.direction = digitalio.Direction.OUTPUT
self.value = value
def switch_to_input(self, **kwargs) -> None: # pylint: disable=no-self-use
"""``switch_to_input`` is not supported."""
raise RuntimeError("Digital input not supported.")
# pylint: enable=unused-argument
@property
def value(self) -> bool:
"""The value of the pin, either True for high or False for low."""
return self._shift_register.gpio[self._byte_pos] & (1 << self._byte_pin) == (
1 << self._byte_pin
)
@value.setter
def value(self, val: bool) -> None:
if (
self._pin >= 0
and self._pin < self._shift_register.number_of_shift_registers * 8
):
gpio = self._shift_register.gpio
if val:
gpio[self._byte_pos] |= 1 << self._byte_pin
else:
gpio[self._byte_pos] &= ~(1 << self._byte_pin)
self._shift_register.gpio = gpio
@property
def direction(self) -> digitalio.Direction.OUTPUT:
"""``Direction`` can only be set to ``OUTPUT``."""
return digitalio.Direction.OUTPUT
@direction.setter
def direction( # pylint: disable=no-self-use
self,
val: digitalio.Direction.OUTPUT,
) -> None:
"""``Direction`` can only be set to ``OUTPUT``."""
if val != digitalio.Direction.OUTPUT:
raise RuntimeError("Digital input not supported.")
@property
def pull(self) -> None:
"""Pull-up/down not supported, return None for no pull-up/down."""
return None
@pull.setter
def pull(self, val: None) -> None: # pylint: disable=no-self-use
"""Only supports null/no pull state."""
if val is not None:
raise RuntimeError("Pull-up and pull-down not supported.")
class ShiftRegister74HC595:
"""Initialise the 74HC595 on specified SPI bus, indicate the
number of shift registers being used and optional baudrate.
"""
_device: spi_device.SPIDevice
_number_of_shift_registers: int
_gpio: ReadableBuffer
def __init__(
self,
spi: busio.SPI,
latch: digitalio.DigitalInOut,
number_of_shift_registers: int = 1,
baudrate: int = 1000000,
) -> None:
self._device = spi_device.SPIDevice(spi, latch, baudrate=baudrate)
self._number_of_shift_registers = number_of_shift_registers
self._gpio = bytearray(self._number_of_shift_registers)
@property
def number_of_shift_registers(self) -> int:
"""The number of shift register chips"""
return self._number_of_shift_registers
@property
def gpio(self) -> ReadableBuffer:
"""The raw GPIO output register. Each bit represents the
output value of the associated pin (0 = low, 1 = high).
"""
return self._gpio
@gpio.setter
def gpio(self, val: ReadableBuffer) -> None:
self._gpio = val
with self._device as spi:
# pylint: disable=no-member
spi.write(self._gpio)
def get_pin(self, pin: int) -> DigitalInOut:
"""Convenience function to create an instance of the DigitalInOut class
pointing at the specified pin of this 74HC595 device .
"""
assert 0 <= pin <= (self._number_of_shift_registers * 8) - 1
return DigitalInOut(pin, self)