pimoroni / pimoroni-pico

Libraries and examples to support Pimoroni Pico add-ons in C++ and MicroPython.
https://shop.pimoroni.com/collections/pico
MIT License
1.28k stars 487 forks source link

Brightness support for 5x5 RGB Matrix Breakout #914

Open alexraileanu opened 5 months ago

alexraileanu commented 5 months ago

Hello,

I'm trying to use the Micropython library for the 5x5 RGB Matrix Breakout but I can't seem to find a way to set the brightness for an individual led. I saw in the examples that there's also no mention of this. Am I missing something?

I have found this other library that does have support for setting brightness but I'm having some issues turning on (some of) the leds on a RPi Pico.

Thanks!

Gadgetoid commented 5 months ago

The Python library just scales the individual colour channels by the brightness value, so you could do something like this:

brightness = 0.5
matrix.set_pixel(x, y, int(r * brightness), int(g * brightness), int(b * brightness))

To achieve the same result.

Brightness control is indeed currently unsupported in the Pico libraries.

alexraileanu commented 5 months ago

That seems to work, thanks a lot @Gadgetoid !

I could try to make a PR to add brightness control (at least for the 5x5 matrix) but I'm not super sure where or how to start. Do you perhaps have any pointers for me?

Thanks again!

Gadgetoid commented 5 months ago

Most of our Python modules are implemented in C++ so it's frustratingly complex. The main thrust of the RGB 5x5 set_pixel is here:

https://github.com/pimoroni/pimoroni-pico/blob/6eb0f90e53e503790150028f2cf181961f589619/libraries/breakout_rgbmatrix5x5/breakout_rgbmatrix5x5.cpp#L27-L36

Which calls:

https://github.com/pimoroni/pimoroni-pico/blob/6eb0f90e53e503790150028f2cf181961f589619/drivers/is31fl3731/is31fl3731.cpp#L83-L85

So ideally here it would multiply the R, G and B channels by a brightness value from 0-255, scale it back to the range 0-255 and then apply gamma using the GAMMA_8BIT table from "common/pimoroni_common.hpp"

That brightness value would then have to be passed through from the Python bindings:

https://github.com/pimoroni/pimoroni-pico/blob/6eb0f90e53e503790150028f2cf181961f589619/micropython/modules/breakout_rgbmatrix5x5/breakout_rgbmatrix5x5.cpp#L49-L85

You can add brightness as an optional keyword arg with something like:

{ MP_QSTR_brightness, MP_ARG_INT, {.u_int = 255I} },

If you fork pimoroni-pico on GitHub, you can push changes up and - with any luck- it'll build you a new MicroPython without you needing to grapple with a local build environment.

Using C bindings for libraries like this was a choice we made to avoid having to write everything twice and it's definitely had its pitfalls. Your time might be better spent trying to get the pure Python library to work 😬

alexraileanu commented 5 months ago

Thanks for the very detailed reply, @Gadgetoid ! I'm not a CPP kinda guy but I'll try!

Your time might be better spent trying to get the pure Python library to work

Trust me I spent far longer on this project than I should have and now that I've found a way to make it work I'm not sure I want to debug that further :p.

Some struggles I had:

Anyway, sorry for the rambling! I'll give the brightness thing a shot somewhere this week. Thanks again!

Gadgetoid commented 5 months ago

flashed it with the Pimoroni Pico Micropython and finally I have both devices working nicely

As contrived and troublesome as our libraries-included MicroPython build is, it's nice when it just has all the things good to go. And good to hear it was your solution!

No time like the present to dive into C++ 😆