From e9a96ceb8a0cb1391e525a0da6b270aa8811d09f Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Wed, 12 May 2021 23:34:43 +0100 Subject: [PATCH] PicoExplorer C demos for Enc and Pot --- examples/CMakeLists.txt | 2 + examples/pico_enc_explorer/CMakeLists.txt | 12 ++ examples/pico_enc_explorer/demo.cpp | 127 ++++++++++++++++++++++ examples/pico_pot_explorer/CMakeLists.txt | 12 ++ examples/pico_pot_explorer/demo.cpp | 111 +++++++++++++++++++ 5 files changed, 264 insertions(+) create mode 100644 examples/pico_enc_explorer/CMakeLists.txt create mode 100644 examples/pico_enc_explorer/demo.cpp create mode 100644 examples/pico_pot_explorer/CMakeLists.txt create mode 100644 examples/pico_pot_explorer/demo.cpp diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 26b90865..9bdc4786 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -21,7 +21,9 @@ add_subdirectory(pico_display) add_subdirectory(pico_unicorn) add_subdirectory(pico_unicorn_plasma) add_subdirectory(pico_scroll) +add_subdirectory(pico_enc_explorer) add_subdirectory(pico_explorer) +add_subdirectory(pico_pot_explorer) add_subdirectory(pico_rgb_keypad) add_subdirectory(pico_rtc_display) add_subdirectory(pico_tof_display) diff --git a/examples/pico_enc_explorer/CMakeLists.txt b/examples/pico_enc_explorer/CMakeLists.txt new file mode 100644 index 00000000..a082bef9 --- /dev/null +++ b/examples/pico_enc_explorer/CMakeLists.txt @@ -0,0 +1,12 @@ +set(OUTPUT_NAME encoder_explorer) + +add_executable( + ${OUTPUT_NAME} + demo.cpp +) + +# Pull in pico libraries that we need +target_link_libraries(${OUTPUT_NAME} pico_stdlib breakout_encoder pico_explorer) + +# create map/bin/hex file etc. +pico_add_extra_outputs(${OUTPUT_NAME}) diff --git a/examples/pico_enc_explorer/demo.cpp b/examples/pico_enc_explorer/demo.cpp new file mode 100644 index 00000000..75250a30 --- /dev/null +++ b/examples/pico_enc_explorer/demo.cpp @@ -0,0 +1,127 @@ +#include "pico/stdlib.h" +#include +#include +#include +#include + +#include "pico_explorer.hpp" +#include "breakout_encoder.hpp" + +using namespace pimoroni; + +uint16_t buffer[PicoExplorer::WIDTH * PicoExplorer::HEIGHT]; +PicoExplorer pico_explorer(buffer); + +static const uint8_t STEPS_PER_REV = 24; + +BreakoutEncoder enc; +bool toggle = false; + +// HSV Conversion expects float inputs in the range of 0.00-1.00 for each channel +// Outputs are rgb in the range 0-255 for each channel +void from_hsv(float h, float s, float v, uint8_t &r, uint8_t &g, uint8_t &b) { + float i = floor(h * 6.0f); + float f = h * 6.0f - i; + v *= 255.0f; + uint8_t p = v * (1.0f - s); + uint8_t q = v * (1.0f - f * s); + uint8_t t = v * (1.0f - (1.0f - f) * s); + + switch (int(i) % 6) { + case 0: r = v; g = t; b = p; break; + case 1: r = q; g = v; b = p; break; + case 2: r = p; g = v; b = t; break; + case 3: r = p; g = q; b = v; break; + case 4: r = t; g = p; b = v; break; + case 5: r = v; g = p; b = q; break; + } +} + +void count_changed(int16_t count) { + printf("Count: %d\n", count); + float h = (count % STEPS_PER_REV) / (float)STEPS_PER_REV; + uint8_t r, g, b; + from_hsv(h, 1.0f, 1.0f, r, g, b); + enc.set_led(r, g, b); + + pico_explorer.set_pen(0, 0, 0); + pico_explorer.clear(); + + { + pico_explorer.set_pen(255, 0, 0); + std::ostringstream ss; + ss << "R = "; + ss << (int)r; + std::string s(ss.str()); + pico_explorer.text(s, Point(10, 10), 220, 6); + } + + { + pico_explorer.set_pen(0, 255, 0); + std::ostringstream ss; + ss << "G = "; + ss << (int)g; + std::string s(ss.str()); + pico_explorer.text(s, Point(10, 70), 220, 6); + } + + { + pico_explorer.set_pen(0, 0, 255); + std::ostringstream ss; + ss << "B = "; + ss << (int)b; + std::string s(ss.str()); + pico_explorer.text(s, Point(10, 130), 220, 6); + } + + { + pico_explorer.set_pen(r, g, b); + std::ostringstream ss; + ss << "#"; + ss << std::uppercase << std::hex << std::setfill('0') << std::setw(2) << (int)r; + ss << std::uppercase << std::hex << std::setfill('0') << std::setw(2) << (int)g; + ss << std::uppercase << std::hex << std::setfill('0') << std::setw(2) << (int)b; + std::string s(ss.str()); + pico_explorer.text(s, Point(10, 190), 220, 5); + } + pico_explorer.update(); +} + +int main() { + gpio_init(PICO_DEFAULT_LED_PIN); + gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT); + + stdio_init_all(); + + pico_explorer.init(); + pico_explorer.update(); + + int16_t count = 0; + if(enc.init()) { + printf("Encoder found...\n"); + + count_changed(count); + enc.clear_interrupt_flag(); + + while(true) { + gpio_put(PICO_DEFAULT_LED_PIN, toggle); + toggle = !toggle; + + if(enc.get_interrupt_flag()) { + count = enc.read(); + enc.clear_interrupt_flag(); + + while(count < 0) + count += STEPS_PER_REV; + + count_changed(count); + } + } + } + else { + printf("Encoder not found :'(\n"); + gpio_put(PICO_DEFAULT_LED_PIN, true); + } + + return 0; +} diff --git a/examples/pico_pot_explorer/CMakeLists.txt b/examples/pico_pot_explorer/CMakeLists.txt new file mode 100644 index 00000000..dc5649d4 --- /dev/null +++ b/examples/pico_pot_explorer/CMakeLists.txt @@ -0,0 +1,12 @@ +set(OUTPUT_NAME potentiometer_explorer) + +add_executable( + ${OUTPUT_NAME} + demo.cpp +) + +# Pull in pico libraries that we need +target_link_libraries(${OUTPUT_NAME} pico_stdlib breakout_potentiometer pico_explorer) + +# create map/bin/hex file etc. +pico_add_extra_outputs(${OUTPUT_NAME}) diff --git a/examples/pico_pot_explorer/demo.cpp b/examples/pico_pot_explorer/demo.cpp new file mode 100644 index 00000000..14752a3c --- /dev/null +++ b/examples/pico_pot_explorer/demo.cpp @@ -0,0 +1,111 @@ +#include "pico/stdlib.h" +#include +#include +#include +#include + +#include "pico_explorer.hpp" +#include "breakout_potentiometer.hpp" + +using namespace pimoroni; + +uint16_t buffer[PicoExplorer::WIDTH * PicoExplorer::HEIGHT]; +PicoExplorer pico_explorer(buffer); + +BreakoutPotentiometer pot; +bool toggle = false; + +// HSV Conversion expects float inputs in the range of 0.00-1.00 for each channel +// Outputs are rgb in the range 0-255 for each channel +void from_hsv(float h, float s, float v, uint8_t &r, uint8_t &g, uint8_t &b) { + float i = floor(h * 6.0f); + float f = h * 6.0f - i; + v *= 255.0f; + uint8_t p = v * (1.0f - s); + uint8_t q = v * (1.0f - f * s); + uint8_t t = v * (1.0f - (1.0f - f) * s); + + switch (int(i) % 6) { + case 0: r = v; g = t; b = p; break; + case 1: r = q; g = v; b = p; break; + case 2: r = p; g = v; b = t; break; + case 3: r = p; g = q; b = v; break; + case 4: r = t; g = p; b = v; break; + case 5: r = v; g = p; b = q; break; + } +} + +int main() { + gpio_init(PICO_DEFAULT_LED_PIN); + gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT); + + stdio_init_all(); + + pico_explorer.init(); + pico_explorer.update(); + + int16_t count = 0; + if(pot.init()) { + printf("Potentiometer found...\n"); + + while(true) { + gpio_put(PICO_DEFAULT_LED_PIN, toggle); + toggle = !toggle; + + float percent = pot.read(); + + printf("Percent: %d\n", (int)(percent * 100)); + uint8_t r, g, b; + from_hsv(percent, 1.0f, 1.0f, r, g, b); + pot.set_led(r, g, b); + + pico_explorer.set_pen(0, 0, 0); + pico_explorer.clear(); + + { + pico_explorer.set_pen(255, 0, 0); + std::ostringstream ss; + ss << "R = "; + ss << (int)r; + std::string s(ss.str()); + pico_explorer.text(s, Point(10, 10), 220, 6); + } + + { + pico_explorer.set_pen(0, 255, 0); + std::ostringstream ss; + ss << "G = "; + ss << (int)g; + std::string s(ss.str()); + pico_explorer.text(s, Point(10, 70), 220, 6); + } + + { + pico_explorer.set_pen(0, 0, 255); + std::ostringstream ss; + ss << "B = "; + ss << (int)b; + std::string s(ss.str()); + pico_explorer.text(s, Point(10, 130), 220, 6); + } + + { + pico_explorer.set_pen(r, g, b); + std::ostringstream ss; + ss << "#"; + ss << std::uppercase << std::hex << std::setfill('0') << std::setw(2) << (int)r; + ss << std::uppercase << std::hex << std::setfill('0') << std::setw(2) << (int)g; + ss << std::uppercase << std::hex << std::setfill('0') << std::setw(2) << (int)b; + std::string s(ss.str()); + pico_explorer.text(s, Point(10, 190), 220, 5); + } + pico_explorer.update(); + } + } + else { + printf("Encoder Potentiometer found :'(\n"); + gpio_put(PICO_DEFAULT_LED_PIN, true); + } + + return 0; +}