#!/usr/bin/python import gpiod import time ##表示メッセージ message= "U U U U R S D P DX D XD XL XK X" PIN_Signals = [ (" " ,(0,0,0,0,0,0,0,0)), ("U" ,(1,0,0,0,0,0,0,0)), ("R" ,(0,1,0,0,0,0,0,0)), ("S" ,(0,0,1,0,0,0,0,0)), ("D" ,(0,0,0,1,0,0,0,0)), ("P" ,(0,0,0,1,0,0,0,1)), ("X" ,(0,0,0,0,0,0,0,1)), ("L" ,(0,0,0,0,1,0,0,0)), ("K" ,(0,0,0,0,0,1,0,0)) ] SIGNALS = dict(PIN_Signals) select_chip0 = gpiod.Chip("gpiochip0") select_chip2 = gpiod.Chip("gpiochip2") line_Dig4 = select_chip2.get_line( 6) # P8_45 line_Dig3 = select_chip2.get_line( 8) # P8_43 line_Dig2 = select_chip2.get_line(12) # P8_39 line_Dig1 = select_chip2.get_line(10) # P8_41 line_LedOna = select_chip2.get_line(11) # P8_42 line_LedOnb = select_chip2.get_line(17) # P8_34 line_LedOnc = select_chip2.get_line(16) # P8_36 line_LedOnd = select_chip2.get_line(15) # P8_38 line_LedOne = select_chip2.get_line(13) # P8_40 line_LedOnf = select_chip0.get_line(11) # P8_32 line_LedOng = select_chip2.get_line( 9) # P8_44 line_LedOnp = select_chip2.get_line( 7) # P8_46 line_Dig4.request( consumer="7SEGDEMO", type=gpiod.LINE_REQ_DIR_OUT, default_vals=[0] ) line_Dig3.request( consumer="7SEGDEMO", type=gpiod.LINE_REQ_DIR_OUT, default_vals=[0] ) line_Dig2.request( consumer="7SEGDEMO", type=gpiod.LINE_REQ_DIR_OUT, default_vals=[0] ) line_Dig1.request( consumer="7SEGDEMO", type=gpiod.LINE_REQ_DIR_OUT, default_vals=[0] ) line_LedOnp.request( consumer="7SEGDEMO", type=gpiod.LINE_REQ_DIR_OUT, default_vals=[0] ) line_LedOng.request( consumer="7SEGDEMO", type=gpiod.LINE_REQ_DIR_OUT, default_vals=[0] ) line_LedOnf.request( consumer="7SEGDEMO", type=gpiod.LINE_REQ_DIR_OUT, default_vals=[0] ) line_LedOne.request( consumer="7SEGDEMO", type=gpiod.LINE_REQ_DIR_OUT, default_vals=[0] ) line_LedOnd.request( consumer="7SEGDEMO", type=gpiod.LINE_REQ_DIR_OUT, default_vals=[0] ) line_LedOnc.request( consumer="7SEGDEMO", type=gpiod.LINE_REQ_DIR_OUT, default_vals=[0] ) line_LedOnb.request( consumer="7SEGDEMO", type=gpiod.LINE_REQ_DIR_OUT, default_vals=[0] ) line_LedOna.request( consumer="7SEGDEMO", type=gpiod.LINE_REQ_DIR_OUT, default_vals=[0] ) DIG_LINES = ( line_Dig1, line_Dig2, line_Dig3, line_Dig4 ) # a,b,c,d,e,f,g,p の順 LED_LINES = ( line_LedOna, line_LedOnb, line_LedOnc, line_LedOnd, line_LedOne, line_LedOnf, line_LedOng, line_LedOnp ) def DigAllOff(): """全桁を消灯する""" for line in DIG_LINES: line.set_value(0) def DispLED(digit, ch): """ 指定した桁に文字を表示する。 digit : 1~4 ch : PIN_Signalsに登録された文字 """ if digit < 1 or digit > 4: raise ValueError("digitは1~4を指定してください") ch = str(ch).upper() if ch not in SIGNALS: raise ValueError("表示できない文字です: " + ch) sw = SIGNALS[ch] # 文字変更中に別のパターンが見えないよう全桁OFF DigAllOff() # セグメント設定 for line, value in zip(LED_LINES, sw): line.set_value(value) # 指定桁をON DIG_LINES[digit - 1].set_value(1) #### 表示 #### waittime = 0.002 holdtime = 0.3 count = int(holdtime / (waittime * 4)) try: while True : for strpos in range(0, len(message), 4): splitmessage = message[strpos:strpos+4].ljust(4) counter = count while counter > 0 : for dig, ch in enumerate(splitmessage, start=1): DispLED(dig,ch) time.sleep(waittime) counter -= 1 finally: DigAllOff()