├── .gitignore ├── LICENSE ├── README.md ├── __init__.py ├── setup.py └── shiftpi.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012 Marian Ignev 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ShiftPi 2 | ======= 3 | 4 | ShiftPi is the easiest way to work with `74HC595` or `TPIC6C595` shift registers on your Raspberry Pi in Arduino style :). If you are an Arduino fan ... you will love it :) This library is inspired from this article: [Can you move over? The 74HC595 8 bit shift register](http://bildr.org/2011/02/74hc595/) 5 | 6 | # How to connect Pi with `74HC595` 7 | ![Scheme](http://i.picresize.com/images/2014/03/11/xM26G.jpg) 8 | 9 | # How to connect Pi with `TPIC6C595` 10 | ![Sheme](http://i.picresize.com/images/2016/11/29/h7l8n.png) 11 | 12 | - Note how the LED's are reversed in this diagram, this is because the ouputs on the `TPIC6C595` are current drains, that is to say they "pull" current through the LED, rather then "push" the current as the `74HC595` does 13 | 14 | #Quick Example ... Arduino style :) 15 | So ... Let's have a look how can we create some magic :) 16 | 17 | from shiftpi import HIGH, LOW, digitalWrite, delay 18 | 19 | while True: 20 | digitalWrite(1, HIGH) 21 | delay(1000) 22 | digitalWrite(1, LOW) 23 | delay(1000) 24 | 25 | Looks familiar? :)) The Blink example ?? Do you like it? :) Cool :)) 26 | 27 | # How can I use the library? 28 | 29 | So if you are not familiar with python you can use the library in 2 ways: 30 | 31 | import shiftpi 32 | 33 | # turns shift register's pin 1 to HIGH 34 | shiftpi.digitalWrite(1, shiftpi.HIGH) 35 | shiftpi.delay(1000) 36 | 37 | # turns shift register's pin 1 to LOW 38 | shiftpi.digitalWrite(1, shiftpi.LOW) 39 | shiftpi.delay(1000) 40 | 41 | # turns all shift register pins to HIGH 42 | shiftpi.digitalWrite(shiftpi.ALL, shiftpi.LOW) 43 | shiftpi.delay(1000) 44 | 45 | or you can use the library methods as shown in the first example, with importing all methods from the `shiftpi` library. If I want to set all shift register's pins to `HIGH` I can do the following: 46 | 47 | from shiftpi import HIGH, LOW, ALL, digitalWrite, delay 48 | 49 | digitalWrite(ALL, HIGH) 50 | 51 | That's it! :) 52 | 53 | # The API look and feel 54 | 55 | In all examples below i will use the second way of importing methods. (from shiftpi import bla bla) 56 | 57 | ## Constants 58 | 59 | * `HIGH` # this is mode of pin 60 | * `LOW` # this is mode of pin 61 | * `ALL` # you can use it as pin number. 62 | 63 | 64 | ## pinsSetup(dict) 65 | By the way by default `SER`, `RCLK`, `SRCLK` are set as follow: 66 | 67 | * SER = 25 (GPIO RPI) #pin 14 on the 75HC595 68 | * RCLK = 24 (GPIO RPI) #pin 12 on the 75HC595 69 | * SRCLK = 23 (GPIO RPI) #pin 11 on the 75HC595 70 | 71 | But ... if you want to use other GPIOs ... you can define them with the following method: 72 | 73 | pinsSetup({"ser": SER_PIN, "rclk": RCLK_PIN, "srclk": SRCLK_PIN}) # that's it! 74 | 75 | ## shiftRegisters(num) 76 | By default shiftpi works with 1 shift register, but if you use more than one shift register you can set the number of all shift registers you use, with this method: 77 | 78 | shiftRegisters(3) # if you use 3 shift registers 79 | 80 | ## startupMode(mode or dict, execute) 81 | By default if you use the library for first time all your pins will be set to `LOW`, but if you want to use some other different configuration you can try with this method. 82 | 83 | # this will set all your pins to `HIGH` but will not execute them. 84 | startupMode(HIGH) 85 | 86 | # if you want to set all your pins to `HIGH` and it will execute them automaticaly ... try this 87 | startupMode(HIGH, True) # works just like digitalWrite(ALL, HIGH) 88 | 89 | # if you want to set only few pins to some mode ... try this 90 | startupMode({1: HIGH, 4: HIGH, 6: HIGH}) 91 | 92 | # and if you want to execute them ... 93 | startupMode({1: HIGH, 4: HIGH, 6: HIGH}, True) 94 | 95 | ## digitalWrite(pin, mode) 96 | The digitalWrite method is inspired from the Arduino API and it works as you expect 97 | 98 | If you want to turn 3rd pin of the shift register to `HIGH` 99 | 100 | digitalWrite(3, HIGH) 101 | 102 | or if you want to turn on all pins of the shift register to `HIGH` 103 | 104 | digitalWrite(ALL, HIGH) # will turn on all pins to HIGH 105 | 106 | ## delay(millis) 107 | If you are an Arduino guy you just need to know that this method works just like the Arduino's `delay` 108 | 109 | delay(30) # will sleep for 30 millis 110 | 111 | #Requirements 112 | 113 | * Raspberry Pi 114 | * Python 2.6+ and Python development tools 115 | * RPi.GPIO (latest version recommended) 116 | 117 | # Installation 118 | 119 | First install RPi.GPIO library and Python development tools: 120 | 121 | # sudo apt-get update && sudo apt-get -y install python-rpi.gpio python-dev 122 | 123 | Get `shiftpi` source and install it: 124 | 125 | # git clone git://github.com/mignev/shiftpi.git 126 | # sudo python shiftpi/setup.py install 127 | 128 | After installation, you can remove the folder as it is no longer needed by running: 129 | 130 | # sudo rm -rf shiftpi 131 | 132 | # Testing 133 | TODO 134 | 135 | # Contributing 136 | Fork the [shiftpi repo on GitHub](https://github.com/mignev/shiftpi), make your super duper awesome changes :) and send me a Pull Request. :) 137 | 138 | # Contributors 139 | 140 | - [Marian Ignev](https://github.com/mignev) 141 | - [Nathan Bookham](https://github.com/inversesandwich) 142 | - [Felix Breidenstein](https://github.com/f-breidenstein) 143 | 144 | # Useful links 145 | * [How Shift Registers work?](http://www.youtube.com/watch?feature=player_embedded&v=6fVbJbNPrEU#!) (Video) 146 | 147 | #Copyright 148 | Copyright (c) 2013 Marian Ignev. See LICENSE for further details. 149 | 150 | 151 | [![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/mignev/shiftpi/trend.png)](https://bitdeli.com/free "Bitdeli Badge") 152 | 153 | -------------------------------------------------------------------------------- /__init__.py: -------------------------------------------------------------------------------- 1 | version = "0.1" 2 | version_info = (0, 1) -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from __future__ import with_statement 4 | from shiftpi import version as shiftpi_version 5 | import distutils.core 6 | import os 7 | 8 | # Importing setuptools adds some features like "setup.py develop", but 9 | # it's optional so swallow the error if it's not there. 10 | try: 11 | import setuptools 12 | except ImportError: 13 | pass 14 | 15 | def read(fname): 16 | return open(os.path.join(os.path.dirname(__file__), fname)).read() 17 | 18 | distutils.core.setup(name='shiftpi', 19 | version=shiftpi_version, 20 | description="ShiftPi is the easiest way to work with 74HC595 shift registers on your Raspberry Pi in Arduino style :). If you are an Arduino fan ... you'l love it :)", 21 | author='Marian Ignev', 22 | author_email='m@ignev.net', 23 | url='http://m.ignev.net/code/shiftpi', 24 | packages=['shiftpi'], 25 | long_description=read('README.md'), 26 | # install_requires = ['RPi.GPIO'], 27 | classifiers=[ 28 | 'Operating System :: POSIX', 29 | 'Operating System :: POSIX :: BSD', 30 | 'Operating System :: POSIX :: Linux', 31 | 'Operating System :: Unix', 32 | 'Programming Language :: Python', 33 | 'Programming Language :: Python :: 2', 34 | 'Programming Language :: Python :: 2.7', 35 | 'Topic :: System :: Shells', 36 | 'Topic :: Utilities', 37 | ], 38 | ) -------------------------------------------------------------------------------- /shiftpi.py: -------------------------------------------------------------------------------- 1 | ''' 2 | A library that allows simple access to 74HC595 shift registers on a Raspberry Pi using any digital I/O pins. 3 | ''' 4 | 5 | 6 | import RPi.GPIO as GPIO 7 | from time import sleep 8 | 9 | GPIO.setmode(GPIO.BCM) 10 | 11 | version = "0.2" 12 | version_info = (0, 2) 13 | 14 | # Define MODES 15 | ALL = -1 16 | HIGH = 1 17 | LOW = 0 18 | 19 | # Define pins 20 | _SER_pin = 25 #pin 14 on the 75HC595 21 | _RCLK_pin = 24 #pin 12 on the 75HC595 22 | _SRCLK_pin = 23 #pin 11 on the 75HC595 23 | 24 | # is used to store states of all pins 25 | _registers = list() 26 | 27 | #How many of the shift registers - you can change them with shiftRegisters method 28 | _number_of_shiftregisters = 1 29 | 30 | def pinsSetup(**kwargs): 31 | ''' 32 | Allows the user to define custom pins 33 | ''' 34 | global _SER_pin, _RCLK_pin, _SRCLK_pin 35 | 36 | custompins = 0 37 | serpin = _SER_pin 38 | rclkpin = _RCLK_pin 39 | srclkpin = _SRCLK_pin 40 | 41 | if len(kwargs) > 0: 42 | custompins = 1 43 | 44 | _SER_pin = kwargs.get('ser', _SER_pin) 45 | _RCLK_pin = kwargs.get('rclk', _RCLK_pin) 46 | _SRCLK_pin = kwargs.get('srclk', _SRCLK_pin) 47 | 48 | if custompins: 49 | if _SER_pin != serpin or _RCLK_pin != rclkpin or _SRCLK_pin != srclkpin: 50 | GPIO.setwarnings(True) 51 | else: 52 | GPIO.setwarnings(False) 53 | 54 | GPIO.setup(_SER_pin, GPIO.OUT) 55 | GPIO.setup(_RCLK_pin, GPIO.OUT) 56 | GPIO.setup(_SRCLK_pin, GPIO.OUT) 57 | 58 | def startupMode(mode, execute = False): 59 | ''' 60 | Allows the user to change the default state of the shift registers outputs 61 | ''' 62 | if isinstance(mode, int): 63 | if mode is HIGH or mode is LOW: 64 | _all(mode, execute) 65 | else: 66 | raise ValueError("The mode can be only HIGH or LOW or Dictionary with specific pins and modes") 67 | elif isinstance(mode, dict): 68 | for pin, mode in mode.iteritems(): 69 | _setPin(pin, mode) 70 | if execute: 71 | _execute() 72 | else: 73 | raise ValueError("The mode can be only HIGH or LOW or Dictionary with specific pins and modes") 74 | 75 | 76 | def shiftRegisters(num): 77 | ''' 78 | Allows the user to define the number of shift registers are connected 79 | ''' 80 | global _number_of_shiftregisters 81 | _number_of_shiftregisters = num 82 | _all(LOW) 83 | 84 | def digitalWrite(pin, mode): 85 | ''' 86 | Allows the user to set the state of a pin on the shift register 87 | ''' 88 | if pin == ALL: 89 | _all(mode) 90 | else: 91 | if len(_registers) == 0: 92 | _all(LOW) 93 | 94 | _setPin(pin, mode) 95 | _execute() 96 | 97 | def delay(millis): 98 | ''' 99 | Used for creating a delay between commands 100 | ''' 101 | millis_to_seconds = float(millis)/1000 102 | return sleep(millis_to_seconds) 103 | 104 | def _all_pins(): 105 | return _number_of_shiftregisters * 8 106 | 107 | def _all(mode, execute = True): 108 | all_shr = _all_pins() 109 | 110 | for pin in range(0, all_shr): 111 | _setPin(pin, mode) 112 | if execute: 113 | _execute() 114 | 115 | return _registers 116 | 117 | def _setPin(pin, mode): 118 | try: 119 | _registers[pin] = mode 120 | except IndexError: 121 | _registers.insert(pin, mode) 122 | 123 | def _execute(): 124 | all_pins = _all_pins() 125 | GPIO.output(_RCLK_pin, GPIO.LOW) 126 | 127 | for pin in range(all_pins -1, -1, -1): 128 | GPIO.output(_SRCLK_pin, GPIO.LOW) 129 | 130 | pin_mode = _registers[pin] 131 | 132 | GPIO.output(_SER_pin, pin_mode) 133 | GPIO.output(_SRCLK_pin, GPIO.HIGH) 134 | 135 | GPIO.output(_RCLK_pin, GPIO.HIGH) 136 | 137 | pinsSetup() 138 | --------------------------------------------------------------------------------