Updated rainbow example to be consistant across languages

This commit is contained in:
ZodiusInfuser 2021-08-18 12:18:25 +01:00
parent fa17f2e77e
commit fa4cf86126
4 changed files with 73 additions and 12 deletions

View File

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

View File

@ -9,6 +9,7 @@
#include "common/pimoroni_common.hpp"
#include "rgbled.hpp"
#include "button.hpp"
#include "analog.hpp"
/*
Press "B" to speed up the LED cycling effect.
@ -27,6 +28,7 @@ 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:
// APA102-style LEDs with Data/Clock lines. AKA DotStar
@ -40,6 +42,7 @@ Button user_sw(plasma::USER_SW, Polarity::ACTIVE_LOW, 0);
Button button_a(plasma::BUTTON_A, Polarity::ACTIVE_LOW, 50);
Button button_b(plasma::BUTTON_B, Polarity::ACTIVE_LOW, 50);
RGBLED led(plasma::LED_R, plasma::LED_G, plasma::LED_B);
Analog sense(plasma::PIN_SENSE, plasma::ADC_GAIN, plasma::SHUNT_RESISTOR);
int main() {
@ -50,6 +53,7 @@ int main() {
int speed = DEFAULT_SPEED;
float offset = 0.0f;
uint count = 0;
while (true) {
bool sw = user_sw.read();
bool a = button_a.read();
@ -73,6 +77,13 @@ int main() {
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
// but *not* the actual framerate at which the buffer is sent to the LEDs
sleep_ms(1000 / UPDATES);

View File

@ -12,6 +12,13 @@ const uint LED_B = 18;
const uint BUTTON_A = 12;
const uint BUTTON_B = 13;
const uint USER_SW = 23;
const uint PIN_CLK = 14; // Used only for APA102
const uint PIN_DAT = 15; // Used for both APA102 and WS2812
const uint PIN_SENSE = 29; // The pin used for current sensing
constexpr float ADC_GAIN = 50;
constexpr float SHUNT_RESISTOR = 0.015f;
}

View File

@ -1,29 +1,67 @@
import plasma
import time
# Import helper for Analog
from pimoroni import Analog
# 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
# The speed that the LEDs will start cycling at
DEFAULT_SPEED = 10
# How many times the LEDs will be updated per second
UPDATES = 60
# WS2812 / NeoPixel™ LEDs
led_strip = plasma.WS2812(NUM_LEDS, 0, 0, 15)
# APA102 / DotStar™ LEDs
# led_strip = plasma.APA102(NUM_LEDS, 0, 0, 15, 14)
# Set up the ADC for reading current
# Pick *one* LED type by uncommenting the relevant line below:
# APA102 / DotStar™ LEDs
# led_strip = plasma.APA102(NUM_LEDS, 0, 0, plasma.PIN_DAT, plasma.PIN_CLK)
# WS2812 / NeoPixel™ LEDs
led_strip = plasma.WS2812(NUM_LEDS, 0, 0, plasma.PIN_DAT)
user_sw = Button(plasma.PIN_USER_SW)
button_a = Button(plasma.PIN_BUTTON_A)
button_b = Button(plasma.PIN_BUTTON_B)
led = RGBLED(plasma.PIN_LED_R, plasma.PIN_LED_G, plasma.PIN_LED_B)
sense = Analog(plasma.PIN_CURRENT_SENSE, plasma.ADC_GAIN, plasma.SHUNT_RESISTOR)
# Start updating the LED strip
led_strip.start()
speed = DEFAULT_SPEED
offset = 0.0
count = 0
# Make rainbows
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):
led_strip.set_hsv(i, t + (i / NUM_LEDS))
hue = float(i) / NUM_LEDS
led_strip.set_hsv(i, hue + offset, 1.0, 1.0)
led.set_rgb(speed, 0, 255 - speed)
count += 1
if count >= UPDATES: