Skip to content

bitcount argument to shift_out() #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 16, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions simpleio.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def shift_in(data_pin, clock, msb_first=True):
i += 1
return value

def shift_out(data_pin, clock, value, msb_first=True):
def shift_out(data_pin, clock, value, msb_first=True, bitcount=8):
"""
Shifts out a byte of data one bit at a time. Data gets written to a data
pin. Then, the clock pulses hi then low
Expand All @@ -138,6 +138,7 @@ def shift_out(data_pin, clock, value, msb_first=True):
:param ~digitalio.DigitalInOut clock: toggled once the data pin is set
:param bool msb_first: True when the first bit is most significant
:param int value: byte to be shifted
:param unsigned bitcount: number of bits to shift

Example for Metro M0 Express:

Expand Down Expand Up @@ -177,14 +178,17 @@ def shift_out(data_pin, clock, value, msb_first=True):
latchpin.value = True
time.sleep(1.0)
"""
value = value&0xFF
for i in range(0, 8):
if msb_first:
tmpval = bool(value & (1 << (7-i)))
data_pin.value = tmpval
else:
tmpval = bool((value & (1 << i)))
data_pin.value = tmpval
if bitcount < 0:
raise ValueError

if msb_first:
f = lambda : range(bitcount-1, -1, -1)
else:
f = lambda : range(0, bitcount)

for i in f():
tmpval = bool(value & (1<<i))
data_pin.value = tmpval
# toggle clock pin True/False
clock.value = True
clock.value = False
Expand Down