Skip to content

Commit b4e86c0

Browse files
Aquire channelMutex before getting time of day.
Use a extra scope block to contain the mutex.
1 parent 5455eaf commit b4e86c0

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

src/dimmerTask.cpp

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -64,37 +64,40 @@ void dimmerTask(void *parameter)
6464
while (1)
6565
{
6666
vTaskDelayUntil(&xLastWakeTime, ticksToWait);
67-
const auto msElapsedToday = msSinceMidnight();
6867

69-
if (msElapsedToday) /* to prevent flashing at 00:00:000 due to the fact that the first timer has no predecessor at this time */
7068
{
7169
std::lock_guard<std::mutex> lock(channelMutex);
7270

73-
for (int index = 0; index < NUMBER_OF_CHANNELS; index++)
71+
const auto msElapsedToday = msSinceMidnight();
72+
73+
if (msElapsedToday) /* to prevent flashing at 00:00:000 due to the fact that the first timer has no predecessor at this time */
7474
{
75-
int currentTimer = 0;
76-
while (channel[index][currentTimer].time * 1000U < msElapsedToday)
77-
currentTimer++;
75+
for (int index = 0; index < NUMBER_OF_CHANNELS; index++)
76+
{
77+
int currentTimer = 0;
78+
while (channel[index][currentTimer].time * 1000U < msElapsedToday)
79+
currentTimer++;
7880

79-
currentTimer = (currentTimer >= channel[index].size()) ? channel[index].size() - 1 : currentTimer;
81+
currentTimer = (currentTimer >= channel[index].size()) ? channel[index].size() - 1 : currentTimer;
8082

81-
const float newPercentage =
82-
(currentTimer > 0 && channel[index][currentTimer].percentage != channel[index][currentTimer - 1].percentage)
83-
? mapf(msElapsedToday,
84-
channel[index][currentTimer - 1].time * 1000U,
85-
channel[index][currentTimer].time * 1000U,
86-
channel[index][currentTimer - 1].percentage,
87-
channel[index][currentTimer].percentage)
88-
: channel[index][currentTimer].percentage;
83+
const float newPercentage =
84+
(currentTimer > 0 && channel[index][currentTimer].percentage != channel[index][currentTimer - 1].percentage)
85+
? mapf(msElapsedToday,
86+
channel[index][currentTimer - 1].time * 1000U,
87+
channel[index][currentTimer].time * 1000U,
88+
channel[index][currentTimer - 1].percentage,
89+
channel[index][currentTimer].percentage)
90+
: channel[index][currentTimer].percentage;
8991

90-
const float currentMoonLevel = fullMoonLevel[index] * moon.percentLit;
92+
const float currentMoonLevel = fullMoonLevel[index] * moon.percentLit;
9193

92-
currentPercentage[index] = newPercentage < currentMoonLevel ? currentMoonLevel : newPercentage;
94+
currentPercentage[index] = newPercentage < currentMoonLevel ? currentMoonLevel : newPercentage;
9395

94-
const int dutyCycle = mapf(currentPercentage[index], 0, 100, 0, LEDC_MAX_VALUE);
96+
const int dutyCycle = mapf(currentPercentage[index], 0, 100, 0, LEDC_MAX_VALUE);
9597

96-
if (!ledcWrite(ledPin[index], dutyCycle))
97-
log_w("Error setting duty cycle %i on pin %i", dutyCycle, ledPin[index]);
98+
if (!ledcWrite(ledPin[index], dutyCycle))
99+
log_w("Error setting duty cycle %i on pin %i", dutyCycle, ledPin[index]);
100+
}
98101
}
99102
}
100103

0 commit comments

Comments
 (0)