Created
May 6, 2019 16:52
-
-
Save erikvanzijst/2f9a991f912d5a53ea91ef73390399c5 to your computer and use it in GitHub Desktop.
16 bit serial to parallel GPIO interface
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# | |
# Code accompanying https://www.youtube.com/watch?v=SnPh4zwQkVI | |
import atexit | |
from time import sleep | |
from gpiozero import DigitalOutputDevice | |
def _pulse(pin): | |
pin.value = 1 | |
sleep(.001) | |
pin.value = 0 | |
class ShiftRegister(object): | |
def __init__(self, width=8, ser=2, oe=3, rclk=4, sclk=17, clr=27): | |
self.width = width | |
self.data = DigitalOutputDevice(ser) | |
self.oe = DigitalOutputDevice(oe) | |
self.rclk = DigitalOutputDevice(rclk) | |
self.sclk = DigitalOutputDevice(sclk) | |
self.clr = DigitalOutputDevice(clr) | |
self.oe.value = 0 | |
self.clr.value = 1 | |
def close(self): | |
self.clear() | |
self.data.close() | |
self.rclk.close() | |
self.sclk.close() | |
self.clr.close() | |
def clear(self): | |
self.clr.value = 0 | |
sleep(.001) | |
_pulse(self.rclk) | |
self.clr.value = 1 | |
def load(self, val): | |
for i in range(self.width - 1, -1, -1): | |
self.data.value = (val >> i) & 1 | |
sleep(.005) | |
_pulse(self.sclk) | |
_pulse(self.rclk) | |
if __name__ == '__main__': | |
shift = ShiftRegister(width=16) | |
atexit.register(shift.close) | |
print('Type any number (use 0xf and 0o7 for hexadecimal and octal input)') | |
while True: | |
try: | |
val = input('> ') | |
if val.lower() in ('quit', 'q'): | |
break | |
val = int(val, | |
16 if val.startswith('0x') else | |
8 if val.startswith('0o') else | |
10) | |
shift.clear() | |
shift.load(val) | |
except EOFError: | |
break | |
except ValueError as e: | |
print(e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment