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

How to do it...

Let's perform the following steps:

  1. First, run the following lines of code in the REPL:
>>> from adafruit_circuitplayground.express import cpx
>>> 
>>> BLACK = 0x000000
>>> GREEN = 0x00FF00
>>> 
>>> cpx.pixels.brightness = 0.10
  1. Keep push button A pressed while you run the following code block. You should see pixel 2, which is located right next to the push button, light up green:
>>> cpx.pixels[2] = GREEN if cpx.button_a else BLACK
  1. Release push button A and run the following code block; you should now see pixel 2 turn off:
>>> cpx.pixels[2] = GREEN if cpx.button_a else BLACK
  1. Add the following code to the main.py file and it will turn pixel 2 and pixel 7 on, depending on whether push button A or push button B is being pressed:
from adafruit_circuitplayground.express import cpx

BLACK = 0x000000
GREEN = 0x00FF00

cpx.pixels.brightness = 0.10
while True:
    cpx.pixels[2] = GREEN if cpx.button_a else BLACK
    cpx.pixels[7] = GREEN if cpx.button_b else BLACK