From ec7bc3665993ee4747844de7dc3cb1bd42a75c42 Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Thu, 21 Jul 2022 14:11:26 +0100 Subject: [PATCH] MicroPython: Add ShiftRegister class. --- micropython/modules_py/pimoroni.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/micropython/modules_py/pimoroni.py b/micropython/modules_py/pimoroni.py index 8c8504f8..40adec99 100644 --- a/micropython/modules_py/pimoroni.py +++ b/micropython/modules_py/pimoroni.py @@ -196,3 +196,31 @@ class Buzzer: self.pwm.freq(freq) self.pwm.duty_u16(int(65535 * duty)) return True + + +class ShiftRegister: + def __init__(self, clk, lat, dat): + self.clk = Pin(clk, Pin.OUT) + self.lat = Pin(lat, Pin.OUT) + self.dat = Pin(dat, Pin.IN) + + def __iter__(self): + self.lat.off() + self.lat.on() + for _ in range(8): + yield self.dat.value() + self.clk.on() + self.clk.off() + + def __getitem__(self, k): + return list(self)[k] + + def read(self): + out = 0 + for bit in self: + out <<= 1 + out += bit + return out + + def is_set(self, mask): + return self.read() & mask == mask