Step 3Dimming algorithm - the cross fade effect and double buffering
I didn't need to have individual LEDs to be controlled at seperate brightness levels to do a cross fade. Just needed the first pattern at one brightness and the second at a low brightness. Then over a short period I'd decrease the brightness of the first a little, and increase the second.....this would keep going until the second pattern as at full. Then the clock would wait till the next pattern was due to show and there would be another transition.
Thus I needed to store two patterns. The one currently being displayed and the second pattern which was about to be displayed. These are in arrays nLedsA[] and nLedsB. (note nothing to do with ports in this case). This is the double buffer.
The update_display() function was modified to cycle through eight frames and show a number of frames from first one array, then the other. Changing the number of frames allocated to each buffer throughout the eight cycles defined how bright each pattern would be. When we finished cycling between buffers we switched the 'display' and 'next display' buffers around, so the pattern generating function would then write only to the 'next display' buffer.
The diagram below shows this hopefully. You should be able to see that the transition will take 64 scan frames. In the picture, the little inset shows the scan fram diagram from the previous page artfully scaled down.
A word on re-fresh rate. All this needs to be done very quickly. We have now two levels of extra computation, one for the ambient display dimmness and one for the eight frame cycles spent doing a transition between two buffers. Thus this code was should be written in assembly, but is good enough in 'C'.
| « Previous Step | Download PDFView All Steps | Next Step » |
![]() |
Add Comment
|










































