A picoStrip Class
Making a class called "picoStrip" to operate the LED strip:
import board
import neopixel
import time
# this is our class
class picoStrip:
def __init__(gpPin = board.GP0, nPix = 20):
self.pixels = neopixel.NeoPixel(gpPin, nPix)
def lightsOff():
for i in range(20):
self.pixels[i] = (0,0,0)
def allBlue():
for i in range(20):
pixels[i] = (0,0,10)
time.sleep(0.1)
# Now we start our main program
myStrip = picoStrip(board.GP0, 20)
myStrip.allBlue() # Make the lights blue
myStrip.lightsOff() # Turn off the lights
class picoStrip:
(line 6)- create the class with the name "picoStrip"
- Everything tabbed over after the class definition is in the class. This includes statements and, in this case, our two functions.
def __init__(gpPin = board.GP0, nPix = 20):
(line 8)- the first function in the class is called "__init__" and we use it to set up and store variables all the functions/methods in the class can use. It defines what variables need to be passed to the class when it's created.
- When we call this class we'll need to pass two variables "gpPin" and "nPix", so it can set up the Pico, but we give the variables default values of "board.GP0" and "20" respectively. The default values are also good hints as to what types of information the class expects.
self.pixels = neopixel.NeoPixel(gpPin, nPix)
(line 9)- here we set up the RPi pico board using the neopixel class we imported on line 2 of the code. This works just like all of our previous programs, however, instead of "pixels = " we use "self.pixels =" because defining the variable with "self." makes it available to the other methods in the class.
- Remember, variables created in a function only exist in that function (scope), so use "self." if you need other methods in a class to use the variable.
def lightsOff():
(line 11)- Our lightsOff function, but in a class we'll call it a "method". One difference from our function, is that we use the variable "self.pixels" we created in the __init__ method instead of "pixels".
myStrip = picoStrip(board.GP0, 20)
(line 21)- We make an "instance" of the class called "myStrip", which implies that we could make multiple instances of the same class in the code, which is true.
- Note that we pass the board pin "board.GP0" and the number of pixels "20" to the initialization function of the class.
- If we had a number of LED strips connected to our pico we'd create one instance of the picoStrip class (with a different name) for each strip.
myStrip.allBlue() # Make the lights blue
(line 23)- This is how we call the allBlue method of our picoStrip class.
myStrip.lightsOff()
(line 24)- Now turn off all the lights.
Assignment 1
Adapt the picoStrip class, to ADD a method that makes all the lights red, then adapt the program so it:- makes all the lights blue,
- Turn off the lights
- Wait for 1 second,
- Light up all the lights again (red)
- Turn off the lights
Code ▼
import board
import neopixel
import time
# this is our class
class picoStrip:
def __init__(gpPin = board.GP0, nPix = 20):
self.pixels = neopixel.NeoPixel(gpPin, nPix)
def lightsOff():
for i in range(20):
self.pixels[i] = (0,0,0)
def allBlue():
for i in range(20):
pixels[i] = (0,0,10)
time.sleep(0.1)
def allRed():
for i in range(20):
pixels[i] = (10,0,0)
time.sleep(0.1)
# Now we start our main program
myStrip = picoStrip(board.GP0, 20)
myStrip.allBlue() # Make the lights blue
myStrip.lightsOff() # Turn off the lights
myStrip.allRed() # Make the lights blue
myStrip.lightsOff() # Turn off the lights
Passing Parameters to a Class and Having Methods Refer to Each Other
Now let's make a method that runs the allBlue and allRed methods a given number of times.
import board
import neopixel
import time
# this is our class
class picoStrip:
def __init__(gpPin = board.GP0, nPix = 20):
self.pixels = neopixel.NeoPixel(gpPin, nPix)
def lightsOff():
for i in range(20):
self.pixels[i] = (0,0,0)
def allBlue():
for i in range(20):
pixels[i] = (0,0,10)
time.sleep(0.1)
def allRed():
for i in range(20):
pixels[i] = (10,0,0)
time.sleep(0.1)
def blueRed(n = 2):
for i in range(n):
self.allBlue()
time.sleep(1)
self.allRed()
time.sleep(1)
# Now we start our main program
myStrip = picoStrip(board.GP0, 20)
myStrip.blueRed()
def blueRed(n = 2):
- Method named "blueRed" that takes a single parameter "n" that has a default value of 2.
for i in range(n): self.allBlue() time.sleep(1) ...
- A loop that runs "n" times and each time it calls the allBlue method using "self.allBlue()" "self."" allows us to refer to other methods in the same class.
Assignment 3: Method practice
Add two more methods to your picoStrip class