Skip to content

ShiftIn/ShiftOut #11

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
Aug 1, 2017
Merged
Changes from 3 commits
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
67 changes: 67 additions & 0 deletions simpleio.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,75 @@

import digitalio
import math
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is needed by neopixel below.

import time

from neopixel_write import neopixel_write

def shiftIn(dataPin, clock, msb_first=True):
"""
Shifts in a byte of data one bit at a time. Starts from either the LSB or
MSB.

:param ~digitalio.DigitalInOut dataPin: pin on which to input each bit
:param ~digitalio.DigitalInOut clock: toggles to signal dataPin reads
:param bool msb_first: True when the first bit is most significant
:return: returns the value read
:rtype: int
"""

value = 0
i = 0

for i in range(0, 8):
clock.value = True
if msb_first:
value |= ((dataPin.value) << (7-i))
else:
value |= ((dataPin.value) << i)
clock.value = False
i+=1
return value

def shiftOut(dataPin, clock, value, msb_first=True):
"""
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

:param ~digitalio.DigitalInOut dataPin: value bits get output on this pin
: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

Example for Metro M0 Express:

.. code-block:: python

import digitalio
import simpleio
from board import *
clock = digitalio.DigitalInOut(D12)
dataPin = digitalio.DigitalInOut(D11)
clock.direction = digitalio.Direction.OUTPUT
dataPin.direction = digitalio.Direction.OUTPUT

while True:
valueSend = 500
# shifting out least significant bits
shiftOut(dataPin, clock, (valueSend>>8), msb_first = False)
shiftOut(dataPin, clock, valueSend, msb_first = False)
# shifting out most significant bits
shiftOut(dataPin, clock, (valueSend>>8))
shiftOut(dataPin, clock, valueSend)
"""
value = value&0xFF
for i in range(0, 8):
if msb_first:
tmpval = bool(value & (1 << (7-i)))
dataPin.value = tmpval
else:
tmpval = bool((value & (1 << i)))
dataPin.value = tmpval

class DigitalOut:
"""
Simple digital output that is valid until soft reset.
Expand Down