0

getpixelcolor implementation in Circuitpython?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

getpixelcolor implementation in Circuitpython?

by carlmcl on Sun Jan 13, 2019 8:34 pm

I'm trying to convert some Arduino code I have to Circuitpython, but I'm running into issues. Mostly I cannot seem to find any documentation on a 'getpixelcolor' type function in Circuitpython. The documentation here: https://circuitpython.readthedocs.io/pr ... en/latest/ makes no mention of it, so I fear it is not implemented.

carlmcl
 
Posts: 2
Joined: Sun Mar 12, 2017 2:02 pm

Re: getpixelcolor implementation in Circuitpython?

by oesterle on Sun Jan 13, 2019 10:26 pm

Hi, carmcl!

In Arduino, there are separate calls for setPixelColor() and getPixelColor().

In CircuitPython, it's actually simpler. Let's say you use the example code in the CircuitPython NeoPixel docs to set the first and last pixels to red and green:

Code: Select all | TOGGLE FULL SIZE
import board
import neopixel

pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, auto_write=False)
pixels[0] = (10, 0, 0)
pixels[9] = (0, 10, 0)
pixels.show()

If you want to get the first pixel value, you use the same notation to read the value:
Code: Select all | TOGGLE FULL SIZE
color = pixels[0]

Now, we've created a new variable color, which contains the value of the first pixel: (10, 0, 0), a dim red.

Then you could do:
Code: Select all | TOGGLE FULL SIZE
pixels[4] = color
pixels.show()

That sets pixel 4 to the same color as pixel 0.

(In this case need to do pixels.show() to cause our new color(s) to show up on the NeoPixel strip. In CircuitPython, it's also possible to set up the strip to show new colors instantly when a list value is changed. But this is slower when changing lots of pixels. See the linked docs above for more.)

Cheers,

Eric

oesterle
 
Posts: 621
Joined: Tue Sep 17, 2013 11:32 pm

Re: getpixelcolor implementation in Circuitpython?

by carlmcl on Sun Jan 13, 2019 11:15 pm

Wow, that is much easier, it hadn’t occurred to me to just reverse the assignment.
Thanks so much for the quick reply.

carlmcl
 
Posts: 2
Joined: Sun Mar 12, 2017 2:02 pm

Please be positive and constructive with your questions and comments.