Skip to content

Commit

Permalink
make JLed::Update(uint32_t t) public
Browse files Browse the repository at this point in the history
allowing minor optimizations and simpler tests
  • Loading branch information
jandelgado committed Sep 21, 2024
1 parent 67b03a1 commit b4abacd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 35 deletions.
25 changes: 13 additions & 12 deletions src/jled_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,6 @@ class TJLed {

HalType& Hal() { return hal_; }

bool Update() { return Update(hal_.millis()); }

// Set physical LED polarity to be low active. This inverts every
// signal physically output to a pin.
B& LowActive() {
Expand Down Expand Up @@ -378,14 +376,6 @@ class TJLed {
// Returns current maximum brightness level.
uint8_t MaxBrightness() const { return maxBrightness_; }

protected:
// test if time stored in last_update_time_ differs from provided timestamp.
bool inline timeChangedSinceLastUpdate(uint32_t now) {
return (now & 255) != last_update_time_;
}

void trackLastUpdateTime(uint32_t t) { last_update_time_ = (t & 255); }

// update brightness of LED using the given brightness evaluator
// (brightness) ________________
// on 255 | ¸-'
Expand All @@ -395,6 +385,8 @@ class TJLed {
// |<-delay before->|<--period-->|<-delay after-> (time)
// | func(t) |
// |<- num_repetitions times ->
bool Update() { return Update(hal_.millis()); }

bool Update(uint32_t now) {
if (state_ == ST_STOPPED || !brightness_eval_) return false;

Expand Down Expand Up @@ -442,6 +434,14 @@ class TJLed {
return true;
}

protected:
// test if time stored in last_update_time_ differs from provided timestamp.
bool inline timeChangedSinceLastUpdate(uint32_t now) {
return (now & 255) != last_update_time_;
}

void trackLastUpdateTime(uint32_t t) { last_update_time_ = (t & 255); }

B& SetBrightnessEval(BrightnessEvaluator* be) {
brightness_eval_ = be;
// start over after the brightness evaluator changed
Expand Down Expand Up @@ -504,8 +504,9 @@ class TJLedSequence {
// active, else false
bool UpdateParallel() {
auto result = false;
uint32_t t = ptr(leds_[0])->Hal().millis();
for (auto i = 0u; i < n_; i++) {
result |= ptr(leds_[i])->Update();
result |= ptr(leds_[i])->Update(t);
}
return result;
}
Expand Down Expand Up @@ -536,7 +537,7 @@ class TJLedSequence {
: mode_{mode}, leds_{leds}, cur_{0}, n_{n} {}

bool Update() {
if (!is_running_) {
if (!is_running_ || n_ < 1) {
return false;
}

Expand Down
35 changes: 12 additions & 23 deletions test/test_jled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,14 +297,11 @@ TEST_CASE("dont evalute twice during one time tick", "[jled]") {
auto eval = MockBrightnessEvaluator(ByteVec{0, 1, 2});
TestJLed jled = TestJLed(1).UserFunc(&eval);

jled.Hal().SetMillis(0);
jled.Update();
jled.Update(0);
CHECK(eval.Count() == 1);
jled.Update();
jled.Update(0);
CHECK(eval.Count() == 1);

jled.Hal().SetMillis(1);
jled.Update();
jled.Update(1);

CHECK(eval.Count() == 2);
}
Expand All @@ -313,21 +310,18 @@ TEST_CASE("Handles millis overflow during effect", "[jled]") {
TestJLed jled = TestJLed(10);
// Set time close to overflow
auto time = std::numeric_limits<uint32_t>::max() - 25;
jled.Hal().SetMillis(time);
CHECK_FALSE(jled.Update());
CHECK_FALSE(jled.Update(time));
// Start fade off
jled.FadeOff(100);
CHECK(jled.Update());
CHECK(jled.Update(time));
CHECK(jled.IsRunning());
CHECK(jled.Hal().Value() > 0);
// Set time after overflow, before effect ends
jled.Hal().SetMillis(time + 50);
CHECK(jled.Update());
CHECK(jled.Update(time+50));
CHECK(jled.IsRunning());
CHECK(jled.Hal().Value() > 0);
// Set time after effect ends
jled.Hal().SetMillis(time + 150);
CHECK_FALSE(jled.Update());
CHECK_FALSE(jled.Update(time+150));
CHECK_FALSE(jled.IsRunning());
CHECK(0 == jled.Hal().Value());
}
Expand Down Expand Up @@ -382,11 +376,10 @@ TEST_CASE("LowActive() inverts signal", "[jled]") {

CHECK(jled.IsLowActive());

jled.Update();
jled.Update(0);
CHECK(255 == jled.Hal().Value());

jled.Hal().SetMillis(1);
jled.Update();
jled.Update(1);
CHECK(0 == jled.Hal().Value());
}

Expand Down Expand Up @@ -449,15 +442,11 @@ TEST_CASE("Update returns true while updating, else false", "[jled]") {
TestJLed jled = TestJLed(10).UserFunc(&eval);

// Update returns FALSE on last step and beyond, else TRUE
auto time = 0;
jled.Hal().SetMillis(time++);
CHECK(jled.Update());
CHECK(jled.Update(0));

// when effect is done, we expect still false to be returned
jled.Hal().SetMillis(time++);
CHECK_FALSE(jled.Update());
jled.Hal().SetMillis(time++);
CHECK_FALSE(jled.Update());
CHECK_FALSE(jled.Update(1));
CHECK_FALSE(jled.Update(2));
}

TEST_CASE("After Reset() the effect can be restarted", "[jled]") {
Expand Down

0 comments on commit b4abacd

Please sign in to comment.