Merge pull request #191 from pimoroni/patch_plasma2040

Updates to Plasma 2040 C++ and MP examples
pull/195/head v0.2.5
Philip Howard 2021-08-19 21:59:47 +01:00 zatwierdzone przez GitHub
commit c4ea05a763
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
34 zmienionych plików z 352 dodań i 118 usunięć

Wyświetl plik

@ -1,3 +1,4 @@
add_subdirectory(analog)
add_subdirectory(esp32spi) add_subdirectory(esp32spi)
add_subdirectory(ioexpander) add_subdirectory(ioexpander)
add_subdirectory(ltp305) add_subdirectory(ltp305)
@ -18,4 +19,5 @@ add_subdirectory(bme68x)
add_subdirectory(bmp280) add_subdirectory(bmp280)
add_subdirectory(bme280) add_subdirectory(bme280)
add_subdirectory(button) add_subdirectory(button)
add_subdirectory(plasma)
add_subdirectory(rgbled) add_subdirectory(rgbled)

Wyświetl plik

@ -0,0 +1 @@
include(analog.cmake)

Wyświetl plik

@ -0,0 +1,11 @@
set(DRIVER_NAME analog)
add_library(${DRIVER_NAME} INTERFACE)
target_sources(${DRIVER_NAME} INTERFACE
${CMAKE_CURRENT_LIST_DIR}/${DRIVER_NAME}.cpp
)
target_include_directories(${DRIVER_NAME} INTERFACE ${CMAKE_CURRENT_LIST_DIR})
# Pull in pico libraries that we need
target_link_libraries(${DRIVER_NAME} INTERFACE pico_stdlib hardware_adc)

Wyświetl plik

@ -0,0 +1,18 @@
#include "analog.hpp"
namespace pimoroni {
uint16_t Analog::read_raw() {
return adc_read();
}
float Analog::read_voltage() {
return ((float)adc_read() * 3.3f) / (1 << 12) / amplifier_gain;
}
float Analog::read_current() {
if(resistor > 0.0f)
return read_voltage() / resistor;
else
return read_voltage();
}
};

Wyświetl plik

@ -0,0 +1,31 @@
#pragma once
#include <stdint.h>
#include "pico/stdlib.h"
#include "hardware/adc.h"
#include "common/pimoroni_common.hpp"
namespace pimoroni {
class Analog {
public:
Analog(uint pin, float amplifier_gain = 1.0f, float resistor = 0.0f) :
pin(pin), amplifier_gain(amplifier_gain), resistor(resistor) {
adc_init();
//Make sure GPIO is high-impedance, no pullups etc
adc_gpio_init(pin);
//Select ADC input 0 (GPIO26)
adc_select_input(pin - 26);
};
uint16_t read_raw();
float read_voltage();
float read_current();
private:
uint pin;
float amplifier_gain;
float resistor;
};
}

Wyświetl plik

@ -0,0 +1 @@
include(plasma.cmake)

Wyświetl plik

@ -0,0 +1,18 @@
set(DRIVER_NAME plasma)
add_library(${DRIVER_NAME} INTERFACE)
target_sources(${DRIVER_NAME} INTERFACE
${CMAKE_CURRENT_LIST_DIR}/apa102.cpp
${CMAKE_CURRENT_LIST_DIR}/ws2812.cpp
)
target_include_directories(${DRIVER_NAME} INTERFACE ${CMAKE_CURRENT_LIST_DIR})
target_link_libraries(${DRIVER_NAME} INTERFACE
pico_stdlib
hardware_pio
hardware_dma
)
pico_generate_pio_header(${DRIVER_NAME} ${CMAKE_CURRENT_LIST_DIR}/apa102.pio)
pico_generate_pio_header(${DRIVER_NAME} ${CMAKE_CURRENT_LIST_DIR}/ws2812.pio)

Wyświetl plik

