2021-07-16 17:01:55 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <cstdint>
|
|
|
|
|
|
|
|
#include "pico/stdlib.h"
|
|
|
|
|
|
|
|
#include "plasma2040.hpp"
|
|
|
|
|
|
|
|
#include "common/pimoroni_common.hpp"
|
|
|
|
#include "rgbled.hpp"
|
|
|
|
#include "button.hpp"
|
2021-08-18 12:18:25 +01:00
|
|
|
#include "analog.hpp"
|
2021-07-16 17:01:55 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
Press "B" to speed up the LED cycling effect.
|
|
|
|
Press "A" to slow it down again.
|
2021-08-18 10:54:31 +01:00
|
|
|
Press "Boot" to reset the speed back to default.
|
2021-07-16 17:01:55 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
using namespace pimoroni;
|
2021-08-19 14:12:56 +01:00
|
|
|
using namespace plasma;
|
2021-07-16 17:01:55 +01:00
|
|
|
|
|
|
|
// Set how many LEDs you have
|
|
|
|
const uint N_LEDS = 30;
|
|
|
|
|
2021-08-18 10:54:31 +01:00
|
|
|
// 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;
|
|
|
|
|
2021-08-18 12:18:25 +01:00
|
|
|
|
2021-07-16 17:01:55 +01:00
|
|
|
// Pick *one* LED type by uncommenting the relevant line below:
|
|
|
|
|
|
|
|
// APA102-style LEDs with Data/Clock lines. AKA DotStar
|
2021-08-19 14:12:56 +01:00
|
|
|
//APA102 led_strip(N_LEDS, pio0, 0, plasma2040::DAT, plasma2040::CLK);
|
2021-07-16 17:01:55 +01:00
|
|
|
|
|
|
|
// WS28X-style LEDs with a single signal line. AKA NeoPixel
|
2021-08-02 12:33:20 +01:00
|
|
|
// by default the WS2812 LED strip will be 400KHz, RGB with no white element
|
2021-08-19 14:12:56 +01:00
|
|
|
WS2812 led_strip(N_LEDS, pio0, 0, plasma2040::DAT);
|
2021-07-16 17:01:55 +01:00
|
|
|
|
2021-08-19 14:12:56 +01:00
|
|
|
Button user_sw(plasma2040::USER_SW, Polarity::ACTIVE_LOW, 0);
|
|
|
|
Button button_a(plasma2040::BUTTON_A, Polarity::ACTIVE_LOW, 50);
|
|
|
|
Button button_b(plasma2040::BUTTON_B, Polarity::ACTIVE_LOW, 50);
|
|
|
|
RGBLED led(plasma2040::LED_R, plasma2040::LED_G, plasma2040::LED_B);
|
2021-08-19 15:16:40 +01:00
|
|
|
Analog sense(plasma2040::CURRENT_SENSE, plasma2040::ADC_GAIN, plasma2040::SHUNT_RESISTOR);
|
2021-07-16 17:01:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
int main() {
|
2021-08-24 14:16:27 +01:00
|
|
|
stdio_init_all();
|
|
|
|
|
|
|
|
led_strip.start(UPDATES);
|
|
|
|
|
|
|
|
int speed = DEFAULT_SPEED;
|
|
|
|
float offset = 0.0f;
|
|
|
|
|
|
|
|
uint count = 0;
|
|
|
|
while(true) {
|
|
|
|
bool sw_pressed = user_sw.read();
|
|
|
|
bool a_pressed = button_a.read();
|
|
|
|
bool b_pressed = button_b.read();
|
|
|
|
|
|
|
|
if(sw_pressed) {
|
|
|
|
speed = DEFAULT_SPEED;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(a_pressed) speed--;
|
|
|
|
if(b_pressed) speed++;
|
|
|
|
}
|
|
|
|
speed = std::min((int)255, std::max((int)1, speed));
|
|
|
|
|
|
|
|
offset += float(speed) / 2000.0f;
|
|
|
|
|
|
|
|
for(auto i = 0u; i < led_strip.num_leds; ++i) {
|
|
|
|
float hue = float(i) / led_strip.num_leds;
|
|
|
|
led_strip.set_hsv(i, hue + offset, 1.0f, 1.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2021-07-16 17:01:55 +01:00
|
|
|
}
|
2021-08-24 14:16:27 +01:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
2021-07-16 17:01:55 +01:00
|
|
|
}
|