diff --git a/examples/breakout_encoder/CMakeLists.txt b/examples/breakout_encoder/CMakeLists.txt index 46729be4..5cb31bfc 100644 --- a/examples/breakout_encoder/CMakeLists.txt +++ b/examples/breakout_encoder/CMakeLists.txt @@ -5,6 +5,10 @@ add_executable( demo.cpp ) +# enable usb output, disable uart output +pico_enable_stdio_usb(${OUTPUT_NAME} 1) +pico_enable_stdio_uart(${OUTPUT_NAME} 0) + # Pull in pico libraries that we need target_link_libraries(${OUTPUT_NAME} pico_stdlib breakout_encoder) diff --git a/examples/breakout_encoder/demo.cpp b/examples/breakout_encoder/demo.cpp index b8e0665d..ae57882f 100644 --- a/examples/breakout_encoder/demo.cpp +++ b/examples/breakout_encoder/demo.cpp @@ -1,22 +1,79 @@ #include "pico/stdlib.h" +#include +#include #include "breakout_encoder.hpp" using namespace pimoroni; +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); +} int main() { gpio_init(PICO_DEFAULT_LED_PIN); gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT); - enc.init(); + stdio_init_all(); - while(true) { + int16_t count = 0; + if(enc.init()) { + printf("Encoder found...\n"); + + //enc.set_direction(BreakoutEncoder::DIRECTION_CCW); // Uncomment this to flip the direction + + 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); + } + + sleep_ms(20); + } + } + else { + printf("Encoder not found :'(\n"); gpio_put(PICO_DEFAULT_LED_PIN, true); - sleep_ms(1000); - gpio_put(PICO_DEFAULT_LED_PIN, false); - sleep_ms(1000); } return 0; diff --git a/examples/breakout_potentiometer/CMakeLists.txt b/examples/breakout_potentiometer/CMakeLists.txt index edbf0709..e7067e04 100644 --- a/examples/breakout_potentiometer/CMakeLists.txt +++ b/examples/breakout_potentiometer/CMakeLists.txt @@ -5,6 +5,10 @@ add_executable( demo.cpp ) +# enable usb output, disable uart output +pico_enable_stdio_usb(${OUTPUT_NAME} 1) +pico_enable_stdio_uart(${OUTPUT_NAME} 0) + # Pull in pico libraries that we need target_link_libraries(${OUTPUT_NAME} pico_stdlib breakout_potentiometer) diff --git a/examples/breakout_potentiometer/demo.cpp b/examples/breakout_potentiometer/demo.cpp index a921bcb1..a7cf1c6e 100644 --- a/examples/breakout_potentiometer/demo.cpp +++ b/examples/breakout_potentiometer/demo.cpp @@ -1,22 +1,63 @@ #include "pico/stdlib.h" +#include +#include #include "breakout_potentiometer.hpp" using namespace pimoroni; 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); - pot.init(); + stdio_init_all(); - while(true) { + int16_t count = 0; + if(pot.init()) { + printf("Potentiometer found...\n"); + + //pot.set_direction(BreakoutPotentiometer::DIRECTION_CCW); // Uncomment this to flip the direction + + 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); + + sleep_ms(20); + } + } + else { + printf("Encoder Potentiometer found :'(\n"); gpio_put(PICO_DEFAULT_LED_PIN, true); - sleep_ms(1000); - gpio_put(PICO_DEFAULT_LED_PIN, false); - sleep_ms(1000); } return 0;