|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <memory> |
| 4 | +#include <string> |
| 5 | +#include <vector> |
| 6 | + |
| 7 | +#include "base_component.hpp" |
| 8 | +#include "i2c.hpp" |
| 9 | +#include "interrupt.hpp" |
| 10 | +#include "neopixel.hpp" |
| 11 | + |
| 12 | +namespace espp { |
| 13 | +/// The QtPy class provides an interface to the Adafruit QtPy ESP32 and QtPy |
| 14 | +/// ESP32-S3 development boards. |
| 15 | +/// |
| 16 | +/// The class provides access to the following features: |
| 17 | +/// - RGB LED |
| 18 | +/// - I2C (qwiic) |
| 19 | +/// |
| 20 | +/// The class is a singleton and can be accessed using the get() method. |
| 21 | +/// |
| 22 | +/// \section qtpy_example Example |
| 23 | +/// \snippet qtpy_example.cpp qtpy example |
| 24 | +class QtPy : public BaseComponent { |
| 25 | +public: |
| 26 | + using button_callback_t = espp::Interrupt::event_callback_fn; |
| 27 | + |
| 28 | + /// @brief Access the singleton instance of the QtPy class |
| 29 | + /// @return Reference to the singleton instance of the QtPy class |
| 30 | + static QtPy &get() { |
| 31 | + static QtPy instance; |
| 32 | + return instance; |
| 33 | + } |
| 34 | + |
| 35 | + QtPy(const QtPy &) = delete; |
| 36 | + QtPy &operator=(const QtPy &) = delete; |
| 37 | + QtPy(QtPy &&) = delete; |
| 38 | + QtPy &operator=(QtPy &&) = delete; |
| 39 | + |
| 40 | + /// Get a reference to the interrupts |
| 41 | + /// \return A reference to the interrupts |
| 42 | + espp::Interrupt &interrupts(); |
| 43 | + |
| 44 | + ///////////////////////////////////////////////////////////////////////////// |
| 45 | + // Button |
| 46 | + ///////////////////////////////////////////////////////////////////////////// |
| 47 | + |
| 48 | + /// Initialize the button |
| 49 | + /// \param callback The callback function to call when the button is pressed |
| 50 | + /// \return true if the button was successfully initialized, false otherwise |
| 51 | + bool initialize_button(const button_callback_t &callback = nullptr); |
| 52 | + |
| 53 | + /// Get the button state |
| 54 | + /// \return The button state (true = button pressed, false = button released) |
| 55 | + bool button_state() const; |
| 56 | + |
| 57 | + ///////////////////////////////////////////////////////////////////////////// |
| 58 | + // I2C |
| 59 | + ///////////////////////////////////////////////////////////////////////////// |
| 60 | + |
| 61 | + /// Initialize the qwiic I2C bus |
| 62 | + /// \param i2c_config The I2C configuration to use |
| 63 | + /// \return true if the qwiic I2C bus was successfully initialized, false |
| 64 | + /// otherwise |
| 65 | + /// \note The SDA/SCL pins in the config will be ignored - overwritten with |
| 66 | + /// the default values for the QtPy board. |
| 67 | + bool initialize_qwiic_i2c(const espp::I2c::Config &i2c_config = {}); |
| 68 | + |
| 69 | + /// Get a shared pointer to the qwiic I2C bus |
| 70 | + /// \return A shared pointer to the qwiic I2C bus |
| 71 | + /// \note The qwiic I2C bus is only available if it was successfully |
| 72 | + /// initialized, otherwise the shared pointer will be nullptr. |
| 73 | + std::shared_ptr<I2c> qwiic_i2c(); |
| 74 | + |
| 75 | + ///////////////////////////////////////////////////////////////////////////// |
| 76 | + // RGB LED |
| 77 | + ///////////////////////////////////////////////////////////////////////////// |
| 78 | + |
| 79 | + /// Initialize the RGB LED |
| 80 | + /// \return true if the RGB LED was successfully initialized, false otherwise |
| 81 | + bool initialize_led(); |
| 82 | + |
| 83 | + /// Get the number of LEDs in the strip |
| 84 | + /// \return The number of LEDs in the strip |
| 85 | + static constexpr size_t num_leds() { return num_leds_; } |
| 86 | + |
| 87 | + /// Get a shared pointer to the RGB LED |
| 88 | + /// \return A shared pointer to the RGB LED |
| 89 | + std::shared_ptr<Neopixel> led() const { return led_; } |
| 90 | + |
| 91 | + /// Set the color of the LED |
| 92 | + /// \param hsv The color of the LED in HSV format |
| 93 | + /// \return true if the color was successfully set, false otherwise |
| 94 | + bool led(const Hsv &hsv); |
| 95 | + |
| 96 | + /// Set the color of the LED |
| 97 | + /// \param rgb The color of the LED in RGB format |
| 98 | + /// \return true if the color was successfully set, false otherwise |
| 99 | + bool led(const Rgb &rgb); |
| 100 | + |
| 101 | +protected: |
| 102 | + QtPy(); |
| 103 | + |
| 104 | +#if CONFIG_IDF_TARGET_ESP32 |
| 105 | + static constexpr bool IS_ESP32 = true; |
| 106 | + static constexpr bool IS_ESP32_S3 = false; |
| 107 | + |
| 108 | + // LED |
| 109 | + static constexpr auto led_data_io = GPIO_NUM_5; |
| 110 | + static constexpr auto led_power_io = GPIO_NUM_8; |
| 111 | + |
| 112 | + // I2C |
| 113 | + static constexpr auto qwiic_sda_io = GPIO_NUM_22; |
| 114 | + static constexpr auto qwiic_scl_io = GPIO_NUM_19; |
| 115 | +#elif CONFIG_IDF_TARGET_ESP32S3 |
| 116 | + static constexpr bool IS_ESP32 = false; |
| 117 | + static constexpr bool IS_ESP32_S3 = true; |
| 118 | + |
| 119 | + // LED |
| 120 | + static constexpr auto led_data_io = GPIO_NUM_39; |
| 121 | + static constexpr auto led_power_io = GPIO_NUM_38; |
| 122 | + |
| 123 | + // I2C |
| 124 | + static constexpr auto qwiic_sda_io = GPIO_NUM_41; |
| 125 | + static constexpr auto qwiic_scl_io = GPIO_NUM_40; |
| 126 | +#else |
| 127 | + #error "Unsupported target" |
| 128 | +#endif |
| 129 | + |
| 130 | + // button (boot button) |
| 131 | + static constexpr gpio_num_t button_io = GPIO_NUM_0; // active low |
| 132 | + |
| 133 | + // common: |
| 134 | + // WS2812 LED |
| 135 | + static constexpr auto led_clock_speed = 10 * 1000 * 1000; // 10 MHz |
| 136 | + static constexpr auto num_leds_ = 1; |
| 137 | + |
| 138 | + // Interrupts |
| 139 | + espp::Interrupt::PinConfig button_interrupt_pin_{ |
| 140 | + .gpio_num = button_io, |
| 141 | + .callback = |
| 142 | + [this](const auto &event) { |
| 143 | + if (button_callback_) { |
| 144 | + button_callback_(event); |
| 145 | + } |
| 146 | + }, |
| 147 | + .active_level = espp::Interrupt::ActiveLevel::LOW, |
| 148 | + .interrupt_type = espp::Interrupt::Type::ANY_EDGE, |
| 149 | + .pullup_enabled = true}; |
| 150 | + |
| 151 | + // we'll only add each interrupt pin if the initialize method is called |
| 152 | + espp::Interrupt interrupts_{ |
| 153 | + {.interrupts = {}, |
| 154 | + .task_config = {.name = "qtpy interrupts", |
| 155 | + .stack_size_bytes = CONFIG_QTPY_INTERRUPT_STACK_SIZE}}}; |
| 156 | + |
| 157 | + // button |
| 158 | + std::atomic<bool> button_initialized_{false}; |
| 159 | + button_callback_t button_callback_{nullptr}; |
| 160 | + |
| 161 | + // led |
| 162 | + std::shared_ptr<Neopixel> led_; |
| 163 | + |
| 164 | + // I2C |
| 165 | + std::shared_ptr<I2c> qwiic_i2c_; |
| 166 | +}; // class QtPy |
| 167 | +} // namespace espp |
0 commit comments