A project that aims at making a LED based light system controlled by microcontroller and with BLE (soon) with smooth color transitions.

Dependencies:   mbed

LEDManager.h

Committer:
ledonger
Date:
2017-08-18
Revision:
0:b350f8395bff
Child:
2:0bbd4fc5e202

File content as of revision 0:b350f8395bff:

#ifndef LEDMANAGER_H
#define LEDMANAGER_H

#include "mbed.h"

class LEDManager
{
    public:
        LEDManager(PinName pin, int period_us)
        {
            this->io = new DigitalOut(pin);
            this->period_us = period_us;
            this->prevUpdate = 0;
            this->highTime = this->period_us*0.5;
            this->lowTime = this->period_us - this->highTime;
            this->fadeFactor = 100;
            this->situation = 1;
            this->fadeUpdatePeriod = 10000;   
        }
        
        LEDManager(DigitalOut *io, int period_us)
        {
            this->io = io;
            this->period_us = period_us;
            this->prevUpdate = 0;
            this->highTime = this->period_us*0.5;
            this->lowTime = this->period_us - this->highTime;
            this->fadeFactor = 100;
            this->situation = 1;
            this->fadeUpdatePeriod = 10000;   
        }
        
        void processLED(int time)
        {
            if(situation == 1){
                if((time - prevUpdate) < highTime){
                    io->write(1);
                }
                else{
                    situation = 2;
                    prevUpdate = time;
                }
            }
            if(situation == 2){
                if((time - prevUpdate) < lowTime){
                    io->write(0);
                } 
                else{
                    situation = 1;   
                    prevUpdate = time;
                }  
            }
            
            
            if(time - prevFadeUpdate > fadeUpdatePeriod){
                highTime = highTime + fadeFactor;
                lowTime = period_us - highTime;
                if(highTime >= period_us){
                    fadeFactor = -fadeFactor;   
                }
                else if(highTime <= 0){
                    fadeFactor = -fadeFactor;
                }
                prevFadeUpdate = time;
            }
        }
        
        /*void setFadeUpdatePeriod(int period)
        {
            this->fadeUpdatePeriod = period;   
        }
        void setFadeFactor(int fadeFactor)
        {
            this->fadeFactor = fadeFactor;   
        }*/
        
    private:
        DigitalOut *io;
        int period_us;
        int prevUpdate;
        int highTime;
        int lowTime;
        int situation;
        
        int prevFadeUpdate;
        int fadeFactor;
        int fadeUpdatePeriod;
};

#endif