1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
| #!/usr/bin/env python
import RPi.GPIO as GPIO
import time
SER = 11
RCLK = 12
SRCLK = 13
OE = 15
#=============== LED Mode Defne ================
# You can define yourself, in binay, and convert it to Hex
# 8 bits a group, 0 means off, 1 means on
# like : 0101 0101, means LED1, 3, 5, 7 are on.(from left to right)
# and convert to 0x55.
LED0 = [0b01010101,] #original mode
#=================================================
def print_msg():
print ('Program is running...')
print ('Please press Ctrl+C to end the program...')
def setup():
GPIO.setmode(GPIO.BOARD) # Number GPIOs by its physical location
GPIO.setup(SER, GPIO.OUT)
GPIO.setup(RCLK, GPIO.OUT)
GPIO.setup(SRCLK, GPIO.OUT)
GPIO.setup(OE, GPIO.OUT)
GPIO.output(SER, GPIO.LOW)
GPIO.output(RCLK, GPIO.LOW)
GPIO.output(SRCLK, GPIO.LOW)
GPIO.output(OE, GPIO.LOW)
def hc595_in(dat):
print("Writing {}".format(bin(dat)))
for bit in range(0, 8):
GPIO.output(SER, 1 & (dat >> bit))
GPIO.output(SRCLK, GPIO.HIGH)
time.sleep(0.001)
GPIO.output(SRCLK, GPIO.LOW)
def hc595_out():
GPIO.output(RCLK, GPIO.HIGH)
time.sleep(0.001)
GPIO.output(RCLK, GPIO.LOW)
def loop():
WhichLeds = LED0 # Change Mode, modes from LED0 to LED3
sleeptime = 0.3 # Change speed, lower value, faster speed
while True:
for i in range(0, len(WhichLeds)):
print("Running phase1, step {}".format(i))
hc595_in(WhichLeds[i])
hc595_out()
time.sleep(sleeptime)
for i in range(len(WhichLeds)-1, -1, -1):
print("Running phase2, step {}".format(i))
hc595_in(WhichLeds[i])
hc595_out()
time.sleep(sleeptime)
def destroy(): # When program ending, the function is executed.
GPIO.cleanup()
if __name__ == '__main__': # Program starting from here
print_msg()
setup()
try:
loop()
except KeyboardInterrupt:
destroy()
|