Coder Social home page Coder Social logo

Comments (5)

tolnai avatar tolnai commented on July 20, 2024 1

Thanks for the quick feedback. I was trying payloads like { "set_pixel" : { "x": 0, "y": 0, "args": [255, 255, 255] } } or { "set_pixel" : { "x": 0, "y": 0, "pixel": [255, 255, 255] } }, but since *args is not named, they didn't work - just as you said. Due to the signature of set_pixel I guess we just can't mixed named and non-named vars.

I tried to do some magic with having a special "args" in kwargs, popping it and passing then both kwargs and args into set_pixel but it was not working, so just simply edited toe code to:

elif f=='set_pixel': sense_led.sense.set_pixel(*f_kwargs)

And then using the payload { "set_pixel" : [0, 0, 255, 255, 255] } works fine.

from rpi-sensehat-mqtt.

cgomesu avatar cgomesu commented on July 20, 2024

none of them worked

can you provide an example of payload?

the portion of the code that is relevant to you is the following:

for f in payload.keys():
f_kwargs = payload[f] if payload[f] else {}
try:
# https://pythonhosted.org/sense-hat/api/#led-matrix
# if a valid setter, call with kwargs; else, log and skip.
if f=='set_rotation': sense_led.sense.set_rotation(**f_kwargs)
elif f=='flip_h': sense_led.sense.flip_h(f_kwargs)
elif f=='flip_v': sense_led.sense.flip_v(f_kwargs)
elif f=='set_pixels': sense_led.sense.set_pixels(**f_kwargs)
elif f=='set_pixel': sense_led.sense.set_pixel(**f_kwargs)
elif f=='load_image': sense_led.sense.load_image(**f_kwargs)
elif f=='clear': sense_led.sense.clear(**f_kwargs)
elif f=='show_message': sense_led.sense.show_message(**f_kwargs)
elif f=='show_letter': sense_led.sense.show_letter(**f_kwargs)
elif f=='wait': stop_streaming.wait(f_kwargs)
else: logger.warning(f"The method '{f}' in the payload '{payload}' is not supported.")
except TypeError as terr:
logger.info(f"Unable to call '{f}' with args '{f_kwargs}': {terr}")
except Exception as oerr:
# catch other exceptions that might propagate from SenseHat methods
logger.warning(f"There was a non-specific error running method '{f}': {oerr}")

in short, it assumes you are passing key:value (kwarg) structure when it mentions f_kwargs, as in a dictionary data structure. so, a method, such as set_pixel() must define named parameters for this to work. according to the code you mentioned, it does--namely, x and y. the problem is how pixels is implemented there. it assumes that they are expanded by args (e.g., tuple or list-like structure) that gives the RGB or an iterable that contains them, but such parameter is not named in the signature of the method, and therefore, the line

elif f=='set_pixel': sense_led.sense.set_pixel(**f_kwargs)

won't work. A simple fix is to edit it to parse as *args instead (unnamed expansion) and in your payload, you send an iterable with the parameter values:

elif f=='set_pixel': sense_led.sense.set_pixel(*f_kwargs)
{
    "set_pixel" : "[x,y,r,g,b]"
}

if that doesn't work, then you might need to do a bit of preprocessing on the payload. say, pass a string with a comma separating the parameters and then do the following:

elif f=='set_pixel': sense_led.sense.set_pixel(*f_kwargs.split(','))
{
    "set_pixel" : "x,y,r,g,b"
}

that's my intuition right now. it's been a while since I last touched this code... anyway, give it a try, play around with it, and let me know if it doesn't work.

from rpi-sensehat-mqtt.

cgomesu avatar cgomesu commented on July 20, 2024

for my own future reference:

elif f=='set_pixel': sense_led.sense.set_pixel(*f_kwargs.split(','))

this actually might be a better strategy for sending and parsing payloads.

from rpi-sensehat-mqtt.

tolnai avatar tolnai commented on July 20, 2024

If I may ask for some more improvement, now I can send multiple commands, but only different commands. That is not that useful (flipping and setting a pixel?) - I guess most use cases are either just show a text/image, or setting 1 or more pixels. This latter is not possible at the moment, it needs separate mqtt messages. I don't mind, will handle it with node-red, but it's a suggestion.
Also, it would be awesome if this module would handle some timing transparently (like fading or blinking), which is difficult todo externally (what if some messages get lost, the case of clearing a blinking pixel means stopping a blinking cycle, etc.). But I know this is really just a wrapper for the sensehat library. :)

from rpi-sensehat-mqtt.

cgomesu avatar cgomesu commented on July 20, 2024

But I know this is really just a wrapper for the sensehat library

exactly. the enhancements you mentioned are sensible ones and there are other things I want to improve in the current implementation (one of the threads use way too much cpu time and I'm not sure why). however, my hands are full at the moment (and will likely continue to be until the end of the year).

if you don't mind, please open a new issue for each of such enhancement requests and I'll try to take a closer look at them when I have a bit of free time. also, feel free to make the changes yourself and submit a PR to add them here. help is always welcomed!

from rpi-sensehat-mqtt.

Related Issues (3)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.