MicroPython Cookbook
上QQ阅读APP看书,第一时间看更新

How it works...

The first line of code imports the cpx object that will be used to control the NeoPixels. This object has an attribute called pixels, which can be accessed like a list. The index that is used indicates which of the 10 NeoPixels is to be manipulated.

In the first code snippet, we set the value to a tuple that represents the desired color—it is comprised of red, green, and blue values. Each value should be represented as an integer from 0 to 255. By setting the value to (255, 0, 0), the red LED will be at its highest value and the green and blue LEDs will be switched off. This will create the color red.

Following the same approach, the NeoPixel is then set to green and blue by providing the correct values for each of those colors. The current RGB values of a specific pixel can also be easily retrieved by simply accessing the value of any specific example.

A pixel can be switched off by setting all the RGB components to a value of 0, as shown in the preceding code in this recipe. The final preceding code snippet just gives an example of setting the second pixel to the color red by referring to the correct index value.