Skip to content
/ jled Public
forked from jandelgado/jled

Non-blocking LED controlling library for Arduino and friends.

License

Notifications You must be signed in to change notification settings

Astymeus/jled

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JLed - Extended LED Library

Build Status

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

// blink and breathe two LEDs (builtin and gpio 9) for 12 seconds.
#include <jled.h>

JLed led_breathe = JLed(9).Breathe(1500).Repeat(6).DelayAfter(500);
JLed led_blink = JLed(LED_BUILTIN).Blink(500, 500).Repeat(11).DelayBefore(1000);

void setup() { }

void loop() {
  led_blink.Update();
  led_breathe.Update();
}

Contents

Features

  • non-blocking
  • simple on/off
  • breathe effect
  • blinking effect
  • fade-on and -off effect
  • user provided effects
  • supports reversed polarity of LED
  • easy configuration using fluent interface

Usage

First the LED object is constructed and configured, then the state is updated with subsequent calls to the Update() method, typically from the loop() function. The constructor takes the pin, to which the LED is connected to as only parameter. Further configuration of the LED object is done using a fluent interface, e.g. JLed led = JLed(13).Breathe(2000).DelayAfter(1000).Repeat(5). See examples and Parameter overview section below for further details.

Static on

Calling On() turns the LED on on after an optional time, specified by DelayBefore(), has elapsed. To immediately turn a LED on, make a call like JLed(LED_BUILTIN).On().Update().

Static on example

#include <jled.h>

// turn builtin LED on after 1 second.
JLed led = JLed(LED_BUILTIN).On().DelayBefore(1000);

void setup() { }

void loop() {
  led.Update();
}

Static off

Off() works like On(), except that it turns the LED off.

Blinking

In blinking mode, the LED cycles through a given number of on-off cycles.

Blinking example

#include <jled.h>

// blink internal LED every second.
JLed led = JLed(LED_BUILTIN).Blink(1000, 1000).Forever();

void setup() { }

void loop() {
  led.Update();
}

Breathing

In breathing mode, the LED smoothly changes brightness using PWM.

Breathing example

#include <jled.h>

// connect LED to pin 13 (PWM capable). LED will breathe with period of
// 2000ms and a delay of 1000ms after each period.
JLed led = JLed(13).Breathe(2000).DelayAfter(1000).Forever();

void setup() { }

void loop() {
  led.Update();
}

FadeOn

In FadeOn mode, the LED is smoothly faded on to 100% brightness using PWM.

FadeOn example

#include <jled.h>

// LED is connected to pin 9 (PWM capable) gpio
JLed led = JLed(9).FadeOn(1000).DelayBefore(2000);

void setup() { }

void loop() {
  led.Update();
}

FadeOff

In FadeOff mode, the LED is smoothly faded off using PWM. The fade starts at 100% brightness.

User provided brightness function

It is also possible to provide a user defined brightness function. The signature of such a function is unit8_t func(unit32_t t, uint16_t period). The function must return the brightness in range 0..255 in dependence of the current time t.

User provided brightness function example

The example uses a lambda function to calculate the brightness.

#include <jled.h>

// this function returns changes between 0 and 255 and vice versa every 250 ms.
auto blinkFunc = [] (uint32_t t, uint16_t) -> uint8_t {return 255*((t/250)%2);};

// Run blinkUserFunc for 5000ms
JLed led = JLed(LED_BUILTIN).UserFunc(blinkFunc, 5000);

void setup() { } 

void loop() {
  led.Update();
}

Immediate Stop

Call Stop() to immediately turn the LED off and stop any running effects.

Parameter overview

The following table shows the applicability of the various parameters in dependence of the chosen effect:

Method Description Default On Off Blink Breath FadeOn FadeOff UserFunc
DelayBefore(t) time to wait before state is initially changed 0 Yes Yes Yes Yes Yes Yes Yes
DelayAfter(t) time to wait after each period 0 Yes Yes Yes Yes
Repeat(n) repeat effect for given number of periods 1 Yes Yes Yes Yes Yes
Forever() repeat infinitely false Yes Yes Yes Yes Yes
LowActive() set output to be low-active (i.e. invert output) false Yes Yes Yes Yes Yes Yes Yes
  • all times are specified in milliseconds
  • time specified by DelayBefore() is relative to first invocation of Update()

Author

Jan Delgado, jdelgado[at]gmx.net.

License

MIT

About

Non-blocking LED controlling library for Arduino and friends.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 98.8%
  • Other 1.2%