QtPy ESP32 Pico and QtPy ESP32-S3

QtPy

The QtPy ESP32 Pico and QtPy ESP32-S3 are development boards for the ESP32 Pico and ESP32-S3, respectively. They feature a USB-C connector, a QWIIC I2C connector, 0.1” headers, an RGB LED, and a button.

The espp::QtPy component provides a singleton hardware abstraction for initializing the Button, I2C, and LED subsystems.

API Reference

Header File

Classes

class QtPy : public espp::BaseComponent

The QtPy class provides an interface to the Adafruit QtPy ESP32 and QtPy ESP32-S3 development boards.

The class provides access to the following features:

  • Button (boot button)

  • RGB LED

  • I2C (qwiic)

The class is a singleton and can be accessed using the get() method.

Example

    auto &qtpy = espp::QtPy::get();

    // Initialize the Button
    logger.info("Initializing the button");
    auto on_button_pressed = [&](const auto &event) {
      if (event.active) {
        logger.info("Button pressed!");
      } else {
        logger.info("Button released!");
      }
    };
    qtpy.initialize_button(on_button_pressed);

    // Initialize and test the LED
    logger.info("Initializing the LED");
    qtpy.initialize_led();

    logger.info("Turning LED off for 1 second");
    qtpy.led(espp::Rgb(0.0f, 0.0f, 0.0f));
    std::this_thread::sleep_for(1s);

    logger.info("Setting LED to red for 1 second");
    qtpy.led(espp::Rgb(1.0f, 0.0f, 0.0f));
    std::this_thread::sleep_for(1s);

    logger.info("Setting LED to green for 1 second");
    qtpy.led(espp::Rgb(0.0f, 1.0f, 0.0f));
    std::this_thread::sleep_for(1s);

    logger.info("Setting LED to blue for 1 second");
    qtpy.led(espp::Rgb(0.0f, 0.0f, 1.0f));
    std::this_thread::sleep_for(1s);

    // Use a task to rotate the LED through the rainbow using HSV
    auto task_fn = [&qtpy](std::mutex &m, std::condition_variable &cv) {
      static auto start = std::chrono::high_resolution_clock::now();
      auto now = std::chrono::high_resolution_clock::now();
      float t = std::chrono::duration<float>(now - start).count();
      // rotate through rainbow colors in hsv based on time, hue is 0-360
      float hue = (cos(t) * 0.5f + 0.5f) * 360.0f;
      espp::Hsv hsv(hue, 1.0f, 1.0f);
      // full brightness (1.0, default) is _really_ bright, so tone it down
      qtpy.led(hsv);
      // NOTE: sleeping in this way allows the sleep to exit early when the
      // task is being stopped / destroyed
      {
        std::unique_lock<std::mutex> lk(m);
        cv.wait_for(lk, 50ms);
      }
      // don't want to stop the task
      return false;
    };

    logger.info("Starting task to rotate LED through rainbow colors");
    auto task = espp::Task({.callback = task_fn,
                            .task_config =
                                {
                                    .name = "Neopixel Task",
                                    .stack_size_bytes = 5 * 1024,
                                },
                            .log_level = espp::Logger::Verbosity::WARN});
    task.start();

    // Now initialize and test the QWIIC I2C bus
    logger.info("Initializing the QWIIC I2C bus");
    qtpy.initialize_qwiic_i2c(); // NOTE: using default i2c config here (400khz)
    auto i2c = qtpy.qwiic_i2c();

    // probe the bus for all addresses and store the ones that were found /
    // responded.
    logger.info("Probing the I2C bus for devices, this may take a while...");
    std::vector<uint8_t> found_addresses;
    for (uint8_t address = 0; address < 128; address++) {
      if (i2c->probe_device(address)) {
        found_addresses.push_back(address);
      }
    }
    // print out the addresses that were found
    if (found_addresses.empty()) {
      logger.info("No devices found on the I2C bus");
    } else {
      logger.info("Found devices at addresses: {::#02x}", found_addresses);
    }

Public Types

using button_callback_t = espp::Interrupt::event_callback_fn

Alias for the button callback function.

Public Functions

espp::Interrupt &interrupts()

Get a reference to the interrupts

Returns

A reference to the interrupts

bool initialize_button(const button_callback_t &callback = nullptr)

Initialize the button

Parameters

callback – The callback function to call when the button is pressed

Returns

true if the button was successfully initialized, false otherwise

bool button_state() const

Get the button state

Returns

The button state (true = button pressed, false = button released)

bool initialize_qwiic_i2c(const espp::I2c::Config &i2c_config = {})

Initialize the qwiic I2C bus

Note

The SDA/SCL pins in the config will be ignored - overwritten with the default values for the QtPy board.

Parameters

i2c_config – The I2C configuration to use

Returns

true if the qwiic I2C bus was successfully initialized, false otherwise

std::shared_ptr<I2c> qwiic_i2c()

Get a shared pointer to the qwiic I2C bus

Note

The qwiic I2C bus is only available if it was successfully initialized, otherwise the shared pointer will be nullptr.

Returns

A shared pointer to the qwiic I2C bus

bool initialize_led()

Initialize the RGB LED

Returns

true if the RGB LED was successfully initialized, false otherwise

inline std::shared_ptr<Neopixel> led() const

Get a shared pointer to the RGB LED

Returns

A shared pointer to the RGB LED

bool led(const Hsv &hsv)

Set the color of the LED

Parameters

hsv – The color of the LED in HSV format

Returns

true if the color was successfully set, false otherwise

bool led(const Rgb &rgb)

Set the color of the LED

Parameters

rgb – The color of the LED in RGB format

Returns

true if the color was successfully set, false otherwise

inline const std::string &get_name() const

Get the name of the component

Note

This is the tag of the logger

Returns

A const reference to the name of the component

inline void set_log_tag(const std::string_view &tag)

Set the tag for the logger

Parameters

tag – The tag to use for the logger

inline espp::Logger::Verbosity get_log_level() const

Get the log level for the logger

Returns

The verbosity level of the logger

inline void set_log_level(espp::Logger::Verbosity level)

Set the log level for the logger

Parameters

level – The verbosity level to use for the logger

inline void set_log_verbosity(espp::Logger::Verbosity level)

Set the log verbosity for the logger

See also

set_log_level

Note

This is a convenience method that calls set_log_level

Parameters

level – The verbosity level to use for the logger

inline espp::Logger::Verbosity get_log_verbosity() const

Get the log verbosity for the logger

See also

get_log_level

Note

This is a convenience method that calls get_log_level

Returns

The verbosity level of the logger

inline void set_log_rate_limit(std::chrono::duration<float> rate_limit)

Set the rate limit for the logger

Note

Only calls to the logger that have _rate_limit suffix will be rate limited

Parameters

rate_limit – The rate limit to use for the logger

Public Static Functions

static inline QtPy &get()

Access the singleton instance of the QtPy class.

Returns

Reference to the singleton instance of the QtPy class

static inline constexpr size_t num_leds()

Get the number of LEDs in the strip

Returns

The number of LEDs in the strip