Skip to content

Commit 5b20cda

Browse files
authored
Merge pull request #41 from iraytrace/master
bitcount argument to shift_out()
2 parents d1ea1bb + 9779fcf commit 5b20cda

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

simpleio.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def shift_in(data_pin, clock, msb_first=True):
126126
i += 1
127127
return value
128128

129-
def shift_out(data_pin, clock, value, msb_first=True):
129+
def shift_out(data_pin, clock, value, msb_first=True, bitcount=8):
130130
"""
131131
Shifts out a byte of data one bit at a time. Data gets written to a data
132132
pin. Then, the clock pulses hi then low
@@ -138,6 +138,7 @@ def shift_out(data_pin, clock, value, msb_first=True):
138138
:param ~digitalio.DigitalInOut clock: toggled once the data pin is set
139139
:param bool msb_first: True when the first bit is most significant
140140
:param int value: byte to be shifted
141+
:param unsigned bitcount: number of bits to shift
141142
142143
Example for Metro M0 Express:
143144
@@ -177,14 +178,17 @@ def shift_out(data_pin, clock, value, msb_first=True):
177178
latchpin.value = True
178179
time.sleep(1.0)
179180
"""
180-
value = value&0xFF
181-
for i in range(0, 8):
182-
if msb_first:
183-
tmpval = bool(value & (1 << (7-i)))
184-
data_pin.value = tmpval
185-
else:
186-
tmpval = bool((value & (1 << i)))
187-
data_pin.value = tmpval
181+
if bitcount < 0 or bitcount > 32:
182+
raise ValueError('bitcount must be in range 0..32 inclusive')
183+
184+
if msb_first:
185+
bitsequence = lambda: range(bitcount-1, -1, -1)
186+
else:
187+
bitsequence = lambda: range(0, bitcount)
188+
189+
for i in bitsequence():
190+
tmpval = bool(value & (1<<i))
191+
data_pin.value = tmpval
188192
# toggle clock pin True/False
189193
clock.value = True
190194
clock.value = False

0 commit comments

Comments
 (0)