Skip to content

Commit fcc98ac

Browse files
committed
fix glimming LED problem (#60)
on ESP32 with LEDs connected as low-active. See espressif/arduino-esp32#689
1 parent 230ea60 commit fcc98ac

File tree

4 files changed

+53
-18
lines changed

4 files changed

+53
-18
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# JLed changelog
22

3+
## [2020-10-24] 4.5.2
4+
5+
* fix: ESP32 led glimming when using low-active connection (#60)
6+
7+
## [2020-07-01] 4.5.1
8+
9+
* fix: support for Nano 33 BLE (#53)
10+
311
## [2020-02-23] 4.5.0
412

513
* new: `JLed::MaxBrightness(uint8_t level)` method to limit output of effects

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=JLed
2-
version=4.5.1
2+
version=4.5.2
33
author=Jan Delgado <jdelgado[at]gmx.net>
44
maintainer=Jan Delgado <jdelgado[at]gmx.net>
55
sentence=An Arduino library to control LEDs

src/esp32_hal.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
// Copyright (c) 2017 Jan Delgado <jdelgado[at]gmx.net>
1+
// Copyright (c) 2017-2020 Jan Delgado <jdelgado[at]gmx.net>
22
// https://github.com/jandelgado/jled
3+
// HAL for the ESP32
4+
// https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/ledc.html
35
//
46
// Permission is hereby granted, free of charge, to any person obtaining a copy
57
// of this software and associated documentation files (the "Software"), to
@@ -84,7 +86,9 @@ class Esp32Hal {
8486
::ledcSetup(chan_, freq, kLedcTimer8Bit);
8587
::ledcAttachPin(pin, chan_);
8688
}
87-
void analogWrite(uint8_t val) const { ::ledcWrite(chan_, val); }
89+
void analogWrite(uint8_t val) const {
90+
::ledcWrite(chan_, (val == 255)? 256 : val);
91+
}
8892
uint32_t millis() const { return ::millis(); }
8993

9094
PinType chan() const { return chan_; }

test/test_esp32_hal.cpp

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// JLed Unit tests (run on host).
2-
// Copyright 2017 Jan Delgado [email protected]
1+
// JLed Unit tests for the ESP32 HAL (run on host).
2+
// Copyright 2017-2020 Jan Delgado [email protected]
33
#include "esp32.h" // NOLINT
44
#include <esp32_hal.h> // NOLINT
55
#include "catch.hpp"
@@ -46,7 +46,7 @@ TEST_CASE("ledc ctor correctly initializes hardware", "[esp32_hal]") {
4646
// attach channel 2 to pin 1
4747
constexpr auto kChan = 5;
4848
constexpr auto kPin = 10;
49-
auto aw[[gnu::unused]] = Esp32Hal(kPin, kChan);
49+
auto hal[[gnu::unused]] = Esp32Hal(kPin, kChan);
5050

5151
// writer expected to used 5000Hz and 8 bits
5252
REQUIRE(arduinoMockGetLedcSetup(kChan).freq == 5000);
@@ -60,40 +60,63 @@ TEST_CASE("ledc selects same channel for same pin", "[esp32_hal]") {
6060

6161
// note: we test a static property here (auto incremented next channel
6262
// number). so test has side effects. TODO avoid?
63-
auto h1 = Esp32Hal(kPin);
64-
auto h2 = Esp32Hal(kPin);
63+
auto hal1 = Esp32Hal(kPin);
64+
auto hal2 = Esp32Hal(kPin);
6565

6666
// same channel is to be selected, since pin did not change
67-
REQUIRE(h1.chan() == h2.chan());
67+
REQUIRE(hal1.chan() == hal2.chan());
6868
}
6969

7070
TEST_CASE("ledc selects different channels for different pins", "[esp32_hal]") {
7171
constexpr auto kPin = 10;
7272

73-
auto h1 = Esp32Hal(kPin);
74-
auto h2 = Esp32Hal(kPin + 1);
73+
auto hal1 = Esp32Hal(kPin);
74+
auto hal2 = Esp32Hal(kPin + 1);
7575

76-
REQUIRE(h1.chan() != h2.chan());
76+
REQUIRE(hal1.chan() != hal2.chan());
7777
}
7878

79-
TEST_CASE("ledc analogWrite() writes value using analogWrite", "[esp32_hal]") {
79+
TEST_CASE("analogWrite() writes value using analogWrite", "[esp32_hal]") {
8080
esp32MockInit();
8181

8282
// attach channel 2 to pin 1
8383
constexpr auto kChan = 5;
8484
constexpr auto kPin = 10;
85-
auto aw = Esp32Hal(kPin, kChan);
85+
auto hal = Esp32Hal(kPin, kChan);
8686

87-
aw.analogWrite(123);
87+
hal.analogWrite(123);
8888

8989
// note: value is written to channel, not pin.
9090
REQUIRE(arduinoMockGetLedcState(kChan) == 123);
9191
}
9292

93+
TEST_CASE("analogWrite() writes 0 as 0", "[esp32_hal]") {
94+
esp32MockInit();
95+
96+
// attach channel 2 to pin 1
97+
constexpr auto kChan = 5;
98+
constexpr auto kPin = 10;
99+
auto hal = Esp32Hal(kPin, kChan);
100+
101+
hal.analogWrite(0);
102+
REQUIRE(arduinoMockGetLedcState(kChan) == 0);
103+
}
104+
105+
TEST_CASE("analogWrite() writes 255 as 256", "[esp32_hal]") {
106+
esp32MockInit();
107+
108+
constexpr auto kChan = 5;
109+
constexpr auto kPin = 10;
110+
auto hal = Esp32Hal(kPin, kChan);
111+
112+
hal.analogWrite(255);
113+
REQUIRE(arduinoMockGetLedcState(kChan) == 256);
114+
}
115+
93116
TEST_CASE("millis() returns correct time", "[esp32_hal]") {
94117
arduinoMockInit();
95-
auto h = Esp32Hal(1);
96-
REQUIRE(h.millis() == 0);
118+
auto hal = Esp32Hal(1);
119+
REQUIRE(hal.millis() == 0);
97120
arduinoMockSetMillis(99);
98-
REQUIRE(h.millis() == 99);
121+
REQUIRE(hal.millis() == 99);
99122
}

0 commit comments

Comments
 (0)