From 970838ed09359e4255e945cd5ec522d67f07a5ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Major=20P=C3=A9ter?= Date: Tue, 4 Jun 2019 12:11:59 +0200 Subject: [PATCH] Scan length for AddressableScanEffect (#608) * Added scan_length to AddressableScanEffect (allow more than one LED) * Added check for scan length being longer than addressable light * Added config option 'scan_length' to AddressableScanEffect (default: 1) * Renamed scan_length to scan_width, removed erroneous length check * Fixed indentation issue in addressable_light_effect.h Co-Authored-By: Otto Winter --- esphome/components/light/addressable_light_effect.h | 10 ++++++++-- esphome/components/light/effects.py | 3 +++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/esphome/components/light/addressable_light_effect.h b/esphome/components/light/addressable_light_effect.h index 1e0b540285..e357d7c136 100644 --- a/esphome/components/light/addressable_light_effect.h +++ b/esphome/components/light/addressable_light_effect.h @@ -143,14 +143,19 @@ class AddressableScanEffect : public AddressableLightEffect { public: explicit AddressableScanEffect(const std::string &name) : AddressableLightEffect(name) {} void set_move_interval(uint32_t move_interval) { this->move_interval_ = move_interval; } + void set_scan_width(uint32_t scan_width) { this->scan_width_ = scan_width; } void apply(AddressableLight &it, const ESPColor ¤t_color) override { it.all() = ESPColor::BLACK; - it[this->at_led_] = current_color; + + for (auto i = 0; i < this->scan_width_; i++) { + it[this->at_led_ + i] = current_color; + } + const uint32_t now = millis(); if (now - this->last_move_ > this->move_interval_) { if (direction_) { this->at_led_++; - if (this->at_led_ == it.size() - 1) + if (this->at_led_ == it.size() - this->scan_width_) this->direction_ = false; } else { this->at_led_--; @@ -163,6 +168,7 @@ class AddressableScanEffect : public AddressableLightEffect { protected: uint32_t move_interval_{}; + uint32_t scan_width_{1}; uint32_t last_move_{0}; int at_led_{0}; bool direction_{true}; diff --git a/esphome/components/light/effects.py b/esphome/components/light/effects.py index a78165fb8a..eb4e196d4e 100644 --- a/esphome/components/light/effects.py +++ b/esphome/components/light/effects.py @@ -16,6 +16,7 @@ from .types import LambdaLightEffect, RandomLightEffect, StrobeLightEffect, \ CONF_ADD_LED_INTERVAL = 'add_led_interval' CONF_REVERSE = 'reverse' CONF_MOVE_INTERVAL = 'move_interval' +CONF_SCAN_WIDTH = 'scan_width' CONF_TWINKLE_PROBABILITY = 'twinkle_probability' CONF_PROGRESS_INTERVAL = 'progress_interval' CONF_SPARK_PROBABILITY = 'spark_probability' @@ -179,10 +180,12 @@ def addressable_color_wipe_effect_to_code(config, effect_id): @register_effect('addressable_scan', AddressableScanEffect, "Scan", { cv.Optional(CONF_MOVE_INTERVAL, default='0.1s'): cv.positive_time_period_milliseconds, + cv.Optional(CONF_SCAN_WIDTH, default=1): cv.int_range(min=1), }) def addressable_scan_effect_to_code(config, effect_id): var = cg.new_Pvariable(effect_id, config[CONF_NAME]) cg.add(var.set_move_interval(config[CONF_MOVE_INTERVAL])) + cg.add(var.set_scan_width(config[CONF_SCAN_WIDTH])) yield var