Skip to content

Commit

Permalink
improve 10bit scaling for ESP8266
Browse files Browse the repository at this point in the history
add "contributing" section to docs
  • Loading branch information
jandelgado committed Sep 22, 2018
1 parent ceb10e1 commit 7021e2b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 11 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ void loop() {
* [PlatformIO](#platformio-1)
* [Arduino IDE](#arduino-ide-1)
* [Unit tests](#unit-tests)
* [Contributing](#contributing)
* [Author](#author)
* [License](#license)

Expand Down Expand Up @@ -271,6 +272,17 @@ the `File` > `Examples` > `JLed` menu.
Info on how to run the host based provided unit tests
[is provided here](test/README.md).

## Contributing

* fork this repository
* create your feature branch
* add code
* add unit test(s)
* add documentation
* make sure linter does not report problems (make lint)
* commit changes
* submit a PR

## Author

Jan Delgado, jdelgado[at]gmx.net.
Expand Down
17 changes: 6 additions & 11 deletions src/jled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@
//
#include "jled.h" // NOLINT

// uncomment to enable workaround (does not solve the problem totally) for
// flickering LED when turning LED off. see
// https://arduino.stackexchange.com/questions/17946/why-does-an-led-sometimes-flash-when-increasing-brightness
// #define PWM_FLICKER_WORKAROUND_ENABLE

constexpr uint8_t JLed::kFadeOnTable[];

JLed::JLed(uint8_t led_pin) : led_pin_(led_pin) {
Expand Down Expand Up @@ -74,16 +69,16 @@ JLed& JLed::LowActive() { return SetFlags(FL_LOW_ACTIVE, true); }

bool JLed::IsLowActive() const { return GetFlag(FL_LOW_ACTIVE); }

// scale an 8bit value to 10bit: 0 -> 0, ..., 255 -> 1023.
uint16_t JLed::ScaleTo10Bit(uint8_t x) {
return (x == 0) ? 0 : (x << 2) + 3;
}

void JLed::AnalogWrite(uint8_t val) {
auto new_val = IsLowActive() ? 255 - val : val;
#ifdef PWM_FLICKER_WORKAROUND_ENABLE
if (new_val == 0) {
analogWrite(led_pin_, 1);
}
#endif
#ifdef ESP8266
// ESP8266 uses 10bit PWM range per default, scale value up
new_val <<= 2;
new_val = JLed::ScaleTo10Bit(new_val);
#endif
analogWrite(led_pin_, new_val);
}
Expand Down
2 changes: 2 additions & 0 deletions src/jled.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ class JLed {
// the period each.
static uint8_t BreatheFunc(uint32_t t, uint16_t period, uintptr_t);

static uint16_t ScaleTo10Bit(uint8_t);

private:
// pre-calculated fade-on function. This table samples the function
// y(x) = exp(sin((t - period / 2.) * PI / period)) - 0.36787944) * 108.
Expand Down
14 changes: 14 additions & 0 deletions test/test_jled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ TEST_CASE("jled ctor set pin mode to OUTPUT", "[jled]") {
REQUIRE(arduinoMockGetPinMode(kTestPin) == OUTPUT);
}

TEST_CASE("properly scale 8bit to 10bit for ESP8266 support") {
class TestableJLed : public JLed {
public:
using JLed::JLed;
static void test() {
REQUIRE(TestableJLed::ScaleTo10Bit(0) == 0);
REQUIRE(TestableJLed::ScaleTo10Bit(127) == (127<<2)+3);
REQUIRE(TestableJLed::ScaleTo10Bit(255) == 1023);
}
};
TestableJLed::test();
}


TEST_CASE("properly initialize brightness_func", "[jled]") {
class TestableJLed : public JLed {
public:
Expand Down

0 comments on commit 7021e2b

Please sign in to comment.