#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
import random
SDI = 11
RCLK = 12
SRCLK = 13
LedPin1 = 15
LedPin2 = 16
holdt=0.001
segCode = [0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x80]
def print_msg():
print 'Program is running...'
print 'Please press Ctrl+C to end the program...'
def setup():
GPIO.setmode(GPIO.BOARD)
#setup pins
GPIO.setup(SDI, GPIO.OUT)
GPIO.setup(RCLK, GPIO.OUT)
GPIO.setup(SRCLK, GPIO.OUT)
#set default state
GPIO.output(SDI, GPIO.LOW)
GPIO.output(RCLK, GPIO.LOW)
GPIO.output(SRCLK, GPIO.LOW)
GPIO.setup(LedPin1, GPIO.OUT)
GPIO.output(LedPin1, GPIO.LOW)
GPIO.setup(LedPin2, GPIO.OUT)
GPIO.output(LedPin2, GPIO.LOW)
def hc595_shift(dat,lcd):
#push 8 bits into the register
GPIO.output(LedPin1, GPIO.LOW)
GPIO.output(LedPin2, GPIO.LOW)
for bit in range(0, 8):
GPIO.output(SDI, 0x80 & (dat << bit)) #shift the bit and push it
GPIO.output(SRCLK, GPIO.HIGH) # set the clk
time.sleep(0.001)
GPIO.output(SRCLK, GPIO.LOW) # set to low (initial state)
GPIO.output(RCLK, GPIO.HIGH) #init output
time.sleep(0.001)
if lcd == 1:
GPIO.output(LedPin1, GPIO.HIGH)
GPIO.output(LedPin2, GPIO.LOW)
else:
GPIO.output(LedPin1, GPIO.LOW)
GPIO.output(LedPin2, GPIO.HIGH)
GPIO.output(RCLK, GPIO.LOW) # output
def loop():
while True:
for i in range(0,6):
i = random.randint(1,100)
x = divmod(i,10)
for i in range(0,25):
for j in range(0,2):
hc595_shift(segCode[x[j]],j+1)
time.sleep(holdt)
def destroy():
GPIO.output(LedPin1, GPIO.LOW)
GPIO.output(LedPin2, GPIO.LOW)
GPIO.cleanup()
if __name__ == '__main__':
print_msg()
setup()
try:
loop()
except KeyboardInterrupt:
destroy()