-
Notifications
You must be signed in to change notification settings - Fork 8
/
lcd_dice.py
63 lines (46 loc) · 1.22 KB
/
lcd_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
#!/usr/bin/python
import random
from time import sleep
import RPi.GPIO as GPIO
from lcd_display import lcd
# use Pi board pin numbers with GPIO.BOARD
GPIO.cleanup()
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.OUT)
GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# see http://elinux.org/Rpi_Low-level_peripherals
# see http://pypi.python.org/pypi/RPi.GPIO
def flash(d):
GPIO.output(11, False)
sleep(d)
GPIO.output(11, True)
sleep(d)
disp = lcd()
disp.display_string("Raspi Dice ", 1)
disp.display_string(" ", 2)
flash(0.2)
flash(0.2)
while True:
go = not(GPIO.input(7))
stop = not(GPIO.input(12))
if go:
# wait for button release
while go:
go = not(GPIO.input(7))
disp.display_string("Throwing ... ", 2)
# get a random number from 0 to 6 and convert to int for use with range
r = int(random.random()*6)+1
# print (r)
for x in range(0, r):
flash(0.2)
text = "You Threw a " + str(r) + " "
disp.display_string(text, 2)
if stop:
disp.display_string("Goodbye ", 2)
flash(0.5)
GPIO.cleanup()
# print ("goodbye")
disp.clear()
exit()
sleep(0.1)