Skip to content

Commit

Permalink
Merge pull request #191 from pimoroni/patch_plasma2040
Browse files Browse the repository at this point in the history
Updates to Plasma 2040 C++ and MP examples
  • Loading branch information
Gadgetoid authored Aug 19, 2021
2 parents c003834 + 4976c90 commit c4ea05a
Show file tree
Hide file tree
Showing 34 changed files with 352 additions and 118 deletions.
2 changes: 2 additions & 0 deletions drivers/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
add_subdirectory(analog)
add_subdirectory(esp32spi)
add_subdirectory(ioexpander)
add_subdirectory(ltp305)
Expand All @@ -18,4 +19,5 @@ add_subdirectory(bme68x)
add_subdirectory(bmp280)
add_subdirectory(bme280)
add_subdirectory(button)
add_subdirectory(plasma)
add_subdirectory(rgbled)
1 change: 1 addition & 0 deletions drivers/analog/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include(analog.cmake)
11 changes: 11 additions & 0 deletions drivers/analog/analog.cmake
Original file line number Diff line number Diff line change
@@ -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)
18 changes: 18 additions & 0 deletions drivers/analog/analog.cpp
Original file line number Diff line number Diff line change
@@ -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();
}
};
31 changes: 31 additions & 0 deletions drivers/analog/analog.hpp
Original file line number Diff line number Diff line change
@@ -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;
};

}
1 change: 1 addition & 0 deletions drivers/plasma/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include(plasma.cmake)
File renamed without changes.
File renamed without changes.
File renamed without changes.
18 changes: 18 additions & 0 deletions drivers/plasma/plasma.cmake
Original file line number Diff line number Diff line change
@@ -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)
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace plasma {
public:
static const uint SERIAL_FREQ_400KHZ = 400000;
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 {
RGB,
RBG,
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ add_subdirectory(pico_trackball_display)
add_subdirectory(pico_audio)
add_subdirectory(pico_wireless)

add_subdirectory(plasma_2040)
add_subdirectory(plasma2040)
File renamed without changes.
15 changes: 15 additions & 0 deletions examples/plasma2040/plasma2040_rainbow.cmake
Original file line number Diff line number Diff line change
@@ -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})
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,64 @@
#include "common/pimoroni_common.hpp"
#include "rgbled.hpp"
#include "button.hpp"
#include "analog.hpp"

/*
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.
*/

using namespace pimoroni;
using namespace plasma;

// Set how many LEDs you have
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:

// 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
// 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 button_b(plasma::BUTTON_B, Polarity::ACTIVE_LOW, 50);
RGBLED led(plasma::LED_R, plasma::LED_G, plasma::LED_B);
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);
Analog sense(plasma2040::CURRENT_SENSE, plasma2040::ADC_GAIN, plasma2040::SHUNT_RESISTOR);


int main() {
stdio_init_all();

led_strip.start(60);
led_strip.start(UPDATES);

int speed = 10;
int speed = DEFAULT_SPEED;
float offset = 0.0f;

uint count = 0;
while (true) {
bool sw = user_sw.read();
bool a = button_a.read();
bool b = button_b.read();

if(a) speed--;
if(b) speed++;

if(sw) {
speed = DEFAULT_SPEED;
}
else {
if(a) speed--;
if(b) speed++;
}
speed = std::min((int)255, std::max((int)1, speed));

offset += float(speed) / 2000.0f;
Expand All @@ -59,8 +78,15 @@ 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 / 60);
sleep_ms(1000 / UPDATES);
}
}
13 changes: 13 additions & 0 deletions examples/plasma2040/plasma2040_rotary.cmake
Original file line number Diff line number Diff line change
@@ -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})
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,28 @@
#include "button.hpp"

using namespace pimoroni;
using namespace plasma;

// Set how many LEDs you have
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:

// 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
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_b(plasma::BUTTON_B);
Button button_a(plasma2040::BUTTON_A);
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);
BreakoutEncoder enc(&i2c);
Expand Down Expand Up @@ -67,7 +71,7 @@ void gauge(uint v, uint vmax = 100) {
int main() {
stdio_init_all();

led_strip.start(60);
led_strip.start(UPDATES);

bool encoder_detected = enc.init();
enc.clear_interrupt_flag();
Expand Down Expand Up @@ -147,6 +151,6 @@ int main() {

// 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 / 60);
sleep_ms(1000 / UPDATES);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ by Gee 'Rabid Inventor' Bartlett
*/

using namespace pimoroni;
using namespace plasma;

// Set how many LEDs you have
const uint N_LEDS = 40;
Expand All @@ -29,18 +30,18 @@ const uint REFRESH_DELAY = 100;
// Pick *one* LED type by uncommenting the relevant line below:

// 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
// 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
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_b(plasma::BUTTON_B, Polarity::ACTIVE_LOW, 50);
RGBLED led(plasma::LED_R, plasma::LED_G, plasma::LED_B);
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);


class playfield_object{
Expand Down
10 changes: 0 additions & 10 deletions examples/plasma_2040/plasma2040_rainbow.cmake

This file was deleted.

12 changes: 0 additions & 12 deletions examples/plasma_2040/plasma2040_rotary.cmake

This file was deleted.

15 changes: 2 additions & 13 deletions libraries/plasma2040/plasma2040.cmake
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
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_link_libraries(plasma2040 INTERFACE
pico_stdlib
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)
# Pull in pico libraries that we need
target_link_libraries(plasma2040 INTERFACE pico_stdlib plasma)
Loading

0 comments on commit c4ea05a

Please sign in to comment.