Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
*.pyc
.svn/entries
.svn/format
*.svn-base
.svn/wc.db
*.db-journal
Binary file added .vs/shiftpi/v14/.suo
Binary file not shown.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ or you can use the library methods as shown in the first example, with importing

digitalWrite(ALL, HIGH)

That's it! :)

Check out the binary counter example program (binCount.py)

# The API look and feel

Expand Down
18 changes: 18 additions & 0 deletions binCount.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#testing shiftpi
#this program uses the outputs to a do a binary counter from 0 to the total number of bits

from shiftpi import HIGH, LOW, digitalWrite, delay

bits = 8 #hard coded to 8 (one chip) for now

#loop from 0 to the max value of the bits available
for x in range(0, 2 ** bits):
temp = x
print 'setting binary ' + str(temp)
for b in range(bits - 1, -1,-1):
if temp >= 2 ** b:
y -= 2 ** b
digitalWrite((bits-1)- b, HIGH)
else:
digitalWrite((bits-1)- b, LOW)
delay(250)
51 changes: 51 additions & 0 deletions shiftTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

import shiftpi

#shiftpi.pinsSetup({"ser": 16, "rclk": 20, "srclk": 21})
#shiftpi.startupMode({1: shiftpi.HIGH, 4: shiftpi.HIGH, 6: shiftpi.HIGH}, True)
shiftpi.delay(1000)
shiftpi.digitalWrite(shiftpi.ALL, shiftpi.LOW)
shiftpi.delay(1000)
for x in range(0, 9):
shiftpi.digitalWrite(0, shiftpi.HIGH)
shiftpi.delay(500)
# turns shift register's pin 1 to LOW
shiftpi.digitalWrite(0, shiftpi.LOW)
shiftpi.delay(500)
shiftpi.digitalWrite(1, shiftpi.HIGH)
shiftpi.delay(500)
# turns shift register's pin 1 to LOW
shiftpi.digitalWrite(1, shiftpi.LOW)
shiftpi.delay(500)
shiftpi.digitalWrite(2, shiftpi.HIGH)
shiftpi.delay(500)
# turns shift register's pin 1 to LOW
shiftpi.digitalWrite(2, shiftpi.LOW)
shiftpi.delay(500)
shiftpi.digitalWrite(3, shiftpi.HIGH)
shiftpi.delay(500)
# turns shift register's pin 1 to LOW
shiftpi.digitalWrite(3, shiftpi.LOW)
shiftpi.delay(500)
shiftpi.digitalWrite(4, shiftpi.HIGH)
shiftpi.delay(500)
# turns shift register's pin 1 to LOW
shiftpi.digitalWrite(4, shiftpi.LOW)
shiftpi.delay(500)
shiftpi.digitalWrite(5, shiftpi.HIGH)
shiftpi.delay(500)
# turns shift register's pin 1 to LOW
shiftpi.digitalWrite(5, shiftpi.LOW)
shiftpi.delay(500)
shiftpi.digitalWrite(6, shiftpi.HIGH)
shiftpi.delay(500)
# turns shift register's pin 1 to LOW
shiftpi.digitalWrite(6, shiftpi.LOW)
shiftpi.delay(500)
shiftpi.digitalWrite(7, shiftpi.HIGH)
shiftpi.delay(500)
# turns shift register's pin 1 to LOW
shiftpi.digitalWrite(7, shiftpi.LOW)
shiftpi.delay(500)


237 changes: 124 additions & 113 deletions shiftpi.py
Original file line number Diff line number Diff line change
@@ -1,137 +1,148 @@
'''
A library that allows simple access to 74HC595 shift registers on a Raspberry Pi using any digital I/O pins.
A library that allows simple access to 74HC595 shift registers on a Raspberry
Pi using any digital I/O pins.
'''


import RPi.GPIO as GPIO
#import RPi.GPIO as GPIO
from time import sleep

GPIO.setmode(GPIO.BCM)

version = "0.2"
version_info = (0, 2)

# Define MODES
ALL = -1
ALL = -1
HIGH = 1
LOW = 0

