alphanumeric
Posts: 1827
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Need help getting joystick to work the way i want.

Thu Nov 17, 2016 3:53 pm

Here is my current code.

Code: Select all

b=(200) # brightness

def clamp(value, min_value=75, max_value=255):
    return min(max_value, max(min_value, value))

def pushed_up(event):
    global b
    if event.action != ACTION_PRESSED:
        b = clamp(b - 5)

def pushed_down(event):
    global b
    if event.action != ACTION_PRESSED:
        b = clamp(b + 5)

def pushed_push(event):
    if event.action != ACTION_PRESSED:
        os.system("sudo shutdown now -P")

def pushed_right(event):
    if event.action != ACTION_PRESSED:
        raise SystemExit

sense.stick.direction_up = pushed_up
sense.stick.direction_down = pushed_down
sense.stick.direction_push = pushed_push
sense.stick.direction_right = pushed_right

b is what I use for the brightness on the LED display. That part works, I cab adjust the brightness up down by pressing the joystick up down. The part that doesn't work is joystick right, that I want to just exit the python program without shutting the PI down. And pressing the joystick in the center position to shut the Pi down completely. I had this all working with evdev but can't figure it out with the new joystick python code. I usually run this Pi headless so being able to shut it down with the joystick was a nice feature. I can post the whole file if needed.

User avatar
Paeryn
Posts: 2562
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Need help getting joystick to work the way i want.

Thu Nov 17, 2016 5:47 pm

You've said that pressing right isn't working but you haven't said what is happening, is the program still running as normal?

You are only raising an exception so it's possible that you have a try...except block that is catching it and not propagating it. Also if the pushed_right() function is being called from within a thread then only that thread would be exited, not the whole program.
She who travels light — forgot something.

alphanumeric
Posts: 1827
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Need help getting joystick to work the way i want.

Thu Nov 17, 2016 8:09 pm

Nothing happens, it just keeps running and ignores it. No errors either.

alphanumeric
Posts: 1827
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Need help getting joystick to work the way i want.

Thu Nov 17, 2016 8:13 pm

This the old code that worked.

Code: Select all

def joy():
    r, w, x = select([dev.fd], [], [],0.01)
    for fd in r:
        for event in dev.read():
            if event.type == ecodes.EV_KEY:# and event.value == 1:
                if event.code == ecodes.KEY_UP: # is really stick down
                    sense.low_light = True
                elif event.code == ecodes.KEY_DOWN: # is really stick up
                    sense.low_light = False                                  
                elif event.code == ecodes.KEY_RIGHT: # is really stick left
                    raise SystemExit
                elif event.code == ecodes.KEY_ENTER: # center press
                    os.system("sudo shutdown now -P")


User avatar
Paeryn
Posts: 2562
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Need help getting joystick to work the way i want.

Thu Nov 17, 2016 9:28 pm

Looking at the source code for the senseHAT, when you set the callbacks like you have with

Code: Select all

sense.stick.direction_right = pushed_right
that initiates a separate thread which waits for the events and will call your function when the joystick is moved. So when you try exiting from within the callback you are just terminating the event reading thread (though I would have expected all joystick events to cease from that point since only one thread handles all events).

Probably you need to set some global value within your callback that your main program looks for so it can quit.
She who travels light — forgot something.

alphanumeric
Posts: 1827
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Need help getting joystick to work the way i want.

Fri Nov 18, 2016 2:58 pm

I got it do what I wanted. I changed the one block of code to this.

Code: Select all

b=(200) # brightness
s=(0.065) # scroll speed
x=(100)
def clamp(value, min_value=75, max_value=255):
    return min(max_value, max(min_value, value))
def pushed_up(event):
    global b
    if event.action != ACTION_PRESSED:
        b = clamp(b - 5)
def pushed_down(event):
    global b
    if event.action != ACTION_PRESSED:
        b = clamp(b + 5)
def pushed_push(event):
    global x
    if event.action != ACTION_PRESSED:
        x = clamp(x + 10)
        
def pushed_right(event):
    global x
    if event.action != ACTION_PRESSED:
        x = clamp(x - 10)
Then added this.

Code: Select all

    if x > 100:
        os.system("sudo shutdown now -P")
    elif x < 100:
        raise SystemExit

User avatar
Davespice
Forum Moderator
Forum Moderator
Posts: 1662
Joined: Fri Oct 14, 2011 8:06 pm
Location: The Netherlands
Contact: Twitter

Re: Need help getting joystick to work the way i want.

Mon Nov 21, 2016 9:37 am

There is a single threaded way to use the new Joystick system.
See here: http://pythonhosted.org/sense-hat/api/#joystick

So your code might be something like;

Code: Select all

from sense_hat import SenseHat, ACTION_PRESSED, ACTION_HELD, ACTION_RELEASED
import os

sense = SenseHat()
b=(200) # brightness

def clamp(value, min_value=75, max_value=255):
    return min(max_value, max(min_value, value))

while True:
    for event in sense.stick.get_events():
        if event.direction == 'up' and event.action != ACTION_PRESSED:
            b = clamp(b - 5)
        elif event.direction == 'down' and event.action != ACTION_PRESSED:
            b = clamp(b + 5)
        elif event.direction == 'middle' and event.action != ACTION_PRESSED:
            os.system("sudo shutdown now -P")
        elif event.direction == 'right' and event.action != ACTION_PRESSED:
            raise SystemExit


alphanumeric
Posts: 1827
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Need help getting joystick to work the way i want.

Mon Nov 21, 2016 10:39 am

That link is what I was using for reference. For somebody like me though, that hasn't had any formal training in Python, its a bit confusing. I have yet to get the center "push" to do anything. It's just totally ignored. I will give your code a try though thanks.

Return to “Astro Pi”