From 791252066a1cc889ef65efd6280cb275930f71c6 Mon Sep 17 00:00:00 2001 From: "Lee A. Butler" Date: Wed, 26 Jun 2019 21:51:18 -0400 Subject: [PATCH 1/5] bitcount argument to shift_out --- simpleio.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/simpleio.py b/simpleio.py index 8b1c3e8..2f349b7 100644 --- a/simpleio.py +++ b/simpleio.py @@ -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 @@ -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: @@ -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< Date: Thu, 27 Jun 2019 20:48:53 -0400 Subject: [PATCH 2/5] argument to ValueError exception --- simpleio.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simpleio.py b/simpleio.py index 2f349b7..9583fc0 100644 --- a/simpleio.py +++ b/simpleio.py @@ -179,7 +179,7 @@ def shift_out(data_pin, clock, value, msb_first=True, bitcount=8): time.sleep(1.0) """ if bitcount < 0: - raise ValueError + raise ValueError('bitcount must be positive') if msb_first: f = lambda : range(bitcount-1, -1, -1) From dcda5b7593619723ab651c16631c5854f4de5bd7 Mon Sep 17 00:00:00 2001 From: "Lee A. Butler" Date: Sat, 13 Jul 2019 22:18:44 -0400 Subject: [PATCH 3/5] pylint fixes --- simpleio.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/simpleio.py b/simpleio.py index 9583fc0..0fdc4a9 100644 --- a/simpleio.py +++ b/simpleio.py @@ -182,11 +182,11 @@ def shift_out(data_pin, clock, value, msb_first=True, bitcount=8): raise ValueError('bitcount must be positive') if msb_first: - f = lambda : range(bitcount-1, -1, -1) + bitsequence = lambda: range(bitcount-1, -1, -1) else: - f = lambda : range(0, bitcount) + bitsequence = lambda: range(0, bitcount) - for i in f(): + for i in bitsequence(): tmpval = bool(value & (1< Date: Sat, 13 Jul 2019 22:37:34 -0400 Subject: [PATCH 4/5] bitcount 0..32 range --- simpleio.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/simpleio.py b/simpleio.py index 0fdc4a9..2e6a094 100644 --- a/simpleio.py +++ b/simpleio.py @@ -178,8 +178,8 @@ def shift_out(data_pin, clock, value, msb_first=True, bitcount=8): latchpin.value = True time.sleep(1.0) """ - if bitcount < 0: - raise ValueError('bitcount must be positive') + if bitcount < 0 or bitcount > 32: + raise ValueError('bitcount must be in range 0..32 inclusive') if msb_first: bitsequence = lambda: range(bitcount-1, -1, -1) From 9779fcfda7ee19cabba90fd08757007ac5b35ffc Mon Sep 17 00:00:00 2001 From: "Lee A. Butler" Date: Sat, 13 Jul 2019 22:49:13 -0400 Subject: [PATCH 5/5] delete spaces on empty lines --- simpleio.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/simpleio.py b/simpleio.py index 2e6a094..42fca14 100644 --- a/simpleio.py +++ b/simpleio.py @@ -180,12 +180,12 @@ def shift_out(data_pin, clock, value, msb_first=True, bitcount=8): """ if bitcount < 0 or bitcount > 32: raise ValueError('bitcount must be in range 0..32 inclusive') - + if msb_first: bitsequence = lambda: range(bitcount-1, -1, -1) else: bitsequence = lambda: range(0, bitcount) - + for i in bitsequence(): tmpval = bool(value & (1<