Demo and document RGBLED and Button

Add constants to `plasma` module for Plasma 2040 pins.
This commit is contained in:
Phil Howard 2021-07-28 13:55:23 +01:00
parent 596fb55a57
commit 59d3c91f1b
3 changed files with 94 additions and 0 deletions

View File

@ -0,0 +1,21 @@
import time
# Import pin constants from plasma
from plasma import PIN_LED_R, PIN_LED_G, PIN_LED_B, PIN_BUTTON_A, PIN_BUTTON_B
# Import helpers for RGB LEDs and Buttons
from pimoroni import RGBLED, Button
led = RGBLED(PIN_LED_R, PIN_LED_G, PIN_LED_B)
led.set_rgb(255, 0, 0)
button_a = Button(PIN_BUTTON_A)
button_b = Button(PIN_BUTTON_B)
while True:
if button_a.read():
print("Pressed A - {}".format(time.ticks_ms()))
led.set_rgb(0, 255, 0)
if button_b.read():
print("Pressed B - {}".format(time.ticks_ms()))
led.set_rgb(0, 0, 255)

View File

@ -14,6 +14,9 @@ The Plasma library is intended to drive APA102 / DotStar™ or WS2812 / NeoPixel
- [Set An LED](#set-an-led-1)
- [RGB](#rgb-1)
- [HSV](#hsv-1)
- [Using the Buttons & RGB LED](#using-the-buttons--rgb-led)
- [Buttons](#buttons)
- [RGBLED](#rgbled)
## Notes On PIO Limitations
@ -111,4 +114,69 @@ Set the first LED - `0` - to Red `0.0`:
```python
led_strip.set_hsv(0, 0.0, 1.0, 1.0)
```
## Using the Buttons & RGB LED
The `pimoroni` module contains `Button` and `RGBLED` classes to simplify button debounce, auto-repeat and PWM'ing an RGB LED.
```python
Button(button, invert=True, repeat_time=200, hold_time=1000)
```
```python
RGBLED(r, g, b, invert=True)
```
The `plasma` module contains constants for the LED and button pins:
* `plasma.PIN_LED_R` = 16
* `plasma.PIN_LED_G` = 17
* `plasma.PIN_LED_B` = 18
* `plasma.PIN_BUTTON_A` = 12
* `plasma.PIN_BUTTON_B` = 13
### Buttons
Import the `Button` class from the `pimoroni` module and the pin constants for the buttons:
```python
from pimoroni import Button
from plasma import PIN_BUTTON_A, PIN_BUTTON_B
```
Set up an instance of `Button` for each button:
```python
button_a = Button(PIN_BUTTON_A)
button_b = Button(PIN_BUTTON_B)
```
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 `hold_time / 3` milliseconds. This is useful for rapidly increasing/decreasing values such as hue:
```python
state = button_a.read()
```
### RGBLED
Import the `RGBLED` class from `pimoroni` and the pin constants for the buttons:
```python
from pimoroni import RGBLED
from plasma import PIN_LED_R, PIN_LED_G, PIN_LED_B
```
And set up an instance of `RGBLED` for the LED:
```python
led = RGBLED(PIN_LED_R, PIN_LED_G, PIN_LED_B)
```
To set the LED colour, call `.set_rgb(r, g, b)`. Each value should be between 0 and 255:
```python
led.set_rgb(255, 0, 0) # Full red
led.set_rgb(0, 255, 0) # Full green
led.set_rgb(0, 0, 255) # Full blue
```

View File

@ -57,6 +57,11 @@ STATIC const mp_map_elem_t plasma_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_plasma) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_APA102), (mp_obj_t)&PlasmaAPA102_type },
{ MP_OBJ_NEW_QSTR(MP_QSTR_WS2812), (mp_obj_t)&PlasmaWS2812_type },
{ MP_ROM_QSTR(MP_QSTR_PIN_LED_R), MP_ROM_INT(16) },
{ MP_ROM_QSTR(MP_QSTR_PIN_LED_G), MP_ROM_INT(17) },
{ MP_ROM_QSTR(MP_QSTR_PIN_LED_B), MP_ROM_INT(18) },
{ MP_ROM_QSTR(MP_QSTR_PIN_BUTTON_A), MP_ROM_INT(12) },
{ MP_ROM_QSTR(MP_QSTR_PIN_BUTTON_B), MP_ROM_INT(13) },
};
STATIC MP_DEFINE_CONST_DICT(mp_module_plasma_globals, plasma_globals_table);