|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <mutex> |
| 4 | + |
| 5 | +#include <driver/gpio.h> |
| 6 | + |
| 7 | +#include "base_component.hpp" |
| 8 | +#include "color.hpp" |
| 9 | +#include "logger.hpp" |
| 10 | +#include "rmt.hpp" |
| 11 | + |
| 12 | +namespace espp { |
| 13 | +/// \brief A class for controlling a Neopixel strip. |
| 14 | +/// \details This class provides a simple interface for controlling a Neopixel |
| 15 | +/// strip. It uses the RMT peripheral to send the data to the strip. |
| 16 | +/// |
| 17 | +/// \section neopixel_ex1 Example 1: QtPy ESP32S3 Neopixel |
| 18 | +/// \snippet neopixel_example.cpp neopixel ex1 |
| 19 | +class Neopixel { |
| 20 | +public: |
| 21 | + /// The configuration structure for the Neopixel. |
| 22 | + struct Config { |
| 23 | + int data_gpio; ///< The GPIO pin for the data line |
| 24 | + int power_gpio{-1}; ///< The GPIO pin for the power line (optional) |
| 25 | + size_t num_leds{1}; ///< The number of LEDs in the strip |
| 26 | + espp::Logger::Verbosity log_level{ |
| 27 | + espp::Logger::Verbosity::WARN}; ///< Verbosity for the neopixel logger |
| 28 | + }; |
| 29 | + |
| 30 | + /// Constructor for the Neopixel. |
| 31 | + /// \param config The configuration structure for the Neopixel. |
| 32 | + explicit Neopixel(const espp::Neopixel::Config &config); |
| 33 | + |
| 34 | + /// Get the number of LEDs in the strip. |
| 35 | + /// \return The number of LEDs in the strip. |
| 36 | + std::size_t num_leds() const { return config_.num_leds; } |
| 37 | + |
| 38 | + /// Set the color of a single pixel. |
| 39 | + /// \param rgb The color to set the pixel to. |
| 40 | + /// \param index The index of the pixel to set. |
| 41 | + void set_color(const espp::Rgb &rgb, std::size_t index = 0); |
| 42 | + |
| 43 | + /// Set the color of a single pixel. |
| 44 | + /// \param hsv The color to set the pixel to. |
| 45 | + /// \param index The index of the pixel to set. |
| 46 | + void set_color(const espp::Hsv &hsv, std::size_t index = 0) { set_color(hsv.rgb(), index); } |
| 47 | + |
| 48 | + /// Set the color of all pixels. |
| 49 | + /// \param rgb The color to set all pixels to. |
| 50 | + void set_all(const espp::Rgb &rgb); |
| 51 | + |
| 52 | + /// Set the color of all pixels. |
| 53 | + /// \param hsv The color to set all pixels to. |
| 54 | + void set_all(const espp::Hsv &hsv) { set_all(hsv.rgb()); } |
| 55 | + |
| 56 | + /// Show the current pixel data. |
| 57 | + void show(); |
| 58 | + |
| 59 | + /// Set the power state of the strip. |
| 60 | + /// \param on The power state to set. |
| 61 | + /// \details If the power GPIO is set, this function will set the power |
| 62 | + /// state of the strip. If the power GPIO is not set, this function |
| 63 | + /// will do nothing. |
| 64 | + void set_power(bool on); |
| 65 | + |
| 66 | +protected: |
| 67 | + static constexpr int WS2812_FREQ_HZ = 10'000'000; |
| 68 | + static constexpr int MICROS_PER_SEC = 1'000'000; |
| 69 | + |
| 70 | + std::size_t encode(rmt_channel_handle_t channel, rmt_encoder_t *copy_encoder, |
| 71 | + rmt_encoder_t *bytes_encoder, const void *data, std::size_t data_size, |
| 72 | + rmt_encode_state_t *ret_state); |
| 73 | + |
| 74 | + void configure_power_pin(); |
| 75 | + |
| 76 | + Config config_; |
| 77 | + std::shared_ptr<espp::Rmt> rmt_; |
| 78 | + int led_encoder_state_{0}; |
| 79 | + std::vector<uint8_t> led_data_; |
| 80 | +}; // class Neopixel |
| 81 | +} // namespace espp |
0 commit comments