Skip to content

Commit

Permalink
- refactorings
Browse files Browse the repository at this point in the history
    - restructured tests
    - update catch.hpp testing framework
    - added user_param for user supplied brightness function
    - fixed bug that last state was not calculated properly
    - reformatted code
  • Loading branch information
jandelgado committed Dec 11, 2017
1 parent ae28ba7 commit 1929128
Show file tree
Hide file tree
Showing 11 changed files with 9,078 additions and 8,602 deletions.
4 changes: 4 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
BasedOnStyle: google
IndentWidth: 4

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ test/test_jled
*.gcno
*.gcda
test/coverage.info
test/report
50 changes: 25 additions & 25 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
dist: trusty
sudo: required
language:
Expand All @@ -8,39 +9,38 @@ compiler:
python:
- "2.7"

sudo: true
cache:
directories:
- "~/.platformio"
- "~/.platformio"

install:
- wget http://ftp.de.debian.org/debian/pool/main/l/lcov/lcov_1.13.orig.tar.gz
- tar xf lcov_1.13.orig.tar.gz
- make -C lcov-1.13 "PREFIX=${HOME}/.local" install
- rm -rf lcov-1.13
- pip install --user platformio
- gem install coveralls-lcov
- mkdir -p ~/.local/bin
- wget -O ~/.local/bin/cpplint "$CPP_LINT_URL" && chmod 755 ~/.local/bin/cpplint
- wget http://ftp.de.debian.org/debian/pool/main/l/lcov/lcov_1.13.orig.tar.gz
- tar xf lcov_1.13.orig.tar.gz
- make -C lcov-1.13 "PREFIX=${HOME}/.local" install
- rm -rf lcov-1.13
- pip install --user platformio
- gem install coveralls-lcov
- mkdir -p ~/.local/bin
- wget -O ~/.local/bin/cpplint "$CPP_LINT_URL" && chmod 755 ~/.local/bin/cpplint

script:
- PATH=~/.local/bin:$PATH
- cpplint --extensions=cpp,h $(find . -name "*\.cpp" -o -name "*\.h" )
- cd test && make coverage
- PATH=~/.local/bin:$PATH
- cpplint --extensions=cpp,h $(find . -name "*\.cpp" -o -name "*\.h" )
- cd test && make coverage

- cd ..
- platformio ci --lib="src/" examples/hello/hello.ino --board=uno --board=esp01
- platformio ci --lib="src/" examples/breathe/breathe.ino --board=uno --board=esp01
- platformio ci --lib="src/" examples/simple_on/simple_on.ino --board=uno --board=esp01
- platformio ci --lib="src/" examples/fade_on/fade_on.ino --board=uno --board=esp01
- platformio ci --lib="src/" examples/fade_off/fade_off.ino --board=uno --board=esp01
- platformio ci --lib="src/" examples/dual_led/dual_led.ino --board=uno --board=esp01
- platformio ci --lib="src/" examples/user_func/user_func.ino --board=uno --board=esp01
- cd ..
- platformio ci --lib="src/" examples/hello/hello.ino --board=uno --board=esp01
- platformio ci --lib="src/" examples/breathe/breathe.ino --board=uno --board=esp01
- platformio ci --lib="src/" examples/simple_on/simple_on.ino --board=uno --board=esp01
- platformio ci --lib="src/" examples/fade_on/fade_on.ino --board=uno --board=esp01
- platformio ci --lib="src/" examples/fade_off/fade_off.ino --board=uno --board=esp01
- platformio ci --lib="src/" examples/dual_led/dual_led.ino --board=uno --board=esp01
- platformio ci --lib="src/" examples/user_func/user_func.ino --board=uno --board=esp01

- cd test
- coveralls-lcov --repo-token "$COVERALLS_TOKEN" coverage.info
- cd test
- coveralls-lcov --repo-token "$COVERALLS_TOKEN" coverage.info

env:
global:
- CPP_LINT_URL=https://raw.githubusercontent.com/google/styleguide/gh-pages/cpplint/cpplint.py
- secure: s5n3rtNF0ODwQY1f0ScuF4Hj1sElj3I3gdliHwCzWX4CQzJj7wX108l/GAXC+oohJlZ9OaFMEiNuHQj1Z5tzFxSS5YaB68h97HZ7+yIfh2pvhbSv09PlGUvfXiFRMzqMdT133JKXcsMwaDzdZNHBN/9E3tvNa61Fb2eVHQnCAQIh6w9UIo9ntyEtsQqcbZqtyOO1vry2sBf5JzhAA4LXwgTO7VfBKPFeFDnjozhPlxbMLJSNTgG9rGU2IPHLxs7WC+d8xRJMzISY6fJ5vbr7k3gDhxVyu89nj/w3dAEhWyzLY7qaRGTF0r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HU7d1Vy5jB5bY2kC5KPFTKahGBLwW7KUNaTRvq3QxYizQkGXoSeTGPEbU=
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# JLed - Extended LED Library

