Skip to content

Commit d0fe655

Browse files
committed
Update firmware to write to the strip only when colors changed
1 parent 4386272 commit d0fe655

9 files changed

Lines changed: 25 additions & 21 deletions

File tree

arduino_code/ArduinoPixel/src/arduino_pixel_server.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ void ArduinoPixelServer::init(led_strip::LedStripBase *strip) {
6565
void ArduinoPixelServer::powerOn() {
6666
power_ = true;
6767
strip_->setMode(mode_);
68-
strip_->colorize();
68+
strip_->colorize(true);
6969
}
7070

7171
void ArduinoPixelServer::powerOff() {
7272
power_ = false;
7373
strip_->setMode(mode_off_);
74-
strip_->colorize();
74+
strip_->colorize(true);
7575
}
7676

7777
RequestData ArduinoPixelServer::parseRequest(Client &client) const {
@@ -264,11 +264,11 @@ void ArduinoPixelServer::updateStrip(const RequestData &request) {
264264
break;
265265
case Uri::MODE_PUT: // Update mode
266266
updateMode(request.data);
267-
strip_->colorize();
267+
strip_->colorize(true);
268268
break;
269269
case Uri::COLOR_PUT: // Update the LED strip color
270270
updateColor(request.data);
271-
strip_->colorize();
271+
strip_->colorize(true);
272272
break;
273273
}
274274
}

arduino_code/ArduinoPixel/src/arduino_pixel_server.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@
2929
#ifndef ARDUINO_PIXEL_ARDUINO_PIXEL_SERVER_H
3030
#define ARDUINO_PIXEL_ARDUINO_PIXEL_SERVER_H
3131

32-
// #define DEBUG
33-
3432
#include <Client.h>
3533

34+
// #define DEBUG
35+
3636
#include "common_types.h"
3737
#include "server_types.h"
3838
#include "led_strip/led_strip_base.h"

arduino_code/ArduinoPixel/src/led_strip/led_strip_base.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ class LedStripBase {
5353
/**
5454
* \brief Updates the LED strip.
5555
* \details Gets the colors from the mode and updates the LED strip.
56+
* \param[in] force Force updating the strip.
5657
*/
57-
virtual void colorize() = 0;
58+
virtual void colorize(bool force = false) = 0;
5859
/**
5960
* \brief Gets the number of LEDs on the strip.
6061
* \return The number of LEDs.

arduino_code/ArduinoPixel/src/led_strip/led_strip_esp_ws2812.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ class LedStripEspWs2812 : public LedStripBase {
4343

4444
virtual void init() override { strip_.init(); }
4545

46-
virtual void colorize() override {
47-
mode_->update();
46+
virtual void colorize(bool force = false) override {
47+
if (not mode_->update() and not force) return;
4848
for (uint16_t idx = 0; idx < strip_.numPixels(); ++idx) {
4949
const Color &color = mode_->getPixel(idx);
5050
strip_.setPixelColor(idx, color.red, color.green, color.blue);

arduino_code/ArduinoPixel/src/led_strip/led_strip_neopixel.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ class LedStripNeoPixel : public LedStripBase {
4646
strip_.show();
4747
}
4848

49-
virtual void colorize() override {
50-
mode_->update();
49+
virtual void colorize(bool force = false) override {
50+
if (not mode_->update() and not force) return;
5151
for (uint16_t idx = 0; idx < strip_.numPixels(); ++idx) {
5252
const Color &color = mode_->getPixel(idx);
5353
strip_.setPixelColor(idx, color.red, color.green, color.blue);

arduino_code/ArduinoPixel/src/mode/mode_base.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ class ModeBase {
4747
* \brief Updates the colors on the LED array.
4848
* \note If you think the mode as a video (sequence of images),
4949
* this is where you fill the LED array with the next image.
50+
* \return Flag to indicate whether the LED array has been updated.
5051
*/
51-
virtual void update() = 0;
52+
virtual bool update() = 0;
5253
/**
5354
* \brief Gets the color of the request LED (aka pixel).
5455
* \param[in] idx the index of the pixel in the array.

arduino_code/ArduinoPixel/src/mode/rainbow_base.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@ class RainbowBase : public ModeBase {
5050

5151
virtual void init() override { setPixels(); }
5252

53-
virtual void update() override {
54-
unsigned long new_update_time = millis();
55-
if ((unsigned long)(new_update_time - last_update_time_) < period_) return;
53+
virtual bool update() override {
54+
unsigned long current_time = millis();
55+
if ((unsigned long)(current_time - last_update_time_) < period_) return false;
5656
setPixels();
57-
last_update_time_ = new_update_time;
57+
last_update_time_ = current_time;
58+
return true;
5859
}
5960

6061
virtual const Color& getPixel(int idx) const override { return pixels_[idx]; }

arduino_code/ArduinoPixel/src/mode/scanner.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,17 @@ class Scanner : public ModeBase {
5959
last_update_time_ = millis();
6060
}
6161

62-
virtual void update() override {
63-
unsigned long new_update_time = millis();
64-
if ((unsigned long)(new_update_time - last_update_time_) < period_) return;
62+
virtual bool update() override {
63+
unsigned long current_time = millis();
64+
if ((unsigned long)(current_time - last_update_time_) < period_) return false;
6565

6666
turnPixelOff(start_idx_);
6767
start_idx_ = (start_idx_ + 1) % num_leds_;
6868
end_idx_ = (end_idx_ + 1) % num_leds_;
6969
turnPixelOn(end_idx_);
7070

71-
last_update_time_ = new_update_time;
71+
last_update_time_ = current_time;
72+
return true;
7273
}
7374

7475
virtual const Color& getPixel(int idx) const override { return pixels_[idx]; }

arduino_code/ArduinoPixel/src/mode/single_color.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class SingleColor : public ModeBase {
4545

4646
virtual void init() override {}
4747

48-
virtual void update() override {}
48+
virtual bool update() override { return false; }
4949

5050
virtual const Color& getPixel(int idx = 0) const override { return color_; }
5151

0 commit comments

Comments
 (0)