@ -29,7 +29,7 @@ namespace plasma {
public: public:
static const uint SERIAL_FREQ_400KHZ = 400000; static const uint SERIAL_FREQ_400KHZ = 400000;
static const uint SERIAL_FREQ_800KHZ = 800000; static const uint SERIAL_FREQ_800KHZ = 800000;
static const uint DEFAULT_SERIAL_FREQ = SERIAL_FREQ_400KHZ; static const uint DEFAULT_SERIAL_FREQ = SERIAL_FREQ_800KHZ;
enum class COLOR_ORDER { enum class COLOR_ORDER {
RGB, RGB,
RBG, RBG,

Wyświetl plik

@ -33,4 +33,4 @@ add_subdirectory(pico_trackball_display)
add_subdirectory(pico_audio) add_subdirectory(pico_audio)
add_subdirectory(pico_wireless) add_subdirectory(pico_wireless)
add_subdirectory(plasma_2040) add_subdirectory(plasma2040)

Wyświetl plik

@ -0,0 +1,15 @@
set(OUTPUT_NAME plasma2040_rainbow)
add_executable(${OUTPUT_NAME} plasma2040_rainbow.cpp)
target_link_libraries(${OUTPUT_NAME}
pico_stdlib
plasma2040
rgbled
button
analog
)
# enable usb output
pico_enable_stdio_usb(${OUTPUT_NAME} 1)
pico_add_extra_outputs(${OUTPUT_NAME})

Wyświetl plik

@ -9,45 +9,64 @@
#include "common/pimoroni_common.hpp" #include "common/pimoroni_common.hpp"
#include "rgbled.hpp" #include "rgbled.hpp"
#include "button.hpp" #include "button.hpp"
#include "analog.hpp"
/* /*
Press "B" to speed up the LED cycling effect. Press "B" to speed up the LED cycling effect.
Press "A" to slow it down again. Press "A" to slow it down again.
Press "Boot" to reset the speed back to default.
*/ */
using namespace pimoroni; using namespace pimoroni;
using namespace plasma;
// Set how many LEDs you have // Set how many LEDs you have
const uint N_LEDS = 30; const uint N_LEDS = 30;
// The speed that the LEDs will start cycling at
const uint DEFAULT_SPEED = 10;
// How many times the LEDs will be updated per second
const uint UPDATES = 60;
// Pick *one* LED type by uncommenting the relevant line below: // Pick *one* LED type by uncommenting the relevant line below:
// APA102-style LEDs with Data/Clock lines. AKA DotStar // APA102-style LEDs with Data/Clock lines. AKA DotStar
//plasma::APA102 led_strip(N_LEDS, pio0, 0, plasma::PIN_DAT, plasma::PIN_CLK); //APA102 led_strip(N_LEDS, pio0, 0, plasma2040::DAT, plasma2040::CLK);
// WS28X-style LEDs with a single signal line. AKA NeoPixel // WS28X-style LEDs with a single signal line. AKA NeoPixel
// by default the WS2812 LED strip will be 400KHz, RGB with no white element // by default the WS2812 LED strip will be 400KHz, RGB with no white element
plasma::WS2812 led_strip(N_LEDS, pio0, 0, plasma::PIN_DAT); WS2812 led_strip(N_LEDS, pio0, 0, plasma2040::DAT);
Button button_a(plasma::BUTTON_A, Polarity::ACTIVE_LOW, 50); Button user_sw(plasma2040::USER_SW, Polarity::ACTIVE_LOW, 0);
Button button_b(plasma::BUTTON_B, Polarity::ACTIVE_LOW, 50); Button button_a(plasma2040::BUTTON_A, Polarity::ACTIVE_LOW, 50);
RGBLED led(plasma::LED_R, plasma::LED_G, plasma::LED_B); Button button_b(plasma2040::BUTTON_B, Polarity::ACTIVE_LOW, 50);
RGBLED led(plasma2040::LED_R, plasma2040::LED_G, plasma2040::LED_B);
Analog sense(plasma2040::CURRENT_SENSE, plasma2040::ADC_GAIN, plasma2040::SHUNT_RESISTOR);
int main() { int main() {
stdio_init_all(); stdio_init_all();
led_strip.start(60); led_strip.start(UPDATES);
int speed = 10; int speed = DEFAULT_SPEED;
float offset = 0.0f; float offset = 0.0f;
uint count = 0;
while (true) { while (true) {
bool sw = user_sw.read();
bool a = button_a.read(); bool a = button_a.read();
bool b = button_b.read(); bool b = button_b.read();
if(a) speed--; if(sw) {
if(b) speed++; speed = DEFAULT_SPEED;
}
else {
if(a) speed--;
if(b) speed++;
}
speed = std::min((int)255, std::max((int)1, speed)); speed = std::min((int)255, std::max((int)1, speed));
offset += float(speed) / 2000.0f; offset += float(speed) / 2000.0f;
@ -59,8 +78,15 @@ int main() {
led.set_rgb(speed, 0, 255 - speed); led.set_rgb(speed, 0, 255 - speed);
count += 1;
if(count >= UPDATES) {
// Display the current value once every second
printf("Current = %f A\n", sense.read_current());
count = 0;
}
// Sleep time controls the rate at which the LED buffer is updated // Sleep time controls the rate at which the LED buffer is updated
// but *not* the actual framerate at which the buffer is sent to the LEDs // but *not* the actual framerate at which the buffer is sent to the LEDs
sleep_ms(1000 / 60); sleep_ms(1000 / UPDATES);
} }
} }

Wyświetl plik

@ -0,0 +1,13 @@
set(OUTPUT_NAME plasma2040_rotary)
add_executable(${OUTPUT_NAME} plasma2040_rotary.cpp)
target_link_libraries(${OUTPUT_NAME}
pico_stdlib
plasma2040
breakout_encoder
pimoroni_i2c
rgbled
button
)
pico_add_extra_outputs(${OUTPUT_NAME})

Wyświetl plik

@ -12,24 +12,28 @@
#include "button.hpp" #include "button.hpp"
using namespace pimoroni; using namespace pimoroni;
using namespace plasma;
// Set how many LEDs you have // Set how many LEDs you have
const uint N_LEDS = 30; const uint N_LEDS = 30;
// How many times the LEDs will be updated per second
const uint UPDATES = 60;
// Pick *one* LED type by uncommenting the relevant line below: // Pick *one* LED type by uncommenting the relevant line below:
// APA102-style LEDs with Data/Clock lines. AKA DotStar // APA102-style LEDs with Data/Clock lines. AKA DotStar
//plasma::APA102 led_strip(N_LEDS, pio0, 0, plasma::PIN_DAT, plasma::PIN_CLK); //APA102 led_strip(N_LEDS, pio0, 0, plasma2040::DAT, plasma2040::CLK);
// WS28X-style LEDs with a single signal line. AKA NeoPixel // WS28X-style LEDs with a single signal line. AKA NeoPixel
plasma::WS2812 led_strip(N_LEDS, pio0, 0, plasma::PIN_DAT); WS2812 led_strip(N_LEDS, pio0, 0, plasma2040::DAT);
Button button_a(plasma::BUTTON_A); Button button_a(plasma2040::BUTTON_A);
Button button_b(plasma::BUTTON_B); Button button_b(plasma2040::BUTTON_B);
RGBLED led(plasma::LED_R, plasma::LED_G, plasma::LED_B); RGBLED led(plasma2040::LED_R, plasma2040::LED_G, plasma2040::LED_B);
I2C i2c(BOARD::PICO_EXPLORER); I2C i2c(BOARD::PICO_EXPLORER);
BreakoutEncoder enc(&i2c); BreakoutEncoder enc(&i2c);
@ -67,7 +71,7 @@ void gauge(uint v, uint vmax = 100) {
int main() { int main() {
stdio_init_all(); stdio_init_all();
led_strip.start(60); led_strip.start(UPDATES);
bool encoder_detected = enc.init(); bool encoder_detected = enc.init();
enc.clear_interrupt_flag(); enc.clear_interrupt_flag();
@ -147,6 +151,6 @@ int main() {
// Sleep time controls the rate at which the LED buffer is updated // Sleep time controls the rate at which the LED buffer is updated
// but *not* the actual framerate at which the buffer is sent to the LEDs // but *not* the actual framerate at which the buffer is sent to the LEDs
sleep_ms(1000 / 60); sleep_ms(1000 / UPDATES);
} }
} }

Wyświetl plik

@ -21,6 +21,7 @@ by Gee 'Rabid Inventor' Bartlett
*/ */
using namespace pimoroni; using namespace pimoroni;
using namespace plasma;
// Set how many LEDs you have // Set how many LEDs you have
const uint N_LEDS = 40; const uint N_LEDS = 40;
@ -29,18 +30,18 @@ const uint REFRESH_DELAY = 100;
// Pick *one* LED type by uncommenting the relevant line below: // Pick *one* LED type by uncommenting the relevant line below:
// APA102-style LEDs with Data/Clock lines. AKA DotStar // APA102-style LEDs with Data/Clock lines. AKA DotStar
//plasma::APA102 led_strip(N_LEDS, pio0, 0, plasma::PIN_DAT, plasma::PIN_CLK); //APA102 led_strip(N_LEDS, pio0, 0, plasma2040::DAT, plasma2040::CLK);
// WS28X-style LEDs with a single signal line. AKA NeoPixel // WS28X-style LEDs with a single signal line. AKA NeoPixel
// by default the WS2812 LED strip will be 400KHz, RGB with no white element // by default the WS2812 LED strip will be 400KHz, RGB with no white element
//plasma::WS2812 led_strip(N_LEDS, pio0, 0, plasma::PIN_DAT); //WS2812 led_strip(N_LEDS, pio0, 0, plasma2040::DAT);
//Uncomment for WS2812 with RGBW running at 800KHz //Uncomment for WS2812 with RGBW running at 800KHz
plasma::WS2812 led_strip(N_LEDS, pio0, 0, plasma::PIN_DAT, 800000, true); WS2812 led_strip(N_LEDS, pio0, 0, plasma2040::DAT, 800000, true);
Button button_a(plasma::BUTTON_A, Polarity::ACTIVE_LOW, 50); Button button_a(plasma2040::BUTTON_A, Polarity::ACTIVE_LOW, 50);
Button button_b(plasma::BUTTON_B, Polarity::ACTIVE_LOW, 50); Button button_b(plasma2040::BUTTON_B, Polarity::ACTIVE_LOW, 50);
RGBLED led(plasma::LED_R, plasma::LED_G, plasma::LED_B); RGBLED led(plasma2040::LED_R, plasma2040::LED_G, plasma2040::LED_B);
class playfield_object{ class playfield_object{

Wyświetl plik

@ -1,10 +0,0 @@
add_executable(plasma2040_rainbow plasma2040_rainbow.cpp)
target_link_libraries(plasma2040_rainbow
pico_stdlib
plasma2040
rgbled
button
)
pico_add_extra_outputs(plasma2040_rainbow)

Wyświetl plik

@ -1,12 +0,0 @@
add_executable(plasma2040_rotary plasma2040_rotary.cpp)
target_link_libraries(plasma2040_rotary
pico_stdlib
plasma2040
breakout_encoder
pimoroni_i2c
rgbled
button
)
pico_add_extra_outputs(plasma2040_rotary)

Wyświetl plik

@ -1,17 +1,6 @@
add_library(plasma2040 INTERFACE) add_library(plasma2040 INTERFACE)
target_sources(plasma2040 INTERFACE
${CMAKE_CURRENT_LIST_DIR}/apa102.cpp
${CMAKE_CURRENT_LIST_DIR}/ws2812.cpp
)
target_include_directories(plasma2040 INTERFACE ${CMAKE_CURRENT_LIST_DIR}) target_include_directories(plasma2040 INTERFACE ${CMAKE_CURRENT_LIST_DIR})
target_link_libraries(plasma2040 INTERFACE # Pull in pico libraries that we need
pico_stdlib target_link_libraries(plasma2040 INTERFACE pico_stdlib plasma)
hardware_pio
hardware_dma
)
pico_generate_pio_header(plasma2040 ${CMAKE_CURRENT_LIST_DIR}/apa102.pio)
pico_generate_pio_header(plasma2040 ${CMAKE_CURRENT_LIST_DIR}/ws2812.pio)

Wyświetl plik

@ -5,13 +5,22 @@
#include "ws2812.hpp" #include "ws2812.hpp"
namespace plasma { namespace plasma {
const uint LED_R = 16; namespace plasma2040 {
const uint LED_G = 17; const uint LED_R = 16;
const uint LED_B = 18; const uint LED_G = 17;
const uint LED_B = 18;
const uint BUTTON_A = 12; const uint BUTTON_A = 12;
const uint BUTTON_B = 13; const uint BUTTON_B = 13;
const uint PIN_CLK = 14; // Used only for APA102 const uint USER_SW = 23;
const uint PIN_DAT = 15; // Used for both APA102 and WS2812
const uint CLK = 14; // Used only for APA102
const uint DAT = 15; // Used for both APA102 and WS2812
const uint CURRENT_SENSE = 29; // The pin used for current sensing
constexpr float ADC_GAIN = 50;
constexpr float SHUNT_RESISTOR = 0.015f;
}
} }

Wyświetl plik

@ -1,19 +1,73 @@
import plasma import plasma
from plasma import plasma2040
import time import time
# Import helpers for RGB LEDs, Buttons, and Analog
from pimoroni import RGBLED, Button, Analog
# Press "B" to speed up the LED cycling effect.
# Press "A" to slow it down again.
# Press "Boot" to reset the speed back to default.
# Set how many LEDs you have
NUM_LEDS = 30 NUM_LEDS = 30
# WS2812 / NeoPixel™ LEDs # The speed that the LEDs will start cycling at
led_strip = plasma.WS2812(NUM_LEDS, 0, 0, 15) DEFAULT_SPEED = 10
# How many times the LEDs will be updated per second
UPDATES = 60
# Pick *one* LED type by uncommenting the relevant line below:
# APA102 / DotStar™ LEDs # APA102 / DotStar™ LEDs
# led_strip = plasma.APA102(NUM_LEDS, 0, 0, 15, 14) # led_strip = plasma.APA102(NUM_LEDS, 0, 0, plasma2040.DAT, plasma2040.CLK)
# WS2812 / NeoPixel™ LEDs
led_strip = plasma.WS2812(NUM_LEDS, 0, 0, plasma2040.DAT)
user_sw = Button(plasma2040.USER_SW)
button_a = Button(plasma2040.BUTTON_A)
button_b = Button(plasma2040.BUTTON_B)
led = RGBLED(plasma2040.LED_R, plasma2040.LED_G, plasma2040.LED_B)
sense = Analog(plasma2040.CURRENT_SENSE, plasma2040.ADC_GAIN, plasma2040.SHUNT_RESISTOR)
# Start updating the LED strip # Start updating the LED strip
led_strip.start() led_strip.start()
speed = DEFAULT_SPEED
offset = 0.0
count = 0
# Make rainbows # Make rainbows
while True: while True:
t = time.ticks_ms() / 1000.0 / 5.0 sw = user_sw.read()
a = button_a.read()
b = button_b.read()
if sw:
speed = DEFAULT_SPEED
else:
if a:
speed -= 1
if b:
speed += 1
speed = min(255, max(1, speed))
offset += float(speed) / 2000.0
for i in range(NUM_LEDS): for i in range(NUM_LEDS):
led_strip.set_hsv(i, t + (i / NUM_LEDS)) hue = float(i) / NUM_LEDS
time.sleep(1.0 / 60) led_strip.set_hsv(i, hue + offset, 1.0, 1.0)
led.set_rgb(speed, 0, 255 - speed)
count += 1
if count >= UPDATES:
# Display the current value once every second
print("Current =", sense.read_current(), "A")
count = 0
time.sleep(1.0 / UPDATES)

Wyświetl plik

@ -1,18 +1,22 @@
import time import time
# Import pin constants from plasma # Import plasma2040
from plasma import PIN_LED_R, PIN_LED_G, PIN_LED_B, PIN_BUTTON_A, PIN_BUTTON_B from plasma import plasma2040
# Import helpers for RGB LEDs and Buttons # Import helpers for RGB LEDs and Buttons
from pimoroni import RGBLED, Button from pimoroni import RGBLED, Button
led = RGBLED(PIN_LED_R, PIN_LED_G, PIN_LED_B) led = RGBLED(plasma2040.LED_R, plasma2040.LED_G, plasma2040.LED_B)
led.set_rgb(255, 0, 0) led.set_rgb(0, 0, 0)
button_a = Button(PIN_BUTTON_A) user_sw = Button(plasma2040.USER_SW)
button_b = Button(PIN_BUTTON_B) button_a = Button(plasma2040.BUTTON_A)
button_b = Button(plasma2040.BUTTON_B)
while True: while True:
if user_sw.read():
print("Pressed User SW - {}".format(time.ticks_ms()))
led.set_rgb(255, 0, 0)
if button_a.read(): if button_a.read():
print("Pressed A - {}".format(time.ticks_ms())) print("Pressed A - {}".format(time.ticks_ms()))
led.set_rgb(0, 255, 0) led.set_rgb(0, 255, 0)

Wyświetl plik

@ -32,5 +32,5 @@ include(pico_unicorn/micropython)
include(pico_display/micropython) include(pico_display/micropython)
include(pico_explorer/micropython) include(pico_explorer/micropython)
include(pico_wireless/micropython) include(pico_wireless/micropython)
include(plasma_2040/micropython) include(plasma/micropython)
include(ulab/code/micropython) include(ulab/code/micropython)

Wyświetl plik

@ -18,6 +18,8 @@ The Plasma library is intended to drive APA102 / DotStar™ or WS2812 / NeoPixel
- [Using the Buttons & RGB LED](#using-the-buttons--rgb-led) - [Using the Buttons & RGB LED](#using-the-buttons--rgb-led)
- [Buttons](#buttons) - [Buttons](#buttons)
- [RGBLED](#rgbled) - [RGBLED](#rgbled)
- [Measuring LED Strip Current Draw](#measuring-led-strip-current-draw)
- [Analog](#analog)
## Notes On PIO Limitations ## Notes On PIO Limitations
@ -33,11 +35,12 @@ Construct a new `WS2812` instance, specifying the number of LEDs, PIO, PIO state
```python ```python
import plasma import plasma
from plasma import plasma2040
LEDS = 30 LEDS = 30
FPS = 60 FPS = 60
led_strip = plasma.WS2812(LEDS, 0, 0, 15) led_strip = plasma.WS2812(LEDS, 0, 0, plasma2040.DAT)
``` ```
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. 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.
@ -50,13 +53,14 @@ led_strip.start(FPS)
Some WS2812-style LED strips have varying colour orders and support an additional white element. Two keyword arguments are supplied to configure this: Some WS2812-style LED strips have varying colour orders and support an additional white element. Two keyword arguments are supplied to configure this:
``` ```python
import plasma import plasma
from plasma import plasma2040
LEDS = 30 LEDS = 30
FPS = 60 FPS = 60
led_strip = plasma.WS2812(LEDS, 0, 0, 15, rgbw=True, color_order=plasma.COLOR_ORDER_GRB) led_strip = plasma.WS2812(LEDS, 0, 0, plasma2040.DAT, rgbw=True, color_order=plasma.COLOR_ORDER_GRB)
``` ```
The available orders are defined as constants in `plasma`: The available orders are defined as constants in `plasma`:
@ -106,11 +110,12 @@ Construct a new `APA102` instance, specifying the number of LEDs, PIO, PIO state
```python ```python
import plasma import plasma
from plasma import plasma2040
LEDS = 30 LEDS = 30
FPS = 60 FPS = 60
led_strip = plasma.APA102(LEDS, 0, 0, 15, 14) led_strip = plasma.APA102(LEDS, 0, 0, plasma2040.DAT, plasma2040.CLK)
``` ```
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. 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.
@ -151,13 +156,14 @@ Button(button, invert=True, repeat_time=200, hold_time=1000)
RGBLED(r, g, b, invert=True) RGBLED(r, g, b, invert=True)
``` ```
The `plasma` module contains constants for the LED and button pins: The `plasma` module contains a `plasma2040` sub module with constants for the LED and button pins:
* `plasma.PIN_LED_R` = 16 * `plasma2040.LED_R` = 16
* `plasma.PIN_LED_G` = 17 * `plasma2040.LED_G` = 17
* `plasma.PIN_LED_B` = 18 * `plasma2040.LED_B` = 18
* `plasma.PIN_BUTTON_A` = 12 * `plasma2040.BUTTON_A` = 12
* `plasma.PIN_BUTTON_B` = 13 * `plasma2040.BUTTON_B` = 13
* `plasma2040.USER_SW` = 23
### Buttons ### Buttons
@ -165,14 +171,14 @@ Import the `Button` class from the `pimoroni` module and the pin constants for t
```python ```python
from pimoroni import Button from pimoroni import Button
from plasma import PIN_BUTTON_A, PIN_BUTTON_B from plasma import plasma2040
``` ```
Set up an instance of `Button` for each button: Set up an instance of `Button` for each button:
```python ```python
button_a = Button(PIN_BUTTON_A) button_a = Button(plasma2040.BUTTON_A)
button_b = Button(PIN_BUTTON_B) button_b = Button(plasma2040.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: 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:
@ -183,17 +189,17 @@ state = button_a.read()
### RGBLED ### RGBLED
Import the `RGBLED` class from `pimoroni` and the pin constants for the buttons: Import the `RGBLED` class from `pimoroni` and the pin constants for the LED:
```python ```python
from pimoroni import RGBLED from pimoroni import RGBLED
from plasma import PIN_LED_R, PIN_LED_G, PIN_LED_B from plasma import plasma2040
``` ```
And set up an instance of `RGBLED` for the LED: And set up an instance of `RGBLED` for the LED:
```python ```python
led = RGBLED(PIN_LED_R, PIN_LED_G, PIN_LED_B) led = RGBLED(plasma2040.LED_R, plasma2040.LED_G, plasma2040.LED_B)
``` ```
To set the LED colour, call `.set_rgb(r, g, b)`. Each value should be between 0 and 255: To set the LED colour, call `.set_rgb(r, g, b)`. Each value should be between 0 and 255:
@ -202,4 +208,41 @@ To set the LED colour, call `.set_rgb(r, g, b)`. Each value should be between 0
led.set_rgb(255, 0, 0) # Full red led.set_rgb(255, 0, 0) # Full red
led.set_rgb(0, 255, 0) # Full green led.set_rgb(0, 255, 0) # Full green
led.set_rgb(0, 0, 255) # Full blue led.set_rgb(0, 0, 255) # Full blue
``` ```
## Measuring LED Strip Current Draw
Plasma 2040 feasures 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.
The `pimoroni` module contains an `Analog` class to simplify the reading of this current draw.
```python
Analog(pin, amplifier_gain=1, resistor=0)
```
The `plasma` module contains a `plasma2040` sub module with constants for the current sensing:
* `plasma2040.CURRENT_SENSE` = 29
* `plasma2040.ADC_GAIN` = 50
* `plasma2040.SHUNT_RESISTOR` = 0.015
### Analog
Import the `Analog` class from `pimoroni` and the pin and gain constants for the current sensing:
```python
from pimoroni import Analog
from plasma import plasma2040
```
And set up an instance of `Analog` for the current sensing:
```python
sense = Analog(plasma2040.CURRENT_SENSE, plasma2040.ADC_GAIN, plasma2040.SHUNT_RESISTOR)
```
To read the current draw, call `.read_current()`. The returned value will be in amps (A):
```python
print("Current =", sense.read_current(), "A")
```

Wyświetl plik

@ -1,19 +1,19 @@
set(MOD_NAME plasma_2040) set(MOD_NAME plasma)
string(TOUPPER ${MOD_NAME} MOD_NAME_UPPER) string(TOUPPER ${MOD_NAME} MOD_NAME_UPPER)
add_library(usermod_${MOD_NAME} INTERFACE) add_library(usermod_${MOD_NAME} INTERFACE)
target_sources(usermod_${MOD_NAME} INTERFACE target_sources(usermod_${MOD_NAME} INTERFACE
${CMAKE_CURRENT_LIST_DIR}/${MOD_NAME}.c ${CMAKE_CURRENT_LIST_DIR}/${MOD_NAME}.c
${CMAKE_CURRENT_LIST_DIR}/${MOD_NAME}.cpp ${CMAKE_CURRENT_LIST_DIR}/${MOD_NAME}.cpp
${CMAKE_CURRENT_LIST_DIR}/../../../libraries/plasma2040/apa102.cpp ${CMAKE_CURRENT_LIST_DIR}/../../../drivers/plasma/apa102.cpp
${CMAKE_CURRENT_LIST_DIR}/../../../libraries/plasma2040/ws2812.cpp ${CMAKE_CURRENT_LIST_DIR}/../../../drivers/plasma/ws2812.cpp
) )
pico_generate_pio_header(usermod_${MOD_NAME} ${CMAKE_CURRENT_LIST_DIR}/../../../libraries/plasma2040/apa102.pio) pico_generate_pio_header(usermod_${MOD_NAME} ${CMAKE_CURRENT_LIST_DIR}/../../../drivers/plasma/apa102.pio)
pico_generate_pio_header(usermod_${MOD_NAME} ${CMAKE_CURRENT_LIST_DIR}/../../../libraries/plasma2040/ws2812.pio) pico_generate_pio_header(usermod_${MOD_NAME} ${CMAKE_CURRENT_LIST_DIR}/../../../drivers/plasma/ws2812.pio)
target_include_directories(usermod_${MOD_NAME} INTERFACE target_include_directories(usermod_${MOD_NAME} INTERFACE
${CMAKE_CURRENT_LIST_DIR} ${CMAKE_CURRENT_LIST_DIR}
${CMAKE_CURRENT_LIST_DIR}/../../../libraries/plasma2040/ ${CMAKE_CURRENT_LIST_DIR}/../../../drivers/plasma/
) )
target_compile_definitions(usermod_${MOD_NAME} INTERFACE target_compile_definitions(usermod_${MOD_NAME} INTERFACE

Wyświetl plik

@ -1,4 +1,4 @@
#include "plasma_2040.h" #include "plasma.h"
/***** Methods *****/ /***** Methods *****/
@ -59,15 +59,33 @@ typedef struct _mp_obj_float_t {
mp_obj_float_t shunt_resistor = {{&mp_type_float}, 0.015f}; mp_obj_float_t shunt_resistor = {{&mp_type_float}, 0.015f};
/***** Globals Table *****/ /***** Globals Table *****/
STATIC const mp_map_elem_t plasma2040_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_plasma2040) },
{ MP_ROM_QSTR(MP_QSTR_LED_R), MP_ROM_INT(16) },
{ MP_ROM_QSTR(MP_QSTR_LED_G), MP_ROM_INT(17) },
{ MP_ROM_QSTR(MP_QSTR_LED_B), MP_ROM_INT(18) },
{ MP_ROM_QSTR(MP_QSTR_BUTTON_A), MP_ROM_INT(12) },
{ MP_ROM_QSTR(MP_QSTR_BUTTON_B), MP_ROM_INT(13) },
{ MP_ROM_QSTR(MP_QSTR_USER_SW), MP_ROM_INT(23) },
{ MP_ROM_QSTR(MP_QSTR_CLK), MP_ROM_INT(14) },
{ MP_ROM_QSTR(MP_QSTR_DAT), MP_ROM_INT(15) },
{ MP_ROM_QSTR(MP_QSTR_CURRENT_SENSE), MP_ROM_INT(29) },
{ MP_ROM_QSTR(MP_QSTR_SHUNT_RESISTOR), MP_ROM_PTR(&shunt_resistor) },
{ MP_ROM_QSTR(MP_QSTR_ADC_GAIN), MP_ROM_INT(50) },
};
STATIC MP_DEFINE_CONST_DICT(mp_module_plasma2040_globals, plasma2040_globals_table);
const mp_obj_module_t plasma2040_user_cmodule = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&mp_module_plasma2040_globals,
};
STATIC const mp_map_elem_t plasma_globals_table[] = { 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___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_APA102), (mp_obj_t)&PlasmaAPA102_type },
{ MP_OBJ_NEW_QSTR(MP_QSTR_WS2812), (mp_obj_t)&PlasmaWS2812_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_OBJ_NEW_QSTR(MP_QSTR_plasma2040), (mp_obj_t)&plasma2040_user_cmodule },
{ 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) },
{ MP_ROM_QSTR(MP_QSTR_COLOR_ORDER_RGB), MP_ROM_INT(0x00) }, { MP_ROM_QSTR(MP_QSTR_COLOR_ORDER_RGB), MP_ROM_INT(0x00) },
{ MP_ROM_QSTR(MP_QSTR_COLOR_ORDER_RBG), MP_ROM_INT(0x01) }, { MP_ROM_QSTR(MP_QSTR_COLOR_ORDER_RBG), MP_ROM_INT(0x01) },
@ -75,9 +93,6 @@ STATIC const mp_map_elem_t plasma_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_COLOR_ORDER_GBR), MP_ROM_INT(0x03) }, { MP_ROM_QSTR(MP_QSTR_COLOR_ORDER_GBR), MP_ROM_INT(0x03) },
{ MP_ROM_QSTR(MP_QSTR_COLOR_ORDER_BRG), MP_ROM_INT(0x04) }, { MP_ROM_QSTR(MP_QSTR_COLOR_ORDER_BRG), MP_ROM_INT(0x04) },
{ MP_ROM_QSTR(MP_QSTR_COLOR_ORDER_BGR), MP_ROM_INT(0x05) }, { MP_ROM_QSTR(MP_QSTR_COLOR_ORDER_BGR), MP_ROM_INT(0x05) },
{ MP_ROM_QSTR(MP_QSTR_SHUNT_RESISTOR), MP_ROM_PTR(&shunt_resistor) },
{ MP_ROM_QSTR(MP_QSTR_ADC_GAIN), MP_ROM_INT(50) },
}; };
STATIC MP_DEFINE_CONST_DICT(mp_module_plasma_globals, plasma_globals_table); STATIC MP_DEFINE_CONST_DICT(mp_module_plasma_globals, plasma_globals_table);

Wyświetl plik

@ -1,4 +1,5 @@
#include "libraries/plasma2040/plasma2040.hpp" #include "drivers/plasma/ws2812.hpp"
#include "drivers/plasma/apa102.hpp"
#include <cstdio> #include <cstdio>
#define MP_OBJ_TO_PTR2(o, t) ((t *)(uintptr_t)(o)) #define MP_OBJ_TO_PTR2(o, t) ((t *)(uintptr_t)(o))
@ -11,7 +12,7 @@
using namespace plasma; using namespace plasma;
extern "C" { extern "C" {
#include "plasma_2040.h" #include "plasma.h"
#include "py/builtin.h" #include "py/builtin.h"
typedef struct _mp_obj_float_t { typedef struct _mp_obj_float_t {

Wyświetl plik

@ -80,6 +80,6 @@ class RGBLED:
r = 255 - r r = 255 - r
g = 255 - g g = 255 - g
b = 255 - b b = 255 - b
self.led_r.duty_u16(r * 255) self.led_r.duty_u16(int((r * 65535) / 255))
self.led_g.duty_u16(g * 255) self.led_g.duty_u16(int((g * 65535) / 255))
self.led_b.duty_u16(b * 255) self.led_b.duty_u16(int((b * 65535) / 255))