python-peripheryを使ってみる

16X2 LCDへの表示

2G-IOTのディジタルポートは15ポートしか使えません。
そこで、そのうちの6ポートを使って16x2のLCDへの表示を試してみました。
こ ちらで公開されているコードを移植しただけです。
移植作業は30分ほどで終わりました。
'''
python-periphery 1602 LCD sample

I ported from here
https://www.raspberrypi-spy.co.uk/2012/07/16x2-lcd-module-control-using-python/

'''
#!/usr/bin/python
#-*- encoding: utf-8 -*-
#import
from periphery import GPIO
import time
 
# Define GPIO to LCD mapping
GPIO.LCD_RS = GPIO(101, "out")
GPIO.LCD_E  = GPIO(121, "out")
GPIO.LCD_D4 = GPIO(122, "out")
GPIO.LCD_D5 = GPIO(123, "out")
GPIO.LCD_D6 = GPIO(124, "out")
GPIO.LCD_D7 = GPIO(125, "out")
 
# Define some device constants
LCD_WIDTH = 16    # Maximum characters per line
LCD_CHR = True
LCD_CMD = False
 
LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
 
# Timing constants
E_PULSE = 0.0005
E_DELAY = 0.0005
 
def main():
  # Main program block
#  GPIO_setwarnings(False)
#  GPIO_setmode(GPIO_BCM)       # Use BCM GPIO numbers
#  GPIO_setup(LCD_E, GPIO_OUT)  # E
#  GPIO_setup(LCD_RS, GPIO_OUT) # RS
#  GPIO_setup(LCD_D4, GPIO_OUT) # DB4
#  GPIO_setup(LCD_D5, GPIO_OUT) # DB5
#  GPIO_setup(LCD_D6, GPIO_OUT) # DB6
#  GPIO_setup(LCD_D7, GPIO_OUT) # DB7
 
  # Initialise display
  lcd_init()
 
  while True:
 
    # Send some test
    lcd_string("OrangePi 2G-IOT",LCD_LINE_1)
    lcd_string("16x2 LCD Test",LCD_LINE_2)
 
    time.sleep(3) # 3 second delay
 
    # Send some text
    lcd_string("1234567890123456",LCD_LINE_1)
    lcd_string("abcdefghijklmnop",LCD_LINE_2)
 
    time.sleep(3) # 3 second delay
 
def lcd_init():
  # Initialise display
  lcd_byte(0x33,LCD_CMD) # 110011 Initialise
  lcd_byte(0x32,LCD_CMD) # 110010 Initialise
  lcd_byte(0x06,LCD_CMD) # 000110 Cursor move direction
  lcd_byte(0x0C,LCD_CMD) # 001100 Display On,Cursor Off, Blink Off
  lcd_byte(0x28,LCD_CMD) # 101000 Data length, number of lines, font size
  lcd_byte(0x01,LCD_CMD) # 000001 Clear display
  time.sleep(E_DELAY)
 
def lcd_byte(bits, mode):
  # Send byte to data pins
  # bits = data
  # mode = True  for character
  #        False for command
 
  GPIO.LCD_RS.write(mode) # RS
 
  # High bits
  GPIO.LCD_D4.write(False)
  GPIO.LCD_D5.write(False)
  GPIO.LCD_D6.write(False)
  GPIO.LCD_D7.write(False)
  if bits&0x10==0x10:
    GPIO.LCD_D4.write(True)
  if bits&0x20==0x20:
    GPIO.LCD_D5.write(True)
  if bits&0x40==0x40:
    GPIO.LCD_D6.write(True)
  if bits&0x80==0x80:
    GPIO.LCD_D7.write(True)
 
  # Toggle 'Enable' pin
  lcd_toggle_enable()
 
  # Low bits
  GPIO.LCD_D4.write(False)
  GPIO.LCD_D5.write(False)
  GPIO.LCD_D6.write(False)
  GPIO.LCD_D7.write(False)
  if bits&0x01==0x01:
    GPIO.LCD_D4.write(True)
  if bits&0x02==0x02:
    GPIO.LCD_D5.write(True)
  if bits&0x04==0x04:
    GPIO.LCD_D6.write(True)
  if bits&0x08==0x08:
    GPIO.LCD_D7.write(True)
 
  # Toggle 'Enable' pin
  lcd_toggle_enable()
 
def lcd_toggle_enable():
  # Toggle enable
  time.sleep(E_DELAY)
  GPIO.LCD_E.write(True)
  time.sleep(E_PULSE)
  GPIO.LCD_E.write(False)
  time.sleep(E_DELAY)
 
def lcd_string(message,line):
  # Send string to display
 
  message = message.ljust(LCD_WIDTH," ")
 
  lcd_byte(line, LCD_CMD)
 
  for i in range(LCD_WIDTH):
    lcd_byte(ord(message[i]),LCD_CHR)
 
if __name__ == '__main__':
 
  try:
    main()
  except KeyboardInterrupt:
    pass
  finally:
    lcd_byte(0x01, LCD_CMD)
    lcd_string("Goodbye!",LCD_LINE_1)
#    GPIO_cleanup()

こんな感じで動きます。




こちらで紹介している74HC595ライブラリを組み込んで、 74HC595経由で動かしてみました。
2G-IOT側のピンはSER/SRCLK/RCLKの3ポートだけ使います。
'''
python-periphery hc595 + 1602 LCD sample

2G_IOT---HC595---LCD
--------------
Pin#29---SER(SerialDataInput)
Pin#31---SRCLK
Pin#33---RCLK
---------Qa----- D4
---------Qb----- D5
---------Qc----- D6
---------Qd----- D7
---------Qe----- RS
---------Qf----- E
---------Qg----- (NC)
---------Qh----- (NC)
---------Qh'---- (NC)
3.3V-----Vcc
3.3V-----BAR(SRCLR)
GND------GND
GND------BAR(OE)

I ported from here
https://www.raspberrypi-spy.co.uk/2012/07/16x2-lcd-module-control-using-python/

'''
#!/usr/bin/python
#-*- encoding: utf-8 -*-
#import
from periphery import GPIO
import time
 
# Define some device constants
LCD_WIDTH = 16    # Maximum characters per line
LCD_CHR = True
LCD_CMD = False
 
LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
 
LCD_D4 = 0 # Q0 pin 15 on the 74HC595
LCD_D5 = 1 # Q1 pin 1 on the 74HC595
LCD_D6 = 2 # Q2 pin 2 on the 74HC595
LCD_D7 = 3 # Q3 pin 3 on the 74HC595
LCD_RS = 4 # Q4 pin 4 on the 74HC595
LCD_E  = 5 # Q5 pin 5 on the 74HC595

# Timing constants
E_PULSE = 0.0005
E_DELAY = 0.0005

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)

    def Output(self, output_number, value):
        self.setOutput(output_number, value)
        self.latch()

 
def main():
  # Main program block
#  GPIO_setwarnings(False)
#  GPIO_setmode(GPIO_BCM)       # Use BCM GPIO numbers
#  GPIO_setup(LCD_E, GPIO_OUT)  # E
#  GPIO_setup(LCD_RS, GPIO_OUT) # RS
#  GPIO_setup(LCD_D4, GPIO_OUT) # DB4
#  GPIO_setup(LCD_D5, GPIO_OUT) # DB5
#  GPIO_setup(LCD_D6, GPIO_OUT) # DB6
#  GPIO_setup(LCD_D7, GPIO_OUT) # DB7
 
  # Initialise display
  lcd_init()
 
  while True:
 
    # Send some test
    lcd_string("OrangePi 2G-IOT",LCD_LINE_1)
    lcd_string("16x2 LCD Test",LCD_LINE_2)
 
    time.sleep(3) # 3 second delay
 
    # Send some text
    lcd_string("1234567890123456",LCD_LINE_1)
    lcd_string("abcdefghijklmnop",LCD_LINE_2)
 
    time.sleep(3) # 3 second delay
 
'''
    # Send some text
    lcd_string("RaspberryPi-spy",LCD_LINE_1)
    lcd_string(".co.uk",LCD_LINE_2)
 
    time.sleep(3)
 
    # Send some text
    lcd_string("Follow me on",LCD_LINE_1)
    lcd_string("Twitter @RPiSpy",LCD_LINE_2)
 
    time.sleep(3)
'''
 
