Re: Passing Variable to Function
Brendan and Alister, thank you both for responding. I am very inexperienced with python, but not new to computers so on my own I realized the strings vs number mistake. (I seem to work with and learn about Python for a few weeks at a time with 6 to 12 months in between. Need to change that and focus on it for awhile.) After my first error I removed the quotation marks from my variables knowing that strings vs numbers was involved. I copied and pasted from my original script when I showed it to you. Opps. Anyway, I had no idea how to expand tuples nor that it was not done automatically. Thank you you both for sharing that info. After making variables numbers by removing the quotes and putting an asterisk in front of my colour1, colour2, etc variables whenever I passed them to a function in order to expand them, the error messages disappeared. It appears to work just fine now. Thanks. -- https://mail.python.org/mailman/listinfo/python-list
Passing Variable to Function
Hello, all. I have a function that takes three arguments, arguments to express an RGB colour. The function controls an LED light strip (a Blinkytape). Sometimes I might need to be change what colour is sent to the function, so I set a variable with the idea that I can change just the variable later if I need to instead of changing a bunch of different lines. So I have variables along the lines of this: colour ="255, 0, 0" colour2 ="100, 0, 0" My function, written by the Blinkytape people: def changeColor(r, g, b): serialPorts = glob.glob("/dev/ttyACM0*") port = serialPorts[0] if not port: sys.exit("Could not locate a BlinkyTape.") print "BlinkyTape found at: %s" % port bt = BlinkyTape.BlinkyTape(port) bt.displayColor(r, g, b) time.sleep(.1) # Give the serial driver some time to actually send the data bt.close() Later, I have conditional statements like: if latitude > maxNetural and latitude < NorthLine: changeColor(colour) elif latitude > NorthLine: changeColor(colour2) (There is a GPS device connected, there are variables defined based on latitude earlier in the script.) I get an error stating that changeColor takes three arguments and I am just giving it one (either "colour1" or "colour2"). Is there a way to format the "colour" variable so that the changeColor function takes it as the three numbers it is meant to be defined as? Entire script: http://hastebin.com/qaqotusozo.py Thanks. -- https://mail.python.org/mailman/listinfo/python-list
Python and GPSD
Good day, all. I need help using the Python bindings for GPSD. Specifically, I would like to take a latitude reading, put it in a variable and use it later. The problem is that every example I see involves constantly taking changing readings. That part I have working for myself by following existing examples. If I put any code in any kind of loop my latitude variable will keep changing. If I do not put in a loop, it will appear to be zero. (I do not live on the equator.) The Python GPS module is installed, cgps works fine, other aspects of the script involving GPS work fine. I want to be able to turn on a single board computing device, have it get a latitude reading, put that coordinate into a variable, then go along its merry way using that variable for the rest of the script. (Running my script upon boot, running the GPSD service, etc, is all fine and working.) My script: http://hastebin.com/zuwamuqoxe.coffee On line 37 I try to define an initial latitude variable, intLatitude. It comes up as zero right now. I need it to be whatever the GPS says for latitude when the script starts and stay that way. Help is appreciated. Apologies to those who read my script for the poor code quality. Learned a little Python last year, then only started again with it recently. Still a Python newbie, and am rushed for this particular project. Thanks. -- https://mail.python.org/mailman/listinfo/python-list
Re: RPI.GPIO Help
Hello, there. MRAB, thank you for teaching me proper Python syntax for how I tried to use the or operator. Dennis, I must have learned allot recently as I believe I understood 99% of that code. I see how it is not just more advanced, but actually better than what I had. However, line 47 (cumTime[colour] += now - changeTime) had an error I could not figure out what to do about. Considering my time line I decided not to leave it to latter. Will be using these electronic flags at next year's game and for then I intend to just be better about everything. Will use your code to learn to get there. Hakugin, thank you you as well. I took the basic ideas you showed me for improvement and used them. The pulse settings variable was not liked by the interpreter, so I simplified it. I turned it into a hex value for each button press, then in the main loop I inserted the whole line for the LED pulse command, but put "pulse_settings" after "hex=" in the arguments. This worked. I added a few green blinks of the LED to indicate the starting and stopping of the script. Also, I got the log files score print outs upon exit working. Very important, and also importantly, I have it so it stops after a certain amount of time. For testing, I have it at 60 seconds, but games will be 3600 seconds on average when really being used. The stopping after a certain amount of time was done in a way that apparently technically works, but seems very weird and probably wrong to me. You may freak out when you see it. I used an else statement inside a while loop and it just feels so strange. At least it works. Hoping I might be able to make it so I boot the Pi, it loads the script, script waits for the user to tell it how long to make a game, game starts, scripts ends game at appropriate time, saves dated log file with scores, then waits for user to enter new game length to start new game. This is probably way to much to hope to accomplish in time. For next year for sure though. It would be better for the referees to operate that way. Next I will try and integrate wireless communications. If anyone here knows Synapse RF modules well, or at all, PLEASE contact me. Here is the code I did up most recently. Again, thanks for all the suggestions, code examples, and general help. import atexit import sys import time from blinkstick import blinkstick import RPi.GPIO as GPIO gamestart = time.time() gamelength = 60 led = blinkstick.find_first() colour = 0 time_red = 0 time_yellow = 0 time_blue = 0 timestamp = time.strftime("%H:%M:%S") pulse_settings = [] led.blink(name="green", repeats=2) GPIO.setmode(GPIO.BCM) GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP) def red_button(channel): global colour global pulse_settings if colour != 1: colour = 1 pulse_settings = "#FF" def yellow_button(channel): global colour global pulse_settings if colour != 2: colour = 2 pulse_settings = "#FF8900" # Corrected yellow for cheap LED strip. def blue_button(channel): global colour global pulse_settings if colour != 3: colour = 3 pulse_settings = "#FF" GPIO.add_event_detect(22, GPIO.FALLING, callback=red_button, bouncetime=200) GPIO.add_event_detect(23, GPIO.FALLING, callback=yellow_button, bouncetime=200) GPIO.add_event_detect(24, GPIO.FALLING, callback=blue_button, bouncetime=200) def exit_handler(): print "\033[0;41;37mRed Team:\033[0m ", time_red print "\033[0;103;30mYellow Team:\033[0m ", time_yellow print "\033[0;44;37mBlue Team:\033[0m ", time_blue flog = open("flag1.log", "a") flog.write("\n" + timestamp + "\n" + "Red Team: " + str(time_red) + "\n" + "Yellow Team: " + str(time_yellow) + "\n" + "Blue Team: " + str (time_blue) + "\n") flog.close() led.blink(name="green", repeats=4) led.set_color(name="black") atexit.register(exit_handler) while time.time() < gamestart + gamelength: if colour == 1: time_red += 1 elif colour == 2: time_yellow += 1 elif colour == 3: time_blue += 1 led.pulse(hex=pulse_settings, repeats=1, duration=2000, steps=50) time.sleep(0.1) else: sys.exit() GPIO.cleanup() -- https://mail.python.org/mailman/listinfo/python-list
Re: RPI.GPIO Help
Hello. Thanks to the help of people here and in other newsgroups I seem to have something working doing the basics. (Buttons work, colours light up appropriately.) When I followed MRAB's instructions and read about scopes of variables that solved my most recent problem, but it introduced a bug. I think I fixed the bug but after all my stupid mistakes and forgetfulness that seems too good to be true. I expect there is a better, more elegant, or more Pythonic way to do what I did so please feel free to share on the subject. I had a problem where if I pressed a button while the LEDs were already flashing the colour of that button it would block a new colour from starting when I pressed a new button. So if the LED strip was red and I pressed the red button again nothing would happen when I pressed the blue or yellow button. Similar problem for the other two buttons. So inside my callbacks I added this code: if colour == 1: pass elif colour == 2 or 3: colour = 1 Now it seems OK from my limited testing. Here is the code that has buttons and colours working and includes my bug fix: import atexit import time from blinkstick import blinkstick import RPi.GPIO as GPIO led = blinkstick.find_first() colour = 0 time_red = 0 time_yellow = 0 time_blue = 0 timestamp = time.strftime("%H:%M:%S") GPIO.setmode(GPIO.BCM) GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP) def red_button(channel): global colour if colour == 1: pass elif colour == 2 or 3: colour = 1 while colour == 1: led.pulse(red=255, green=0, blue=0, repeats=1, duration=2000, steps=50) def yellow_button(channel): global colour if colour == 2: pass elif colour == 1 or 3: colour = 2 while colour == 2: led.pulse(red=255, green=96, blue=0, repeats=1, duration=2000, steps=50) def blue_button(channel): global colour if colour == 3: pass elif colour == 1 or 2: colour = 3 while colour == 3: led.pulse(red=0, green=0, blue=255, repeats=1, duration=2000, steps=50) GPIO.add_event_detect(22, GPIO.FALLING, callback=red_button, bouncetime=200) GPIO.add_event_detect(23, GPIO.FALLING, callback=yellow_button, bouncetime=200) GPIO.add_event_detect(24, GPIO.FALLING, callback=blue_button, bouncetime=200) while True: if colour == 1: time_red += 1 elif colour == 2: time_yellow += 1 elif colour == 3: time_blue += 1 time.sleep(0.1) def exit_handler(): print "\033[0;41;37mRed Team:\033[0m ", time_red print "\033[0;43;30mYellow Time:\033[0m ", time_yellow print "\033[0;44;37mBlue Time:\033[0m ", time_blue flog = open("flag1.log", "a") flog.write(timestamp + "\n" + "Red Team: " + str(time_red) + "\n" + "Yellow Team: " + str(time_yellow) + "\n" + "Blue Team: " + str (time_blue) + "\n") flog.close() led.set_color(name="black") atexit.register(exit_handler) GPIO.cleanup() I think I am OK GPIO wise now, although always happy to improve the code and in the long term I want to do so. Will start new threads for more straight forward Python questions like help with saving a log of the results, timing, etc. Thanks for your help, everyone. -- https://mail.python.org/mailman/listinfo/python-list
Re: RPI.GPIO Help
MRAB: Thanks for replying. I got so hyper focused on solving my hardware problems, and excited that I did, that I forgot details from previous comments. Thanks for your post. Off to make things global... -- https://mail.python.org/mailman/listinfo/python-list
Re: RPI.GPIO Help
Hello. As per the suggestion of two of you I went to the Raspberry Pi newsgroup. Dennis is also there and has been posting in response to my problems. Between there and the Raspberry Foundation website I discovered that my wiring did not match my code and changed all PUD_DOWN to PUD_UP and all GPIO.RISING to GPIO.FALLING because I was wired to the ground pin. Someone suggested code very close to what hakugin suggested. Right now I have response buttons that work, but the colour of the LED strip is not correct. It adds colours instead of changing to a new one. I hit red, it pulses red, I hit blue, it pulses red and blue, making purple. It appears now my problem is more about Python usage than anything else, so I am asking for help here as well as having already asked elsewhere. Tried using a go to black command (led.set_color(name="black")) and a turn off command (led.turn_off()) before the pulse command to reset the colour and these did not work. To see what would happen I removed the "while colour == 1:" (or == 2: or ==3:) line and it acted as expected. The LED pulsed once. However, it would pulse the correct colour, it would not add colours together. So the while loop seems to be the cause of my problem, but it is needed to keep the pulse repeating as far I can know. Maybe Python has another way to repeat the function. Here is the code I was working from: import atexit import time from blinkstick import blinkstick import RPi.GPIO as GPIO led = blinkstick.find_first() colour = 0 time_red = 0 time_yellow = 0 time_blue = 0 timestamp = time.strftime("%H:%M:%S") GPIO.setmode(GPIO.BCM) GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP) def red_button(channel): led.set_color(name="black") colour = 1 while colour == 1: led.pulse(red=255, green=0, blue=0, repeats=1, duration=3000, steps=50) def yellow_button(channel): led.set_color(name="black") colour = 2 while colour == 2: led.pulse(red=255, green=255, blue=0, repeats=1, duration=3000, steps=50) def blue_button(channel): led.set_color(name="black") colour = 3 while colour == 3: led.pulse(red=0, green=0, blue=255, repeats=1, duration=2000, steps=50) GPIO.add_event_detect(22, GPIO.FALLING, callback=red_button, bouncetime=200) GPIO.add_event_detect(23, GPIO.FALLING, callback=yellow_button, bouncetime=200) GPIO.add_event_detect(24, GPIO.FALLING, callback=blue_button, bouncetime=200) while True: if colour == 1: time_red += 1 elif colour == 2: time_yellow += 1 elif colour == 3: time_blue += 1 time.sleep(0.1) def exit_handler(): print "\033[0;41;37mRed Team:\033[0m ", time_red print "\033[0;43;30mYellow Time:\033[0m ", time_yellow print "\033[0;44;37mBlue Time:\033[0m ", time_blue flog = open("flag1.log", "a") flog.write(timestamp + "\n" + "Red Team: " + str(time_red) + "\n" + "Yellow Team: " + str(time_yellow) + "\n" + "Blue Team: " + str (time_blue) + "\n") flog.close() GPIO.cleanup() atexit.register(exit_handler) Any advice about the while loop for the colour pulsing is appreciated. -- https://mail.python.org/mailman/listinfo/python-list
Re: RPI.GPIO Help
Hakugin: Thanks for the correction. Someone elsewhere showed me example code that was very close to yours, with that being the main difference. His gave an error message that red_button was undefined so I moved the code block below the callbacks. After that it ran without producing errors but it was unresponsive. Will try your updated version tomorrow and will keep fiddling with his. Thank you all for warning me about debouncing. The RPi.GPIO library has built in debouncing. You can even adjust the timing of it. Turns out I got one thing right. Someone who knows better than I explained that GPIO.Cleanup() does go at the bottom and outside the def exit_handler() code block like I had it originally. Johannes: Thanks for sharing that. I will take a look at it when I have more time. Must head to the hospital for my treatment in a few minutes. >Are you still calling GPIO.add_event_detect in a while loop? MRAB, apparently yes. Hakugin noticed and update his example code. -- https://mail.python.org/mailman/listinfo/python-list
Re: RPI.GPIO Help
Dennis, Hakugin, I tried your scripts and had to alter a typo here or there, but once the basic errors disappeared I had the same error message. "Conflicting edge detection already enabled for this GPIO channel". As much as I despise web based bulletin board systems I registered on the Raspberry Pi website to ask for help as well. Appreciate the effort you both put in those scripts. Will keep working with them and the general points you made. Still checking here and am discussing all this in the Raspberry pi newsgroup. Thanks to the several people who mentioned it. Again, still listening here if anyone has any more to add. -- https://mail.python.org/mailman/listinfo/python-list
Re: RPI.GPIO Help
Thanks for the replies, everyone. Two of you suggested I ask in comp.sys.raspberry-pi. We leave in a world where people cannot tell you the difference between the world wide web and the Internet and tech support for my ISP once told me in response to mentioning that thei news server was not functioning properly that I should "try turning on cookies" and further suggested I speak to the "owner of usenet" for help. Because of this new world it never occured to me that a new newsgroup would have been created in the last five years. I expected to be laughed at if I asked about Raspberry Pi having a presence on usenet. So thank you both for suggesting it. I will absolutely move my question there then come back here for more straight forward pure Python stuff. Dennis replied in detail and asked a question of me. > I don't see you doing anything with the LED... As I mentioned in my original post I intend to turn LED lights a different colour but for developmented purposes I anm replace the code for that with a print statement. LED variable was leftover from the original script and I forgot to delete that line when sharing the development version. >What is "channel"? Channel is something that is in every GPIO library example, and I pretty sure it is literal, not a placeholder. If I replace it with channel numbers it gives more errors. Channel is in place in working script examples you can download. > timered is not declared "global", so will be considered local to><> the Rereading global variables info. Never understood why all variables aren't always global at all times in any language. Why would you not want the variable accessible whenever you wanted it? Maybe it is related to performance or something. Anyway, will look at the scopes of variables again and look at the code again. > You initialize "colour" to 1, and then loop until "colour" is NOT 1 -- I thought the existance of the other callbacks would be able to interupt it. Thanks for another point for me to look into and learn about. > You start an infinite loop, nothing below this point will be executed The start of the infinite sleep loop is nessecary to have the script go reiterate instead of run once in under a second and stop. Not saying this has to be the way to do it, just explaining why I did it. What I used was the example used in dozens and dozens of answers given online to solve that same problem when using RPI.GPIO. > The exit handler will not be defined... > Ugh... Please read up on Python string interpolation (or format method, >depending on Python version) Not sure what you are saying about that part of the code because it is the one part of the code that works perfectly. I copied from a generic example to start with and added my own text and ANSI codes and in every test it does what it is supposed exactly as it is supposed to. In addition to wanting to say thanks for your comments, I would like to say thanks for your example pseudo-code. I appreciate it and will, like you warned, keep in mind you do not have a Pi. Thanks everyone who replied to my questions for that matter. Now that I finnally have some time to work on this weekend I will take all your comments in and look at the code suggestions provided. Thank you. -- https://mail.python.org/mailman/listinfo/python-list
Re: RPI.GPIO Help
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. import atexit import time from blinkstick import blinkstick import RPi.GPIO as GPIO led = blinkstick.find_first() 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): colour = 1 while colour == 1: print "Red Button pressed" timered += 1 def yellow_button(channel): colour = 2 while colour == 2: print "Yellow Button pressed" timeyellow += 1 def blue_button(channel): colour = 3 while colour == 3: print "Blue Button pressed" timeblue += 1 GPIO.add_event_detect(23, GPIO.RISING, callback=yellow_button, bouncetime=200) GPIO.add_event_detect(22, GPIO.RISING, callback=red_button, bouncetime=200) GPIO.add_event_detect(24, GPIO.RISING, callback=blue_button, bouncetime=200) while True: time.sleep(0.1) def exit_handler(): 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') flog.close() atexit.register(exit_handler) GPIO.cleanup() Thanks. -- https://mail.python.org/mailman/listinfo/python-list
Re: Keypress Input
Thanks again to everyone who tried to help. Michael, I especially appreciate your encouragement and chiming in to point out that telling newbies to learn everything there is before posting question was not helpful in getting more people using Python. Have the Pi wired up directly to the buttons, read up on the GPIO library and I just posted for help regarding the error messages I am getting from my Python buttons script. Thanks. -- https://mail.python.org/mailman/listinfo/python-list
RPI.GPIO Help
Hello, all. I am hoping some people here are familiar with the RPi.GPIO python module for the Raspberry Pi. Very new to Python and electronics. Not to computing in general though. I posted for help about accepting key presses and then discovered that wiring up buttons directly to the Pi was 1/50th as difficult as I thought it would be so I am going a different route than keyboard emulation and needing GUI toolkits, etc. However, I am getting error messages with RPi.GPIO. I have three buttons, Red, Yellow and Blue in colour, attached to the Pi. The eventual goal is to have pressing one button result in changing the colour of an LED lightstrip to that colour and the Pi record how long the strip spent as each colour. For development purposes I have the controls near me and my desktop computer, and the Pi networked to this computer. For now I have each button press result in a print statement. When I get this working I will replace the print statements with the code to change colours on the LED strip using the Blinkstick module. This is the basic test code. import atexit import time from blinkstick import blinkstick import RPi.GPIO as GPIO led = blinkstick.find_first() 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): colour = 1 print "Red Button Pressed" while colour == 1: timered += 1 def yellow_button(channel): colour = 2 print "Yellow Button Pressed" while colour == 2: timeyellow += 1 def blue_button(channel): colour = 3 print "Blue Button Pressed" while colour == 3: timeblue += 1 while True: GPIO.add_event_detect(22, GPIO.RISING, callback=red_button, bouncetime=200) GPIO.add_event_detect(23, GPIO.RISING, callback=yellow_button, bouncetime=200) GPIO.add_event_detect(24, GPIO.RISING, callback=blue_button, bouncetime=200) def exit_handler(): print '\033[0;41;37mRed Team:\033[0m ', timered print '\033[0;43;30mYellow Time:\033[0m ', timeyellow print '\033[0;44;37mBlue Time:\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') flog.close() atexit.register(exit_handler) GPIO.cleanup() This results in the error message "RuntimeError: Conflicting edge detection already enabled for this GPIO channel". Running GPIO.cleanup() in the interpreter results in a message stating the GPIO pins are not assigned and there is nothing to cleanup. Removing line 40, the while True: line, removes the error, but the program does not sit and wait waiting for a button press, it just runs and ends a second later. There are other things this script will need, but this is the core function that I need to get working -pressing a button does what I want and the script keeps running so I can press another button if I want. If are familiar with the RPi.GPIO or see a more general Python mistake that could be affecting everything know I would appreciate your help. Thanks. -- https://mail.python.org/mailman/listinfo/python-list
Re: Keypress Input
Hello, all. Thanks to everyone who responded to my post. I decided to make sure I had something that worked with what I have now and used Curses to finish it. However, it turns out that the extra work and problems with using GPIO pins and wiring up controllers that way is a small amount of headaches and work compared to what I thought it would be and the software part is actually easier than doing it this way. So in the end I will hooking the Raspberry Pi up directly to the buttons and use the Raspberry Pi's GPIO library to do it all. For posterity and in case other beginners want to look at it, here is what I have with curses that works now. Tried to add a function to cause it to only work for a certain amount of time and that did not work. Will worry about that when I do the new programme based off of directly connected buttons and have those working. Thanks everyone. Here is the keyboard controls the colours script. (Raspberry Pi B+, Blinkstick Pro and LED Adapter, analouge RGB LED light strip.) import curses import atexit import time from datetime import datetime from blinkstick import blinkstick starttime = time.time() screen = curses.initscr() curses.noecho() curses.curs_set(0) screen.keypad(1) screen.nodelay(1) led = blinkstick.find_first() timered = 0 timeyellow = 0 timeblue = 0 timestamp = str(datetime.now()) colour = 0 screen.addstr("Eflag 1") while True: event = screen.getch() if event == ord("q"): flog = open('flag1log.text', 'a') flog.write(timestamp + '\n' + 'Red Team: ' + str(timered) + '\n' + 'Yellow Team: ' + str(timeyellow) + '\n' + 'Blue Team: ' + str(timeblue) + '\n') flog.close() curses.endwin() break elif event == ord("r"): colour = 1 screen.addstr("Red Activated") elif event == ord("y"): colour = 2 screen.addstr("Yellow Activated") elif event == ord("b"): colour = 3 screen.addstr("Blue Activated") if colour == 1: led.pulse(red=255, green=0, blue=0, repeats=1, duration=3000, steps=50) timered += 1 print timered if colour == 2: led.pulse(red=255, green=255, blue=0, repeats=1, duration=3000, steps=50) timeyellow += 1 if colour == 3: led.pulse(red=0, green=0, blue=255, repeats=1, duration=2000, steps=50) timeblue += 1 if time.time() == (time.time() + 30): flog = open('flag1log.text', 'a') flog.write(timestamp + '\n' + 'Red Team: ' + str(timered) + '\n' + 'Yellow Team: ' + str(timeyellow) + '\n' + 'Blue Team: ' + str(timeblue) + '\n') flog.close() curses.endwin() break -- https://mail.python.org/mailman/listinfo/python-list
Re: Keypress Input
Guys, thanks for the various code examples for GPIO and the warning about debouncing issues. I am still considering going the route of more complex wiring and doing it a more traditional GPIO way. -- https://mail.python.org/mailman/listinfo/python-list
Re: Keypress Input
Christian, are you suggesting I learn to do everything perfectly before I ask how to do everything perfectly? Despite your tone and insults I honestly appreciate the response. I know what to focus on and less than 5 minutes from now I will be looking for e- books on the specific subjects you point me to and will have a better idea of where the issues are. -- https://mail.python.org/mailman/listinfo/python-list
Re: Keypress Input
That was the impression I got reading some comments people made online and doing research, so I focused on tkinter. As I mentioned in the 4th sentence of the post you quoted I discovered that was not the case, but by then I had already done some work on the tkinter script so I kept with it. Before I actually tried any of this and was just thinking of it conceptually I bought the Kade device. It was my hope that it would save on soldering, something I hate so very, very much, and I had not started learning Python at that point. It never occurred to me something so simple as keystrokes would not be present in Python, a language rated as being terrific by everyone I know who knows it. Out of great ignorance I thought I could hook up the Kade device and do something like if keypress == r then do this. Also out of ignorance I thought that using the GPIO would require days of soldering and incredibly complicated coding to talk to the pins. Only since I got much more deep into actually doing this did I start to find out otherwise. Spent the last few days looking at breakout boards to see if they would make the wiring easier to test out. When I saw one that had built in electrical protection (RaspIO Pro) I thought about getting it and going the route you suggest. However, I cannot find any option that does not require a breadboard. The lack of compactness and all the exposed wires does bother me a bit. More than it should, but I need a compact setup I can place outdoors. Breadboards and loose wires worry me in this case. Still, I must admit I spent the last 3 or 4 days thinking about exactly what you said and I may do it that way after all just to get it to work software wise. And BTW, I am not using any GPIO for the lights, the LED light strip is hooked up to a Blinkstick Pro through a USB port. -- https://mail.python.org/mailman/listinfo/python-list
Re: Keypress Input
It appears that one of my posts was cut off. It contains my script but none of the lengthy text in front of it. To summarize, my set-up consists of three "massive arcade buttons" from Adafruit. one red, one blue, one yellow. They are connected to a Kade Device that is connected to a Raspberry Pi via USB cable and emulates a USB keyboard. The goal is to get the LED light strip attached to the Pi via a Blinkstick Pro and LED adapter to glow the colour of the button pushed. Also, I want the Pi to record how long it spent as each colour. For development purposes I am using an actual keyboard. The script brings up a blank tkinker window (expected) and the window seems to have focus when it is created. Pressing the keys does nothing. Would appreciate any insight into why it does not respond. Just look a few posts above (Date: Mon, 15 Jun 2015 05:15:31 GMT) and you will see my script. Tried a bunch of different approaches and in some of them, an error message appeared when I pressed a key. It may not have worked, but it was registering the key press. Thanks. -- https://mail.python.org/mailman/listinfo/python-list
Re: Keypress Input
Thank to the others who joined in and posted replies. Michael, your assumption is correct. To quote my original post, "and I want this working on a Raspberry Pi." Doing a superficial look at curses and getch it looks excessively complicated. I was under the impression it was not multi-platform and Linux was excluded. Searching for getch and raspberry pi on the web I see it is not and is available for Raspian. Worried about implementing it but I will consider it again. May spend time reading about during my treatment tomorrow as the hospital has wifi and Internet access and I am allowed to have my laptop there. -- https://mail.python.org/mailman/listinfo/python-list
Re: Keypress Input
from Tkinter import * from blinkstick import blinkstick led = blinkstick.find_first() timered = 0 timeyellow = 0 timeblue = 0 colour = None root = Tk() root.title('eFlag 1') def red1(event): colour = 1 def yellow1(event): colour = 2 def blue1(event): colour = 3 root.bind_all('r', red1) root.bind_all('b', blue1) root.bind_all('y', yellow1) root.mainloop() while colour == None: led.pulse(red=0, green=255, blue=0, repeats=1, duration=5000, steps=50) while colour == 1: led.pulse(red=255, green=0, blue=0, repeats=1, duration=3000, steps=50) timered += 1 while colour == 2: led.pulse(red=255, green=255, blue=0, repeats=1, duration=3000, steps=50) timeyellow += 1 while colour == 3: led.pulse(red=0, green=0, blue=255, repeats=1, duration=2000, steps=50) timeblue += 1 def exit_handler(): print '\033[0;41;37mRed Team:\033[0m ', timered print '\033[0;43;30mYellow Time:\033[0m ', timeyellow print '\033[0;44;37mBlue Time:\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') flog.close() atexit.register(exit_handler) -- https://mail.python.org/mailman/listinfo/python-list
Re: Keypress Input
Laura and Gary, thank you for your replies. I have three physical buttons connected to a Kade device emulating a keyboard. These buttons control an LED light strip. So there is no screen, so a GUI did not cross my mind. I thought it made sense as it is easily done by other scripting languages. Thank you both for pointing my in the right direction. It turns out Tkinter is installed on Raspian and my Pi has it. Typing import tkinter into the Python interpreter gave me an error, then I corrected my spelling. The T should be upper case. No errors with "import Tkinter". Laura, thank you for typing up example code. I had to remove one indent on line 9, but after that it worked on my desktop. The Pi gave an error about Tkinter when I tried to run your code but I will work on that. In the meantime I will work my basic code out on the desktop and then move it over to the Pi, adapting it for and fixing Pi issues then. In my mind the Tkinter information I read on the web contradicts the examples given with the text, so obviously I am not getting it at all. Tkinter seems very confusing to me right now, but I think I just need to review the conceptual stuff again and keep trying. Also, I have your example, which I can experiment with. Thanks. -- https://mail.python.org/mailman/listinfo/python-list
Keypress Input
Hello. Very new to Python and looking for some basic help. Would like a set-up where something happens when a key is pressed. Not propose a question, have the user type something, then hit return, then something happens, but just the R key is pressed, something happens, then something else happens if the B key is pressed, then a third thing happens if the G key is pressed. My research only served to confuse me. Firstly, I do not understand how it is possible for this to be a difficult thing not built into the system for any scripting language made within the last few decades. More to the point I am unclear on specific suggestions. Most of them seem to be for Windows only and I want this working on a Raspberry Pi. Saw getch but I am still confused if it is platform specific or not, or requires a module to be installed or not. Just get errors if I try to install getch using PIP. Other suggestions seemed to be overkill and confused me to due to my beginner level knowledge and the fact these suggestions have other, more complicated elements to them. I just want a button press on a device connected to a Raspberry Pi to trigger an action. If anyone can give me some guidance on this I would appreciate it. Thank you. -- https://mail.python.org/mailman/listinfo/python-list