![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
A project that aims at making a LED based light system controlled by microcontroller and with BLE (soon) with smooth color transitions.
LEDManager.h@0:b350f8395bff, 2017-08-18 (annotated)
- Committer:
- ledonger
- Date:
- Fri Aug 18 08:39:43 2017 +0000
- Revision:
- 0:b350f8395bff
- Child:
- 2:0bbd4fc5e202
FlowerColor v0.1 problem with L073 : LED not synchronized
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ledonger | 0:b350f8395bff | 1 | #ifndef LEDMANAGER_H |
ledonger | 0:b350f8395bff | 2 | #define LEDMANAGER_H |
ledonger | 0:b350f8395bff | 3 | |
ledonger | 0:b350f8395bff | 4 | #include "mbed.h" |
ledonger | 0:b350f8395bff | 5 | |
ledonger | 0:b350f8395bff | 6 | class LEDManager |
ledonger | 0:b350f8395bff | 7 | { |
ledonger | 0:b350f8395bff | 8 | public: |
ledonger | 0:b350f8395bff | 9 | LEDManager(PinName pin, int period_us) |
ledonger | 0:b350f8395bff | 10 | { |
ledonger | 0:b350f8395bff | 11 | this->io = new DigitalOut(pin); |
ledonger | 0:b350f8395bff | 12 | this->period_us = period_us; |
ledonger | 0:b350f8395bff | 13 | this->prevUpdate = 0; |
ledonger | 0:b350f8395bff | 14 | this->highTime = this->period_us*0.5; |
ledonger | 0:b350f8395bff | 15 | this->lowTime = this->period_us - this->highTime; |
ledonger | 0:b350f8395bff | 16 | this->fadeFactor = 100; |
ledonger | 0:b350f8395bff | 17 | this->situation = 1; |
ledonger | 0:b350f8395bff | 18 | this->fadeUpdatePeriod = 10000; |
ledonger | 0:b350f8395bff | 19 | } |
ledonger | 0:b350f8395bff | 20 | |
ledonger | 0:b350f8395bff | 21 | LEDManager(DigitalOut *io, int period_us) |
ledonger | 0:b350f8395bff | 22 | { |
ledonger | 0:b350f8395bff | 23 | this->io = io; |
ledonger | 0:b350f8395bff | 24 | this->period_us = period_us; |
ledonger | 0:b350f8395bff | 25 | this->prevUpdate = 0; |
ledonger | 0:b350f8395bff | 26 | this->highTime = this->period_us*0.5; |
ledonger | 0:b350f8395bff | 27 | this->lowTime = this->period_us - this->highTime; |
ledonger | 0:b350f8395bff | 28 | this->fadeFactor = 100; |
ledonger | 0:b350f8395bff | 29 | this->situation = 1; |
ledonger | 0:b350f8395bff | 30 | this->fadeUpdatePeriod = 10000; |
ledonger | 0:b350f8395bff | 31 | } |
ledonger | 0:b350f8395bff | 32 | |
ledonger | 0:b350f8395bff | 33 | void processLED(int time) |
ledonger | 0:b350f8395bff | 34 | { |
ledonger | 0:b350f8395bff | 35 | if(situation == 1){ |
ledonger | 0:b350f8395bff | 36 | if((time - prevUpdate) < highTime){ |
ledonger | 0:b350f8395bff | 37 | io->write(1); |
ledonger | 0:b350f8395bff | 38 | } |
ledonger | 0:b350f8395bff | 39 | else{ |
ledonger | 0:b350f8395bff | 40 | situation = 2; |
ledonger | 0:b350f8395bff | 41 | prevUpdate = time; |
ledonger | 0:b350f8395bff | 42 | } |
ledonger | 0:b350f8395bff | 43 | } |
ledonger | 0:b350f8395bff | 44 | if(situation == 2){ |
ledonger | 0:b350f8395bff | 45 | if((time - prevUpdate) < lowTime){ |
ledonger | 0:b350f8395bff | 46 | io->write(0); |
ledonger | 0:b350f8395bff | 47 | } |
ledonger | 0:b350f8395bff | 48 | else{ |
ledonger | 0:b350f8395bff | 49 | situation = 1; |
ledonger | 0:b350f8395bff | 50 | prevUpdate = time; |
ledonger | 0:b350f8395bff | 51 | } |
ledonger | 0:b350f8395bff | 52 | } |
ledonger | 0:b350f8395bff | 53 | |
ledonger | 0:b350f8395bff | 54 | |
ledonger | 0:b350f8395bff | 55 | if(time - prevFadeUpdate > fadeUpdatePeriod){ |
ledonger | 0:b350f8395bff | 56 | highTime = highTime + fadeFactor; |
ledonger | 0:b350f8395bff | 57 | lowTime = period_us - highTime; |
ledonger | 0:b350f8395bff | 58 | if(highTime >= period_us){ |
ledonger | 0:b350f8395bff | 59 | fadeFactor = -fadeFactor; |
ledonger | 0:b350f8395bff | 60 | } |
ledonger | 0:b350f8395bff | 61 | else if(highTime <= 0){ |
ledonger | 0:b350f8395bff | 62 | fadeFactor = -fadeFactor; |
ledonger | 0:b350f8395bff | 63 | } |
ledonger | 0:b350f8395bff | 64 | prevFadeUpdate = time; |
ledonger | 0:b350f8395bff | 65 | } |
ledonger | 0:b350f8395bff | 66 | } |
ledonger | 0:b350f8395bff | 67 | |
ledonger | 0:b350f8395bff | 68 | /*void setFadeUpdatePeriod(int period) |
ledonger | 0:b350f8395bff | 69 | { |
ledonger | 0:b350f8395bff | 70 | this->fadeUpdatePeriod = period; |
ledonger | 0:b350f8395bff | 71 | } |
ledonger | 0:b350f8395bff | 72 | void setFadeFactor(int fadeFactor) |
ledonger | 0:b350f8395bff | 73 | { |
ledonger | 0:b350f8395bff | 74 | this->fadeFactor = fadeFactor; |
ledonger | 0:b350f8395bff | 75 | }*/ |
ledonger | 0:b350f8395bff | 76 | |
ledonger | 0:b350f8395bff | 77 | private: |
ledonger | 0:b350f8395bff | 78 | DigitalOut *io; |
ledonger | 0:b350f8395bff | 79 | int period_us; |
ledonger | 0:b350f8395bff | 80 | int prevUpdate; |
ledonger | 0:b350f8395bff | 81 | int highTime; |
ledonger | 0:b350f8395bff | 82 | int lowTime; |
ledonger | 0:b350f8395bff | 83 | int situation; |
ledonger | 0:b350f8395bff | 84 | |
ledonger | 0:b350f8395bff | 85 | int prevFadeUpdate; |
ledonger | 0:b350f8395bff | 86 | int fadeFactor; |
ledonger | 0:b350f8395bff | 87 | int fadeUpdatePeriod; |
ledonger | 0:b350f8395bff | 88 | }; |
ledonger | 0:b350f8395bff | 89 | |
ledonger | 0:b350f8395bff | 90 | #endif |