0

After messing around with the SenseHAT for the Raspberry Pi (a Model 3B in my case) and testing the LEDs and sensors, I wanted to try out the included joystick as well. Naively going after the "getting-started"-documents and API references, I tried the following (Python, of course):

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from time import sleep
from sense_hat import SenseHat  # SenseHAT API

def stick_data(sense):

    text_col = [255, 127, 0]        # Set local text colour (orange-ish)

    while True:                     # Main joystick event loop
        for event in sense.stick.get_events():
            # event: Joystick "pressed" in any Direction
            if event.action == "pressed":
                # check which direction:
                if event.direction == "up":
                    # get data, round it
                    hum = round(sense.get_humidity(), 2)
                    # scrolling message on SenseHat LED matrix
                    sense.show_message("Humidity(%): " + \
                            str(hum), text_colour=text_col)
                elif event.direction == "down":
                    prs = round(sense.get_pressure(), 2)
                    sense.show_message("Pressure(mbar): " + \
                            str(prs), text_colour=text_col)
                elif event.direction == "left":
                    temph = round(sense.get_temperature_from_humidity(), 2)
                    sense.show_message("Temp.Hum.(C): " + \
                            str(temph), text_colour=text_col)
                elif event.direction == "right":
                    tempp = round(sense.get_temperature_from_pressure(), 2)
                    sense.show_message("Prs.Temp.(C): " + \
                            str(tempp), text_colour=text_col)
                elif event.direction == "middle":
                    sense.show_message("Middle Joystick button pressed", \
                            text_colour=text_col)
                sleep(1)
                sense.clear()
    return

Now, this works just fine when called in a script. However, I was asking myself if this really HAS to draw near-100% cpu loads (when activated via threading, it's more like 130%-140%).

The main culprit is the infinite while-loop, of course(*), but I always assumed there would be some built-in throttling of such loops. But here, it seems the RPi just calls that loop as fast as it possibly can; therefore, some "manual" throttling seems to be required, I guess?

Could somebody kindly drop a hint on how to do such event-handling loop in an economic way? Any help is greatly appreciated!

(*) I monitored cpu load while testing the script and while the script was busy displaying the messages, CPU load dropped way down, so I'm fairly certain the infinte loop is the culprit.

2
  • Why not just add time.sleep(0.1) to your loop?
    – CoderMike
    Commented Feb 7, 2019 at 8:24
  • Now I feel somewhat embarrassed: Adding a sleep(0.1) on the while-loop indentation level indeed brings the cpu load down while the whole thing still works perfectly.. I was always under the assumption that inserting a sleep() there would halt script execution completely; i.e. with a sleep(0.1), you only have a few ms every 100ms to get your joystick movement registered, therefore almost breaking the whole thing. Turns out this is somehow buffered, apparently. Oh well, at least I learnt something! Thanks a lot, CoderMike!
    – user58973
    Commented Feb 7, 2019 at 19:21

1 Answer 1

0

Can you use wait_for_event() or the callback mechanism instead of sense.stick.get_events()?

1)

while True:
    event = sense.stick.wait_for_event()
    if event.direction == 'up' and event.action == 'pressed':
        print("up")
    elif event.direction == 'down' and event.action == 'pressed':
        print("down")

2)

def pushed_up(event):
    if event.action == 'pressed':
        print("up")

def pushed_down(event):
    if event.action == 'pressed':
        print("down")

sense.stick.direction_up = pushed_up
sense.stick.direction_down = pushed_down

More info: https://pythonhosted.org/sense-hat/api/#joystick

1
  • Thanks for showing me these! Tried them and they work just fine, without that massive cpu load! :) I'll let it stand for a little while longer and mark it as Answer then. Thanks again!
    – user58973
    Commented Feb 7, 2019 at 19:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.