From 6672e9d68381a79dab0747243056d18942e272de Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Tue, 17 Aug 2021 12:48:45 +0100 Subject: [PATCH 01/30] Added pins for user sw and current sense --- micropython/modules/plasma_2040/plasma_2040.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/micropython/modules/plasma_2040/plasma_2040.c b/micropython/modules/plasma_2040/plasma_2040.c index c606f0f98..6f651da01 100644 --- a/micropython/modules/plasma_2040/plasma_2040.c +++ b/micropython/modules/plasma_2040/plasma_2040.c @@ -68,6 +68,8 @@ STATIC const mp_map_elem_t plasma_globals_table[] = { { 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_PIN_USER_SW), MP_ROM_INT(23) }, + { MP_ROM_QSTR(MP_QSTR_PIN_CURRENT_SENSE), MP_ROM_INT(29) }, { MP_ROM_QSTR(MP_QSTR_COLOR_ORDER_RGB), MP_ROM_INT(0x00) }, { MP_ROM_QSTR(MP_QSTR_COLOR_ORDER_RBG), MP_ROM_INT(0x01) }, From 78952ff070f088a00922ac307ec54522e3e48e66 Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Tue, 17 Aug 2021 12:59:36 +0100 Subject: [PATCH 02/30] Changed rgb to use full pwm range --- micropython/modules_py/pimoroni.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/micropython/modules_py/pimoroni.py b/micropython/modules_py/pimoroni.py index 643b5caf4..1b6fe06f6 100644 --- a/micropython/modules_py/pimoroni.py +++ b/micropython/modules_py/pimoroni.py @@ -80,6 +80,6 @@ def set_rgb(self, r, g, b): r = 255 - r g = 255 - g b = 255 - b - self.led_r.duty_u16(r * 255) - self.led_g.duty_u16(g * 255) - self.led_b.duty_u16(b * 255) + self.led_r.duty_u16((r * 65535) / 255) + self.led_g.duty_u16((g * 65535) / 255) + self.led_b.duty_u16((b * 65535) / 255) From 5a911ad46e2e7108ca652a88a9107fe91d5ad49d Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Tue, 17 Aug 2021 13:07:34 +0100 Subject: [PATCH 03/30] Fix for "Changed rgb to use full pwm range" --- micropython/modules_py/pimoroni.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/micropython/modules_py/pimoroni.py b/micropython/modules_py/pimoroni.py index 1b6fe06f6..9dd3c6343 100644 --- a/micropython/modules_py/pimoroni.py +++ b/micropython/modules_py/pimoroni.py @@ -80,6 +80,6 @@ def set_rgb(self, r, g, b): r = 255 - r g = 255 - g b = 255 - b - self.led_r.duty_u16((r * 65535) / 255) - self.led_g.duty_u16((g * 65535) / 255) - self.led_b.duty_u16((b * 65535) / 255) + self.led_r.duty_u16(int((r * 65535) / 255)) + self.led_g.duty_u16(int((g * 65535) / 255)) + self.led_b.duty_u16(int((b * 65535) / 255)) From e045cb9615227feb145ba965065325ff92a2cf3f Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Tue, 17 Aug 2021 13:25:46 +0100 Subject: [PATCH 04/30] Updated plasma mp examples to use user_sw and current sensing --- micropython/examples/plasma_2040/rainbow.py | 19 +++++++++++++++++-- .../plasma_2040/rgb-led-and-buttons.py | 8 ++++++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/micropython/examples/plasma_2040/rainbow.py b/micropython/examples/plasma_2040/rainbow.py index 89e93dc4a..2e88281c1 100644 --- a/micropython/examples/plasma_2040/rainbow.py +++ b/micropython/examples/plasma_2040/rainbow.py @@ -1,19 +1,34 @@ import plasma import time +# Import helper for Analog +from pimoroni import Analog + NUM_LEDS = 30 +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) +#led_strip = plasma.APA102(NUM_LEDS, 0, 0, 15, 14) + +# Set up the ADC for reading current +sense = Analog(plasma.PIN_CURRENT_SENSE, plasma.ADC_GAIN, plasma.SHUNT_RESISTOR) # Start updating the LED strip led_strip.start() +count = 0 # Make rainbows while True: t = time.ticks_ms() / 1000.0 / 5.0 for i in range(NUM_LEDS): led_strip.set_hsv(i, t + (i / NUM_LEDS)) - time.sleep(1.0 / 60) + + 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) diff --git a/micropython/examples/plasma_2040/rgb-led-and-buttons.py b/micropython/examples/plasma_2040/rgb-led-and-buttons.py index a6d4501ef..f4efa4563 100644 --- a/micropython/examples/plasma_2040/rgb-led-and-buttons.py +++ b/micropython/examples/plasma_2040/rgb-led-and-buttons.py @@ -1,18 +1,22 @@ 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 +from plasma import PIN_LED_R, PIN_LED_G, PIN_LED_B, PIN_USER_SW, 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) +led.set_rgb(0, 0, 0) +user_sw = Button(PIN_USER_SW) button_a = Button(PIN_BUTTON_A) button_b = Button(PIN_BUTTON_B) while True: + if user_sw.read(): + print("Pressed User SW - {}".format(time.ticks_ms())) + led.set_rgb(255, 0, 0) if button_a.read(): print("Pressed A - {}".format(time.ticks_ms())) led.set_rgb(0, 255, 0) From 71e95805fa9d24823eb152ae5260c4ef20447843 Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Tue, 17 Aug 2021 13:43:42 +0100 Subject: [PATCH 05/30] Linting fixes --- micropython/examples/plasma_2040/rainbow.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/micropython/examples/plasma_2040/rainbow.py b/micropython/examples/plasma_2040/rainbow.py index 2e88281c1..d364f433c 100644 --- a/micropython/examples/plasma_2040/rainbow.py +++ b/micropython/examples/plasma_2040/rainbow.py @@ -10,7 +10,7 @@ # 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) +# led_strip = plasma.APA102(NUM_LEDS, 0, 0, 15, 14) # Set up the ADC for reading current sense = Analog(plasma.PIN_CURRENT_SENSE, plasma.ADC_GAIN, plasma.SHUNT_RESISTOR) @@ -30,5 +30,5 @@ # Display the current value once every second print("Current =", sense.read_current(), "A") count = 0 - + time.sleep(1.0 / UPDATES) From 49b944a40a5b4395aeb13ca39a3c8aef152fef30 Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Wed, 18 Aug 2021 10:54:31 +0100 Subject: [PATCH 06/30] Added user button to rainbow example --- examples/plasma_2040/plasma2040_rainbow.cpp | 26 ++++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/examples/plasma_2040/plasma2040_rainbow.cpp b/examples/plasma_2040/plasma2040_rainbow.cpp index ac189208e..bdeef29d1 100644 --- a/examples/plasma_2040/plasma2040_rainbow.cpp +++ b/examples/plasma_2040/plasma2040_rainbow.cpp @@ -13,6 +13,7 @@ /* 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; @@ -20,6 +21,12 @@ using namespace pimoroni; // 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 @@ -29,6 +36,7 @@ const uint N_LEDS = 30; // 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); +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); @@ -37,17 +45,23 @@ RGBLED led(plasma::LED_R, plasma::LED_G, plasma::LED_B); int main() { stdio_init_all(); - led_strip.start(60); + led_strip.start(UPDATES); - int speed = 10; + int speed = DEFAULT_SPEED; float offset = 0.0f; 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; @@ -61,6 +75,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); } } From 84acf406a29bc5953ec99d08706e8d4c7140765b Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Wed, 18 Aug 2021 11:59:22 +0100 Subject: [PATCH 07/30] Added Dat and Clk defines --- micropython/modules/plasma_2040/plasma_2040.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/micropython/modules/plasma_2040/plasma_2040.c b/micropython/modules/plasma_2040/plasma_2040.c index 6f651da01..5b1bb383e 100644 --- a/micropython/modules/plasma_2040/plasma_2040.c +++ b/micropython/modules/plasma_2040/plasma_2040.c @@ -69,6 +69,8 @@ STATIC const mp_map_elem_t plasma_globals_table[] = { { 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_PIN_USER_SW), MP_ROM_INT(23) }, + { MP_ROM_QSTR(MP_QSTR_PIN_CLK), MP_ROM_INT(14) }, + { MP_ROM_QSTR(MP_QSTR_PIN_DAT), MP_ROM_INT(15) }, { MP_ROM_QSTR(MP_QSTR_PIN_CURRENT_SENSE), MP_ROM_INT(29) }, { MP_ROM_QSTR(MP_QSTR_COLOR_ORDER_RGB), MP_ROM_INT(0x00) }, From fa17f2e77e521b245cdc5c540e4c9d797416b652 Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Wed, 18 Aug 2021 12:16:51 +0100 Subject: [PATCH 08/30] Added analog class for plasma current reading --- drivers/CMakeLists.txt | 1 + drivers/analog/CMakeLists.txt | 1 + drivers/analog/analog.cmake | 11 +++++++++++ drivers/analog/analog.cpp | 18 ++++++++++++++++++ drivers/analog/analog.hpp | 31 +++++++++++++++++++++++++++++++ 5 files changed, 62 insertions(+) create mode 100644 drivers/analog/CMakeLists.txt create mode 100644 drivers/analog/analog.cmake create mode 100644 drivers/analog/analog.cpp create mode 100644 drivers/analog/analog.hpp diff --git a/drivers/CMakeLists.txt b/drivers/CMakeLists.txt index 58a2bbd38..88d0b0993 100644 --- a/drivers/CMakeLists.txt +++ b/drivers/CMakeLists.txt @@ -1,3 +1,4 @@ +add_subdirectory(analog) add_subdirectory(esp32spi) add_subdirectory(ioexpander) add_subdirectory(ltp305) diff --git a/drivers/analog/CMakeLists.txt b/drivers/analog/CMakeLists.txt new file mode 100644 index 000000000..06d944437 --- /dev/null +++ b/drivers/analog/CMakeLists.txt @@ -0,0 +1 @@ +include(analog.cmake) \ No newline at end of file diff --git a/drivers/analog/analog.cmake b/drivers/analog/analog.cmake new file mode 100644 index 000000000..69caf0a67 --- /dev/null +++ b/drivers/analog/analog.cmake @@ -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) \ No newline at end of file diff --git a/drivers/analog/analog.cpp b/drivers/analog/analog.cpp new file mode 100644 index 000000000..f3179a7e1 --- /dev/null +++ b/drivers/analog/analog.cpp @@ -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(); + } +}; \ No newline at end of file diff --git a/drivers/analog/analog.hpp b/drivers/analog/analog.hpp new file mode 100644 index 000000000..cad26e46e --- /dev/null +++ b/drivers/analog/analog.hpp @@ -0,0 +1,31 @@ +#pragma once + +#include +#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; + }; + +} \ No newline at end of file From fa4cf86126d2b73d8b09ace0e131119dc41faece Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Wed, 18 Aug 2021 12:18:25 +0100 Subject: [PATCH 09/30] Updated rainbow example to be consistant across languages --- examples/plasma_2040/plasma2040_rainbow.cmake | 11 ++-- examples/plasma_2040/plasma2040_rainbow.cpp | 11 ++++ libraries/plasma2040/plasma2040.hpp | 7 +++ micropython/examples/plasma_2040/rainbow.py | 54 ++++++++++++++++--- 4 files changed, 72 insertions(+), 11 deletions(-) diff --git a/examples/plasma_2040/plasma2040_rainbow.cmake b/examples/plasma_2040/plasma2040_rainbow.cmake index c0558e65f..ceb1bd096 100644 --- a/examples/plasma_2040/plasma2040_rainbow.cmake +++ b/examples/plasma_2040/plasma2040_rainbow.cmake @@ -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}) diff --git a/examples/plasma_2040/plasma2040_rainbow.cpp b/examples/plasma_2040/plasma2040_rainbow.cpp index bdeef29d1..13b4799d0 100644 --- a/examples/plasma_2040/plasma2040_rainbow.cpp +++ b/examples/plasma_2040/plasma2040_rainbow.cpp @@ -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); diff --git a/libraries/plasma2040/plasma2040.hpp b/libraries/plasma2040/plasma2040.hpp index d57a66e83..c4d142727 100644 --- a/libraries/plasma2040/plasma2040.hpp +++ b/libraries/plasma2040/plasma2040.hpp @@ -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; } \ No newline at end of file diff --git a/micropython/examples/plasma_2040/rainbow.py b/micropython/examples/plasma_2040/rainbow.py index d364f433c..40b0b80f0 100644 --- a/micropython/examples/plasma_2040/rainbow.py +++ b/micropython/examples/plasma_2040/rainbow.py @@ -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) + +# Pick *one* LED type by uncommenting the relevant line below: + # APA102 / DotStar™ LEDs -# led_strip = plasma.APA102(NUM_LEDS, 0, 0, 15, 14) +# 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) -# Set up the ADC for reading current +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: From 33dcdd2e22be422f6f98eb7f8f576f68a42fb25b Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Wed, 18 Aug 2021 12:19:10 +0100 Subject: [PATCH 10/30] Tweak to rotary, for consistency --- examples/plasma_2040/plasma2040_rotary.cmake | 7 ++++--- examples/plasma_2040/plasma2040_rotary.cpp | 7 +++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/examples/plasma_2040/plasma2040_rotary.cmake b/examples/plasma_2040/plasma2040_rotary.cmake index 5dfc7087a..a9a632143 100644 --- a/examples/plasma_2040/plasma2040_rotary.cmake +++ b/examples/plasma_2040/plasma2040_rotary.cmake @@ -1,6 +1,7 @@ -add_executable(plasma2040_rotary plasma2040_rotary.cpp) +set(OUTPUT_NAME plasma2040_rotary) +add_executable(${OUTPUT_NAME} plasma2040_rotary.cpp) -target_link_libraries(plasma2040_rotary +target_link_libraries(${OUTPUT_NAME} pico_stdlib plasma2040 breakout_encoder @@ -9,4 +10,4 @@ target_link_libraries(plasma2040_rotary button ) -pico_add_extra_outputs(plasma2040_rotary) +pico_add_extra_outputs(${OUTPUT_NAME}) diff --git a/examples/plasma_2040/plasma2040_rotary.cpp b/examples/plasma_2040/plasma2040_rotary.cpp index 650491d43..260ef9a6e 100644 --- a/examples/plasma_2040/plasma2040_rotary.cpp +++ b/examples/plasma_2040/plasma2040_rotary.cpp @@ -16,6 +16,9 @@ using namespace pimoroni; // 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 @@ -67,7 +70,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(); @@ -147,6 +150,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); } } From a9883788f834a5b35422d38b7186b1157743541d Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Tue, 17 Aug 2021 12:48:45 +0100 Subject: [PATCH 11/30] Added pins for user sw and current sense --- micropython/modules/plasma_2040/plasma_2040.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/micropython/modules/plasma_2040/plasma_2040.c b/micropython/modules/plasma_2040/plasma_2040.c index c606f0f98..6f651da01 100644 --- a/micropython/modules/plasma_2040/plasma_2040.c +++ b/micropython/modules/plasma_2040/plasma_2040.c @@ -68,6 +68,8 @@ STATIC const mp_map_elem_t plasma_globals_table[] = { { 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_PIN_USER_SW), MP_ROM_INT(23) }, + { MP_ROM_QSTR(MP_QSTR_PIN_CURRENT_SENSE), MP_ROM_INT(29) }, { MP_ROM_QSTR(MP_QSTR_COLOR_ORDER_RGB), MP_ROM_INT(0x00) }, { MP_ROM_QSTR(MP_QSTR_COLOR_ORDER_RBG), MP_ROM_INT(0x01) }, From 2b8fbde65923e4df5892b303b15ae0e5c598b6a6 Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Tue, 17 Aug 2021 12:59:36 +0100 Subject: [PATCH 12/30] Changed rgb to use full pwm range --- micropython/modules_py/pimoroni.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/micropython/modules_py/pimoroni.py b/micropython/modules_py/pimoroni.py index 643b5caf4..1b6fe06f6 100644 --- a/micropython/modules_py/pimoroni.py +++ b/micropython/modules_py/pimoroni.py @@ -80,6 +80,6 @@ def set_rgb(self, r, g, b): r = 255 - r g = 255 - g b = 255 - b - self.led_r.duty_u16(r * 255) - self.led_g.duty_u16(g * 255) - self.led_b.duty_u16(b * 255) + self.led_r.duty_u16((r * 65535) / 255) + self.led_g.duty_u16((g * 65535) / 255) + self.led_b.duty_u16((b * 65535) / 255) From 5b27d7ac0d2def3584104579421219c6ed47a67e Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Tue, 17 Aug 2021 13:07:34 +0100 Subject: [PATCH 13/30] Fix for "Changed rgb to use full pwm range" --- micropython/modules_py/pimoroni.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/micropython/modules_py/pimoroni.py b/micropython/modules_py/pimoroni.py index 1b6fe06f6..9dd3c6343 100644 --- a/micropython/modules_py/pimoroni.py +++ b/micropython/modules_py/pimoroni.py @@ -80,6 +80,6 @@ def set_rgb(self, r, g, b): r = 255 - r g = 255 - g b = 255 - b - self.led_r.duty_u16((r * 65535) / 255) - self.led_g.duty_u16((g * 65535) / 255) - self.led_b.duty_u16((b * 65535) / 255) + self.led_r.duty_u16(int((r * 65535) / 255)) + self.led_g.duty_u16(int((g * 65535) / 255)) + self.led_b.duty_u16(int((b * 65535) / 255)) From 336791ed772d323c9fa171e388ba8d22b6b8fe78 Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Tue, 17 Aug 2021 13:25:46 +0100 Subject: [PATCH 14/30] Updated plasma mp examples to use user_sw and current sensing --- micropython/examples/plasma_2040/rainbow.py | 19 +++++++++++++++++-- .../plasma_2040/rgb-led-and-buttons.py | 8 ++++++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/micropython/examples/plasma_2040/rainbow.py b/micropython/examples/plasma_2040/rainbow.py index 89e93dc4a..2e88281c1 100644 --- a/micropython/examples/plasma_2040/rainbow.py +++ b/micropython/examples/plasma_2040/rainbow.py @@ -1,19 +1,34 @@ import plasma import time +# Import helper for Analog +from pimoroni import Analog + NUM_LEDS = 30 +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) +#led_strip = plasma.APA102(NUM_LEDS, 0, 0, 15, 14) + +# Set up the ADC for reading current +sense = Analog(plasma.PIN_CURRENT_SENSE, plasma.ADC_GAIN, plasma.SHUNT_RESISTOR) # Start updating the LED strip led_strip.start() +count = 0 # Make rainbows while True: t = time.ticks_ms() / 1000.0 / 5.0 for i in range(NUM_LEDS): led_strip.set_hsv(i, t + (i / NUM_LEDS)) - time.sleep(1.0 / 60) + + 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) diff --git a/micropython/examples/plasma_2040/rgb-led-and-buttons.py b/micropython/examples/plasma_2040/rgb-led-and-buttons.py index a6d4501ef..f4efa4563 100644 --- a/micropython/examples/plasma_2040/rgb-led-and-buttons.py +++ b/micropython/examples/plasma_2040/rgb-led-and-buttons.py @@ -1,18 +1,22 @@ 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 +from plasma import PIN_LED_R, PIN_LED_G, PIN_LED_B, PIN_USER_SW, 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) +led.set_rgb(0, 0, 0) +user_sw = Button(PIN_USER_SW) button_a = Button(PIN_BUTTON_A) button_b = Button(PIN_BUTTON_B) while True: + if user_sw.read(): + print("Pressed User SW - {}".format(time.ticks_ms())) + led.set_rgb(255, 0, 0) if button_a.read(): print("Pressed A - {}".format(time.ticks_ms())) led.set_rgb(0, 255, 0) From 373a7df9239690dd9875375b13e9858afeb33dc4 Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Tue, 17 Aug 2021 13:43:42 +0100 Subject: [PATCH 15/30] Linting fixes --- micropython/examples/plasma_2040/rainbow.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/micropython/examples/plasma_2040/rainbow.py b/micropython/examples/plasma_2040/rainbow.py index 2e88281c1..d364f433c 100644 --- a/micropython/examples/plasma_2040/rainbow.py +++ b/micropython/examples/plasma_2040/rainbow.py @@ -10,7 +10,7 @@ # 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) +# led_strip = plasma.APA102(NUM_LEDS, 0, 0, 15, 14) # Set up the ADC for reading current sense = Analog(plasma.PIN_CURRENT_SENSE, plasma.ADC_GAIN, plasma.SHUNT_RESISTOR) @@ -30,5 +30,5 @@ # Display the current value once every second print("Current =", sense.read_current(), "A") count = 0 - + time.sleep(1.0 / UPDATES) From f5aba964192b432a7c75c5567c6347d8f180ba6e Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Wed, 18 Aug 2021 10:54:31 +0100 Subject: [PATCH 16/30] Added user button to rainbow example --- examples/plasma_2040/plasma2040_rainbow.cpp | 26 ++++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/examples/plasma_2040/plasma2040_rainbow.cpp b/examples/plasma_2040/plasma2040_rainbow.cpp index ac189208e..bdeef29d1 100644 --- a/examples/plasma_2040/plasma2040_rainbow.cpp +++ b/examples/plasma_2040/plasma2040_rainbow.cpp @@ -13,6 +13,7 @@ /* 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; @@ -20,6 +21,12 @@ using namespace pimoroni; // 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 @@ -29,6 +36,7 @@ const uint N_LEDS = 30; // 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); +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); @@ -37,17 +45,23 @@ RGBLED led(plasma::LED_R, plasma::LED_G, plasma::LED_B); int main() { stdio_init_all(); - led_strip.start(60); + led_strip.start(UPDATES); - int speed = 10; + int speed = DEFAULT_SPEED; float offset = 0.0f; 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; @@ -61,6 +75,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); } } From 281288a6f44708cc00a7821df8746dec82181599 Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Wed, 18 Aug 2021 11:59:22 +0100 Subject: [PATCH 17/30] Added Dat and Clk defines --- micropython/modules/plasma_2040/plasma_2040.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/micropython/modules/plasma_2040/plasma_2040.c b/micropython/modules/plasma_2040/plasma_2040.c index 6f651da01..5b1bb383e 100644 --- a/micropython/modules/plasma_2040/plasma_2040.c +++ b/micropython/modules/plasma_2040/plasma_2040.c @@ -69,6 +69,8 @@ STATIC const mp_map_elem_t plasma_globals_table[] = { { 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_PIN_USER_SW), MP_ROM_INT(23) }, + { MP_ROM_QSTR(MP_QSTR_PIN_CLK), MP_ROM_INT(14) }, + { MP_ROM_QSTR(MP_QSTR_PIN_DAT), MP_ROM_INT(15) }, { MP_ROM_QSTR(MP_QSTR_PIN_CURRENT_SENSE), MP_ROM_INT(29) }, { MP_ROM_QSTR(MP_QSTR_COLOR_ORDER_RGB), MP_ROM_INT(0x00) }, From ae0949895354340f6592113ce3bd15d2b1112fcd Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Wed, 18 Aug 2021 12:16:51 +0100 Subject: [PATCH 18/30] Added analog class for plasma current reading --- drivers/CMakeLists.txt | 1 + drivers/analog/CMakeLists.txt | 1 + drivers/analog/analog.cmake | 11 +++++++++++ drivers/analog/analog.cpp | 18 ++++++++++++++++++ drivers/analog/analog.hpp | 31 +++++++++++++++++++++++++++++++ 5 files changed, 62 insertions(+) create mode 100644 drivers/analog/CMakeLists.txt create mode 100644 drivers/analog/analog.cmake create mode 100644 drivers/analog/analog.cpp create mode 100644 drivers/analog/analog.hpp diff --git a/drivers/CMakeLists.txt b/drivers/CMakeLists.txt index 58a2bbd38..88d0b0993 100644 --- a/drivers/CMakeLists.txt +++ b/drivers/CMakeLists.txt @@ -1,3 +1,4 @@ +add_subdirectory(analog) add_subdirectory(esp32spi) add_subdirectory(ioexpander) add_subdirectory(ltp305) diff --git a/drivers/analog/CMakeLists.txt b/drivers/analog/CMakeLists.txt new file mode 100644 index 000000000..06d944437 --- /dev/null +++ b/drivers/analog/CMakeLists.txt @@ -0,0 +1 @@ +include(analog.cmake) \ No newline at end of file diff --git a/drivers/analog/analog.cmake b/drivers/analog/analog.cmake new file mode 100644 index 000000000..69caf0a67 --- /dev/null +++ b/drivers/analog/analog.cmake @@ -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) \ No newline at end of file diff --git a/drivers/analog/analog.cpp b/drivers/analog/analog.cpp new file mode 100644 index 000000000..f3179a7e1 --- /dev/null +++ b/drivers/analog/analog.cpp @@ -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(); + } +}; \ No newline at end of file diff --git a/drivers/analog/analog.hpp b/drivers/analog/analog.hpp new file mode 100644 index 000000000..cad26e46e --- /dev/null +++ b/drivers/analog/analog.hpp @@ -0,0 +1,31 @@ +#pragma once + +#include +#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; + }; + +} \ No newline at end of file From 64e6d248d51bb3b767d602a4b9a75441b74d9b14 Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Wed, 18 Aug 2021 12:18:25 +0100 Subject: [PATCH 19/30] Updated rainbow example to be consistant across languages --- examples/plasma_2040/plasma2040_rainbow.cmake | 11 ++-- examples/plasma_2040/plasma2040_rainbow.cpp | 11 ++++ libraries/plasma2040/plasma2040.hpp | 7 +++ micropython/examples/plasma_2040/rainbow.py | 54 ++++++++++++++++--- 4 files changed, 72 insertions(+), 11 deletions(-) diff --git a/examples/plasma_2040/plasma2040_rainbow.cmake b/examples/plasma_2040/plasma2040_rainbow.cmake index c0558e65f..ceb1bd096 100644 --- a/examples/plasma_2040/plasma2040_rainbow.cmake +++ b/examples/plasma_2040/plasma2040_rainbow.cmake @@ -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}) diff --git a/examples/plasma_2040/plasma2040_rainbow.cpp b/examples/plasma_2040/plasma2040_rainbow.cpp index bdeef29d1..13b4799d0 100644 --- a/examples/plasma_2040/plasma2040_rainbow.cpp +++ b/examples/plasma_2040/plasma2040_rainbow.cpp @@ -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); diff --git a/libraries/plasma2040/plasma2040.hpp b/libraries/plasma2040/plasma2040.hpp index d57a66e83..c4d142727 100644 --- a/libraries/plasma2040/plasma2040.hpp +++ b/libraries/plasma2040/plasma2040.hpp @@ -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; } \ No newline at end of file diff --git a/micropython/examples/plasma_2040/rainbow.py b/micropython/examples/plasma_2040/rainbow.py index d364f433c..40b0b80f0 100644 --- a/micropython/examples/plasma_2040/rainbow.py +++ b/micropython/examples/plasma_2040/rainbow.py @@ -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) + +# Pick *one* LED type by uncommenting the relevant line below: + # APA102 / DotStar™ LEDs -# led_strip = plasma.APA102(NUM_LEDS, 0, 0, 15, 14) +# 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) -# Set up the ADC for reading current +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: From c1cc505e0a9035ed7dd5b0d7119df816a63fc598 Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Wed, 18 Aug 2021 12:19:10 +0100 Subject: [PATCH 20/30] Tweak to rotary, for consistency --- examples/plasma_2040/plasma2040_rotary.cmake | 7 ++++--- examples/plasma_2040/plasma2040_rotary.cpp | 7 +++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/examples/plasma_2040/plasma2040_rotary.cmake b/examples/plasma_2040/plasma2040_rotary.cmake index 5dfc7087a..a9a632143 100644 --- a/examples/plasma_2040/plasma2040_rotary.cmake +++ b/examples/plasma_2040/plasma2040_rotary.cmake @@ -1,6 +1,7 @@ -add_executable(plasma2040_rotary plasma2040_rotary.cpp) +set(OUTPUT_NAME plasma2040_rotary) +add_executable(${OUTPUT_NAME} plasma2040_rotary.cpp) -target_link_libraries(plasma2040_rotary +target_link_libraries(${OUTPUT_NAME} pico_stdlib plasma2040 breakout_encoder @@ -9,4 +10,4 @@ target_link_libraries(plasma2040_rotary button ) -pico_add_extra_outputs(plasma2040_rotary) +pico_add_extra_outputs(${OUTPUT_NAME}) diff --git a/examples/plasma_2040/plasma2040_rotary.cpp b/examples/plasma_2040/plasma2040_rotary.cpp index 650491d43..260ef9a6e 100644 --- a/examples/plasma_2040/plasma2040_rotary.cpp +++ b/examples/plasma_2040/plasma2040_rotary.cpp @@ -16,6 +16,9 @@ using namespace pimoroni; // 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 @@ -67,7 +70,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(); @@ -147,6 +150,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); } } From 823729dc243d789a4130fabaf027a99f63ed993f Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Thu, 19 Aug 2021 13:40:46 +0100 Subject: [PATCH 21/30] Move apa and ws PIO into drivers so they can be easily used with more boards --- drivers/CMakeLists.txt | 1 + drivers/plasma/CMakeLists.txt | 1 + .../plasma2040 => drivers/plasma}/apa102.cpp | 0 .../plasma2040 => drivers/plasma}/apa102.hpp | 0 .../plasma2040 => drivers/plasma}/apa102.pio | 0 drivers/plasma/plasma.cmake | 18 ++++++++++++++++++ .../plasma2040 => drivers/plasma}/ws2812.cpp | 0 .../plasma2040 => drivers/plasma}/ws2812.hpp | 0 .../plasma2040 => drivers/plasma}/ws2812.pio | 0 libraries/plasma2040/plasma2040.cmake | 15 ++------------- .../modules/plasma_2040/micropython.cmake | 9 +++++---- 11 files changed, 27 insertions(+), 17 deletions(-) create mode 100644 drivers/plasma/CMakeLists.txt rename {libraries/plasma2040 => drivers/plasma}/apa102.cpp (100%) rename {libraries/plasma2040 => drivers/plasma}/apa102.hpp (100%) rename {libraries/plasma2040 => drivers/plasma}/apa102.pio (100%) create mode 100644 drivers/plasma/plasma.cmake rename {libraries/plasma2040 => drivers/plasma}/ws2812.cpp (100%) rename {libraries/plasma2040 => drivers/plasma}/ws2812.hpp (100%) rename {libraries/plasma2040 => drivers/plasma}/ws2812.pio (100%) diff --git a/drivers/CMakeLists.txt b/drivers/CMakeLists.txt index 88d0b0993..948b56ed9 100644 --- a/drivers/CMakeLists.txt +++ b/drivers/CMakeLists.txt @@ -19,4 +19,5 @@ add_subdirectory(bme68x) add_subdirectory(bmp280) add_subdirectory(bme280) add_subdirectory(button) +add_subdirectory(plasma) add_subdirectory(rgbled) diff --git a/drivers/plasma/CMakeLists.txt b/drivers/plasma/CMakeLists.txt new file mode 100644 index 000000000..3f66aef6d --- /dev/null +++ b/drivers/plasma/CMakeLists.txt @@ -0,0 +1 @@ +include(plasma.cmake) \ No newline at end of file diff --git a/libraries/plasma2040/apa102.cpp b/drivers/plasma/apa102.cpp similarity index 100% rename from libraries/plasma2040/apa102.cpp rename to drivers/plasma/apa102.cpp diff --git a/libraries/plasma2040/apa102.hpp b/drivers/plasma/apa102.hpp similarity index 100% rename from libraries/plasma2040/apa102.hpp rename to drivers/plasma/apa102.hpp diff --git a/libraries/plasma2040/apa102.pio b/drivers/plasma/apa102.pio similarity index 100% rename from libraries/plasma2040/apa102.pio rename to drivers/plasma/apa102.pio diff --git a/drivers/plasma/plasma.cmake b/drivers/plasma/plasma.cmake new file mode 100644 index 000000000..8c3a948c5 --- /dev/null +++ b/drivers/plasma/plasma.cmake @@ -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) \ No newline at end of file diff --git a/libraries/plasma2040/ws2812.cpp b/drivers/plasma/ws2812.cpp similarity index 100% rename from libraries/plasma2040/ws2812.cpp rename to drivers/plasma/ws2812.cpp diff --git a/libraries/plasma2040/ws2812.hpp b/drivers/plasma/ws2812.hpp similarity index 100% rename from libraries/plasma2040/ws2812.hpp rename to drivers/plasma/ws2812.hpp diff --git a/libraries/plasma2040/ws2812.pio b/drivers/plasma/ws2812.pio similarity index 100% rename from libraries/plasma2040/ws2812.pio rename to drivers/plasma/ws2812.pio diff --git a/libraries/plasma2040/plasma2040.cmake b/libraries/plasma2040/plasma2040.cmake index 55abb9b07..cebcfe6d9 100644 --- a/libraries/plasma2040/plasma2040.cmake +++ b/libraries/plasma2040/plasma2040.cmake @@ -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) \ No newline at end of file +# Pull in pico libraries that we need +target_link_libraries(plasma2040 INTERFACE pico_stdlib plasma) \ No newline at end of file diff --git a/micropython/modules/plasma_2040/micropython.cmake b/micropython/modules/plasma_2040/micropython.cmake index 662521a14..4cd79faf9 100644 --- a/micropython/modules/plasma_2040/micropython.cmake +++ b/micropython/modules/plasma_2040/micropython.cmake @@ -5,14 +5,15 @@ add_library(usermod_${MOD_NAME} INTERFACE) target_sources(usermod_${MOD_NAME} INTERFACE ${CMAKE_CURRENT_LIST_DIR}/${MOD_NAME}.c ${CMAKE_CURRENT_LIST_DIR}/${MOD_NAME}.cpp - ${CMAKE_CURRENT_LIST_DIR}/../../../libraries/plasma2040/apa102.cpp - ${CMAKE_CURRENT_LIST_DIR}/../../../libraries/plasma2040/ws2812.cpp + ${CMAKE_CURRENT_LIST_DIR}/../../../drivers/plasma/apa102.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}/../../../libraries/plasma2040/ws2812.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}/../../../drivers/plasma/ws2812.pio) target_include_directories(usermod_${MOD_NAME} INTERFACE ${CMAKE_CURRENT_LIST_DIR} + ${CMAKE_CURRENT_LIST_DIR}/../../../drivers/plasma/ ${CMAKE_CURRENT_LIST_DIR}/../../../libraries/plasma2040/ ) From 8e763f5efe19ec9c4c0aac6a08e8d3f794fbfbeb Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Thu, 19 Aug 2021 14:12:56 +0100 Subject: [PATCH 22/30] Added a plasma2040 namespaces --- examples/CMakeLists.txt | 2 +- .../CMakeLists.txt | 0 .../plasma2040_rainbow.cmake | 0 .../plasma2040_rainbow.cpp | 15 ++++++------ .../plasma2040_rotary.cmake | 0 .../plasma2040_rotary.cpp | 11 +++++---- .../plasma2040_stacker.cmake | 0 .../plasma2040_stacker.cpp | 13 +++++----- libraries/plasma2040/plasma2040.hpp | 24 ++++++++++--------- 9 files changed, 35 insertions(+), 30 deletions(-) rename examples/{plasma_2040 => plasma2040}/CMakeLists.txt (100%) rename examples/{plasma_2040 => plasma2040}/plasma2040_rainbow.cmake (100%) rename examples/{plasma_2040 => plasma2040}/plasma2040_rainbow.cpp (80%) rename examples/{plasma_2040 => plasma2040}/plasma2040_rotary.cmake (100%) rename examples/{plasma_2040 => plasma2040}/plasma2040_rotary.cpp (93%) rename examples/{plasma_2040 => plasma2040}/plasma2040_stacker.cmake (100%) rename examples/{plasma_2040 => plasma2040}/plasma2040_stacker.cpp (95%) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 9b4ad7596..d514d21d5 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -33,4 +33,4 @@ add_subdirectory(pico_trackball_display) add_subdirectory(pico_audio) add_subdirectory(pico_wireless) -add_subdirectory(plasma_2040) +add_subdirectory(plasma2040) diff --git a/examples/plasma_2040/CMakeLists.txt b/examples/plasma2040/CMakeLists.txt similarity index 100% rename from examples/plasma_2040/CMakeLists.txt rename to examples/plasma2040/CMakeLists.txt diff --git a/examples/plasma_2040/plasma2040_rainbow.cmake b/examples/plasma2040/plasma2040_rainbow.cmake similarity index 100% rename from examples/plasma_2040/plasma2040_rainbow.cmake rename to examples/plasma2040/plasma2040_rainbow.cmake diff --git a/examples/plasma_2040/plasma2040_rainbow.cpp b/examples/plasma2040/plasma2040_rainbow.cpp similarity index 80% rename from examples/plasma_2040/plasma2040_rainbow.cpp rename to examples/plasma2040/plasma2040_rainbow.cpp index 13b4799d0..51811ec92 100644 --- a/examples/plasma_2040/plasma2040_rainbow.cpp +++ b/examples/plasma2040/plasma2040_rainbow.cpp @@ -18,6 +18,7 @@ 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; @@ -32,17 +33,17 @@ 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 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); +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::SENSE, plasma2040::ADC_GAIN, plasma2040::SHUNT_RESISTOR); int main() { diff --git a/examples/plasma_2040/plasma2040_rotary.cmake b/examples/plasma2040/plasma2040_rotary.cmake similarity index 100% rename from examples/plasma_2040/plasma2040_rotary.cmake rename to examples/plasma2040/plasma2040_rotary.cmake diff --git a/examples/plasma_2040/plasma2040_rotary.cpp b/examples/plasma2040/plasma2040_rotary.cpp similarity index 93% rename from examples/plasma_2040/plasma2040_rotary.cpp rename to examples/plasma2040/plasma2040_rotary.cpp index 260ef9a6e..7ee59c422 100644 --- a/examples/plasma_2040/plasma2040_rotary.cpp +++ b/examples/plasma2040/plasma2040_rotary.cpp @@ -12,6 +12,7 @@ #include "button.hpp" using namespace pimoroni; +using namespace plasma; // Set how many LEDs you have const uint N_LEDS = 30; @@ -22,17 +23,17 @@ 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); diff --git a/examples/plasma_2040/plasma2040_stacker.cmake b/examples/plasma2040/plasma2040_stacker.cmake similarity index 100% rename from examples/plasma_2040/plasma2040_stacker.cmake rename to examples/plasma2040/plasma2040_stacker.cmake diff --git a/examples/plasma_2040/plasma2040_stacker.cpp b/examples/plasma2040/plasma2040_stacker.cpp similarity index 95% rename from examples/plasma_2040/plasma2040_stacker.cpp rename to examples/plasma2040/plasma2040_stacker.cpp index 1cc630713..ee50b7176 100644 --- a/examples/plasma_2040/plasma2040_stacker.cpp +++ b/examples/plasma2040/plasma2040_stacker.cpp @@ -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; @@ -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{ diff --git a/libraries/plasma2040/plasma2040.hpp b/libraries/plasma2040/plasma2040.hpp index c4d142727..8b007fce1 100644 --- a/libraries/plasma2040/plasma2040.hpp +++ b/libraries/plasma2040/plasma2040.hpp @@ -5,20 +5,22 @@ #include "ws2812.hpp" namespace plasma { -const uint LED_R = 16; -const uint LED_G = 17; -const uint LED_B = 18; + namespace plasma2040 { + static const uint LED_R = 16; + static const uint LED_G = 17; + static const uint LED_B = 18; -const uint BUTTON_A = 12; -const uint BUTTON_B = 13; + static const uint BUTTON_A = 12; + static const uint BUTTON_B = 13; -const uint USER_SW = 23; + static 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 + static const uint CLK = 14; // Used only for APA102 + static const uint DAT = 15; // Used for both APA102 and WS2812 -const uint PIN_SENSE = 29; // The pin used for current sensing + static const uint SENSE = 29; // The pin used for current sensing -constexpr float ADC_GAIN = 50; -constexpr float SHUNT_RESISTOR = 0.015f; + static constexpr float ADC_GAIN = 50; + static constexpr float SHUNT_RESISTOR = 0.015f; + } } \ No newline at end of file From 3d34d70722f1ee5db3d42269340c6f40232adc8c Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Thu, 19 Aug 2021 14:22:43 +0100 Subject: [PATCH 23/30] Removed unneeded statics --- libraries/plasma2040/plasma2040.hpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/libraries/plasma2040/plasma2040.hpp b/libraries/plasma2040/plasma2040.hpp index 8b007fce1..d0df74d90 100644 --- a/libraries/plasma2040/plasma2040.hpp +++ b/libraries/plasma2040/plasma2040.hpp @@ -6,21 +6,21 @@ namespace plasma { namespace plasma2040 { - static const uint LED_R = 16; - static const uint LED_G = 17; - static const uint LED_B = 18; + const uint LED_R = 16; + const uint LED_G = 17; + const uint LED_B = 18; - static const uint BUTTON_A = 12; - static const uint BUTTON_B = 13; + const uint BUTTON_A = 12; + const uint BUTTON_B = 13; - static const uint USER_SW = 23; + const uint USER_SW = 23; - static const uint CLK = 14; // Used only for APA102 - static const uint 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 - static const uint SENSE = 29; // The pin used for current sensing + const uint SENSE = 29; // The pin used for current sensing - static constexpr float ADC_GAIN = 50; - static constexpr float SHUNT_RESISTOR = 0.015f; + constexpr float ADC_GAIN = 50; + constexpr float SHUNT_RESISTOR = 0.015f; } } \ No newline at end of file From aac8c8d2fc4afb560193f9c4301b7705ab4059cf Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Thu, 19 Aug 2021 14:51:37 +0100 Subject: [PATCH 24/30] Test of sub module --- micropython/modules/plasma_2040/plasma_2040.c | 35 ++++++++++++------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/micropython/modules/plasma_2040/plasma_2040.c b/micropython/modules/plasma_2040/plasma_2040.c index 5b1bb383e..092488edb 100644 --- a/micropython/modules/plasma_2040/plasma_2040.c +++ b/micropython/modules/plasma_2040/plasma_2040.c @@ -59,19 +59,33 @@ typedef struct _mp_obj_float_t { mp_obj_float_t shunt_resistor = {{&mp_type_float}, 0.015f}; /***** 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_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[] = { { 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) }, - { MP_ROM_QSTR(MP_QSTR_PIN_USER_SW), MP_ROM_INT(23) }, - { MP_ROM_QSTR(MP_QSTR_PIN_CLK), MP_ROM_INT(14) }, - { MP_ROM_QSTR(MP_QSTR_PIN_DAT), MP_ROM_INT(15) }, - { MP_ROM_QSTR(MP_QSTR_PIN_CURRENT_SENSE), MP_ROM_INT(29) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_plasma2040), (mp_obj_t)&plasma2040_user_cmodule }, { MP_ROM_QSTR(MP_QSTR_COLOR_ORDER_RGB), MP_ROM_INT(0x00) }, { MP_ROM_QSTR(MP_QSTR_COLOR_ORDER_RBG), MP_ROM_INT(0x01) }, @@ -79,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_BRG), MP_ROM_INT(0x04) }, { 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); From 0faa312358dd4120e06afe9792f19a5686e99ca2 Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Thu, 19 Aug 2021 15:16:40 +0100 Subject: [PATCH 25/30] Updated micropython example to use working submodule --- examples/plasma2040/plasma2040_rainbow.cpp | 2 +- libraries/plasma2040/plasma2040.hpp | 2 +- micropython/examples/plasma_2040/rainbow.py | 15 ++++++++------- micropython/modules/plasma_2040/plasma_2040.c | 2 +- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/examples/plasma2040/plasma2040_rainbow.cpp b/examples/plasma2040/plasma2040_rainbow.cpp index 51811ec92..488959543 100644 --- a/examples/plasma2040/plasma2040_rainbow.cpp +++ b/examples/plasma2040/plasma2040_rainbow.cpp @@ -43,7 +43,7 @@ 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::SENSE, plasma2040::ADC_GAIN, plasma2040::SHUNT_RESISTOR); +Analog sense(plasma2040::CURRENT_SENSE, plasma2040::ADC_GAIN, plasma2040::SHUNT_RESISTOR); int main() { diff --git a/libraries/plasma2040/plasma2040.hpp b/libraries/plasma2040/plasma2040.hpp index d0df74d90..9e6cdf7a1 100644 --- a/libraries/plasma2040/plasma2040.hpp +++ b/libraries/plasma2040/plasma2040.hpp @@ -18,7 +18,7 @@ namespace plasma { const uint CLK = 14; // Used only for APA102 const uint DAT = 15; // Used for both APA102 and WS2812 - const uint SENSE = 29; // The pin used for current sensing + const uint CURRENT_SENSE = 29; // The pin used for current sensing constexpr float ADC_GAIN = 50; constexpr float SHUNT_RESISTOR = 0.015f; diff --git a/micropython/examples/plasma_2040/rainbow.py b/micropython/examples/plasma_2040/rainbow.py index 40b0b80f0..ea2ae340f 100644 --- a/micropython/examples/plasma_2040/rainbow.py +++ b/micropython/examples/plasma_2040/rainbow.py @@ -1,4 +1,5 @@ import plasma +from plasma import plasma2040 import time # Import helpers for RGB LEDs, Buttons, and Analog @@ -21,16 +22,16 @@ # 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) +#led_strip = plasma.APA102(NUM_LEDS, 0, 0, plasma2040.DAT, plasma2040.CLK) # WS2812 / NeoPixel™ LEDs -led_strip = plasma.WS2812(NUM_LEDS, 0, 0, plasma.PIN_DAT) +led_strip = plasma.WS2812(NUM_LEDS, 0, 0, plasma2040.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) +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 led_strip.start() diff --git a/micropython/modules/plasma_2040/plasma_2040.c b/micropython/modules/plasma_2040/plasma_2040.c index 092488edb..8429f004a 100644 --- a/micropython/modules/plasma_2040/plasma_2040.c +++ b/micropython/modules/plasma_2040/plasma_2040.c @@ -69,7 +69,7 @@ STATIC const mp_map_elem_t plasma2040_globals_table[] = { { 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_SENSE), MP_ROM_INT(29) }, + { 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) }, From c4e0316fd899b6469e9fad8954a973cd1f5b37b2 Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Thu, 19 Aug 2021 15:19:01 +0100 Subject: [PATCH 26/30] Linting fix --- micropython/examples/plasma_2040/rainbow.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/micropython/examples/plasma_2040/rainbow.py b/micropython/examples/plasma_2040/rainbow.py index ea2ae340f..f03d08d5e 100644 --- a/micropython/examples/plasma_2040/rainbow.py +++ b/micropython/examples/plasma_2040/rainbow.py @@ -22,7 +22,7 @@ # Pick *one* LED type by uncommenting the relevant line below: # APA102 / DotStar™ LEDs -#led_strip = plasma.APA102(NUM_LEDS, 0, 0, plasma2040.DAT, plasma2040.CLK) +# 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) From 7496e8c8ba762576d4868a86861f354bc3b220fd Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Thu, 19 Aug 2021 15:25:12 +0100 Subject: [PATCH 27/30] Renamed plasma mp directory to match module name --- micropython/modules/micropython.cmake | 2 +- micropython/modules/{plasma_2040 => plasma}/README.md | 0 .../modules/{plasma_2040 => plasma}/micropython.cmake | 3 +-- .../modules/{plasma_2040/plasma_2040.c => plasma/plasma.c} | 2 +- .../{plasma_2040/plasma_2040.cpp => plasma/plasma.cpp} | 5 +++-- .../modules/{plasma_2040/plasma_2040.h => plasma/plasma.h} | 0 6 files changed, 6 insertions(+), 6 deletions(-) rename micropython/modules/{plasma_2040 => plasma}/README.md (100%) rename micropython/modules/{plasma_2040 => plasma}/micropython.cmake (90%) rename micropython/modules/{plasma_2040/plasma_2040.c => plasma/plasma.c} (99%) rename micropython/modules/{plasma_2040/plasma_2040.cpp => plasma/plasma.cpp} (99%) rename micropython/modules/{plasma_2040/plasma_2040.h => plasma/plasma.h} (100%) diff --git a/micropython/modules/micropython.cmake b/micropython/modules/micropython.cmake index b982cec64..30044027e 100644 --- a/micropython/modules/micropython.cmake +++ b/micropython/modules/micropython.cmake @@ -32,5 +32,5 @@ include(pico_unicorn/micropython) include(pico_display/micropython) include(pico_explorer/micropython) include(pico_wireless/micropython) -include(plasma_2040/micropython) +include(plasma/micropython) include(ulab/code/micropython) diff --git a/micropython/modules/plasma_2040/README.md b/micropython/modules/plasma/README.md similarity index 100% rename from micropython/modules/plasma_2040/README.md rename to micropython/modules/plasma/README.md diff --git a/micropython/modules/plasma_2040/micropython.cmake b/micropython/modules/plasma/micropython.cmake similarity index 90% rename from micropython/modules/plasma_2040/micropython.cmake rename to micropython/modules/plasma/micropython.cmake index 4cd79faf9..c16c3cf32 100644 --- a/micropython/modules/plasma_2040/micropython.cmake +++ b/micropython/modules/plasma/micropython.cmake @@ -1,4 +1,4 @@ -set(MOD_NAME plasma_2040) +set(MOD_NAME plasma) string(TOUPPER ${MOD_NAME} MOD_NAME_UPPER) add_library(usermod_${MOD_NAME} INTERFACE) @@ -14,7 +14,6 @@ pico_generate_pio_header(usermod_${MOD_NAME} ${CMAKE_CURRENT_LIST_DIR}/../../../ target_include_directories(usermod_${MOD_NAME} INTERFACE ${CMAKE_CURRENT_LIST_DIR} ${CMAKE_CURRENT_LIST_DIR}/../../../drivers/plasma/ - ${CMAKE_CURRENT_LIST_DIR}/../../../libraries/plasma2040/ ) target_compile_definitions(usermod_${MOD_NAME} INTERFACE diff --git a/micropython/modules/plasma_2040/plasma_2040.c b/micropython/modules/plasma/plasma.c similarity index 99% rename from micropython/modules/plasma_2040/plasma_2040.c rename to micropython/modules/plasma/plasma.c index 8429f004a..795ca5b6c 100644 --- a/micropython/modules/plasma_2040/plasma_2040.c +++ b/micropython/modules/plasma/plasma.c @@ -1,4 +1,4 @@ -#include "plasma_2040.h" +#include "plasma.h" /***** Methods *****/ diff --git a/micropython/modules/plasma_2040/plasma_2040.cpp b/micropython/modules/plasma/plasma.cpp similarity index 99% rename from micropython/modules/plasma_2040/plasma_2040.cpp rename to micropython/modules/plasma/plasma.cpp index ccd3105f2..d3d319c0d 100644 --- a/micropython/modules/plasma_2040/plasma_2040.cpp +++ b/micropython/modules/plasma/plasma.cpp @@ -1,4 +1,5 @@ -#include "libraries/plasma2040/plasma2040.hpp" +#include "drivers/plasma/ws2812.hpp" +#include "drivers/plasma/apa102.hpp" #include #define MP_OBJ_TO_PTR2(o, t) ((t *)(uintptr_t)(o)) @@ -11,7 +12,7 @@ using namespace plasma; extern "C" { -#include "plasma_2040.h" +#include "plasma.h" #include "py/builtin.h" typedef struct _mp_obj_float_t { diff --git a/micropython/modules/plasma_2040/plasma_2040.h b/micropython/modules/plasma/plasma.h similarity index 100% rename from micropython/modules/plasma_2040/plasma_2040.h rename to micropython/modules/plasma/plasma.h From 681edd255b47a2729633c0fdf6ea6c22daf4a6e3 Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Thu, 19 Aug 2021 15:36:25 +0100 Subject: [PATCH 28/30] Changed default ws2812 freq to 800khz --- drivers/plasma/ws2812.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/plasma/ws2812.hpp b/drivers/plasma/ws2812.hpp index 4067dccb4..3da8722a6 100644 --- a/drivers/plasma/ws2812.hpp +++ b/drivers/plasma/ws2812.hpp @@ -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, From 41365fd89ec717321a47b6acf1d65c9c3479ecc1 Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Thu, 19 Aug 2021 15:50:16 +0100 Subject: [PATCH 29/30] Updated button example for new mp --- .../examples/plasma_2040/rgb-led-and-buttons.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/micropython/examples/plasma_2040/rgb-led-and-buttons.py b/micropython/examples/plasma_2040/rgb-led-and-buttons.py index f4efa4563..c54440cd1 100644 --- a/micropython/examples/plasma_2040/rgb-led-and-buttons.py +++ b/micropython/examples/plasma_2040/rgb-led-and-buttons.py @@ -1,17 +1,17 @@ import time -# Import pin constants from plasma -from plasma import PIN_LED_R, PIN_LED_G, PIN_LED_B, PIN_USER_SW, PIN_BUTTON_A, PIN_BUTTON_B +# Import plasma2040 +from plasma import plasma2040 # Import helpers for RGB LEDs and Buttons 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(0, 0, 0) -user_sw = Button(PIN_USER_SW) -button_a = Button(PIN_BUTTON_A) -button_b = Button(PIN_BUTTON_B) +user_sw = Button(plasma2040.USER_SW) +button_a = Button(plasma2040.BUTTON_A) +button_b = Button(plasma2040.BUTTON_B) while True: if user_sw.read(): From 4976c90ab30752aa17e0ce351c2e76bc3e13120b Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Thu, 19 Aug 2021 16:15:38 +0100 Subject: [PATCH 30/30] Updated readme with changes, and description of current sensing --- micropython/modules/plasma/README.md | 77 ++++++++++++++++++++++------ 1 file changed, 60 insertions(+), 17 deletions(-) diff --git a/micropython/modules/plasma/README.md b/micropython/modules/plasma/README.md index 94f74ba91..5e5ca4b08 100644 --- a/micropython/modules/plasma/README.md +++ b/micropython/modules/plasma/README.md @@ -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) - [Buttons](#buttons) - [RGBLED](#rgbled) +- [Measuring LED Strip Current Draw](#measuring-led-strip-current-draw) + - [Analog](#analog) ## Notes On PIO Limitations @@ -33,11 +35,12 @@ Construct a new `WS2812` instance, specifying the number of LEDs, PIO, PIO state ```python import plasma +from plasma import plasma2040 LEDS = 30 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. @@ -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: -``` +```python import plasma +from plasma import plasma2040 LEDS = 30 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`: @@ -106,11 +110,12 @@ Construct a new `APA102` instance, specifying the number of LEDs, PIO, PIO state ```python import plasma +from plasma import plasma2040 LEDS = 30 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. @@ -151,13 +156,14 @@ Button(button, invert=True, repeat_time=200, hold_time=1000) 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 -* `plasma.PIN_LED_G` = 17 -* `plasma.PIN_LED_B` = 18 -* `plasma.PIN_BUTTON_A` = 12 -* `plasma.PIN_BUTTON_B` = 13 +* `plasma2040.LED_R` = 16 +* `plasma2040.LED_G` = 17 +* `plasma2040.LED_B` = 18 +* `plasma2040.BUTTON_A` = 12 +* `plasma2040.BUTTON_B` = 13 +* `plasma2040.USER_SW` = 23 ### Buttons @@ -165,14 +171,14 @@ Import the `Button` class from the `pimoroni` module and the pin constants for t ```python 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: ```python -button_a = Button(PIN_BUTTON_A) -button_b = Button(PIN_BUTTON_B) +button_a = Button(plasma2040.BUTTON_A) +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: @@ -183,17 +189,17 @@ state = button_a.read() ### 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 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: ```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: @@ -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(0, 255, 0) # Full green led.set_rgb(0, 0, 255) # Full blue -``` \ No newline at end of file +``` + +## 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") +```