Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #96379

Re: RPI.GPIO Help

From Dennis Lee Bieber <wlfraed@ix.netcom.com>
Subject Re: RPI.GPIO Help
Date 2015-09-11 21:10 -0400
Organization IISS Elusive Unicorn
References <lG5Ax.73238$E26.47630@fx20.iad> <MKhIx.9889$6l6.5720@fx08.iad> <V%EIx.91820$zz6.84582@fx04.iad>
Newsgroups comp.lang.python
Message-ID <mailman.387.1442020255.8327.python-list@python.org> (permalink)

Show all headers | View raw


On Fri, 11 Sep 2015 18:24:53 GMT, John McKenzie <davros@bellaliant.net>
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/

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

RPI.GPIO Help John McKenzie <davros@bellaliant.net> - 2015-08-16 19:40 +0000
  Re: RPI.GPIO Help MRAB <python@mrabarnett.plus.com> - 2015-08-16 21:15 +0100
    Re: RPI.GPIO Help John McKenzie <davros@bellaliant.net> - 2015-08-20 15:12 +0000
      Re: RPI.GPIO Help MRAB <python@mrabarnett.plus.com> - 2015-08-20 16:45 +0100
        Re: RPI.GPIO Help alister <alister.nospam.ware@ntlworld.com> - 2015-08-20 15:54 +0000
      Re: RPI.GPIO Help Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-08-20 21:27 -0400
        Re: RPI.GPIO Help John McKenzie <davros@bellaliant.net> - 2015-08-28 17:40 +0000
          Re: RPI.GPIO Help hakugin.gin@gmail.com - 2015-08-28 13:56 -0700
          Re: RPI.GPIO Help Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-08-29 14:21 -0400
  Re: RPI.GPIO Help John McKenzie <davros@bellaliant.net> - 2015-08-31 17:41 +0000
    Re: RPI.GPIO Help hakugin.gin@gmail.com - 2015-08-31 11:25 -0700
    Re: RPI.GPIO Help MRAB <python@mrabarnett.plus.com> - 2015-08-31 19:34 +0100
    Re: RPI.GPIO Help Johannes Bauer <dfnsonfsduifb@gmx.de> - 2015-09-01 10:58 +0200
  Re: RPI.GPIO Help Tim Daneliuk <tundra@bogus-city.tundraware.com> - 2015-08-31 14:07 -0500
    Re: RPI.GPIO Help Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-09-01 02:08 -0400
  Re: RPI.GPIO Help John McKenzie <davros@bellaliant.net> - 2015-09-02 18:50 +0000
  Re: RPI.GPIO Help John McKenzie <davros@bellaliant.net> - 2015-09-09 19:03 +0000
    Re: RPI.GPIO Help MRAB <python@mrabarnett.plus.com> - 2015-09-09 20:29 +0100
  Re: RPI.GPIO Help John McKenzie <davros@bellaliant.net> - 2015-09-10 15:56 +0000
    Re: RPI.GPIO Help John McKenzie <davros@bellaliant.net> - 2015-09-11 18:24 +0000
      Re: RPI.GPIO Help MRAB <python@mrabarnett.plus.com> - 2015-09-11 19:49 +0100
      Re: RPI.GPIO Help hakugin.gin@gmail.com - 2015-09-11 12:00 -0700
      Re: RPI.GPIO Help hakugin.gin@gmail.com - 2015-09-11 12:24 -0700
      Re: RPI.GPIO Help Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-09-11 21:10 -0400
  Re: RPI.GPIO Help John McKenzie <davros@bellaliant.net> - 2015-09-13 06:08 +0000
    Re: RPI.GPIO Help Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-09-13 12:24 -0400
    Re: RPI.GPIO Help hakugin.gin@gmail.com - 2015-09-14 05:53 -0700

csiph-web