# Define pins
_SER_pin = 25 #pin 14 on the 75HC595
_RCLK_pin = 24 #pin 12 on the 75HC595
_SRCLK_pin = 23 #pin 11 on the 75HC595

# is used to store states of all pins
_registers = list()

#How many of the shift registers - you can change them with shiftRegisters method
_number_of_shiftregisters = 1

def pinsSetup(**kwargs):
'''
Allows the user to define custom pins
'''
global _SER_pin, _RCLK_pin, _SRCLK_pin

custompins = 0
serpin = _SER_pin
rclkpin = _RCLK_pin
srclkpin = _SRCLK_pin

if len(kwargs) > 0:
custompins = 1

_SER_pin = kwargs.get('ser', _SER_pin)
_RCLK_pin = kwargs.get('rclk', _RCLK_pin)
_SRCLK_pin = kwargs.get('srclk', _SRCLK_pin)

if custompins:
if _SER_pin != serpin or _RCLK_pin != rclkpin or _SRCLK_pin != srclkpin:
GPIO.setwarnings(True)
else:
GPIO.setwarnings(False)

GPIO.setup(_SER_pin, GPIO.OUT)
GPIO.setup(_RCLK_pin, GPIO.OUT)
GPIO.setup(_SRCLK_pin, GPIO.OUT)

def startupMode(mode, execute = False):
'''
Allows the user to change the default state of the shift registers outputs
'''
if isinstance(mode, int):
if mode is HIGH or mode is LOW:
_all(mode, execute)
LOW = 0

class Shiftpi:
def __init__(self, GPIO=None):

if GPIO is None:
import RPi.GPIO as GPIO
self.GPIO = GPIO

self.GPIO.setmode(self.GPIO.BCM)

# Define pins
self._SER_pin = 25 # pin 14 on the 75HC595
self._RCLK_pin = 24 # pin 12 on the 75HC595
self._SRCLK_pin = 23 # pin 11 on the 75HC595

# is used to store states of all pins
self._registers = list()

'''
How many of the shift registers - you can change them with
shiftRegisters method
'''
self._number_of_shiftregisters = 1
self.pinsSetup()

def pinsSetup(self, **kwargs):
'''
Allows the user to define custom pins
'''
custompins = 0
serpin = self._SER_pin
rclkpin = self._RCLK_pin
srclkpin = self._SRCLK_pin

if len(kwargs) > 0:
custompins = 1

self._SER_pin = kwargs.get('ser', self._SER_pin)
self._RCLK_pin = kwargs.get('rclk', self._RCLK_pin)
self._SRCLK_pin = kwargs.get('srclk', self._SRCLK_pin)

if custompins:
if ((self._SER_pin != serpin or
self._RCLK_pin != rclkpin or
self._SRCLK_pin != srclkpin)):
self.GPIO.setwarnings(True)
else:
raise ValueError("The mode can be only HIGH or LOW or Dictionary with specific pins and modes")
elif isinstance(mode, dict):
for pin, mode in mode.iteritems():
_setPin(pin, mode)
if execute:
_execute()
else:
raise ValueError("The mode can be only HIGH or LOW or Dictionary with specific pins and modes")


def shiftRegisters(num):
'''
Allows the user to define the number of shift registers are connected
'''
global _number_of_shiftregisters
_number_of_shiftregisters = num
_all(LOW)

def digitalWrite(pin, mode):
'''
Allows the user to set the state of a pin on the shift register
'''
if pin == ALL:
_all(mode)
else:
if len(_registers) == 0:
_all(LOW)

_setPin(pin, mode)
_execute()
self.GPIO.setwarnings(False)

self.GPIO.setup(self._SER_pin, self.GPIO.OUT)
self.GPIO.setup(self._RCLK_pin, self.GPIO.OUT)
self.GPIO.setup(self._SRCLK_pin, self.GPIO.OUT)

def startupMode(self, mode, execute=False):
'''
Allows the user to change the default state of the shift registers
outputs
'''
if isinstance(mode, int):
if mode is HIGH or mode is LOW:
self._all(mode, execute)
else:
raise ValueError('''The mode can be only HIGH or LOW or
Dictionary with specific pins and modes''')
elif isinstance(mode, dict):
for pin, mode in mode.iteritems():
self._setPin(pin, mode)
if execute:
self._execute()
else:
raise ValueError('''The mode can be only HIGH or LOW or Dictionary
with specific pins and modes''')

