From c0bf20e5cbebc5af103940a74f934a16575cdd59 Mon Sep 17 00:00:00 2001 From: danielgohlke-zanox Date: Fri, 18 Nov 2016 20:56:41 +0100 Subject: [PATCH] Update 11_dice.py: added missing functions Fixed script, rewrote it mostly similar to https://github.com/sunfounder/Sunfounder_SuperKit_C_code_for_RaspberryPi/blob/master/11_Segment/dice.c --- 11_dice.py | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/11_dice.py b/11_dice.py index cb25cdd..d469293 100644 --- a/11_dice.py +++ b/11_dice.py @@ -1,6 +1,8 @@ #!/usr/bin/env python import RPi.GPIO as GPIO import time +from random import randint + # Set up pins SDI = 17 @@ -12,9 +14,6 @@ # Define a segment code from 0 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...' @@ -24,8 +23,7 @@ def setup(): 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(TouchPin, GPIO.IN) - GPIO.add_event_detect(TouchPin, GPIO.RISING) + GPIO.setup(TouchPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Shift the data to 74HC595 def hc595_shift(dat): @@ -38,10 +36,22 @@ def hc595_shift(dat): time.sleep(0.001) GPIO.output(RCLK, GPIO.LOW) -def randomISR(): - if GPIO.event_detect(TouchPin): - flag = 1 +def loop(): + while True: + if not GPIO.input(TouchPin): + hc595_shift(SegCode[randint(0,5)]) + time.sleep(2.0) + + hc595_shift(SegCode[randint(0,5)]) + time.sleep(0.060) + +def destroy(): #When program ending, the function is executed. + GPIO.cleanup() -def main(): +if __name__ == '__main__': #Program starting from here print_msg() - \ No newline at end of file + setup() + try: + loop() + except KeyboardInterrupt: + destroy()