theMusicMan
Posts: 111
Joined: Tue May 23, 2017 8:41 pm

Sense Hat - how to detect joystick button press in python and node-red

Wed Apr 18, 2018 7:17 pm

Hi all

Not sure if this thread should be in two separate posts... hey ho...

So, I am trying to write some code in both python3 and node-red that I can use to detect the pressing of the joystick button on the pi Sense Hat but sadly I am having no luck finding anything online about it.

Can anyone offer some help please?

Many thanks
John

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

Re: Sense Hat - how to detect joystick button press in python and node-red

Wed Apr 18, 2018 7:41 pm

Have a look here, https://pythonhosted.org/sense-hat/api/#joystick
I use it on mine to do things. here is some of my code. I have my sense hat rotated 180 degrees, its essentially upside down. Can't help you with the node red stuff though.

Code: Select all

import os
import time, datetime
from sense_hat import SenseHat, ACTION_PRESSED, ACTION_HELD, ACTION_RELEASED
        
sense = SenseHat()
sense.set_rotation(180)
sense.set_imu_config(False, False, False)
sense.low_light = True

s=(0.065) # scroll speed
w=(0) # color all white toggle
x=(2) #shutdown variable

# is really stick down
def pushed_up(event):
    if event.action == ACTION_PRESSED:
       sense.low_light = True
        
# is really stick up
def pushed_down(event):
    if event.action == ACTION_PRESSED:
       sense.low_light = False

#is really stick right
def pushed_left(event):
    global w
    if event.action == ACTION_PRESSED:
        w = (255)
        
# is really stick left
def pushed_right(event):
    global w
    if event.action == ACTION_PRESSED:
        w = (0)

def pushed_middle(event):
    global x
    if event.action == ACTION_PRESSED:
        x = 0

sense.stick.direction_up = pushed_up
sense.stick.direction_down = pushed_down
sense.stick.direction_left = pushed_left
sense.stick.direction_right = pushed_right
sense.stick.direction_middle = pushed_middle

while True:



TinRobot
Posts: 13
Joined: Thu Apr 12, 2018 10:12 pm

Re: Sense Hat - how to detect joystick button press in python and node-red

Wed Apr 18, 2018 7:43 pm

I will be interested in this as well. I just got a sense hat to learn some python coding. This looks like fun stuff, thanks for any help given!

theMusicMan
Posts: 111
Joined: Tue May 23, 2017 8:41 pm

Re: Sense Hat - how to detect joystick button press in python and node-red

Wed Apr 18, 2018 8:11 pm

Excellent resource alphanumeric - many thanks for the heads up!

It really is fantastic how wonderfully supportive everyone here is. Thanks so much - really appreciated.

Return to “Beginners”