Skip to content

Commit

Permalink
Add support for mbed framework (jandelgado#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
jandelgado authored Feb 20, 2020
1 parent 7219c3a commit 45510b6
Show file tree
Hide file tree
Showing 30 changed files with 460 additions and 177 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.pio/
**/.vscode
test/bin
test/.depend
Expand Down
3 changes: 3 additions & 0 deletions .mbedignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test/*
examples/*
doc/*
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.4.0

* JLed now support the mbed framework. See README.md and `examples/multiled_mbed`
for examples.

## [2019-09-21] 4.3.0

* new example: [custom HAL](examples/custom_hal/custom_hal.ino) showing
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

# some of the examples use LED_BUILTIN which is not defined for ESP32
CIOPTS=--board=uno --board=esp01 --lib="src"
CIOPTS_MBED=--board=nucleo_f401re -Oframework=mbed --lib="src"
CIOPTSALL=--board=esp32dev --board=uno --board=esp01 --lib="src"

all:
Expand All @@ -14,6 +15,8 @@ lint:
-type f -a \( -name "*\.cpp" -o -name "*\.h" -o -name "*\.ino" \) )

ci:
platformio ci $(CIOPTS) examples/custom_hal/custom_hal.ino
platformio ci $(CIOPTS_MBED) examples/multiled_mbed/multiled_mbed.cpp
platformio ci $(CIOPTS) --lib="examples/morse" examples/morse/morse.ino
platformio ci $(CIOPTS) examples/candle/candle.ino
platformio ci $(CIOPTS) examples/multiled/multiled.ino
Expand Down
71 changes: 49 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![Build Status](https://travis-ci.org/jandelgado/jled.svg?branch=master)](https://travis-ci.org/jandelgado/jled)
[![Coverage Status](https://coveralls.io/repos/github/jandelgado/jled/badge.svg?branch=master&dummy=1)](https://coveralls.io/github/jandelgado/jled?branch=master)

An Arduino library to control LEDs. It uses a **non-blocking** approach and can
A library to control LEDs. It uses a **non-blocking** approach and can
control LEDs in simple (**on**/**off**) and complex (**blinking**,
**breathing** and more) ways in a **time-driven** manner.