def lcd_init():
  # Initialise display
  lcd_byte(0x33,LCD_CMD) # 110011 Initialise
  lcd_byte(0x32,LCD_CMD) # 110010 Initialise
  lcd_byte(0x06,LCD_CMD) # 000110 Cursor move direction
  lcd_byte(0x0C,LCD_CMD) # 001100 Display On,Cursor Off, Blink Off
  lcd_byte(0x28,LCD_CMD) # 101000 Data length, number of lines, font size
  lcd_byte(0x01,LCD_CMD) # 000001 Clear display
  time.sleep(E_DELAY)
 
def lcd_byte(bits, mode):
  # Send byte to data pins
  # bits = data
  # mode = True  for character
  #        False for command
 
#  GPIO.LCD_RS.write(mode) # RS
  shift_register.Output(LCD_RS, mode)
 
  # High bits
#  GPIO.LCD_D4.write(False)
  shift_register.setOutput(LCD_D4, 0)
#  GPIO.LCD_D5.write(False)
  shift_register.setOutput(LCD_D5, 0)
#  GPIO.LCD_D6.write(False)
  shift_register.setOutput(LCD_D6, 0)
#  GPIO.LCD_D7.write(False)
  shift_register.setOutput(LCD_D7, 0)
  if bits&0x10==0x10:
#    GPIO.LCD_D4.write(True)
    shift_register.setOutput(LCD_D4, 1)
  if bits&0x20==0x20:
#    GPIO.LCD_D5.write(True)
    shift_register.setOutput(LCD_D5, 1)
  if bits&0x40==0x40:
#    GPIO.LCD_D6.write(True)
    shift_register.setOutput(LCD_D6, 1)
  if bits&0x80==0x80:
#    GPIO.LCD_D7.write(True)
    shift_register.setOutput(LCD_D7, 1)
 
  # Toggle 'Enable' pin
  lcd_toggle_enable()
 
  # Low bits
#  GPIO.LCD_D4.write(False)
  shift_register.setOutput(LCD_D4, 0)
#  GPIO.LCD_D5.write(False)
  shift_register.setOutput(LCD_D5, 0)
#  GPIO.LCD_D6.write(False)
  shift_register.setOutput(LCD_D6, 0)
#  GPIO.LCD_D7.write(False)
  shift_register.setOutput(LCD_D7, 0)
  if bits&0x01==0x01:
#    GPIO.LCD_D4.write(True)
    shift_register.setOutput(LCD_D4, 1)
  if bits&0x02==0x02:
#    GPIO.LCD_D5.write(True)
    shift_register.setOutput(LCD_D5, 1)
  if bits&0x04==0x04:
#    GPIO.LCD_D6.write(True)
    shift_register.setOutput(LCD_D6, 1)
  if bits&0x08==0x08:
#    GPIO.LCD_D7.write(True)
    shift_register.setOutput(LCD_D7, 1)
 
  # Toggle 'Enable' pin
  lcd_toggle_enable()
 
def lcd_toggle_enable():
  # Toggle enable
  time.sleep(E_DELAY)
#  GPIO.LCD_E.write(True)
  shift_register.Output(LCD_E, 1)
  time.sleep(E_PULSE)
#  GPIO.LCD_E.write(False)
  shift_register.Output(LCD_E, 0)
  time.sleep(E_DELAY)
 
def lcd_string(message,line):
  # Send string to display
 
  message = message.ljust(LCD_WIDTH," ")
 
  lcd_byte(line, LCD_CMD)
 
  for i in range(LCD_WIDTH):
    lcd_byte(ord(message[i]),LCD_CHR)
 
if __name__ == '__main__':
# Define 74HC595 Pin mapping
  data_pin = 122 #pin 14 on the 75HC595
  latch_pin = 123 #pin 12 on the 75HC595
  clock_pin = 124 #pin 11 on the 75HC595

# Setup 74HC595
  shift_register = ShiftRegister(data_pin, latch_pin, clock_pin)
  shift_register.setOutputs([0,0,0,0,0,0,0,0])
  shift_register.latch()
 
  try:
    main()
  except KeyboardInterrupt:
    pass
  finally:
    lcd_byte(0x01, LCD_CMD)
    lcd_string("Goodbye!",LCD_LINE_1)
#    GPIO_cleanup()

続く...