Skip to content

Commit

Permalink
Custom HAL example (jandelgado#33)
Browse files Browse the repository at this point in the history
* new custom hal example
* prepare release
* fix goveralls invocation
  • Loading branch information
jandelgado authored Nov 6, 2019
1 parent 75c093c commit 7219c3a
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ script:
- make ci
- cd test && make clean test OPT=-O2
- make clean coverage OPT=-O0
- coveralls-lcov --repo-token "$COVERALLS_TOKEN" coverage.info
- 'if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then coveralls-lcov --repo-token "$COVERALLS_TOKEN" coverage.info; fi'
- 'if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then coveralls-lcov coverage.info; fi'

env:
global:
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# JLed changelog

## [2019-09-21] 4.3.0

* new example: [custom HAL](examples/custom_hal/custom_hal.ino) showing
how to implment a custom HAL.

## [2019-08-30] 4.2.1

* fix: make sure memory alignment is correct (caused hard fault on
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ Example sketches are provided in the [examples](examples/) directory.
* [Controlling multiple LEDs sequentially](examples/sequence)
* [Simple User provided effect](examples/user_func)
* [Morsecode example](examples/morse)
* [Custom HAL example](examples/custom_hal)
### PlatformIO
Expand Down
45 changes: 45 additions & 0 deletions examples/custom_hal/custom_hal.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// JLed custom HAL example.
// Copyright 2019 by Jan Delgado. All rights reserved.
// https://github.com/jandelgado/jled

// we include jled_base.h instead of "jled.h" since we define our own JLed
// class using our custom HAL.
#include <jled_base.h>

// a custom HAL for the Arduino, inverting output and ticking with half
// the speed.
class CustomHal : jled::JLedHal {
private:
template <typename T, typename B>
friend class jled::TJLed;
CustomHal() {}

public:
explicit CustomHal(uint8_t pin) noexcept : pin_(pin) {}

void analogWrite(uint8_t val) const {
// some platforms, e.g. STM need lazy initialization
if (!setup_) {
::pinMode(pin_, OUTPUT);
setup_ = true;
}
::analogWrite(pin_, 255 - val);
}

uint32_t millis() const { return ::millis() >> 1; }

private:
mutable bool setup_ = false;
uint8_t pin_;
};

class JLed : public jled::TJLed<CustomHal, JLed> {
using jled::TJLed<CustomHal, JLed>::TJLed;
};

// uses above defined CustomHal
auto led = JLed(LED_BUILTIN).Blink(1000, 1000).Repeat(5);

void setup() {}

void loop() { led.Update(); }
1 change: 1 addition & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ src_dir = examples/hello
;src_dir = examples/multiled
;src_dir = examples/user_func
;src_dir = examples/sequence
;src_dir = examples/custom_hal

[env:nanoatmega328]
platform = atmelavr
Expand Down
6 changes: 5 additions & 1 deletion src/arduino_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@
namespace jled {

class ArduinoHal : JLedHal {
public:
private:
template <typename T, typename B>
friend class TJLed;
ArduinoHal() {}

public:
explicit ArduinoHal(uint8_t pin) noexcept : pin_(pin) {}

void analogWrite(uint8_t val) const {
Expand Down
12 changes: 8 additions & 4 deletions src/esp32_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#define SRC_ESP32_HAL_H_

#include <Arduino.h>
#include "jled_hal.h" // NOLINT

namespace jled {

Expand Down Expand Up @@ -50,7 +51,7 @@ class Esp32ChanMapper {
// no more free slots, start over
auto i = nextChan_;
chanMap_[i] = pin;
nextChan_ = (nextChan_+1) % kLedcMaxChan;
nextChan_ = (nextChan_ + 1) % kLedcMaxChan;
return i;
}

Expand All @@ -59,14 +60,17 @@ class Esp32ChanMapper {
uint8_t chanMap_[kLedcMaxChan];
};

class Esp32Hal {
class Esp32Hal : JLedHal {
private:
template <typename T, typename B>
friend class TJLed;
Esp32Hal() {}

static constexpr auto kLedcTimer8Bit = 8;

public:
static constexpr auto kAutoSelectChan = -1;

Esp32Hal() {}

// construct an ESP32 analog write object connected to the given pin.
// chan specifies the EPS32 ledc channel to use. If set to kAutoSelectChan,
// the next available channel will be used, otherwise the specified one.
Expand Down
11 changes: 8 additions & 3 deletions src/esp8266_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,25 @@
#define SRC_ESP8266_HAL_H_

#include <Arduino.h>
#include "jled_hal.h" // NOLINT

namespace jled {
class Esp8266Hal /*: public AnalogWriter */ {
public:

class Esp8266Hal : JLedHal {
private:
template <typename T, typename B>
friend class TJLed;
Esp8266Hal() {}

public:
explicit Esp8266Hal(uint8_t pin) noexcept : pin_(pin) {
::pinMode(pin_, OUTPUT);
}
void analogWrite(uint8_t val) const {
// ESP8266 uses 10bit PWM range per default, scale value up
::analogWrite(pin_, Esp8266Hal::ScaleTo10Bit(val));
}
uint32_t millis() const {return ::millis();}
uint32_t millis() const { return ::millis(); }

protected:
// scale an 8bit value to 10bit: 0 -> 0, ..., 255 -> 1023,
Expand Down

0 comments on commit 7219c3a

Please sign in to comment.