[![Build Status](https://travis-ci.org/jandelgado/jled.svg?branch=master)](https://travis-ci.org/jandelgado/jled)
[![Coverage Status](https://coveralls.io/repos/github/jandelgado/jled/badge.svg?branch=master)](https://coveralls.io/github/jandelgado/jled?branch=master)
[![Coverage Status](https://coveralls.io/repos/github/jandelgado/jled/badge.svg?branch=master&dummy=1)](https://coveralls.io/github/jandelgado/jled?branch=master)

An Arduino library to control LEDs. It uses a **non-blocking** approach and can
control LEDs in simple (**on**/**off**) and complex (**blinking**,
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=JLed
version=2.0.1
version=2.1.0
author=Jan Delgado <jdelgado[at]gmx.net>
maintainer=Jan Delgado <jdelgado[at]gmx.net>
sentence=An Arduino library to control LEDs
Expand Down
42 changes: 19 additions & 23 deletions src/jled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,11 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//
#include "jled.h" // NOLINT

JLed::JLed(uint8_t led_pin) :
effect_inverted_(false), low_active_(false),
led_pin_(led_pin), num_repetitions_(1), last_update_time_(0),
delay_before_(0), delay_after_(0),
time_start_(kTimeUndef), period_(0), brightness_func_(nullptr),
effect_param_(0) {
pinMode(led_pin_, OUTPUT);
}
#include "jled.h" // NOLINT

JLed::JLed(uint8_t led_pin) : led_pin_(led_pin) { pinMode(led_pin_, OUTPUT); }

void JLed::AnalogWrite(uint8_t val) {
void JLed::AnalogWrite(uint8_t val) const {
analogWrite(led_pin_, low_active_ ? 255 - val : val);
}

Expand Down Expand Up @@ -65,21 +58,23 @@ void JLed::Update() {
last_update_time_ = now;
// wait until delay_before time is elapsed before actually doing anything
if (delay_before_ > 0) {
delay_before_ = max(0, static_cast<int64_t>(delay_before_) - delta_time); // NOLINT
if (delay_before_ > 0)
return;
delay_before_ =
max(0, static_cast<int64_t>(delay_before_) - delta_time); // NOLINT
if (delay_before_ > 0) return;
}

const auto time_end = time_start_ +
(uint32_t)(period_ + delay_after_) * num_repetitions_;
const auto time_end =
time_start_ + (uint32_t)(period_ + delay_after_) * num_repetitions_;

if (!IsForever() && now >= time_end) {
// make sure final value is set
AnalogWrite(EvalBrightness(period_ - 1));
brightness_func_ = nullptr;
return;
}

// t cycles in range [0..period+delay_after-1]
auto t = (now - time_start_) % (period_ + delay_after_);
const auto t = (now - time_start_) % (period_ + delay_after_);

if (t < period_) {
AnalogWrite(EvalBrightness(t));
Expand All @@ -89,7 +84,7 @@ void JLed::Update() {
}
}

uint8_t JLed::EvalBrightness(uint32_t t) {
uint8_t JLed::EvalBrightness(uint32_t t) const {
auto val = brightness_func_(t, period_, effect_param_);
return effect_inverted_ ? 255 - val : val;
}
Expand Down Expand Up @@ -118,7 +113,8 @@ JLed& JLed::FadeOn(uint16_t duration) {
}

JLed& JLed::FadeOff(uint16_t duration) {
return FadeOn(duration).Invert();
period_ = duration;
return Init(&JLed::FadeOffFunc);
}

JLed& JLed::On() {
Expand All @@ -131,11 +127,11 @@ JLed& JLed::Off() {
return Init(&JLed::OffFunc);
}

JLed& JLed::Set(bool on) {
return on ? On() : Off();
}
JLed& JLed::Set(bool on) { return on ? On() : Off(); }

JLed& JLed::UserFunc(BrightnessEvalFunction func, uint16_t period) {
JLed& JLed::UserFunc(BrightnessEvalFunction func,
uint16_t period, uint32_t user_param) {
effect_param_ = user_param;
period_ = period;
return Init(func);
}
Loading

0 comments on commit 1929128

Please sign in to comment.