@@ -135,41 +135,30 @@ void effectTask(void* pvParameters) {
135135 xSemaphoreGive (swapMutex);
136136
137137 if (isPositions == 0 ) { // driver task can change this
138- if (layerP.lights .useDoubleBuffer ) {
139-
140- if (canProduce) {
141- // Copy previous frame (channelsD) to working buffer (channelsE)
142- memcpy (layerP.lights .channelsE , layerP.lights .channelsD , layerP.lights .header .nrOfChannels );
143-
144- layerP.loop ();
145-
146- if (millis () - last20ms >= 20 ) {
147- last20ms = millis ();
148- layerP.loop20ms ();
149- }
150-
151- // Atomic swap channels
152- xSemaphoreTake (swapMutex, portMAX_DELAY);
153- xSemaphoreTake (monitorMutex, portMAX_DELAY);
154- uint8_t * temp = layerP.lights .channelsD ;
155- layerP.lights .channelsD = layerP.lights .channelsE ;
156- layerP.lights .channelsE = temp;
157- newFrameReady = true ;
158- xSemaphoreGive (monitorMutex);
159- xSemaphoreGive (swapMutex);
138+ if (canProduce) {
139+ if (layerP.lights .useDoubleBuffer ) {
140+ memcpy (layerP.lights .channelsE , layerP.lights .channelsD , layerP.lights .header .nrOfChannels ); // Copy previous frame (channelsD) to working buffer (channelsE)
160141 }
161142
162- } else {
163- // Single buffer mode
164- xSemaphoreTake (swapMutex, portMAX_DELAY);
165143 layerP.loop ();
166144
167145 if (millis () - last20ms >= 20 ) {
168146 last20ms = millis ();
169147 layerP.loop20ms ();
170148 }
171149
150+ xSemaphoreTake (monitorMutex, portMAX_DELAY);
151+
152+ xSemaphoreTake (swapMutex, portMAX_DELAY);
153+ if (layerP.lights .useDoubleBuffer ) { // Atomic swap channels
154+ uint8_t * temp = layerP.lights .channelsD ;
155+ layerP.lights .channelsD = layerP.lights .channelsE ;
156+ layerP.lights .channelsE = temp;
157+ }
158+ newFrameReady = true ;
172159 xSemaphoreGive (swapMutex);
160+
161+ xSemaphoreGive (monitorMutex);
173162 }
174163 }
175164
@@ -189,29 +178,24 @@ void driverTask(void* pvParameters) {
189178 EXT_LOGD (ML_TAG, " positions done (3 -> 0)" );
190179 layerP.lights .header .isPositions = 0 ;
191180 }
192- uint8_t isPositions = layerP.lights .header .isPositions ;
193- xSemaphoreGive (swapMutex);
194181
195- if (isPositions == 0 ) {
196- xSemaphoreTake (swapMutex, portMAX_DELAY);
197- if (layerP.lights .useDoubleBuffer ) {
198- if (newFrameReady) {
199- newFrameReady = false ;
200- // Double buffer: release lock, then send
201- xSemaphoreGive (swapMutex);
182+ bool mutexGiven = false ;
202183
203- esp32sveltekit.lps ++;
204- layerP.loopDrivers (); // ✅ No lock needed
205- } else {
206- xSemaphoreGive (swapMutex);
184+ if (layerP.lights .header .isPositions == 0 ) {
185+ if (newFrameReady) {
186+ newFrameReady = false ;
187+ if (layerP.lights .useDoubleBuffer ) {
188+ xSemaphoreGive (swapMutex); // Double buffer: release lock, then send
189+ mutexGiven = true ;
207190 }
208- } else {
209- // Single buffer: keep lock while sending
191+
210192 esp32sveltekit.lps ++;
211- layerP.loopDrivers (); // ✅ Protected by lock
212- xSemaphoreGive (swapMutex);
193+ layerP.loopDrivers (); // ✅ No lock needed
213194 }
214195 }
196+
197+ if (!mutexGiven) xSemaphoreGive (swapMutex);
198+
215199 vTaskDelay (1 );
216200 }
217201}
0 commit comments