Created
November 16, 2023 15:30
-
-
Save stonehippo/e67e07d71b6108da859ad283f010eeca to your computer and use it in GitHub Desktop.
Some CircuitPython to control an RGBW LED strip.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Simple CircuitPython control of an RGB strip via Mosfetti or plain MOSFETS. | |
Copyright (c) 2023 George White <stonehippo@gmail.com> | |
Written for the Adafruit Metro M0, but will work with any board with | |
4 PWM-capable pins. | |
""" | |
import board | |
from pwmio import PWMOut | |
from time import sleep | |
# Each color is on a single pin and is applied to the whole strip. | |
green = PWMOut(board.D3) | |
red = PWMOut(board.D4) | |
blue = PWMOut(board.D5) | |
white = PWMOut(board.D6) | |
# Convenience levels. You can use any value from 0-65535. | |
OFF=0 | |
HALF= 2 ** 15 | |
FULL = 2 ** 16 - 1 | |
# Set a single cooler at a given level | |
def set_light(light, val): | |
light.duty_cycle = val | |
""" | |
blend() is a universal control function. It can take a list of lights | |
(it could use a tuple, too, but this breaks enumeration with a single | |
light), and a level that gets applied to all of the list LEDs. | |
Using a combination of calls to blend, you can set any color and level | |
you might desire. | |
""" | |
def blend(lights=(red,green,blue,white), level=OFF): | |
[set_light(l, level) for l in lights] | |
def all_off(): | |
blend() | |
def all_on(): | |
blend(level=HALF) | |
def fade(steps=64, direction=True, min=OFF, max=(FULL+1)): | |
r = range(min, max, int((max)/steps)) | |
if direction: | |
for i in r: | |
blend(level=i) | |
sleep(0.05) | |
else: | |
for i in reversed(r): | |
blend(level=i) | |
sleep(0.05) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment