python-peripheryを使ってみる

GPIO出力ポートの拡張(74HC595)

2G-IOTのディジタルポートは15ポートしか使えません。
そこで、定番の74HC595を使って出力ポートを拡張してみます。
以下のコードで74HC595経由でLチカが動きます。
結線はコメント欄を参照してください。
'''
python-periphery hc595 sample

2G_IOT---HC595
--------------
Pin#29---SER(SerialDataInput)
Pin#31---SRCLK
Pin#33---RCLK
---------Qa----- LED1
---------Qb----- LED2
---------Qc----- LED3
---------Qd----- LED4
---------Qe----- LED5
---------Qf----- LED6
---------Qg----- LED7
---------Qh----- LED8
---------Qh'---- Next HC595(Daisy chaining more than 74HC595s)
3.3V-----Vcc
3.3V-----BAR(SRCLR)
GND------GND
GND------BAR(OE)

'''
#!/usr/bin/python
#-*- encoding: utf-8 -*-

from periphery import GPIO
import time
import sys

def hc595_write(data):
  print 'hc595_write date=0x{0:02x}'.format(data)
  mask=0x80
  latch_out.write(False)
  for x in range(0,8):
    flag = data & mask
    if (flag == 0):
#      print 'flag=',flag,'mask=',mask,'False'
      data_out.write(False)
    if (flag != 0):
#      print 'flag=',flag,'mask=',mask,'True'
      data_out.write(True)
    clock_out.write(True)
    clock_out.write(False)
    mask = mask >> 1
  latch_out.write(True)

data_out = GPIO(122, "out")  #Pin#29
clock_out = GPIO(123, "out") #Pin#31
latch_out = GPIO(124, "out") #Pin#33

data=0x01
for x in range(0,8):
  hc595_write(data)
  data = data << 1
  time.sleep(2)
 
data_out.close()
clock_out.close()
latch_out.close()

こ ちらのページに74HC595のディジーチェーンの回路が紹介されています。
ディジーチェーンすることで、最大32ポートまで出力ポートを拡張することができます。

こちらで公開されてい るRaspberry用のライブラリを移植してみました。
'''
python-periphery hc595 sample

2G_IOT---HC595
--------------
Pin#29---SER(SerialDataInput)
Pin#31---SRCLK
Pin#33---RCLK
---------Qa----- LED1
---------Qb----- LED2
---------Qc----- LED3
---------Qd----- LED4
---------Qe----- LED5
---------Qf----- LED6
---------Qg----- LED7
---------Qh----- LED8
---------Qh'---- Next HC595(Daisy chaining more than 74HC595s)
3.3V-----Vcc
3.3V-----BAR(SRCLR)
GND------GND
GND------BAR(OE)

'''
#!/usr/bin/python
#-*- encoding: utf-8 -*-
#import
from periphery import GPIO
from time import sleep

class ShiftRegister:
    register_type = '74HC595'

    """
    data_pin => pin 14 on the 74HC595
    latch_pin => pin 12 on the 74HC595
    clock_pin => pin 11 on the 74HC595
    """
    def __init__(self, data_pin, latch_pin, clock_pin):
        self.data_pin = data_pin
        self.latch_pin = latch_pin
        self.clock_pin = clock_pin

        self.gpio_data_pin = GPIO(self.data_pin,"out")
        self.gpio_latch_pin = GPIO(self.latch_pin,"out")
        self.gpio_clock_pin = GPIO(self.clock_pin,"out")

        self.outputs = [0] * 8

    """
    output_number => Value from 0 to 7 pointing to the output pin on the 74HC595
    0 => Q0 pin 15 on the 74HC595
    1 => Q1 pin 1 on the 74HC595
    2 => Q2 pin 2 on the 74HC595
    3 => Q3 pin 3 on the 74HC595
    4 => Q4 pin 4 on the 74HC595
    5 => Q5 pin 5 on the 74HC595
    6 => Q6 pin 6 on the 74HC595
    7 => Q7 pin 7 on the 74HC595
    value => a state to pass to the pin, could be HIGH or LOW
    """
    def setOutput(self, output_number, value):
        try:
            self.outputs[output_number] = value
        except IndexError:
            raise ValueError("Invalid output number. Can be only an int from 0 to 7")

    def setOutputs(self, outputs):
        if 8 != len(outputs):
            raise ValueError("setOutputs must be an array with 8 elements")

        self.outputs = outputs

    def showOutputs(self):
        print self.outputs

    def latch(self):
        self.gpio_latch_pin.write(False)

        for i in range(7, -1, -1):
            self.gpio_clock_pin.write(False)
            if (self.outputs[i] == 0):
              self.gpio_data_pin.write(False)
            if (self.outputs[i] == 1):
              self.gpio_data_pin.write(True)
            self.gpio_clock_pin.write(True)

        self.gpio_latch_pin.write(True)

if __name__ == '__main__':
  data_pin = 122 #pin 14 on the 75HC595
  latch_pin = 123 #pin 12 on the 75HC595
  clock_pin = 124 #pin 11 on the 75HC595

  shift_register = ShiftRegister(data_pin, latch_pin, clock_pin)

  # Set all LOW
  shift_register.setOutputs([0,0,0,0,0,0,0,0])
  shift_register.showOutputs()
  # Display
  shift_register.latch()

  for x in range(8):
    # Set some output individually
    shift_register.setOutput(x, 1)
    shift_register.showOutputs()
    # Display
    shift_register.latch()
    sleep(1)

  for x in range(8):
    # Set some output individually
    shift_register.setOutput(x, 0)
    shift_register.showOutputs()
    # Display
    shift_register.latch()
    sleep(1)

続く...