diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 2b11e0ec..26b90865 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -12,6 +12,11 @@ add_subdirectory(breakout_sgp30) add_subdirectory(breakout_colourlcd240x240) add_subdirectory(breakout_msa301) +add_subdirectory(ioe_adc_bg) +add_subdirectory(ioe_adc_min_case) +add_subdirectory(ioe_bg) +add_subdirectory(ioe_enc_min_case) + add_subdirectory(pico_display) add_subdirectory(pico_unicorn) add_subdirectory(pico_unicorn_plasma) diff --git a/examples/ioe_adc_bg/CMakeLists.txt b/examples/ioe_adc_bg/CMakeLists.txt new file mode 100644 index 00000000..3ab39235 --- /dev/null +++ b/examples/ioe_adc_bg/CMakeLists.txt @@ -0,0 +1,14 @@ +add_executable( + ioe_adc_bg + ioe_adc_bg.cpp +) + +# Pull in pico libraries that we need +target_link_libraries(ioe_adc_bg pico_stdlib pico_explorer ioexpander) + +# disable usb output, enable uart output +pico_enable_stdio_usb(ioe_adc_bg 0) +pico_enable_stdio_uart(ioe_adc_bg 1) + +# create map/bin/hex file etc. +pico_add_extra_outputs(ioe_adc_bg) diff --git a/examples/ioe_adc_bg/ioe_adc_bg.cpp b/examples/ioe_adc_bg/ioe_adc_bg.cpp new file mode 100644 index 00000000..a2b06091 --- /dev/null +++ b/examples/ioe_adc_bg/ioe_adc_bg.cpp @@ -0,0 +1,177 @@ +#include +#include +#include +#include +#include +#include +#include "pico/stdlib.h" + +#include "pico_explorer.hpp" +#include "ioexpander.hpp" + +static const uint8_t PIN_RED = 1; +static const uint8_t PIN_GREEN = 7; +static const uint8_t PIN_BLUE = 2; + +static const uint8_t PIN_TERM_A = 12; +static const uint8_t PIN_TERM_B = 3; +static const uint8_t PIN_INPUT = 11; + +static const bool INVERT_OUTPUT = true; //true for common cathode, false for common anode + +using namespace pimoroni; + +uint16_t buffer[PicoExplorer::WIDTH * PicoExplorer::HEIGHT]; +PicoExplorer pico_explorer(buffer); + +IOExpander ioe(0x0e); +bool toggle = false; + +void HSVtoRGB(float hue, float sat, float val, uint8_t& rOut, uint8_t& gOut, uint8_t& bOut) +{ + //////////////////////////////// + // Convert HSV values to RGB + // H(Hue): 0-360 degrees + // S(Saturation): 0-100 percent + // V(Value): 0-100 percent + // + // RGB out is in range 0->255. + // This method was found at: + // https://www.codespeedy.com/hsv-to-rgb-in-cpp/ + //////////////////////////////// + if(hue > 360.0f || hue < 0.0f || sat > 100.0f || sat < 0.0f || val > 100.0f || val < 0.0f) { + //The given HSV values are not in valid range + rOut = 0; + gOut = 0; + bOut = 0; + return; + } + + float s = sat / 100.0f; + float v = val / 100.0f; + float c = s * v; + float x = c * (1.0f - fabsf(fmodf(hue / 60.0f, 2.0f) - 1.0f)); + float m = v - c; + + float r, g, b; + if(hue >= 0.0f && hue < 60.0f) + r = c, g = x, b = 0.0f; + else if(hue >= 60.0f && hue < 120.0f) + r = x, g = c, b = 0.0f; + else if(hue >= 120.0f && hue < 180.0f) + r = 0.0f, g = c, b = x; + else if(hue >= 180.0f && hue < 240.0f) + r = 0.0f, g = x, b = c; + else if(hue >= 240.0f && hue < 300.0f) + r = x, g = 0.0f, b = c; + else + r = c, g = 0.0f, b = x; + + rOut = (uint8_t)((r + m) * 255.0f); + gOut = (uint8_t)((g + m) * 255.0f); + bOut = (uint8_t)((b + m) * 255.0f); +} + +int main() { + pico_explorer.init(); + pico_explorer.update(); + stdio_init_all(); + + sleep_ms(5000); + + const uint LED_PIN = 25; + gpio_init(LED_PIN); + gpio_set_dir(LED_PIN, GPIO_OUT); + + const uint DBG_PIN = 26; + gpio_init(DBG_PIN); + gpio_set_dir(DBG_PIN, GPIO_OUT); + + float brightness = 1.0f; + + int16_t count = 0; + if(ioe.init()) { + printf("IOExpander found...\n"); + + ioe.set_mode(PIN_TERM_A, IOExpander::PIN_OUT); + ioe.set_mode(PIN_TERM_B, IOExpander::PIN_OUT); + ioe.set_mode(PIN_INPUT, IOExpander::PIN_ADC); + + ioe.output(PIN_TERM_A, 0); + ioe.output(PIN_TERM_B, 1); + + //Calculate a period large enough to get 0-255 steps at the desired brightness + unsigned int period = (unsigned int)(255.0f / brightness); + + ioe.set_pwm_period(period); + ioe.set_pwm_control(2); //PWM as fast as we can to avoid LED flicker + + ioe.set_mode(PIN_RED, IOExpander::PIN_PWM, false, INVERT_OUTPUT); + ioe.set_mode(PIN_GREEN, IOExpander::PIN_PWM, false, INVERT_OUTPUT); + ioe.set_mode(PIN_BLUE, IOExpander::PIN_PWM, false, INVERT_OUTPUT); + + sleep_ms(1000); + + while (true) { + gpio_put(LED_PIN, toggle); + toggle = !toggle; + + float analog = ioe.input_as_voltage(PIN_INPUT) / ioe.get_adc_vref(); + + float h = (analog * 240.0f); + uint8_t r, g, b; + HSVtoRGB(h, 100.0f, 100.0f, r, g, b); + + ioe.output(PIN_RED, r, false); //Hold off pwm load until the last + ioe.output(PIN_GREEN, g, false); //Hold off pwm load until the last + ioe.output(PIN_BLUE, b); //Loads all 3 pwms + + + + 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.update(); + + sleep_ms(10); + } + } + else { + printf("IOExpander not found :'(\n"); + + while(true) { + gpio_put(LED_PIN, 1); + sleep_ms(250); + gpio_put(LED_PIN, 0); + sleep_ms(250); + } + } + + return 0; +} diff --git a/examples/ioe_adc_min_case/CMakeLists.txt b/examples/ioe_adc_min_case/CMakeLists.txt new file mode 100644 index 00000000..018c1307 --- /dev/null +++ b/examples/ioe_adc_min_case/CMakeLists.txt @@ -0,0 +1,14 @@ +add_executable( + ioe_adc_min_case + ioe_adc_min_case.cpp +) + +# Pull in pico libraries that we need +target_link_libraries(ioe_adc_min_case pico_stdlib ioexpander) + +# disable usb output, enable uart output +pico_enable_stdio_usb(ioe_adc_min_case 1) +pico_enable_stdio_uart(ioe_adc_min_case 0) + +# create map/bin/hex file etc. +pico_add_extra_outputs(ioe_adc_min_case) diff --git a/examples/ioe_adc_min_case/ioe_adc_min_case.cpp b/examples/ioe_adc_min_case/ioe_adc_min_case.cpp new file mode 100644 index 00000000..0c0b27fc --- /dev/null +++ b/examples/ioe_adc_min_case/ioe_adc_min_case.cpp @@ -0,0 +1,48 @@ +#include +#include "pico/stdlib.h" +#include "ioexpander.hpp" + +static const uint8_t PIN_TERM_A = 12; +static const uint8_t PIN_TERM_B = 3; +static const uint8_t PIN_INPUT = 11; + +using namespace pimoroni; + +IOExpander ioe(0x0e); + +int main() { + stdio_init_all(); + + sleep_ms(5000); //Delay to give chance to connect terminal + + const uint LED_PIN = 25; + gpio_init(LED_PIN); + gpio_set_dir(LED_PIN, GPIO_OUT); + + if(ioe.init()) { + printf("IOExpander found...\n"); + + ioe.set_mode(PIN_TERM_A, IOExpander::PIN_OUT); + ioe.set_mode(PIN_TERM_B, IOExpander::PIN_OUT); + ioe.set_mode(PIN_INPUT, IOExpander::PIN_ADC); + + ioe.output(PIN_TERM_A, 0); + ioe.output(PIN_TERM_B, 1); + + bool toggle = false; + while(true) { + gpio_put(LED_PIN, toggle); + toggle = !toggle; + + printf("%d\n", ioe.input(PIN_INPUT)); + + sleep_ms(20); + } + } + else { + printf("IOExpander not found :'(\n"); + gpio_put(LED_PIN, 1); + } + + return 0; +} diff --git a/examples/ioe_bg/CMakeLists.txt b/examples/ioe_bg/CMakeLists.txt new file mode 100644 index 00000000..1dc5901d --- /dev/null +++ b/examples/ioe_bg/CMakeLists.txt @@ -0,0 +1,14 @@ +add_executable( + ioe_bg + ioe_bg.cpp +) + +# Pull in pico libraries that we need +target_link_libraries(ioe_bg pico_stdlib pico_explorer ioexpander) + +# disable usb output, enable uart output +pico_enable_stdio_usb(ioe_bg 0) +pico_enable_stdio_uart(ioe_bg 1) + +# create map/bin/hex file etc. +pico_add_extra_outputs(ioe_bg) diff --git a/examples/ioe_bg/ioe_bg.cpp b/examples/ioe_bg/ioe_bg.cpp new file mode 100644 index 00000000..ba983ccf --- /dev/null +++ b/examples/ioe_bg/ioe_bg.cpp @@ -0,0 +1,176 @@ +#include +#include +#include +#include +#include +#include +#include "pico/stdlib.h" + +#include "pico_explorer.hpp" +#include "ioexpander.hpp" + +static const uint8_t PIN_RED = 1; +static const uint8_t PIN_GREEN = 7; +static const uint8_t PIN_BLUE = 2; + +static const uint8_t PIN_ENC_A = 12; +static const uint8_t PIN_ENC_B = 3; +static const uint8_t PIN_ENC_C = 11; + +static const uint8_t ENC_CHANNEL = 1; + +static const bool INVERT_OUTPUT = true; //true for common cathode, false for common anode + +using namespace pimoroni; + +uint16_t buffer[PicoExplorer::WIDTH * PicoExplorer::HEIGHT]; +PicoExplorer pico_explorer(buffer); + +IOExpander ioe(0x0f); +bool toggle = false; + +void HSVtoRGB(float hue, float sat, float val, uint8_t& rOut, uint8_t& gOut, uint8_t& bOut) +{ + //////////////////////////////// + // Convert HSV values to RGB + // H(Hue): 0-360 degrees + // S(Saturation): 0-100 percent + // V(Value): 0-100 percent + // + // RGB out is in range 0->255. + // This method was found at: + // https://www.codespeedy.com/hsv-to-rgb-in-cpp/ + //////////////////////////////// + if(hue > 360.0f || hue < 0.0f || sat > 100.0f || sat < 0.0f || val > 100.0f || val < 0.0f) { + //The given HSV values are not in valid range + rOut = 0; + gOut = 0; + bOut = 0; + return; + } + + float s = sat / 100.0f; + float v = val / 100.0f; + float c = s * v; + float x = c * (1.0f - fabsf(fmodf(hue / 60.0f, 2.0f) - 1.0f)); + float m = v - c; + + float r, g, b; + if(hue >= 0.0f && hue < 60.0f) + r = c, g = x, b = 0.0f; + else if(hue >= 60.0f && hue < 120.0f) + r = x, g = c, b = 0.0f; + else if(hue >= 120.0f && hue < 180.0f) + r = 0.0f, g = c, b = x; + else if(hue >= 180.0f && hue < 240.0f) + r = 0.0f, g = x, b = c; + else if(hue >= 240.0f && hue < 300.0f) + r = x, g = 0.0f, b = c; + else + r = c, g = 0.0f, b = x; + + rOut = (uint8_t)((r + m) * 255.0f); + gOut = (uint8_t)((g + m) * 255.0f); + bOut = (uint8_t)((b + m) * 255.0f); +} + +int main() { + pico_explorer.init(); + pico_explorer.update(); + stdio_init_all(); + + sleep_ms(5000); + + const uint LED_PIN = 25; + gpio_init(LED_PIN); + gpio_set_dir(LED_PIN, GPIO_OUT); + + const uint DBG_PIN = 26; + gpio_init(DBG_PIN); + gpio_set_dir(DBG_PIN, GPIO_OUT); + + float brightness = 1.0f; + + int16_t count = 0; + if(ioe.init()) { + printf("IOExpander found...\n"); + + ioe.setup_rotary_encoder(ENC_CHANNEL, PIN_ENC_A, PIN_ENC_B, PIN_ENC_C); + + //Calculate a period large enough to get 0-255 steps at the desired brightness + unsigned int period = (unsigned int)(255.0f / brightness); + + ioe.set_pwm_period(period); + ioe.set_pwm_control(2); //PWM as fast as we can to avoid LED flicker + + ioe.set_mode(PIN_RED, IOExpander::PIN_PWM, false, INVERT_OUTPUT); + ioe.set_mode(PIN_GREEN, IOExpander::PIN_PWM, false, INVERT_OUTPUT); + ioe.set_mode(PIN_BLUE, IOExpander::PIN_PWM, false, INVERT_OUTPUT); + + sleep_ms(1000); + + while(true) { + gpio_put(LED_PIN, toggle); + toggle = !toggle; + + if(ioe.get_interrupt_flag() > 0) { //This should work but + gpio_put(DBG_PIN, 1); + count = ioe.read_rotary_encoder(ENC_CHANNEL); + ioe.clear_interrupt_flag(); + gpio_put(DBG_PIN, 0); + } + + while(count < 0) + count += 24; + + float h = ((float)(count % 24) * 360.0f) / 24.0f; + uint8_t r, g, b; + HSVtoRGB(h, 100.0f, 100.0f, r, g, b); + + ioe.output(PIN_RED, r, false); //Hold off pwm load until the last + ioe.output(PIN_GREEN, g, false); //Hold off pwm load until the last + ioe.output(PIN_BLUE, b); //Loads all 3 pwms + + + + 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.update(); + + sleep_ms(10); + } + } + else { + printf("IOExpander not found :'(\n"); + gpio_put(LED_PIN, 1); + } + + return 0; +} diff --git a/examples/ioe_enc_min_case/CMakeLists.txt b/examples/ioe_enc_min_case/CMakeLists.txt new file mode 100644 index 00000000..9cc97ae3 --- /dev/null +++ b/examples/ioe_enc_min_case/CMakeLists.txt @@ -0,0 +1,14 @@ +add_executable( + ioe_enc_min_case + ioe_enc_min_case.cpp +) + +# Pull in pico libraries that we need +target_link_libraries(ioe_enc_min_case pico_stdlib ioexpander) + +# disable usb output, enable uart output +pico_enable_stdio_usb(ioe_enc_min_case 1) +pico_enable_stdio_uart(ioe_enc_min_case 0) + +# create map/bin/hex file etc. +pico_add_extra_outputs(ioe_enc_min_case) diff --git a/examples/ioe_enc_min_case/ioe_enc_min_case.cpp b/examples/ioe_enc_min_case/ioe_enc_min_case.cpp new file mode 100644 index 00000000..1b1c2eab --- /dev/null +++ b/examples/ioe_enc_min_case/ioe_enc_min_case.cpp @@ -0,0 +1,57 @@ +#include +#include "pico/stdlib.h" +#include "ioexpander.hpp" + +static const uint8_t PIN_ENC_A = 12; +static const uint8_t PIN_ENC_B = 3; +static const uint8_t PIN_ENC_C = 11; + +static const uint8_t ENC_CHANNEL = 1; + +using namespace pimoroni; + +IOExpander ioe(0x0f); + +int main() { + stdio_init_all(); + + sleep_ms(5000); //Delay to give chance to connect terminal + + const uint LED_PIN = 25; + gpio_init(LED_PIN); + gpio_set_dir(LED_PIN, GPIO_OUT); + + if(ioe.init()) { + printf("IOExpander found...\n"); + + ioe.setup_rotary_encoder(ENC_CHANNEL, PIN_ENC_A, PIN_ENC_B, PIN_ENC_C); + + bool toggle = false; + int16_t count = 0; + while(true) { + gpio_put(LED_PIN, toggle); + toggle = !toggle; + + //Polling encoder directly works + //printf("%d\n", ioe.read_rotary_encoder(ENC_CHANNEL)); + + //Only reading encoder when the interrupt pin changes does not. + if(ioe.get_interrupt_flag()) { + printf("new count: "); + count = ioe.read_rotary_encoder(ENC_CHANNEL); + ioe.clear_interrupt_flag(); + } + + printf("%d\n", count); + + + sleep_ms(20); + } + } + else { + printf("IOExpander not found :'(\n"); + gpio_put(LED_PIN, 1); + } + + return 0; +}