@@ -126,7 +126,7 @@ def shift_in(data_pin, clock, msb_first=True):
126
126
i += 1
127
127
return value
128
128
129
- def shift_out (data_pin , clock , value , msb_first = True ):
129
+ def shift_out (data_pin , clock , value , msb_first = True , bitcount = 8 ):
130
130
"""
131
131
Shifts out a byte of data one bit at a time. Data gets written to a data
132
132
pin. Then, the clock pulses hi then low
@@ -138,6 +138,7 @@ def shift_out(data_pin, clock, value, msb_first=True):
138
138
:param ~digitalio.DigitalInOut clock: toggled once the data pin is set
139
139
:param bool msb_first: True when the first bit is most significant
140
140
:param int value: byte to be shifted
141
+ :param unsigned bitcount: number of bits to shift
141
142
142
143
Example for Metro M0 Express:
143
144
@@ -177,14 +178,17 @@ def shift_out(data_pin, clock, value, msb_first=True):
177
178
latchpin.value = True
178
179
time.sleep(1.0)
179
180
"""
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
188
192
# toggle clock pin True/False
189
193
clock .value = True
190
194
clock .value = False
0 commit comments