Skip to content

Commit 847e619

Browse files
committed
Fine tune Dimmer for more convenient usage.
1 parent 474d27a commit 847e619

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed

src/Dimmer.cpp

+14-7
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,20 @@
2727
#include "SoftTimer.h"
2828
#include "Dimmer.h"
2929

30-
Dimmer::Dimmer(SoftPwmTask* pwm, int frequencyMs) : Task(10, &(Dimmer::step))
30+
Dimmer::Dimmer(SoftPwmTask* pwm, int frequencyMs, byte stepCount) : Task(10, &(Dimmer::step))
3131
{
3232
this->_pwm = pwm;
3333
this->direction = DIMMER_DIRECTION_HIGH;
3434
this->value = 0;
3535
this->_pwm->analogWrite((byte)this->value);
36-
this->stepCount = DEFAULT_STEP_COUNT;
36+
this->stepCount = stepCount;
3737

3838
this->setFrequency(frequencyMs);
3939
}
4040

4141

42-
void Dimmer::startPulsate() {
43-
this->stopOnLimit = false;
42+
void Dimmer::start(boolean stopOnLimit) {
43+
this->stopOnLimit = stopOnLimit;
4444
SoftTimer.add(this->_pwm);
4545
SoftTimer.add(this);
4646
}
@@ -52,8 +52,16 @@ void Dimmer::hold() {
5252
void Dimmer::off() {
5353
this->hold();
5454
this->_pwm->off();
55-
SoftTimer.remove(this);
55+
this->value = 0;
5656
SoftTimer.remove(this->_pwm);
57+
this->direction = DIMMER_DIRECTION_HIGH;
58+
}
59+
60+
void Dimmer::on() {
61+
this->hold();
62+
this->value = this->_pwm->upperLimit;
63+
this->_pwm->analogWrite(this->_pwm->upperLimit);
64+
this->direction = DIMMER_DIRECTION_LOW;
5765
}
5866

5967
void Dimmer::revertDirection() {
@@ -63,7 +71,6 @@ void Dimmer::revertDirection() {
6371
void Dimmer::setFrequency(int frequencyMs) {
6472
this->_stepLevel = (float)this->_pwm->upperLimit / (float)this->stepCount;
6573
this->periodMicros = (float)frequencyMs * 500.0 / (float)this->stepCount;
66-
6774
}
6875

6976
byte Dimmer::getUpperLimit() {
@@ -95,4 +102,4 @@ void Dimmer::step(Task* task)
95102
}
96103

97104
}
98-
105+

src/Dimmer.h

+18-5
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,36 @@ class Dimmer : public Task
4343
* We use the SoftPwmTask for dimming.
4444
* pwm - The predefined SoftPwm task.
4545
* frequencyMs - Milliseconds will be passed in the OFF->ON->OFF cicle.
46+
* stepCount - Steps should be perform between a full ON-OFF state.
4647
*/
47-
Dimmer(SoftPwmTask* pwm, int frequencyMs);
48+
Dimmer(SoftPwmTask* pwm, int frequencyMs, byte stepCount = DEFAULT_STEP_COUNT);
4849

4950
/**
5051
* Start an unlimited pulsation from the current value on, in the current direction.
5152
*/
52-
void startPulsate();
53+
void start(byte direction, byte stopOnLimit);
54+
55+
/**
56+
* Start dimming from the current value on, in the current direction.
57+
* stopOnLimit - An unlimited pulsation is starting.
58+
*/
59+
void start(boolean stopOnLimit);
60+
void startPulsate() { this->start(false); }
5361

5462
/**
5563
* Hold current PWM value on the output.
5664
*/
5765
void hold();
5866

67+
/**
68+
* Stop PWM, and set output to LOW.
69+
*/
70+
void off();
71+
5972
/**
60-
* Stop PWM, and set output to LOW.
73+
* Stop PWM, and set output to pwm->upperLimit.
6174
*/
62-
void off();
75+
void on();
6376

6477
/**
6578
* Make the dimming to change direction.
@@ -103,4 +116,4 @@ class Dimmer : public Task
103116
};
104117

105118
#endif
106-
119+

src/SoftPwmTask.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,17 @@ class SoftPwmTask : public Task
4848
* Turns the output to low.
4949
*/
5050
void off();
51+
52+
/**
53+
* Sets the PWM frequency (count of ON-OFFs per second).
54+
*/
5155
void setFrequency(unsigned long freq);
5256

5357
/**
5458
* The "always on" level of the PWM. The default is 255.
5559
*/
5660
byte upperLimit;
57-
61+
5862
private:
5963
int _outPin;
6064
byte _value;

0 commit comments

Comments
 (0)