Classes

Now that we have a set of functions for working with the LED's, we'll organize them into a single structure, called a class.
Note on Pico vs. Zero:
  1. For the Pico use board.GP0 instead of board.D18 because they are set up to use different input pins by default.

References:

  1. QuickRef: Classes

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:

docFuncs.py

              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.

ledClass.py

              import board
              import neopixel
              import time

              pixels = neopixel.NeoPixel(board.GP0, 20)

              class uLED:

                def __init__(self, nPix):
                    self.nPix = nPix

                

            

Note:

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:

ledClass.py

              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)

            

Let's add one more method, lightUp that turns on all the lights for a given color (the default color is blue).

ledClass.py

              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)

            

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).

classTest.py

              from ledClass import *

              leds = uLED(20)

              leds.lightUp()

            

Running the classTest.py file should cause the all the lights to light up as blue.

Assignment 1

Create your own class to control the leds in its own separate file. Write a program in a separate file that imports the class and tests the methods.