Keypress Input

classic Classic list List threaded Threaded
25 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Keypress Input

Michael Torrie
On 06/18/2015 01:35 PM, Oscar Benjamin wrote:
> I use the following. I found in testing that when you push the button it
> prints 'Button pressed' 10 times a second (in actual use it calls poweroff
> so I guess bounce isn't an issue there). Is there some reason it needs to
> be cleverer in this case?

Yes, that would be expected, given your code has a while loop that never
exits.  Just curious what you expect the code to do that it's not doing.

You are probably right that debouncing isn't important in your
application.  So just add your poweroff command after the print()
statement, and break out of the loop:

...
while True:
    time.sleep(0.1)
    if not GPIO.input(PIN_NUM):
        print('Button pressed')
        # run shutdown command here
        os.system('/usr/sbin/shutdown')
        break




Reply | Threaded
Open this post in threaded view
|

Keypress Input

Christian Gollwitzer
In reply to this post by John McKenzie
Am 15.06.15 um 07:15 schrieb John McKenzie:

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

The nonsense starts here:

===================

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

it seems you don't understand event based programming. root.mainloop()
never exits. It waits for the user input and does the dispatching, i.e.
when a key is pressed, then according to your bindings, the functions
red1, yellow1, blue1 are called, which set a variable but do not do
nything else. To see that, just insert a print statement into these
functions:

  def red1(event):
       colour = 1
       print("Red ws called")


Now your job is to also do the functionality there, i.e. you have to
reformulate your task (waiting for red, then blue...) as a state
machine. Alternatively you can circumvent to redo the logic in a state
machine by using a coroutine.

You should read a text about GUI programming, or more specifically event
based programming, to understand your mistake.

        Christian


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


Reply | Threaded
Open this post in threaded view
|

Keypress Input

John McKenzie

 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.



Reply | Threaded
Open this post in threaded view
|

Keypress Input

John McKenzie
In reply to this post by John McKenzie

 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.

Reply | Threaded
Open this post in threaded view
|

Keypress Input

Michael Torrie
On 06/20/2015 09:02 AM, John McKenzie wrote:
>
>  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.

You can wire up the button without a little breadboard or circuit board
using this general diagram:

https://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/robot/buttons_and_switches/

Just solder the resistors to the wires themselves, and keep them close
to the pi to keep them clean.  Then you you just have to route one wire
per switch, with one common wire hitting the other side of each switch.
 Probably you will want to use the pull-up version where the switch
pulls the signal down to ground.  You may want to use a header to plug
into the rpi.  But besides that should just be a matter of routing wires.

But on the original track, you might want to ask on the kade forums if
there's any way to interact with kade and get events besides HID
emulation.  For example, maybe you could communicate with it over a
virtual serial port.  Or some other protocol so you don't have to worry
about ttys and Linux kernel HID handling for your program, especially in
a headless environment.  Using HID emulation makes sense in a MAME
arcade environment but not for a headless raspberry pi application.



12