From 2f3c6fc8787d3dac9b91143f16b467d2d7aafcb2 Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Thu, 13 May 2021 14:35:11 +0100 Subject: [PATCH] Added C++ examples for IOExpander --- examples/CMakeLists.txt | 1 + examples/breakout_ioexpander/CMakeLists.txt | 15 +---- .../breakout_ioexpander/adc/CMakeLists.txt | 16 +++++ examples/breakout_ioexpander/adc/adc.cpp | 42 +++++++++++++ .../breakout_ioexpander/button/CMakeLists.txt | 16 +++++ .../breakout_ioexpander/button/button.cpp | 49 +++++++++++++++ examples/breakout_ioexpander/demo.cpp | 23 ------- .../breakout_ioexpander/servo/CMakeLists.txt | 16 +++++ examples/breakout_ioexpander/servo/servo.cpp | 60 +++++++++++++++++++ 9 files changed, 203 insertions(+), 35 deletions(-) create mode 100644 examples/breakout_ioexpander/adc/CMakeLists.txt create mode 100644 examples/breakout_ioexpander/adc/adc.cpp create mode 100644 examples/breakout_ioexpander/button/CMakeLists.txt create mode 100644 examples/breakout_ioexpander/button/button.cpp delete mode 100644 examples/breakout_ioexpander/demo.cpp create mode 100644 examples/breakout_ioexpander/servo/CMakeLists.txt create mode 100644 examples/breakout_ioexpander/servo/servo.cpp diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 9bdc4786..03d9b7c5 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,5 +1,6 @@ add_subdirectory(breakout_dotmatrix) add_subdirectory(breakout_encoder) +add_subdirectory(breakout_ioexpander) add_subdirectory(breakout_ltr559) add_subdirectory(breakout_colourlcd160x80) add_subdirectory(breakout_roundlcd) diff --git a/examples/breakout_ioexpander/CMakeLists.txt b/examples/breakout_ioexpander/CMakeLists.txt index eeda7d77..a6c814cd 100644 --- a/examples/breakout_ioexpander/CMakeLists.txt +++ b/examples/breakout_ioexpander/CMakeLists.txt @@ -1,12 +1,3 @@ -set(OUTPUT_NAME ioexpander_demo) - -add_executable( - ${OUTPUT_NAME} - demo.cpp -) - -# Pull in pico libraries that we need -target_link_libraries(${OUTPUT_NAME} pico_stdlib breakout_ioexpander) - -# create map/bin/hex file etc. -pico_add_extra_outputs(${OUTPUT_NAME}) +add_subdirectory(adc) +add_subdirectory(button) +add_subdirectory(servo) \ No newline at end of file diff --git a/examples/breakout_ioexpander/adc/CMakeLists.txt b/examples/breakout_ioexpander/adc/CMakeLists.txt new file mode 100644 index 00000000..ecba6fa9 --- /dev/null +++ b/examples/breakout_ioexpander/adc/CMakeLists.txt @@ -0,0 +1,16 @@ +set(OUTPUT_NAME ioexpander_adc) + +add_executable( + ${OUTPUT_NAME} + adc.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_ioexpander) + +# create map/bin/hex file etc. +pico_add_extra_outputs(${OUTPUT_NAME}) diff --git a/examples/breakout_ioexpander/adc/adc.cpp b/examples/breakout_ioexpander/adc/adc.cpp new file mode 100644 index 00000000..0e8de8ef --- /dev/null +++ b/examples/breakout_ioexpander/adc/adc.cpp @@ -0,0 +1,42 @@ +#include "pico/stdlib.h" +#include + +#include "breakout_ioexpander.hpp" + +using namespace pimoroni; + +static const uint8_t IOE_ADC_PIN = 10; + +BreakoutIOExpander ioe(0x18); +bool toggle = false; + +int main() { + gpio_init(PICO_DEFAULT_LED_PIN); + gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT); + + stdio_init_all(); + + if(ioe.init()) { + printf("IOExpander found...\n"); + + // ioe.set_adc_vref(5.0f); //Uncomment this if running the IOExpander off a 5V supply + ioe.set_mode(IOE_ADC_PIN, IOExpander::PIN_ADC); + + while(true) { + gpio_put(PICO_DEFAULT_LED_PIN, toggle); + toggle = !toggle; + + float voltage = ioe.input_as_voltage(IOE_ADC_PIN); + + printf("Voltage: %f\n", voltage); + + sleep_ms(20); + } + } + else { + printf("IOExpander not found :'(\n"); + gpio_put(PICO_DEFAULT_LED_PIN, true); + } + + return 0; +} diff --git a/examples/breakout_ioexpander/button/CMakeLists.txt b/examples/breakout_ioexpander/button/CMakeLists.txt new file mode 100644 index 00000000..c20a8f08 --- /dev/null +++ b/examples/breakout_ioexpander/button/CMakeLists.txt @@ -0,0 +1,16 @@ +set(OUTPUT_NAME ioexpander_button) + +add_executable( + ${OUTPUT_NAME} + button.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_ioexpander) + +# create map/bin/hex file etc. +pico_add_extra_outputs(${OUTPUT_NAME}) diff --git a/examples/breakout_ioexpander/button/button.cpp b/examples/breakout_ioexpander/button/button.cpp new file mode 100644 index 00000000..03035c71 --- /dev/null +++ b/examples/breakout_ioexpander/button/button.cpp @@ -0,0 +1,49 @@ +#include "pico/stdlib.h" +#include + +#include "breakout_ioexpander.hpp" + +using namespace pimoroni; + +//Connect a button between this pin and ground +static const uint8_t IOE_BUTTON_PIN = 14; + +BreakoutIOExpander ioe(0x18); +bool last_state = true; + +int main() { + gpio_init(PICO_DEFAULT_LED_PIN); + gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT); + + stdio_init_all(); + + if(ioe.init()) { + printf("IOExpander found...\n"); + + ioe.set_mode(IOE_BUTTON_PIN, IOExpander::PIN_IN_PULL_UP); + + while(true) { + bool state = ioe.input(IOE_BUTTON_PIN); + if(state != last_state) { + if(state) { + printf("Button has been released\n"); + gpio_put(PICO_DEFAULT_LED_PIN, false); + } + else { + printf("Button has been pressed\n"); + gpio_put(PICO_DEFAULT_LED_PIN, true); + } + + last_state = state; + } + + sleep_ms(20); + } + } + else { + printf("IOExpander not found :'(\n"); + gpio_put(PICO_DEFAULT_LED_PIN, true); + } + + return 0; +} diff --git a/examples/breakout_ioexpander/demo.cpp b/examples/breakout_ioexpander/demo.cpp deleted file mode 100644 index d6477bf0..00000000 --- a/examples/breakout_ioexpander/demo.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include "pico/stdlib.h" - -#include "breakout_ioexpander.hpp" - -using namespace pimoroni; - -BreakoutIOExpander ioe; - -int main() { - gpio_init(PICO_DEFAULT_LED_PIN); - gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT); - - ioe.init(); - - while(true) { - 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_ioexpander/servo/CMakeLists.txt b/examples/breakout_ioexpander/servo/CMakeLists.txt new file mode 100644 index 00000000..bb2bb7b7 --- /dev/null +++ b/examples/breakout_ioexpander/servo/CMakeLists.txt @@ -0,0 +1,16 @@ +set(OUTPUT_NAME ioexpander_servo) + +add_executable( + ${OUTPUT_NAME} + servo.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_ioexpander) + +# create map/bin/hex file etc. +pico_add_extra_outputs(${OUTPUT_NAME}) diff --git a/examples/breakout_ioexpander/servo/servo.cpp b/examples/breakout_ioexpander/servo/servo.cpp new file mode 100644 index 00000000..860cb17f --- /dev/null +++ b/examples/breakout_ioexpander/servo/servo.cpp @@ -0,0 +1,60 @@ +#include "pico/stdlib.h" +#include +#include + +#include "breakout_ioexpander.hpp" + +using namespace pimoroni; + +static const uint8_t IOE_SERVO_PIN = 1; + +// Settings to produce a 50Hz output from the 24MHz clock. +// 24,000,000 Hz / 8 = 3,000,000 Hz +// 3,000,000 Hz / 60,000 Period = 50 Hz +static const uint8_t DIVIDER = 8; +static const uint16_t PERIOD = 60000; +static constexpr float CYCLE_TIME = 5.0f; +static constexpr float SERVO_RANGE = 1000.0f; // Between 1000 and 2000us (1-2ms) + +BreakoutIOExpander ioe(0x18); +bool toggle = false; + +int main() { + gpio_init(PICO_DEFAULT_LED_PIN); + gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT); + + stdio_init_all(); + + if(ioe.init()) { + printf("IOExpander found...\n"); + + ioe.set_pwm_period(PERIOD); + ioe.set_pwm_control(DIVIDER); + + ioe.set_mode(IOE_SERVO_PIN, IOExpander::PIN_PWM); + + while(true) { + gpio_put(PICO_DEFAULT_LED_PIN, toggle); + toggle = !toggle; + + absolute_time_t at = get_absolute_time(); + float t = to_us_since_boot(at) / 1000000.0f; + float s = sinf((t * M_PI * 2.0f) / CYCLE_TIME) / 2.0f; + float servo_us = 1500.0f + (s * SERVO_RANGE); + + float duty_per_microsecond = (float)PERIOD / (float)(20 * 1000); // Default is 3 LSB per microsecond + + uint16_t duty_cycle = (uint16_t)(roundf(servo_us * duty_per_microsecond)); + printf("Cycle Time: %.2f, Pulse: %.1fus, Duty Cycle: %d\n", fmodf(t, CYCLE_TIME), servo_us, duty_cycle); + ioe.output(IOE_SERVO_PIN, duty_cycle); + + sleep_ms(20); + } + } + else { + printf("IOExpander not found :'(\n"); + gpio_put(PICO_DEFAULT_LED_PIN, true); + } + + return 0; +}