|
| 1 | +#include <chrono> |
| 2 | +#include <sdkconfig.h> |
| 3 | +#include <vector> |
| 4 | + |
| 5 | +#include "i2c.hpp" |
| 6 | +#include "lp5817.hpp" |
| 7 | +#include "task.hpp" |
| 8 | + |
| 9 | +using namespace std::chrono_literals; |
| 10 | + |
| 11 | +extern "C" void app_main(void) { |
| 12 | + espp::Logger logger({.tag = "LP5817 Example", .level = espp::Logger::Verbosity::INFO}); |
| 13 | + logger.info("Starting"); |
| 14 | + |
| 15 | + //! [lp5817 example] |
| 16 | + espp::I2c i2c({ |
| 17 | + .port = I2C_NUM_0, |
| 18 | + .sda_io_num = (gpio_num_t)CONFIG_EXAMPLE_I2C_SDA_GPIO, |
| 19 | + .scl_io_num = (gpio_num_t)CONFIG_EXAMPLE_I2C_SCL_GPIO, |
| 20 | + .sda_pullup_en = GPIO_PULLUP_ENABLE, |
| 21 | + .scl_pullup_en = GPIO_PULLUP_ENABLE, |
| 22 | + .clk_speed = CONFIG_EXAMPLE_I2C_CLOCK_SPEED_HZ, |
| 23 | + .log_level = espp::Logger::Verbosity::INFO, |
| 24 | + }); |
| 25 | + |
| 26 | + espp::Lp5817 lp({.device_address = espp::Lp5817::DEFAULT_ADDRESS, |
| 27 | + .write = std::bind_front(&espp::I2c::write, &i2c), |
| 28 | + .write_then_read = std::bind_front(&espp::I2c::write_read, &i2c), |
| 29 | + .log_level = espp::Logger::Verbosity::WARN}); |
| 30 | + std::error_code ec; |
| 31 | + if (!lp.initialize(ec)) { |
| 32 | + logger.error("Failed to initialize LP5817: {}", ec.message()); |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + // configure the max current per channel |
| 37 | + static constexpr auto max_current = espp::Lp5817::GlobalMaxCurrent::MA_51; |
| 38 | + logger.info("Setting max current to {}", max_current); |
| 39 | + if (!lp.set_max_current(max_current, ec)) { |
| 40 | + logger.error("Failed to set max current: {}", ec.message()); |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + // configure the fade time |
| 45 | + static constexpr auto fade_time = espp::Lp5817::FadeTime::TIME_300MS; |
| 46 | + logger.info("Setting fade time to {}", fade_time); |
| 47 | + if (!lp.set_fade_time(fade_time, ec)) { |
| 48 | + logger.error("Failed to set fade time: {}", ec.message()); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + // enable fading and exponential dimming on all channels |
| 53 | + using Ch = espp::Lp5817::Channel; |
| 54 | + for (auto ch : {Ch::OUT0, Ch::OUT1, Ch::OUT2}) { |
| 55 | + if (!lp.set_fade_enable(ch, true, ec)) { |
| 56 | + logger.error("Failed to enable fading on channel {}: {}", ch, ec.message()); |
| 57 | + return; |
| 58 | + } |
| 59 | + if (!lp.set_exponential_dimming_enable(ch, true, ec)) { |
| 60 | + logger.error("Failed to enable exponential dimming on channel {}: {}", ch, ec.message()); |
| 61 | + return; |
| 62 | + } |
| 63 | + if (!lp.set_output_enable(ch, true, ec)) { |
| 64 | + logger.error("Failed to enable output on channel {}: {}", ch, ec.message()); |
| 65 | + return; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // Latch the settings (update) |
| 70 | + if (!lp.update(ec)) { |
| 71 | + logger.error("Failed to latch settings: {}", ec.message()); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + // Simple breathing RGB demo |
| 76 | + auto task_fn = [&](std::mutex &m, std::condition_variable &cv) { |
| 77 | + static float brightness = 0.0f; |
| 78 | + brightness = (brightness == 0.0f) ? 1.0f : 0.0f; |
| 79 | + // for now we'll just to white and fade it in and out |
| 80 | + float r = brightness; |
| 81 | + float g = brightness; |
| 82 | + float b = brightness; |
| 83 | + if (!lp.set_rgb_pwm(r, g, b, ec)) { |
| 84 | + logger.error("LP5817 set_rgb failed: {}", ec.message().c_str()); |
| 85 | + return true; |
| 86 | + } |
| 87 | + { |
| 88 | + std::unique_lock<std::mutex> lk(m); |
| 89 | + // waiting for 300ms since that is our fade time |
| 90 | + cv.wait_for(lk, 300ms); |
| 91 | + } |
| 92 | + return false; |
| 93 | + }; |
| 94 | + |
| 95 | + auto task = espp::Task({.callback = task_fn, |
| 96 | + .task_config = {.name = "LP5817 Task", .stack_size_bytes = 4 * 1024}, |
| 97 | + .log_level = espp::Logger::Verbosity::WARN}); |
| 98 | + task.start(); |
| 99 | + //! [lp5817 example] |
| 100 | + |
| 101 | + while (true) { |
| 102 | + std::this_thread::sleep_for(100ms); |
| 103 | + } |
| 104 | +} |
0 commit comments