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


Groups > comp.lang.python > #95514

Re: RPI.GPIO Help

From Dennis Lee Bieber <wlfraed@ix.netcom.com>
Subject Re: RPI.GPIO Help
Date 2015-08-20 21:27 -0400
Organization IISS Elusive Unicorn
References <lG5Ax.73238$E26.47630@fx20.iad> <mailman.45.1439756138.4764.python-list@python.org> <F7mBx.11314$Md3.3869@fx01.iad>
Newsgroups comp.lang.python
Message-ID <mailman.0.1440120462.13558.python-list@python.org> (permalink)

Show all headers | View raw


On Thu, 20 Aug 2015 15:12:37 GMT, John McKenzie <davros@bellaliant.net>
declaimed the following:

>
> Thanks for the reply. Also, thanks to Laura who replied via email.
>
> Tried a bunch of things based off these comments and I always ended up 
>with one of two situations, the channel conflict error, or an instant run 
>and quit issue. This new version of the code runs but is unresponsive. I 
>removed loops then put in a short sleep loop. while True:
>    time.sleep(0.1) It could be my hardware is done up wrong, but it 
>looks OK. Perhaps it is always sleeping.
>
> Anyone at all know about GPIO and the Pi under the Python library 
>RPi.GPIO please feel free to advise as to what the problem is most likely 
>to be.

	Just curious, but have you considered asking in the news group
comp.sys.raspberry-pi? Granted, most of them seem to argue more about
getting the OS to behave than in programming...

>
>led = blinkstick.find_first()

	I don't see you doing anything with the LED...

>colour = 0
>timered = 0
>timeyellow = 0
>timeblue = 0
>timestamp = time.strftime("%H:%M:%S")
>
>
>
>GPIO.setmode(GPIO.BCM)
>GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
>GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)  
>GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) 
>
>
>
>def red_button(channel):

	What is "channel"? Does it map to the ID of the pin that triggered? If
it does, you could get rid of the three callback functions and just use on
callback with a set of IF statements testing "channel" to determine the
button.

>    colour = 1
>    while colour == 1:
>        print "Red Button pressed"
>        timered += 1

	timered is not declared "global", so will be considered local to the
function... And since it isn't initialized, should be raising an error.

	You initialize "colour" to 1, and then loop until "colour" is NOT 1 --
but there is nothing inside the loop that changes the value of colour, so
the loop will never exit, which means the callback never exits, which means
the GPIO process will never regain control to test for other button
presses.
	

>
>while True:
>    time.sleep(0.1)
>
	You start an infinite loop, nothing below this point will be executed
meaning...

>def exit_handler():

	The exit handler will not be defined...

>    print '\033[0;41;37mRed Team:\033[0m ', timered
>    print '\033[0;103;30mYellow Team:\033[0m ', timeyellow
>    print '\033[0;44;37mBlue Team:\033[0m ', timeblue
>    flog = open('flag1log.text', 'a')
>    flog.write(timestamp + '\n' + 'Red Team: ' + str(timered) + '\n' + 
>'Yellow Team: ' + str(timeyellow) + '\n' + 'Blue Team: ' + str(timeblue) 
>+ '\n')

	Ugh... Please read up on Python string interpolation (or format method,
depending on Python version)

	I don't have an R-Pi, nor your colorful LED, so this is just
pseudo-code...

-=-=-=-=-=-=-

import atexit
import time
import blinkstick as bs
import RPi.GPIO as GPIO


class Button(object):
    modeSet = False
    
    def __init__(self, buttonPin, LED, colorCode):
        self.totalTime = 0
        self.downTime = 0
        self.LED = LED
        if colorCode.lower() in "rgb":
            self.colorCode = colorCode
        else:
            self.colorCode = None

        if not Button.modeSet:  #classwide (shared) value
            GPIO.setmode(GPIO.BCN)  #so only performed once
            Button.modeSet = True

        GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
        GPIO.add_event_detect(buttonPin, GPIO.RISING,
                              callback=self.buttonDown,
                              bouncetime=200)
        GPIO.add_event_detect(buttonPin, GPIO.FALLING, #guessing
                              callback=self.buttonUp,
                              bouncetime=200)

    def buttonDown(self, buttonPin):   #this design doesn't use the pin
        self.downTime = time.time()     #record when button pressed
        if self.colorCode:              #if a valid RGB assigned
            (r, g, b) = LED.get_color() #modify LED color
            if self.colorCode == "r":
                LED.set_color(red=0xFF, green=g, blue=b)
            elif self.colorCode == "g":
                LED.set_color(red=r, green=0xFF, blue=b)
            elif self.colorCode == "b":
                LED.set_color(red=r, green=g, blue=0xFF)

    def buttonUp(self, buttonPin):
        self.totalTime += (time.time() - self.downTimp) #increment total
time
        self.downTime = 0   #just paranoia
        if self.colorCode:  #if valid RGB, turn the element off
            if self.colorCode == "r":
                LED.set_color(red=0x00, green=g, blue=b)
            elif self.colorCode == "g":
                LED.set_color(red=r, green=0x00, blue=b)
            elif self.colorCode == "b":
                LED.set_color(red=r, green=g, blue=0x00)

def exit_handler():
    #   I don't have a reference to terminal escape codes, so
    #   I'm going to skip that part
    print "Red Team  :\t%s" % redButton.totalTime
    print "Green Team:\t%s" % greenButton.totalTime
    print "Blue Team :\t%s" % blueButton.totalTime
    flog = open("flag1.log", "a")
    flog.write("%s\n" % time.strftime("%H:%M:%S"))
    flog.write("Red Team  :\t%s\n" % redButton.totalTime)
    flog.write("Green Team:\t%s\n" % greenButton.totalTime)
    flog.write("Blue Team :\t%s\n\n" % blueButton.totalTime)
    flog.close()

    GPIO.cleanup()



if __name__ == "__main__":
    LED = bs.blinkstick().find_first()   #get LED object

    redButton = Button(22, LED, "r")    #create button objects
    greenButton = Button(23, LED, "g")
    blueButton = Button(24, LED, "b")

    atexit.register(exit_handler)       #register exit handler
    
    while True:
        time.sleep(0)   #loop forever

-- 
	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