pic32/libraries/SoftPWMServo/SoftPWMServo.cpp
// If the ISR has just swapped buffers, then we need to copy the ActiveBuffer's
// contents into the inactive buffer's contents so that we start modifying 
// data that matches what is currently 'live'. InactiveBufferReady will be false if
// this is the first call to Write after an ISR swap.
If the ISR has just swapped buffers, then we need to copy the ActiveBuffer's
contents into the inactive buffer's contents so that we start modifying
data that matches what is currently 'live'. InactiveBufferReady will be false if
this is the first call to Write after an ISR swap.
If the ISR has just swapped buffers then we need to copy the ActiveBuffer ' s contents into the inactive buffer ' s contents so that we start modifying data that matches what is currently ' live ' . InactiveBufferReady will be false if this is the first call to Write after an ISR swap .
static void CopyBuffers(void)
{
    uint32_t i;
    if (!InactiveBufferReady)
    {
        for (i = 0; i < SOFTPWMSERVO_MAX_PINS; i++)
        {
            Chan[InactiveBuffer][i] = Chan[ActiveBuffer][i];
            if (Chan[ActiveBuffer][i].NextChanP != NULL)
            {
                Chan[InactiveBuffer][i].NextChanP = 
                    (ChanType *)(
                        (uint32_t)&Chan[InactiveBuffer] 
                        +
                        ((uint32_t)Chan[ActiveBuffer][i].NextChanP - (uint32_t)&Chan[ActiveBuffer])
                    );
            }
        }
        if (FirstChanP[ActiveBuffer] != NULL)
        {
            FirstChanP[InactiveBuffer] = 
                (ChanType *)(
                    (uint32_t)&Chan[InactiveBuffer] 
                    +
                    ((uint32_t)FirstChanP[ActiveBuffer] - (uint32_t)&Chan[ActiveBuffer])
                );
        }
        else
        {
            FirstChanP[InactiveBuffer] = NULL;
        }
    }
    InactiveBufferReady = true;
}
static void CopyBuffers ( void ) { uint32_t i ; if ( ! InactiveBufferReady ) { for ( i = 0 ; i < SOFTPWMSERVO_MAX_PINS ; i ++ ) { Chan [ InactiveBuffer ] [ i ] = Chan [ ActiveBuffer ] [ i ] ; if ( Chan [ ActiveBuffer ] [ i ] . NextChanP != NULL ) { Chan [ InactiveBuffer ] [ i ] . NextChanP = ( ChanType * ) ( ( uint32_t ) & Chan [ InactiveBuffer ] + ( ( uint32_t ) Chan [ ActiveBuffer ] [ i ] . NextChanP - ( uint32_t ) & Chan [ ActiveBuffer ] ) ) ; } } if ( FirstChanP [ ActiveBuffer ] != NULL ) { FirstChanP [ InactiveBuffer ] = ( ChanType * ) ( ( uint32_t ) & Chan [ InactiveBuffer ] + ( ( uint32_t ) FirstChanP [ ActiveBuffer ] - ( uint32_t ) & Chan [ ActiveBuffer ] ) ) ; } else { FirstChanP [ InactiveBuffer ] = NULL ; } } InactiveBufferReady = true ; }
If the ISR has just swapped buffers, then we need to copy the ActiveBuffer's
contents into the inactive buffer's contents so that we start modifying
data that matches what is currently 'live'.
If the ISR has just swapped buffers then we need to copy the ActiveBuffer ' s contents into the inactive buffer ' s contents so that we start modifying data that matches what is currently ' live ' .
// TODO: Replace with memcpy? // Now comes the hard part. Because we have pointers in this array, we can't  // just use the copy we have (to inactive) as is. We need to go fix it up so  // that all of the pointers are internally consistent and point only to  // channels in InactiveBuffer. // TODO: There MUST be a better way to do this without resorting to indexes  //  rather than pointers.