Skip to content

Commit fe48533

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

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

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: 30 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,55 @@ 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 and 255 as 256", "[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+
hal.analogWrite(255);
104+
REQUIRE(arduinoMockGetLedcState(kChan) == 256);
105+
}
106+
107+
93108
TEST_CASE("millis() returns correct time", "[esp32_hal]") {
94109
arduinoMockInit();
95-
auto h = Esp32Hal(1);
96-
REQUIRE(h.millis() == 0);
110+
auto hal = Esp32Hal(1);
111+
REQUIRE(hal.millis() == 0);
97112
arduinoMockSetMillis(99);
98-
REQUIRE(h.millis() == 99);
113+
REQUIRE(hal.millis() == 99);
99114
}

0 commit comments

Comments
 (0)