pic32/libraries/SoftPWMServo/SoftPWMServo.cpp
// Add a channel into the linked list, based upon PWMValue (i.e. in order)
Add a channel into the linked list, based upon PWMValue
Add a channel into the linked list based upon PWMValue
void Add(uint32_t Channel)
{
    ChanType * ChanP = FirstChanP[InactiveBuffer];
    ChanType * LastChanP = FirstChanP[InactiveBuffer];
    if (ChanP == NULL)
    {
        FirstChanP[InactiveBuffer] = &Chan[InactiveBuffer][Channel];
        Chan[InactiveBuffer][Channel].NextChanP = NULL;
        return;
    }
    while (ChanP != NULL)
    {
        if (ChanP->PWMValue > Chan[InactiveBuffer][Channel].PWMValue) 
        {
            if (ChanP == FirstChanP[InactiveBuffer])
            {
                Chan[InactiveBuffer][Channel].NextChanP = ChanP;
                FirstChanP[InactiveBuffer] = &Chan[InactiveBuffer][Channel];
            }
            else
            {
                Chan[InactiveBuffer][Channel].NextChanP = ChanP;
                LastChanP->NextChanP = &Chan[InactiveBuffer][Channel];
            }
            return;
        }
        LastChanP = ChanP;
        ChanP = ChanP->NextChanP;
    }
    LastChanP->NextChanP = &Chan[InactiveBuffer][Channel];
    Chan[InactiveBuffer][Channel].NextChanP = NULL;
}
void Add ( uint32_t Channel ) { ChanType * ChanP = FirstChanP [ InactiveBuffer ] ; ChanType * LastChanP = FirstChanP [ InactiveBuffer ] ; if ( ChanP == NULL ) { FirstChanP [ InactiveBuffer ] = & Chan [ InactiveBuffer ] [ Channel ] ; Chan [ InactiveBuffer ] [ Channel ] . NextChanP = NULL ; return ; } while ( ChanP != NULL ) { if ( ChanP -> PWMValue > Chan [ InactiveBuffer ] [ Channel ] . PWMValue ) { if ( ChanP == FirstChanP [ InactiveBuffer ] ) { Chan [ InactiveBuffer ] [ Channel ] . NextChanP = ChanP ; FirstChanP [ InactiveBuffer ] = & Chan [ InactiveBuffer ] [ Channel ] ; } else { Chan [ InactiveBuffer ] [ Channel ] . NextChanP = ChanP ; LastChanP -> NextChanP = & Chan [ InactiveBuffer ] [ Channel ] ; } return ; } LastChanP = ChanP ; ChanP = ChanP -> NextChanP ; } LastChanP -> NextChanP = & Chan [ InactiveBuffer ] [ Channel ] ; Chan [ InactiveBuffer ] [ Channel ] . NextChanP = NULL ; }
Add a channel into the linked list, based upon PWMValue (i.e.
Add a channel into the linked list based upon PWMValue ( i . e .
// As a speical case, if there are no elements in the list, then just add this one to the front // Yes, so put us first in line // Walk through the linked list, looking for a PWMValue that's bigger than ours // Is this element's PWMValue greater than ours? // Yup. So let's add ourselves right here in the list // Are we on the first channel? // Yes, so put us first in line // Nope, we're in the list. So change this element's previous pointer // And we're done so bail // Advance our pointers by one element in the list // Did we blow by the end of the list without finding any greater than ours?