def shiftRegisters(self, num):
'''
Allows the user to define the number of shift registers are connected
'''
self._number_of_shiftregisters = num
self._all(LOW)

def digitalWrite(self, pin, mode):
'''
Allows the user to set the state of a pin on the shift register
'''
if pin == ALL:
self._all(mode)
else:
if len(self._registers) == 0:
self._all(LOW)

def delay(millis):
'''
Used for creating a delay between commands
'''
millis_to_seconds = float(millis)/1000
return sleep(millis_to_seconds)
self._setPin(pin, mode)
self._execute()

def _all_pins():
return _number_of_shiftregisters * 8
def delay(self, millis):
'''
Used for creating a delay between commands
'''
millis_to_seconds = float(millis)/1000
return sleep(millis_to_seconds)

def _all(mode, execute = True):
all_shr = _all_pins()
def _all_pins(self):
return self._number_of_shiftregisters * 8

for pin in range(0, all_shr):
_setPin(pin, mode)
if execute:
_execute()
def _all(self, mode, execute=True):
all_shr = self._all_pins()

return _registers
for pin in range(0, all_shr):
self._setPin(pin, mode)
if execute:
self._execute()

def _setPin(pin, mode):
try:
_registers[pin] = mode
except IndexError:
_registers.insert(pin, mode)
return self._registers

def _execute():
all_pins = _all_pins()
GPIO.output(_RCLK_pin, GPIO.LOW)
def _setPin(self, pin, mode):
try:
self._registers[pin] = mode
except IndexError:
self._registers.insert(pin, mode)

for pin in range(all_pins -1, -1, -1):
GPIO.output(_SRCLK_pin, GPIO.LOW)
def _execute(self):
all_pins = self._all_pins()
self.GPIO.output(self._RCLK_pin, self.GPIO.LOW)

pin_mode = _registers[pin]
for pin in range(all_pins - 1, -1, -1):
self.GPIO.output(self._SRCLK_pin, self.GPIO.LOW)

GPIO.output(_SER_pin, pin_mode)
GPIO.output(_SRCLK_pin, GPIO.HIGH)
pin_mode = self._registers[pin]

GPIO.output(_RCLK_pin, GPIO.HIGH)
self.GPIO.output(self._SER_pin, pin_mode)
self.GPIO.output(self._SRCLK_pin, self.GPIO.HIGH)

pinsSetup()
self.GPIO.output(self._RCLK_pin, self.GPIO.HIGH)
33 changes: 33 additions & 0 deletions shiftpi.pyproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{d9a3d37c-2fa6-4b80-bdbc-587f98c93e5b}</ProjectGuid>
<ProjectHome />
<StartupFile>binCount.py</StartupFile>
<SearchPath />
<WorkingDirectory>.</WorkingDirectory>
<OutputPath>.</OutputPath>
<ProjectTypeGuids>{888888a0-9f3d-457c-b088-3a5042f75d52}</ProjectTypeGuids>
<LaunchProvider>Standard Python launcher</LaunchProvider>
<InterpreterId />
<InterpreterVersion />
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Debug'" />
<PropertyGroup Condition="'$(Configuration)' == 'Release'" />
<PropertyGroup>
<VisualStudioVersion Condition=" '$(VisualStudioVersion)' == '' ">10.0</VisualStudioVersion>
<PtvsTargetsFile>$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Python Tools\Microsoft.PythonTools.targets</PtvsTargetsFile>
</PropertyGroup>
<ItemGroup>
<Compile Include="binCount.py" />
<Compile Include="setup.py" />
<Compile Include="shiftpi.py" />
<Compile Include="shiftTest.py" />
<Compile Include="testonepin.py" />
<Compile Include="__init__.py" />
</ItemGroup>
<Import Project="$(PtvsTargetsFile)" Condition="Exists($(PtvsTargetsFile))" />
<Import Project="$(MSBuildToolsPath)\Microsoft.Common.targets" Condition="!Exists($(PtvsTargetsFile))" />
</Project>
Loading