geekbrother / LedWinker

Blink LED, change LED state async (without delay) PlatformIO and Arduino library.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

LedWinker

Build with PlatformIO

This is a tiny Platform.io (Arduino) library to control state of LED(s) in async manner.

Blink your LED (FAST or SLOW), ON, OFF. Change LED state in simple async manner without delays or blocking the main thread.

Installation:

PlatfromIO:

You can install library as a project dependency by adding it to platformio.ini:

[env:myenv]
platform = ...
framework = ...
board = ...
lib_deps =
  LedWinker
  ...

Usage:

Initialization:

First of all you need to initialize your LED(s) for LedWinker by creating an instance of LedWinker. You need to pass LED GPIO number as a paramater for constructor (see example below). You can initialize as many LEDs as you wish by creating as many instances, they all will work async.

States:

You can change the state of the LED by call Wink(STATE) method of the instance. STATE is the ENUM which includes LED_ON, LED_OFF, LED_FAST, LED_SLOW states. Calling function doesn't hang main thread or make any delay. Changing state(s) will be full async.

You can get current state by calling GetState() method.

Loop:

To work properly library needs to observe the main loop. Thats why you need to call Loop() method of each LedWink instance inside main loop() function of the program (see example below).

Usage example:

// LedWinker async LED control library usage example.
//
// Example program listen for commands from serial console to control the LED.
// Available commands: ON, OFF, SLOW, FAST
//
// Tested with Platform.io and Arduino
// Author: Max Kalashnikov
// Git Repo: https://github.com/geekbrother/LedWinker

#include <Arduino.h>
#include <LedWinker.hpp>


#define LED_GPIO 13
// Initialize winker on LED GPIO number
LedWinker winker(LED_GPIO);

// serial incoming message
String incoming;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("Start Winkering ;)");
  Serial.println("Enter one of the state: ON | OFF | SLOW | FAST:");
}

void loop() {
  // put your main code here, to run repeatedly:
  // You need to put winker.Loop() inside your main loop() to observe the changes.
  winker.Loop();

  // Listen to commands from serial
  // You can use ON, OFF, FAST (blinking), SLOW (blinking)
  if (Serial.available() > 0) {
    // read the incoming
    incoming = Serial.readString();
    // say what you got:
    Serial.println(incoming);
    // change led state depend on command
    if (incoming == "ON") winker.Wink(LED_ON); // ON LED
    if (incoming == "OFF") winker.Wink(LED_OFF); // OFF LED
    if (incoming == "FAST") winker.Wink(LED_FAST); // Blink Fast
    if (incoming == "SLOW") winker.Wink(LED_SLOW); // Blink Slow
  } 
}

About

Blink LED, change LED state async (without delay) PlatformIO and Arduino library.

License:Apache License 2.0


Languages

Language:C++ 100.0%