Path: csiph.com!eternal-september.org!feeder.eternal-september.org!border1.nntp.ams1.giganews.com!nntp.giganews.com!usenetcore.com!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'elif': 0.04; 'handler': 0.04; '#if': 0.05; 'none:': 0.05; '#define': 0.07; 'exit': 0.07; '(0,': 0.09; 'exited': 0.09; 'globals': 0.09; 'message-id:@4ax.com': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'timestamp': 0.09; 'subject:Help': 0.10; 'thread': 0.10; 'def': 0.13; '"a")': 0.16; '"global"': 0.16; '0.0,': 0.16; '23,': 0.16; 'atexit': 0.16; 'call?': 0.16; 'instances,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'statements,': 0.16; 'team:': 0.16; 'time.time()': 0.16; 'url:home': 0.18; '2015': 0.20; 'delta': 0.22; 'sep': 0.22; 'import': 0.24; 'header:X-Complaints- To:1': 0.26; 'skip:" 20': 0.26; 'fri,': 0.27; 'blocking': 0.29; 'loop,': 0.29; "i'm": 0.30; 'print': 0.30; 'creating': 0.30; 'skip:g 30': 0.30; 'run': 0.33; 'class': 0.33; 'channel': 0.34; 'so,': 0.35; 'next': 0.35; 'done': 0.35; 'false': 0.35; 'saved': 0.35; 'but': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'received:org': 0.37; 'charset:us-ascii': 0.37; 'doing': 0.38; 'busy': 0.38; "won't": 0.38; 'button': 0.38; 'skip:p 20': 0.38; 'does': 0.39; 'rather': 0.39; 'to:addr:python.org': 0.40; 'still': 0.40; 'called': 0.40; 'future': 0.60; 'determine': 0.61; 'john': 0.61; 'account': 0.66; 'results': 0.66; 'color': 0.67; 'online': 0.71; 'led': 0.72; 'rgb': 0.84; 'colour': 0.91; 'dennis': 0.91; 'received:108': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dennis Lee Bieber Subject: Re: RPI.GPIO Help Date: Fri, 11 Sep 2015 21:10:46 -0400 Organization: IISS Elusive Unicorn References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: adsl-108-68-178-61.dsl.klmzmi.sbcglobal.net X-Newsreader: Forte Agent 6.00/32.1186 X-No-Archive: YES X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 102 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1442020255 news.xs4all.nl 23725 [2001:888:2000:d::a6]:42370 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:96379 On Fri, 11 Sep 2015 18:24:53 GMT, John McKenzie declaimed the following: Take into account that I still don't have an R-Pi, so I'm basing all this on the online documentation. I still don't like all the "global" statements, but creating class instances, and blocking queues rather than the busy-loop can be saved for the future import atexit import time from blinkstick import blinkstick import RPi.GPIO as GPIO #define "constants" so updates only need to be done once (R, G, B) = (0, 1, 2) #G is also being used for Y (REDCH, YELLOWCH, BLUECH) = (22, 23, 24) RGB = ("#FF0000", "#FF6000", "#0000FF") led = blinkstick.find_first() colour = None changeTime = None cumTime = (0.0, 0.0, 0.0) timestamp = time.strftime("%H:%M:%S") GPIO.setmode(GPIO.BCM) GPIO.setup(REDCH, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(YELLOWCH, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(BLUECH, GPIO.IN, pull_up_down=GPIO.PUD_UP) def buttonHandler(channel): global colour global changeTime # get current time now = time.time() # determine if a change in color is called for if colour != R and channel == REDCH: newColour = R elif colour != G and channel == YELLOWCH: newColour = G elif colour != B and channel == BLUECH: newColour = B else: return #do nothing with LED #if first time button pressed, no cumulative time if changeTime is not None: #save delta time from last change to this change cumTime[colour] += now - changeTime #set globals for next button action changeTime = now colour = newColour #activate LED -- is this a blocking call? if so, other #button presses won't respond until after this #finishes led.pulse(hex=RGB[colour], repeats=1, duration=2000, steps=50) GPIO.add_event_detect(REDCH, GPIO.FALLING, callback=buttonHandler, bouncetime=200) GPIO.add_event_detect(YELLOWCH, GPIO.FALLING, callback=buttonHandler, bouncetime=200) GPIO.add_event_detect(BLUECH, GPIO.FALLING, callback=buttonHandler, bouncetime=200) def exit_handler(): # all the exit handler does is set a flag to break out the main # endless loop, letting the main thread clean up global keepLooping keepLooping = False atexit.register(exit_handler) keepLooping = True while keepLooping: time.sleep(0.1) #eat time doing nothing # busy loop has exited because atexit handler has set the it False # so dump the results of this run led.set_color(name="black") print "\033[0;41;37mRed Team:\033[0m ", cumTime[R] print "\033[0;43;30mYellow Time:\033[0m ", cumTime[G] print "\033[0;44;37mBlue Time:\033[0m ", cumTime[B] flog = open("flag1.log", "a") flog.write("%s\nRed Team: %s\nYellow Team: %s\nBlue Team:%s\n" % (timestamp, cumTime[R], cumTime[G], cumTime[B])) flog.close() GPIO.cleanup() -- Wulfraed Dennis Lee Bieber AF6VN wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/