Skip to content

Commit

Permalink
Merge 1c37d02 into 6ebb7fc
Browse files Browse the repository at this point in the history
  • Loading branch information
boraozgen committed Oct 12, 2021
2 parents 6ebb7fc + 1c37d02 commit 83eff72
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/jled_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ class TJLed {
}
last_update_time_ = now;

if (now < time_start_) return true;
if ((int32_t)(now - time_start_) < 0) return true;

// t cycles in range [0..period+delay_after-1]
const auto period = brightness_eval_->Period();
Expand All @@ -399,7 +399,7 @@ class TJLed {
time_start_ + (uint32_t)(period + delay_after_) * num_repetitions_ -
1;

if (now >= time_end) {
if ((int32_t)(now - time_end) >= 0) {
// make sure final value of t = (period-1) is set
state_ = ST_STOPPED;
Write(brightness_eval_->Eval(period - 1));
Expand Down
6 changes: 1 addition & 5 deletions src/mbed_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,7 @@ class MbedHal {
}

uint32_t millis() const {
// TODO(JD)
// us_ticker_read() returns an unsigned 32 bit value with the micro
// seconds elapsed since booting the mcu. This value wraps over after
// 4294 seconds, or approx. 71 minutes.
return us_ticker_read() / 1000;
return Kernel::Clock::now().time_since_epoch().count();
}

private:
Expand Down
5 changes: 5 additions & 0 deletions test/mbed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,8 @@ void mbedMockSetUsTicks(uint32_t ticks) { MbedState_.us_ticks = ticks; }
uint32_t us_ticker_read() { return MbedState_.us_ticks; }

void PwmOut::write(float val) { mbedMockWrite(pin_, val); }

Kernel::Clock::time_point Kernel::Clock::now() {
return Kernel::Clock::time_point(
std::chrono::microseconds(MbedState_.us_ticks));
}
8 changes: 8 additions & 0 deletions test/mbed.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define TEST_MBED_H_

#include <stdint.h>
#include <chrono>

using PinName = uint8_t;

Expand All @@ -26,4 +27,11 @@ void mbedMockInit();
void mbedMockSetUsTicks(uint32_t ticks);
float mbedMockGetPinState(uint8_t pin);

namespace Kernel {
struct Clock {
using time_point = std::chrono::time_point<std::chrono::system_clock>;
static time_point now();
};
}; // namespace Kernel

#endif // TEST_MBED_H_
24 changes: 24 additions & 0 deletions test/test_jled.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// JLed Unit tests (runs on host)
// Copyright 2017 Jan Delgado jdelgado@gmx.net
#include <jled_base.h> // NOLINT
#include <limits>
#include <map>
#include <utility>
#include "catch.hpp"
Expand Down Expand Up @@ -266,6 +267,29 @@ TEST_CASE("dont evalute twice during one time tick", "[jled]") {
REQUIRE(eval.Count() == 2);
}

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);
REQUIRE_FALSE(jled.Update());
// Start fade off
jled.FadeOff(100);
REQUIRE(jled.Update());
REQUIRE(jled.IsRunning());
REQUIRE(jled.Hal().Value() > 0);
// Set time after overflow, before effect ends
jled.Hal().SetMillis(time + 50);
REQUIRE(jled.Update());
REQUIRE(jled.IsRunning());
REQUIRE(jled.Hal().Value() > 0);
// Set time after effect ends
jled.Hal().SetMillis(time + 150);
REQUIRE_FALSE(jled.Update());
REQUIRE_FALSE(jled.IsRunning());
REQUIRE(0 == jled.Hal().Value());
}

TEST_CASE("Stop() stops the effect", "[jled]") {
constexpr auto kDuration = 100;

Expand Down

0 comments on commit 83eff72

Please sign in to comment.