|
| 1 | +# Plasma <!-- omit in toc --> |
| 2 | + |
| 3 | +The Plasma library is intended to drive APA102 / DotStar™ or WS2812 / NeoPixel™ LEDs on our [Plasma 2040](https://shop.pimoroni.com/products/plasma-2040), [Plasma 2350](https://shop.pimoroni.com/products/plasma-2350) and [Plasma 2350 W](https://shop.pimoroni.com/products/plasma-2350-w) boards, though it can also be used with your own custom pins/wiring. |
| 4 | + |
| 5 | +It can also be used to drive WS2812 / NeoPixel™ LEDs from [Plasma Stick](https://shop.pimoroni.com/products/plasma-stick-2040-w). Note that APA102 compatibility, user buttons, RGB LED and current sensing functions are not present on Plasma Stick. |
| 6 | + |
| 7 | +- [Notes On PIO Limitations](#notes-on-pio-limitations) |
| 8 | +- [WS2812](#ws2812) |
| 9 | + - [Getting Started](#getting-started) |
| 10 | + - [RGBW and Setting Colour Order](#rgbw-and-setting-colour-order) |
| 11 | + - [Set An LED](#set-an-led) |
| 12 | + - [RGB](#rgb) |
| 13 | + - [HSV](#hsv) |
| 14 | +- [APA102](#apa102) |
| 15 | + - [Getting Started](#getting-started-1) |
| 16 | + - [Set An LED](#set-an-led-1) |
| 17 | + - [RGB](#rgb-1) |
| 18 | + - [HSV](#hsv-1) |
| 19 | + - [Set Brightness](#set-brightness) |
| 20 | +- [Using the Buttons and RGB LED](#using-the-buttons-and-rgb-led) |
| 21 | + - [Buttons](#buttons) |
| 22 | + - [RGBLED](#rgbled) |
| 23 | +- [Measuring LED Strip Current Draw (Plasma 2040 only)](#measuring-led-strip-current-draw-plasma-2040-only) |
| 24 | + - [Analog](#analog) |
| 25 | + |
| 26 | +## Notes On PIO Limitations |
| 27 | + |
| 28 | +The WS2812 and APA102 drivers use the PIO hardware on the RP2040. There are only two PIOs with four state machines each, placing a hard limit on how many separate LED strips you can drive. |
| 29 | + |
| 30 | +The default is `0` for PIO and `0` for PIO state-machine, but you should change these if you plan on running different strand types together, or if you're using something else that uses PIO. |
| 31 | + |
| 32 | +## WS2812 |
| 33 | + |
| 34 | +### Getting Started |
| 35 | + |
| 36 | +Construct a new `WS2812` instance. specifying the number of LEDs: |
| 37 | + |
| 38 | +```python |
| 39 | +import plasma |
| 40 | + |
| 41 | +LEDS = 30 |
| 42 | +FPS = 60 |
| 43 | + |
| 44 | +led_strip = plasma.WS2812(LEDS) |
| 45 | +``` |
| 46 | + |
| 47 | +Start the LED strip by calling `start`. This sets up a timer which tells the RP2040 to DMA the pixel data into the PIO (a fast, asyncronous memory->peripheral copy) at the specified framerate. |
| 48 | + |
| 49 | +```python |
| 50 | +led_strip.start(FPS) |
| 51 | +``` |
| 52 | + |
| 53 | +### RGBW and Setting Colour Order |
| 54 | + |
| 55 | +Some WS2812-style LED strips have varying colour orders and support an additional white element. Two keyword arguments are supplied to configure this: |
| 56 | + |
| 57 | +```python |
| 58 | +led_strip = plasma.WS2812(LEDS, rgbw=True, color_order=plasma.COLOR_ORDER_GRB) |
| 59 | +``` |
| 60 | + |
| 61 | +The available orders are defined as constants in `plasma`: |
| 62 | + |
| 63 | +* `COLOR_ORDER_RGB` |
| 64 | +* `COLOR_ORDER_RBG` |
| 65 | +* `COLOR_ORDER_GRB` |
| 66 | +* `COLOR_ORDER_GBR` |
| 67 | +* `COLOR_ORDER_BRG` |
| 68 | +* `COLOR_ORDER_BGR` |
| 69 | + |
| 70 | +### Set An LED |
| 71 | + |
| 72 | +You can set the colour of an LED in either the RGB colourspace, or HSV (Hue, Saturation, Value). HSV is useful for creating rainbow patterns. |
| 73 | + |
| 74 | +#### RGB |
| 75 | + |
| 76 | +Set the first LED - `0` - to Purple `255, 0, 255`: |
| 77 | + |
| 78 | +```python |
| 79 | +led_strip.set_rgb(0, 255, 0, 255) |
| 80 | +``` |
| 81 | + |
| 82 | +#### HSV |
| 83 | + |
| 84 | +Set the first LED - `0` - to Red `0.0`: |
| 85 | + |
| 86 | +```python |
| 87 | +led_strip.set_hsv(0, 0.0, 1.0, 1.0) |
| 88 | +``` |
| 89 | + |
| 90 | +## APA102 |
| 91 | + |
| 92 | +### Getting Started |
| 93 | + |
| 94 | +Construct a new `APA102` instance, specifying the number of LEDs. |
| 95 | + |
| 96 | +```python |
| 97 | +import plasma |
| 98 | + |
| 99 | +LEDS = 30 |
| 100 | +FPS = 60 |
| 101 | + |
| 102 | +led_strip = plasma.APA102(LEDS) |
| 103 | +``` |
| 104 | + |
| 105 | +Start the LED strip by calling `start`. This sets up a timer which tells the RP2040 to DMA the pixel data into the PIO (a fast, asyncronous memory->peripheral copy) at the specified framerate. |
| 106 | + |
| 107 | +```python |
| 108 | +led_strip.start(FPS) |
| 109 | +``` |
| 110 | + |
| 111 | +### Set An LED |
| 112 | + |
| 113 | +You can set the colour of an LED in either the RGB colourspace, or HSV (Hue, Saturation, Value). HSV is useful for creating rainbow patterns. |
| 114 | + |
| 115 | +#### RGB |
| 116 | + |
| 117 | +Set the first LED - `0` - to Purple `255, 0, 255`: |
| 118 | + |
| 119 | +```python |
| 120 | +led_strip.set_rgb(0, 255, 0, 255) |
| 121 | +``` |
| 122 | + |
| 123 | +#### HSV |
| 124 | + |
| 125 | +Set the first LED - `0` - to Red `0.0`: |
| 126 | + |
| 127 | +```python |
| 128 | +led_strip.set_hsv(0, 0.0, 1.0, 1.0) |
| 129 | +``` |
| 130 | + |
| 131 | +### Set Brightness |
| 132 | + |
| 133 | +APA102 pixels support global brightness, allowing their brightness to be specified independent of their colour. You can set the overall brightness of your strip by calling: |
| 134 | + |
| 135 | +```python |
| 136 | +led_strip.set_brightness(15) |
| 137 | +``` |
| 138 | + |
| 139 | +You can set brightness from `0` to `31`. This directly maps to the 5-bit brightness value sent to the APA102 LEDs. |
| 140 | + |
| 141 | +## Using the Buttons and RGB LED |
| 142 | + |
| 143 | +The `pimoroni` module contains `Button` and `RGBLED` classes to simplify button debounce, auto-repeat and PWM'ing an RGB LED. |
| 144 | + |
| 145 | +```python |
| 146 | +Button(button, invert=True, repeat_time=200, hold_time=1000) |
| 147 | +``` |
| 148 | + |
| 149 | +```python |
| 150 | +RGBLED(r, g, b, invert=True) |
| 151 | +``` |
| 152 | + |
| 153 | +If you're using one of the MicroPython builds from this repo, you can now specify the pin names directly. `help(machine.Pin.board)` will give you a convenient list of available pins and their names. |
| 154 | + |
| 155 | +### Buttons |
| 156 | + |
| 157 | +Import the `Button` class from the `pimoroni` module: |
| 158 | + |
| 159 | +```python |
| 160 | +from pimoroni import Button |
| 161 | +``` |
| 162 | + |
| 163 | +Set up an instance of `Button` for each button. |
| 164 | + |
| 165 | +```python |
| 166 | +button_a = Button("BUTTON_A") |
| 167 | +button_b = Button("BUTTON_B") |
| 168 | +user_sw = Button("USER_SW") |
| 169 | +``` |
| 170 | + |
| 171 | +To get the button state, call `.read()`. If the button is held down, then this will return `True` at the interval specified by `repeat_time` until `hold_time` is reached, at which point it will return `True` every `repeat_time / 3` milliseconds. This is useful for rapidly increasing/decreasing values such as hue: |
| 172 | + |
| 173 | +```python |
| 174 | +state = button_a.read() |
| 175 | +``` |
| 176 | + |
| 177 | +### RGBLED |
| 178 | + |
| 179 | +Import the `RGBLED` class from `pimoroni` : |
| 180 | + |
| 181 | +```python |
| 182 | +from pimoroni import RGBLED |
| 183 | +``` |
| 184 | + |
| 185 | +And set up an instance of `RGBLED` for the LED: |
| 186 | + |
| 187 | +```python |
| 188 | +led = RGBLED("LED_R", "LED_G", "LED_B") |
| 189 | +``` |
| 190 | + |
| 191 | +To set the LED colour, call `.set_rgb(r, g, b)`. Each value should be between 0 and 255: |
| 192 | + |
| 193 | +```python |
| 194 | +led.set_rgb(255, 0, 0) # Full red |
| 195 | +led.set_rgb(0, 255, 0) # Full green |
| 196 | +led.set_rgb(0, 0, 255) # Full blue |
| 197 | +``` |
| 198 | + |
| 199 | +## Measuring LED Strip Current Draw (Plasma 2040 only) |
| 200 | + |
| 201 | +Plasma 2040 features low-side current sensing, letting you measure how much current a strip of LEDs is drawing. This could be used just for monitoring, or as a way to reduce the maximum brightness of a strip to keep its current draw within the range of the USB port or power supply being used. |
| 202 | + |
| 203 | +The `pimoroni` module contains an `Analog` class to simplify the reading of this current draw. |
| 204 | + |
| 205 | +```python |
| 206 | +Analog(pin, amplifier_gain=1, resistor=0) |
| 207 | +``` |
| 208 | + |
| 209 | +### Analog |
| 210 | + |
| 211 | +Import the `Analog` class from `pimoroni` and specify the pin and gain constants for the current sensing: |
| 212 | + |
| 213 | +```python |
| 214 | +from pimoroni import Analog |
| 215 | + |
| 216 | +ADC_GAIN = 50 |
| 217 | +SHUNT_RESISTOR = 0.015 |
| 218 | +``` |
| 219 | + |
| 220 | +And set up an instance of `Analog` for the current sensing: |
| 221 | + |
| 222 | +```python |
| 223 | +sense = Analog("CURRENT_SENSE", ADC_GAIN, SHUNT_RESISTOR) |
| 224 | +``` |
| 225 | + |
| 226 | +To read the current draw, call `.read_current()`. The returned value will be in amps (A): |
| 227 | + |
| 228 | +```python |
| 229 | +print("Current =", sense.read_current(), "A") |
| 230 | +``` |
0 commit comments