Skip to content

Commit

Permalink
Ensure atomicity, update README and improve examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
lfchavier committed Jul 21, 2016
1 parent 1b458fa commit 2bae950
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ To install, just click **Download ZIP** and install it using **Sketch > Include
The following examples_ are provided:

* Fade_ Arduino Fade example using an AC lamp.
* FadeMinimum_ Arduino Fade example using an AC lamp and setting a minimum power level (useful for dimmable LED or CFL lamps).
* RandomLamps_ Control 3 dimmable lamps with random values (can be extended to 10 lamps).
* WaveLamps_ Control 3 dimmable lamps in a wave form (can be extended to 10 lamps).
* CountMode_ Control high, low response AC loads without introducing noise using count mode.
Expand All @@ -23,6 +24,7 @@ The following examples_ are provided:
.. _Circuitar: https://www.circuitar.com/
.. _examples: https://github.com/circuitar/Dimmer/tree/master/examples/
.. _Fade: https://github.com/circuitar/Dimmer/blob/master/examples/Fade/Fade.ino
.. _FadeMinimum: https://github.com/circuitar/Dimmer/blob/master/examples/FadeMinimum/FadeMinimum.ino
.. _RandomLamps: https://github.com/circuitar/Dimmer/blob/master/examples/RandomLamps/RandomLamps.ino
.. _WaveLamps: https://github.com/circuitar/Dimmer/blob/master/examples/WaveLamps/WaveLamps.ino
.. _CountMode: https://github.com/circuitar/Dimmer/blob/master/examples/CountMode/CountMode.ino
Expand Down
6 changes: 3 additions & 3 deletions examples/FadeMinimum/FadeMinimum.ino
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ Dimmer dimmer(3, DIMMER_RAMP, 1.5);

void setup() {
dimmer.begin();
dimmer.setMinimum(25);
dimmer.setMinimum(20);
}

void loop() {
dimmer.set(100);
delay(1500);
delay(2500);
dimmer.set(0);
delay(1500);
delay(2500);
}
31 changes: 20 additions & 11 deletions src/Dimmer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "Arduino.h"
#include "Dimmer.h"
#include <util/atomic.h>

// Timer configuration macros
#define _TCNT(X) TCNT ## X
Expand Down Expand Up @@ -154,15 +155,19 @@ void Dimmer::on() {
}

void Dimmer::toggle() {
lampState = !lampState;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
lampState = !lampState;
}
}

bool Dimmer::getState() {
return lampState;
}

uint8_t Dimmer::getValue() {
return rampStartValue + ((int32_t) lampValue - rampStartValue) * rampCounter / rampCycles;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
return rampStartValue + ((int32_t) lampValue - rampStartValue) * rampCounter / rampCycles;
}
}

void Dimmer::set(uint8_t value) {
Expand All @@ -174,14 +179,16 @@ void Dimmer::set(uint8_t value) {
value = minValue;
}

if (operatingMode == DIMMER_RAMP) {
rampStartValue = getValue();
rampCounter = 0;
} else if (operatingMode == DIMMER_COUNT) {
pulses = 0;
pulseCount = 0;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
if (operatingMode == DIMMER_RAMP) {
rampStartValue = getValue();
rampCounter = 0;
} else if (operatingMode == DIMMER_COUNT) {
pulses = 0;
pulseCount = 0;
}
lampValue = value;
}
lampValue = value;
}

void Dimmer::set(uint8_t value, bool on) {
Expand All @@ -196,8 +203,10 @@ void Dimmer::setMinimum(uint8_t value) {

minValue = value;

if (lampValue < minValue) {
set(value);
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
if (lampValue < minValue) {
set(value);
}
}
}

Expand Down

0 comments on commit 2bae950

Please sign in to comment.