![](/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@2:0bbd4fc5e202, 2017-08-18 (annotated)
- Committer:
- ledonger
- Date:
- Fri Aug 18 11:26:01 2017 +0000
- Revision:
- 2:0bbd4fc5e202
- Parent:
- 0:b350f8395bff
- Child:
- 3:25af55580ef6
Bug Fix => Variables uninitialized
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 | 2:0bbd4fc5e202 | 19 | this->prevFadeUpdate = 0; |
ledonger | 0:b350f8395bff | 20 | } |
ledonger | 0:b350f8395bff | 21 | |
ledonger | 0:b350f8395bff | 22 | LEDManager(DigitalOut *io, int period_us) |
ledonger | 0:b350f8395bff | 23 | { |
ledonger | 0:b350f8395bff | 24 | this->io = io; |
ledonger | 0:b350f8395bff | 25 | this->period_us = period_us; |
ledonger | 0:b350f8395bff | 26 | this->prevUpdate = 0; |
ledonger | 0:b350f8395bff | 27 | this->highTime = this->period_us*0.5; |
ledonger | 0:b350f8395bff | 28 | this->lowTime = this->period_us - this->highTime; |
ledonger | 0:b350f8395bff | 29 | this->fadeFactor = 100; |
ledonger | 0:b350f8395bff | 30 | this->situation = 1; |
ledonger | 0:b350f8395bff | 31 | this->fadeUpdatePeriod = 10000; |
ledonger | 0:b350f8395bff | 32 | } |
ledonger | 0:b350f8395bff | 33 | |
ledonger | 0:b350f8395bff | 34 | void processLED(int time) |
ledonger | 0:b350f8395bff | 35 | { |
ledonger | 0:b350f8395bff | 36 | if(situation == 1){ |
ledonger | 0:b350f8395bff | 37 | if((time - prevUpdate) < highTime){ |
ledonger | 0:b350f8395bff | 38 | io->write(1); |
ledonger | 0:b350f8395bff | 39 | } |
ledonger | 0:b350f8395bff | 40 | else{ |
ledonger | 0:b350f8395bff | 41 | situation = 2; |
ledonger | 0:b350f8395bff | 42 | prevUpdate = time; |
ledonger | 0:b350f8395bff | 43 | } |
ledonger | 0:b350f8395bff | 44 | } |
ledonger | 0:b350f8395bff | 45 | if(situation == 2){ |
ledonger | 0:b350f8395bff | 46 | if((time - prevUpdate) < lowTime){ |
ledonger | 0:b350f8395bff | 47 | io->write(0); |
ledonger | 0:b350f8395bff | 48 | } |
ledonger | 0:b350f8395bff | 49 | else{ |
ledonger | 0:b350f8395bff | 50 | situation = 1; |
ledonger | 0:b350f8395bff | 51 | prevUpdate = time; |
ledonger | 0:b350f8395bff | 52 | } |
ledonger | 0:b350f8395bff | 53 | } |
ledonger | 0:b350f8395bff | 54 | |
ledonger | 0:b350f8395bff | 55 | |
ledonger | 0:b350f8395bff | 56 | if(time - prevFadeUpdate > fadeUpdatePeriod){ |
ledonger | 0:b350f8395bff | 57 | highTime = highTime + fadeFactor; |
ledonger | 0:b350f8395bff | 58 | lowTime = period_us - highTime; |
ledonger | 0:b350f8395bff | 59 | if(highTime >= period_us){ |
ledonger | 0:b350f8395bff | 60 | fadeFactor = -fadeFactor; |
ledonger | 0:b350f8395bff | 61 | } |
ledonger | 0:b350f8395bff | 62 | else if(highTime <= 0){ |
ledonger | 0:b350f8395bff | 63 | fadeFactor = -fadeFactor; |
ledonger | 0:b350f8395bff | 64 | } |
ledonger | 0:b350f8395bff | 65 | prevFadeUpdate = time; |
ledonger | 0:b350f8395bff | 66 | } |
ledonger | 0:b350f8395bff | 67 | } |
ledonger | 0:b350f8395bff | 68 | |
ledonger | 0:b350f8395bff | 69 | /*void setFadeUpdatePeriod(int period) |
ledonger | 0:b350f8395bff | 70 | { |
ledonger | 0:b350f8395bff | 71 | this->fadeUpdatePeriod = period; |
ledonger | 0:b350f8395bff | 72 | } |
ledonger | 0:b350f8395bff | 73 | void setFadeFactor(int fadeFactor) |
ledonger | 0:b350f8395bff | 74 | { |
ledonger | 0:b350f8395bff | 75 | this->fadeFactor = fadeFactor; |
ledonger | 0:b350f8395bff | 76 | }*/ |
ledonger | 2:0bbd4fc5e202 | 77 | int getHighTime() |
ledonger | 2:0bbd4fc5e202 | 78 | { |
ledonger | 2:0bbd4fc5e202 | 79 | return this->highTime; |
ledonger | 2:0bbd4fc5e202 | 80 | } |
ledonger | 0:b350f8395bff | 81 | |
ledonger | 0:b350f8395bff | 82 | private: |
ledonger | 0:b350f8395bff | 83 | DigitalOut *io; |
ledonger | 0:b350f8395bff | 84 | int period_us; |
ledonger | 0:b350f8395bff | 85 | int prevUpdate; |
ledonger | 0:b350f8395bff | 86 | int highTime; |
ledonger | 0:b350f8395bff | 87 | int lowTime; |
ledonger | 0:b350f8395bff | 88 | int situation; |
ledonger | 0:b350f8395bff | 89 | |
ledonger | 0:b350f8395bff | 90 | int prevFadeUpdate; |
ledonger | 0:b350f8395bff | 91 | int fadeFactor; |
ledonger | 0:b350f8395bff | 92 | int fadeUpdatePeriod; |
ledonger | 0:b350f8395bff | 93 | }; |
ledonger | 0:b350f8395bff | 94 | |
ledonger | 0:b350f8395bff | 95 | #endif |