diff --git a/.gitignore b/.gitignore index 0d20b64..5d4f7b4 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,6 @@ *.pyc +.svn/entries +.svn/format +*.svn-base +.svn/wc.db +*.db-journal diff --git a/.vs/shiftpi/v14/.suo b/.vs/shiftpi/v14/.suo new file mode 100644 index 0000000..3c012be Binary files /dev/null and b/.vs/shiftpi/v14/.suo differ diff --git a/README.md b/README.md index eb2d849..f98da1c 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/binCount.py b/binCount.py new file mode 100644 index 0000000..c4e80ed --- /dev/null +++ b/binCount.py @@ -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) \ No newline at end of file diff --git a/shiftTest.py b/shiftTest.py new file mode 100644 index 0000000..30c19de --- /dev/null +++ b/shiftTest.py @@ -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) + + diff --git a/shiftpi.py b/shiftpi.py index 074b453..4828dd6 100644 --- a/shiftpi.py +++ b/shiftpi.py @@ -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) diff --git a/shiftpi.pyproj b/shiftpi.pyproj new file mode 100644 index 0000000..348562c --- /dev/null +++ b/shiftpi.pyproj @@ -0,0 +1,33 @@ + + + + Debug + 2.0 + {d9a3d37c-2fa6-4b80-bdbc-587f98c93e5b} + + binCount.py + + . + . + {888888a0-9f3d-457c-b088-3a5042f75d52} + Standard Python launcher + + + + + + + 10.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Python Tools\Microsoft.PythonTools.targets + + + + + + + + + + + + \ No newline at end of file diff --git a/shiftpi.sln b/shiftpi.sln new file mode 100644 index 0000000..f1bb5af --- /dev/null +++ b/shiftpi.sln @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 14 +VisualStudioVersion = 14.0.25123.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{888888A0-9F3D-457C-B088-3A5042F75D52}") = "shiftpi", "shiftpi.pyproj", "{D9A3D37C-2FA6-4B80-BDBC-587F98C93E5B}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {D9A3D37C-2FA6-4B80-BDBC-587F98C93E5B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D9A3D37C-2FA6-4B80-BDBC-587F98C93E5B}.Release|Any CPU.ActiveCfg = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/testonepin.py b/testonepin.py new file mode 100644 index 0000000..cc4eed3 --- /dev/null +++ b/testonepin.py @@ -0,0 +1,7 @@ +from shiftpi import HIGH, LOW, digitalWrite, delay + +while True: + digitalWrite(1, HIGH) + delay(1000) + digitalWrite(1, LOW) + delay(1000) \ No newline at end of file