Skip to content

Commit

Permalink
fix glimming LED problem (#60) (#61)
Browse files Browse the repository at this point in the history
on ESP32 with LEDs connected as low-active.

See espressif/arduino-esp32#689
  • Loading branch information
jandelgado authored Oct 24, 2020
1 parent 230ea60 commit e476f2d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 18 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# JLed changelog

## [2020-10-24] 4.5.2

* fix: ESP32 led glimming when using low-active connection (#60)

## [2020-07-01] 4.5.1

* fix: support for Nano 33 BLE (#53)

## [2020-02-23] 4.5.0

* new: `JLed::MaxBrightness(uint8_t level)` method to limit output of effects
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=4.5.1
version=4.5.2
author=Jan Delgado <jdelgado[at]gmx.net>
maintainer=Jan Delgado <jdelgado[at]gmx.net>
sentence=An Arduino library to control LEDs
Expand Down
8 changes: 6 additions & 2 deletions src/esp32_hal.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2017 Jan Delgado <jdelgado[at]gmx.net>
// Copyright (c) 2017-2020 Jan Delgado <jdelgado[at]gmx.net>
// https://github.com/jandelgado/jled
// HAL for the ESP32
// https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/ledc.html
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
Expand Down Expand Up @@ -84,7 +86,9 @@ class Esp32Hal {
::ledcSetup(chan_, freq, kLedcTimer8Bit);
::ledcAttachPin(pin, chan_);
}
void analogWrite(uint8_t val) const { ::ledcWrite(chan_, val); }
void analogWrite(uint8_t val) const {
::ledcWrite(chan_, (val == 255)? 256 : val);
}
uint32_t millis() const { return ::millis(); }

PinType chan() const { return chan_; }
Expand Down
53 changes: 38 additions & 15 deletions test/test_esp32_hal.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// JLed Unit tests (run on host).
// Copyright 2017 Jan Delgado [email protected]
// JLed Unit tests for the ESP32 HAL (run on host).
// Copyright 2017-2020 Jan Delgado [email protected]
#include "esp32.h" // NOLINT
#include <esp32_hal.h> // NOLINT
#include "catch.hpp"
Expand Down Expand Up @@ -46,7 +46,7 @@ TEST_CASE("ledc ctor correctly initializes hardware", "[esp32_hal]") {
// attach channel 2 to pin 1
constexpr auto kChan = 5;
constexpr auto kPin = 10;
auto aw[[gnu::unused]] = Esp32Hal(kPin, kChan);
auto hal[[gnu::unused]] = Esp32Hal(kPin, kChan);

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

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

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

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

auto h1 = Esp32Hal(kPin);
auto h2 = Esp32Hal(kPin + 1);
auto hal1 = Esp32Hal(kPin);
auto hal2 = Esp32Hal(kPin + 1);

REQUIRE(h1.chan() != h2.chan());
REQUIRE(hal1.chan() != hal2.chan());
}

TEST_CASE("ledc analogWrite() writes value using analogWrite", "[esp32_hal]") {
TEST_CASE("analogWrite() writes value using analogWrite", "[esp32_hal]") {
esp32MockInit();

// attach channel 2 to pin 1
constexpr auto kChan = 5;
constexpr auto kPin = 10;
auto aw = Esp32Hal(kPin, kChan);
auto hal = Esp32Hal(kPin, kChan);

aw.analogWrite(123);
hal.analogWrite(123);

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

TEST_CASE("analogWrite() writes 0 as 0", "[esp32_hal]") {
esp32MockInit();

// attach channel 2 to pin 1
constexpr auto kChan = 5;
constexpr auto kPin = 10;
auto hal = Esp32Hal(kPin, kChan);

hal.analogWrite(0);
REQUIRE(arduinoMockGetLedcState(kChan) == 0);
}

TEST_CASE("analogWrite() writes 255 as 256", "[esp32_hal]") {
esp32MockInit();

constexpr auto kChan = 5;
constexpr auto kPin = 10;
auto hal = Esp32Hal(kPin, kChan);

hal.analogWrite(255);
REQUIRE(arduinoMockGetLedcState(kChan) == 256);
}

TEST_CASE("millis() returns correct time", "[esp32_hal]") {
arduinoMockInit();
auto h = Esp32Hal(1);
REQUIRE(h.millis() == 0);
auto hal = Esp32Hal(1);
REQUIRE(hal.millis() == 0);
arduinoMockSetMillis(99);
REQUIRE(h.millis() == 99);
REQUIRE(hal.millis() == 99);
}

0 comments on commit e476f2d

Please sign in to comment.