-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy path11_dice.py
69 lines (58 loc) · 1.38 KB
/
11_dice.py
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
#!/usr/bin/env python3
import RPi.GPIO as GPIO
import time
import random
# Set up pins
SDI = 17
RCLK = 18
SRCLK = 27
buttonPin = 22
# Define a segment code from 1 to 6 in Hexadecimal
SegCode = [0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d]
# Used to record button press
flag = 0
def print_msg():
print 'Program is running...'
print 'Please press Ctrl+C to end the program...'
def setup():
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(SDI, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(RCLK, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(SRCLK, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(buttonPin, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.add_event_detect(buttonPin, GPIO.RISING, callback = randomISR, bouncetime = 20)
# Shift the data to 74HC595
def hc595_shift(dat):
for bit in range(0, 8):
GPIO.output(SDI, 0x80 & (dat << bit))
GPIO.output(SRCLK, GPIO.HIGH)
time.sleep(0.001)
GPIO.output(SRCLK, GPIO.LOW)
GPIO.output(RCLK, GPIO.HIGH)
time.sleep(0.001)
GPIO.output(RCLK, GPIO.LOW)
def randomISR(channel):
global flag
flag = 1
def destroy():
GPIO.cleanup()
def main():
global flag
print_msg()
while True:
num = random.randint(1,6)
hc595_shift(SegCode[num-1])
print num, hex(SegCode[num-1])
if flag == 1:
print "Num: ", num
time.sleep(2)
flag = 0
else:
time.sleep(0.01)
if __name__ == '__main__':
setup()
try:
main()
except KeyboardInterrupt:
destroy()