Debinix Team Library LedTask
The Arduino library shows the power of non-preemptive multitasking
Public Member Functions | Static Public Member Functions | Static Public Attributes | Private Attributes | List of all members
LedTask Class Reference

Class that maintain and change state of LED's. More...

#include <LedTask.h>

Public Member Functions

 LedTask (uint16_t pin)
 Set which pin the user LED is attached to. More...
 
 ~LedTask (void)
 Deallocate LedTask object, set data pin back to INPUT.
 
void begin (uint32_t ms_on_tm, uint32_t ms_off_tm)
 Sets the LED on time, and off time respectively. More...
 
void begin (float hz_pwm_frequency=1000)
 Sets the frequency (in Hz) for pwm operation. More...
 
void updateBlinkLed (void)
 If defined time has elapsed, changes the state of the LED. This method is called in the loop() section in Arduino's sketch. More...
 
void updatePwmTask (float percentage_high)
 Updates the PWM signal output. More...
 
void pulseLedBlk (uint8_t pulse_cnt, uint32_t ms_on_tm, uint32_t ms_off_tm)
 Pulses a LED a number of time with set duration. Note: This method uses delay() calls. More...
 

Static Public Member Functions

static uint16_t getInstanceCount (void)
 Get number of instances created. More...
 

Static Public Attributes

static uint16_t instance_count
 counter to keep track of instances More...
 

Private Attributes

uint16_t led_pin
 LED pin.
 
uint32_t on_time
 milliseconds of on-time
 
uint32_t off_time
 milliseconds of off-time
 
int led_state
 led_state (ON/OFF) for LED
 
unsigned long previous_millis
 stores last time LED was updated
 
float pwm_frequency
 repetition frequency for pwm output
 

Detailed Description

Class that maintain and change state of LED's.

Constructor & Destructor Documentation

◆ LedTask()

LedTask::LedTask ( uint16_t  pin)
explicit

Set which pin the user LED is attached to.

Parameters
pinLED pin to toggle ON/OFF.

Member Function Documentation

◆ begin() [1/2]

void LedTask::begin ( uint32_t  ms_on_tm,
uint32_t  ms_off_tm 
)

Sets the LED on time, and off time respectively.

Parameters
ms_on_tmSets the time, in milliseconds for LED light should stay on.
ms_off_tmSets the time, in milliseconds for LED light should stay off.

◆ begin() [2/2]

void LedTask::begin ( float  hz_pwm_frequency = 1000)

Sets the frequency (in Hz) for pwm operation.

Parameters
hz_pwm_frequency

◆ updateBlinkLed()

void LedTask::updateBlinkLed ( void  )

If defined time has elapsed, changes the state of the LED. This method is called in the loop() section in Arduino's sketch.

Example

Each instance of a LedTask requires just three lines of code. Avoid any delay() calls for non-preemptive multitasking.

  • one to declare the instance, on pin 12
  • one to setup timing in setup
  • and one call to update in the loop
#include <LedTask.h>
LedTask LedOne = LedTask(12);
void setup() {
// on_ms,off_ms
LedOne.begin(100, 400);
}
void loop() {
LedOne.updateBlinkLed();
}
Class that maintain and change state of LED's.
Definition: LedTask.h:17
void begin(uint32_t ms_on_tm, uint32_t ms_off_tm)
Sets the LED on time, and off time respectively.
Definition: LedTask.cpp:66
LedTask(uint16_t pin)
Set which pin the user LED is attached to.
Definition: LedTask.cpp:36
void updateBlinkLed(void)
If defined time has elapsed, changes the state of the LED. This method is called in the loop() sectio...
Definition: LedTask.cpp:145

Run four LED tasks like this.

#include <LedTask.h>
LedTask LedOne = LedTask(12);
LedTask LedTwo = LedTask(13);
LedTask LedThree = LedTask(8);
LedTask LedFour = LedTask(7);
void setup() {
// LED time: on_ms,off_ms
LedOne.begin(100, 400);
LedTwo.begin(350, 350);
LedThree.begin(125, 250);
LedFour.begin(500, 400);
}
void loop() {
// Demo of four independent (blinking LED) tasks
LedOne.updateBlinkLed();
LedTwo.updateBlinkLed();
LedThree.updateBlinkLed();
LedFour.updateBlinkLed();
}

◆ updatePwmTask()

void LedTask::updatePwmTask ( float  percentage_high)

Updates the PWM signal output.

Parameters
percentage_highAdjusts the PWM ratio for the high- and the low level output.

◆ pulseLedBlk()

void LedTask::pulseLedBlk ( uint8_t  pulse_cnt,
uint32_t  ms_on_tm,
uint32_t  ms_off_tm 
)

Pulses a LED a number of time with set duration. Note: This method uses delay() calls.

Parameters
pulse_cntThe number of pulses (blinks).
ms_on_tmSets the time, in milliseconds for LED light should stay on.
ms_off_tmSets the time, in milliseconds for LED light should stay off.

◆ getInstanceCount()

uint16_t LedTask::getInstanceCount ( void  )
static

Get number of instances created.

Returns
The instance count, from 1 ... n

Member Data Documentation

◆ instance_count

uint16_t LedTask::instance_count
inlinestatic
Initial value:
=
0

counter to keep track of instances


The documentation for this class was generated from the following files: