My LED Class
To, hopefully, keep things more organized, we'll create a class to control the LEDs in a separate file and import it as we did when we imported functions.
Let's start with the file docFuncs.py with all the functions:
import board
import neopixel
import time
pixels = neopixel.NeoPixel(board.GP0, 20)
def lightsOff():
for i in range(20):
pixels[i] = (0,0,0)
def lightUp(n=20, color=(0,0,10)):
for i in range(n):
pixels[i] = color
time.sleep(0.1)
def lastLights(n=20, color=(0,10,0)):
startLed = 20-n
for i in range(startLed, 20):
print(i)
pixels[i] = color
time.sleep(0.1)
def complementaryColor(color):
r = 255 - color[0]
g = 255 - color[1]
b = 255 - color[2]
return (r, g, b)
We'll create a new file called ledClass.py and create a class called uLED. I've named the file and the class different things so it will be a little bit clearer what we're doing when we import the class later on, but it's pretty common to give them the same names.
import board
import neopixel
import time
pixels = neopixel.NeoPixel(board.GP0, 20)
class uLED:
def __init__(self, nPix):
self.nPix = nPix
Note:
- The class has an initialization function (method) called __init__
- Functions inside classes are referred to as methods
- The first arguement passed to the __init__ method is self which will be used to help different methods in the class communicate with each other. You'll recall that a variable created within a function is locally scoped, which means it only exists within that function and can not (or at least should not) be used in other functions. self helps get around this.
- Line 10: The second parameter passed to the __init__ function (on line 9) is nPix, which is the number of pixels in the LED array. To store this variable within the class for use by other methods we assign it to self.nPix
Now we add in one of our functions (lightsOff, which turns off all the LEDs) from the docFuncs.py file as a method in the uLED class:
import board
import neopixel
import time
pixels = neopixel.NeoPixel(board.GP0, 20)
class uLED:
def __init__(self, nPix):
self.nPix = nPix
def lightsOff(self):
for i in range(self.nPix):
pixels[i] = (0,0,0)
- Line 12: We pass the self variable as the first argument to the lightsOff method because we want to be able to use the self.nPix variable (on Line 13) that was stored as self.nPix when the class was initialized (on Line 10).
Let's add one more method, lightUp that turns on all the lights for a given color (the default color is blue).
import board
import neopixel
import time
pixels = neopixel.NeoPixel(board.GP0, 20)
class uLED:
def __init__(self, nPix):
self.nPix = nPix
def lightsOff(self):
for i in range(self.nPix):
pixels[i] = (0,0,0)
def lightUp(self, color=(0,0,10)):
for i in range(self.nPix):
pixels[i] = color
time.sleep(0.1)
- Line 16: Again, the first argument that is passed to the method is self, which allows the self.nPix variable to be used on line 17.
Now that we have our class, we'll need to create a file (classTest.py) to use it. The file must be in the same folder/directory as the ledClass.py file, and for the Raspberry Pi Pico, they must be in the root directory of the Pico (the same directory that has the default code.py file).
from ledClass import *
leds = uLED(20)
leds.lightUp()
Running the classTest.py file should cause the all the lights to light up as blue.
- Line 1: This basically means 'from the ledClass.py file, import everything'. The '*' wildcard means everything. You don't add the .py extension to the name in the import statement.
- Line 3: Create an instance of the uLED class with the variable name leds and set the number of pixels (nPix) to 20. When we create an instance of a class it runs the __init__ method, which, if you recall, saves the nPix value as self.nPix.
- The __init__ method in the uLED class looks like it requires two parameters to be passed to it, self and nPix, however, we only need to pass one (nPix) because the self parameter is implied in methods of a class.
- Line 5: We use the lightUp method to light the LEDs, letting it use the default color.