Expand Down Expand Up @@ -63,10 +63,12 @@ void loop() {
* [Misc functions](#misc-functions)
* [Low active for inverted output](#low-active-for-inverted-output)
* [Controlling a group of LEDs](#controlling-a-group-of-leds)
* [Framework notes](#framework-notes)
* [Platform notes](#platform-notes)
* [ESP8266](#esp8266)
* [ESP32](#esp32)
* [STM32](#stm32)
* [Arduino framework](#arduino-framework)
* [Example sketches](#example-sketches)
* [PlatformIO](#platformio-1)
* [Arduino IDE](#arduino-ide-1)
Expand All @@ -75,7 +77,7 @@ void loop() {
* [Unit tests](#unit-tests)
* [Contributing](#contributing)
* [FAQ](#faq)
* [How do I check if an JLed object is still being updated?](#how-do-i-check-if-an-jled-object-is-still-being-updated)
* [How do I check if a JLed object is still being updated?](#how-do-i-check-if-a-jled-object-is-still-being-updated)
* [How do I restart an effect?](#how-do-i-restart-an-effect)
* [How do I change a running effect?](#how-do-i-change-a-running-effect)
* [Author and Copyright](#author-and-copyright)
Expand All @@ -91,7 +93,7 @@ void loop() {
* easy configuration using fluent interface
* can control groups of LEDs sequentially or in parallel
* Arduino, ESP8266 and ESP32 platform compatible
* portable
* supports Arduino and [mbed](www.mbed.com) frameworks
* well [tested](https://coveralls.io/github/jandelgado/jled)

## Cheat Sheet
Expand Down Expand Up @@ -386,6 +388,34 @@ The `JLedSequence` provides the following methods:
* `Reset()` - Resets all `JLed` objects controlled by the sequence and
the sequence, resulting in a start-over.

## Framework notes

JLed supports the Arduino and [mbed](https://www.mbed.org) frameworks. When
using platformio, the framework to be used is configured in the `platform.ini`
file, as shown in the following example, which for example selects the `mbed`
framework:

```ini
[env:nucleo_f401re_mbed]
platform=ststm32
board = nucleo_f401re
framework = mbed
build_flags = -Isrc
src_filter = +<../../src/> +<./>
upload_protocol=stlink
```

An [mbed example is provided here](examples/multiled_mbed/multiled_mbed.cpp).
To compile it for the F401RE, make your [plaform.ini](platform.ini) look like:

```ini
...
[platformio]
default_envs = nucleo_f401re_mbed
src_dir = examples/multiled_mbed
...
```

## Platform notes

### ESP8266
Expand All @@ -409,40 +439,38 @@ automatically picks the next free channel, starting with channel 0 and wrapping
over after channel 15. To manually specify a channel, the JLed object must be
constructed this way:

```
```c++
auto esp32Led = JLed(Esp32Hal(2, 7)).Blink(1000, 1000).Forever();
```

The `Esp32Hal(pin, chan)` constructor takes the pin number as the first
argument and the ESP32 ledc channel number on the second position. Note that using
the above-mentioned constructor yields non-platform independent code, so it
should be avoided and is normally not necessary.
argument and the ESP32 ledc channel number on the second position. Note that
using the above-mentioned constructor yields non-platform independent code, so
it should be avoided and is normally not necessary.

### STM32

#### Arduino framework

I had success running JLed on a [STM32 Nucleo64 F401RE
board](https://www.st.com/en/evaluation-tools/nucleo-f401re.html) using this
[STM32 Arduino
core](https://github.com/rogerclarkmelbourne/Arduino_STM32/tree/master/STM32F4)
and compiling from the Arduino IDE. Note that the `stlink` tool might be
and compiling examples from the Arduino IDE. Note that the `stlink` is
necessary to upload sketches to the microcontroller.

PlatformIO does not support the Arduino platform for the F401RE in the [current
version](https://docs.platformio.org/en/latest/boards/ststm32/nucleo_f401re.html),
but has support on the [master
branch](https://github.com/platformio/platform-ststm32.git). See
[plaform.ini](platform.ini) for an example on how to use the Arduino framework
with this board.
## Example sketches

Example sketches are provided in the [examples](examples/) directory.

* [Hello, world](examples/hello)
* [Turn LED on after a delay](examples/simple_on)
* [Breathe effect](examples/breathe)
* [Candle effect](examples/candle)
* [Fade LED on](examples/fade_on)
* [Fade LED off](examples/fade_off)
* [Controlling multiple LEDs in parallel](examples/multiled)
* [Controlling multiple LEDs in parallel (mbed)](examples/multiled_mbed)
* [Controlling multiple LEDs sequentially](examples/sequence)
* [Simple User provided effect](examples/user_func)
* [Morsecode example](examples/morse)
Expand All @@ -455,12 +483,10 @@ uncomment the example to be built in the [platformio.ini](platformio.ini)
project file, e.g.:

```ini
...
[platformio]
; uncomment example to build
src_dir = examples/hello
;src_dir = examples/breathe
...
```

### Arduino IDE
Expand All @@ -472,11 +498,12 @@ the `File` > `Examples` > `JLed` menu.

### Support new hardware

JLed uses a very thin hardware abstraction layer (hal) to abstract access to the
actual used MCU (e.g. ESP32, ESP8266). The hal objects encapsulate access to
the GPIO and time functionality of the MCU under the framework being used.
During the unit test, mocked hal instances are used, enabling tests to check the
generated output.
JLed uses a very thin hardware abstraction layer (hal) to abstract access to
the actual MCU/framework used (e.g. ESP32, ESP8266). The hal object encapsulate
access to the GPIO and time functionality of the MCU under the framework being
used. During the unit test, mocked hal instances are used, enabling tests to
check the generated output. The [Custom HAL project](examples/custom_hal)
provides an example for a user define HAL.

## Unit tests

Expand Down
Binary file added doc/multiled_mbed.fzz
Binary file not shown.
Binary file added doc/multiled_mbed_bb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 15 additions & 9 deletions examples/custom_hal/custom_hal.ino
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@
#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() {}

// the speed. In general, a JLed HAL class must satisfy the following
// interface:
//
// class JledHal {
// public:
// JledHal(PinType pin);
// void analogWrite(uint8_t val) const;
// uint32_t millis() const;
// }
//
class CustomHal {
public:
explicit CustomHal(uint8_t pin) noexcept : pin_(pin) {}
using PinType = uint8_t;

explicit CustomHal(PinType pin) noexcept : pin_(pin) {}

void analogWrite(uint8_t val) const {
// some platforms, e.g. STM need lazy initialization
Expand All @@ -30,7 +36,7 @@ class CustomHal : jled::JLedHal {

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

class JLed : public jled::TJLed<CustomHal, JLed> {
Expand Down
16 changes: 16 additions & 0 deletions examples/multiled_mbed/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Multi LED example for mbed

This example controls 4+1 LEDs, showing different effects, yet all synchronized:

* blue LED: breathe (period 2s)
* green LED: blink (0.75s on/0.25s off)
* red LED: fade off (period 1s)
* yellow LED: fade on (period 1s)
* built-in LED: blink (0.5s on/0.5s off)

## Wiring

The example uses a STM32 Nucleo F401RE and is wired like shown:

![mutliled](../../doc/multiled_mbed_bb.png)

27 changes: 27 additions & 0 deletions examples/multiled_mbed/multiled_mbed.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// JLed multi LED demo for mbed. Controls multiple LEDs parallel in-sync.
//
// mbed version tested with ST Nucleo F401RE
//
// See https://os.mbed.com/platforms/ST-Nucleo-F401RE/ for pin names and
// assignments.
//
// Copyright 2020 by Jan Delgado. All rights reserved.
// https://github.com/jandelgado/jled
//
#include <jled.h>
#include <mbed.h>

int main() {
JLed leds[] = {JLed(LED1).Blink(750, 250).Forever(),
JLed(PA_8).Breathe(2000).Forever(),
JLed(PB_10).FadeOff(1000).Forever(),
JLed(PB_4).FadeOn(1000).Forever(),
JLed(PB_3).Blink(500, 500).Forever()};

JLedSequence sequence(JLedSequence::eMode::PARALLEL, leds);

while (1) {
sequence.Update();
}
return 0;
}
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.2.1
version=4.4.0
author=Jan Delgado <jdelgado[at]gmx.net>
maintainer=Jan Delgado <jdelgado[at]gmx.net>
sentence=An Arduino library to control LEDs
Expand Down
29 changes: 21 additions & 8 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,25 @@
; http://docs.platformio.org/page/projectconf.html

[platformio]
env_default = nanoatmega328
;env_default = nucleo_f401re
;env_default = esp8266
;env_default = esp32
;env_default = sparkfun_samd21_dev_usb
; uncomment platform to build for
;default_envs = nucleo_f401re_mbed
;default_envs = nucleo_f401re
default_envs = nanoatmega328
;default_envs = nucleo_f401re
;default_envs = esp8266
;default_envs = esp32
;default_envs = sparkfun_samd21_dev_usb

; uncomment example to build
src_dir = examples/hello
;src_dir = examples/hello
;src_dir = examples/morse
;src_dir = examples/breathe
src_dir = examples/breathe
;src_dir = examples/candle
;src_dir = examples/fade_on
;src_dir = examples/fade_off
;src_dir = examples/simple_on
;src_dir = examples/multiled
;src_dir = examples/multiled_mbed
;src_dir = examples/user_func
;src_dir = examples/sequence
;src_dir = examples/custom_hal
Expand All @@ -35,9 +39,18 @@ framework = arduino
build_flags = -Isrc
src_filter = +<../../src/> +<./>

[env:nucleo_f401re_mbed]
platform=ststm32
board = nucleo_f401re
framework = mbed
build_flags = -Isrc
src_filter = +<../../src/> +<./>
upload_protocol=stlink

[env:nucleo_f401re]
# nucleo f401re arduino framework support only on master at the moment
platform=https://github.com/platformio/platform-ststm32.git
platform=ststm32
;platform=https://github.com/platformio/platform-ststm32.git
board = nucleo_f401re
framework = arduino
build_flags = -Isrc
Expand Down
14 changes: 5 additions & 9 deletions src/arduino_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,14 @@
#define SRC_ARDUINO_HAL_H_

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

namespace jled {

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

class ArduinoHal {
public:
explicit ArduinoHal(uint8_t pin) noexcept : pin_(pin) {}
using PinType = uint8_t;

explicit ArduinoHal(PinType pin) noexcept : pin_(pin) {}

void analogWrite(uint8_t val) const {
// some platforms, e.g. STM need lazy initialization
Expand All @@ -49,7 +45,7 @@ class ArduinoHal : JLedHal {

private:
mutable bool setup_ = false;
uint8_t pin_;
PinType pin_;
};
} // namespace jled
#endif // SRC_ARDUINO_HAL_H_
Loading

0 comments on commit 45510b6

Please sign in to comment.