⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 headset_leds.c

📁 bluelab的一个很好的例程
💻 C
📖 第 1 页 / 共 3 页
字号:

void LedsIndicateNoState ( LedTaskData * pLEDTask  )  
{
     /*Find the LEDS that are set to indicate states and Cancel the messages,
    -do not want to indicate more than one state at a time*/
    LedsCancelStateIndications (  pLEDTask ) ;
}
/****************************************************************************
NAME 
 LEDManagerMessageHandler

DESCRIPTION
 The main message handler for the LED task. Controls the PIO in question, then 
    queues a message back to itself for the next LED update.

RETURNS
 void
    
*/
static void LedsMessageHandler( Task task, MessageId id, Message message )
{  
    LedTaskData * lLEDTask = (LedTaskData *) task ;
    bool lOldState = LED_OFF ;
    uint16 lTime   = 0 ;
    LEDColour_t lColour ;    
    
    LEDActivity_t * lLED   = &lLEDTask->gActiveLEDS[id] ;
    LEDPattern_t *  lPattern = NULL ;
    bool lPatternComplete = FALSE ;
    
        /*which pattern are we currently indicating for this LED pair*/
    if ( lLED->Type == IT_StateIndication)
    {
       lPattern = lLEDTask->gStatePatterns[ lLED->Index] ;
    }
    else
    {      /*is an event indication*/
        lPattern = lLEDTask->gEventPatterns [ lLED->Index ] ;
    }
        /*get which of the LEDs we are interested in for the pattern we are dealing with*/
    lColour = LedsGetPatternColour ( lLEDTask , lPattern ) ;
     
        /*get the state of the LED we are dealing with*/
    lOldState = LedsGetLEDStateFromPattern ( lLEDTask , lPattern , lLED ) ;
   
    LED_DEBUG(("LM : LED[%d] [%d] f[%d]of[%d]\n", id ,lOldState , lLED->NumFlashesComplete , lPattern->NumFlashes )) ;
    
         
        /*The actual LED handling*/
    if (lOldState == LED_OFF)
    {
        lTime = lPattern->OnTime ;
           /*Increment the number of flashes*/
        lLED->NumFlashesComplete++ ;
              
        LED_DEBUG(("LED: Pair On\n")) ;
        LedsTurnOnLEDPair ( lLEDTask , lPattern , lLED ) ;
        
    }
    else
    {    /*restart the pattern if we have palayed all of the required flashes*/
        if ( lLED->NumFlashesComplete >= lPattern->NumFlashes )
        {
            lTime = lPattern->RepeatTime ;
            lLED->NumFlashesComplete = 0 ;       
                /*inc the Num times the pattern has been played*/
            lLED->NumRepeatsComplete ++ ;
            LED_DEBUG(("LED: Pat Rpt [%d][%d]\n",lLED->NumRepeatsComplete , lPattern->TimeOut)) ;
      
            /*if a single pattern has completed*/
            if ( lPattern->RepeatTime == 0 ) 
            {
                LED_DEBUG(("LED: PC: Rpt\n")) ;
                lPatternComplete = TRUE ;
            }
               /*a pattern timeout has occured*/
            if ( ( lPattern->TimeOut !=0 )  && ( lLED->NumRepeatsComplete >= lPattern->TimeOut) )
            {
                lPatternComplete = TRUE ;
                LED_DEBUG(("LED: PC: Rpt b\n")) ;
            }              
            
            /*if we have reached the end of the pattern and are using a follower then revert to the orig pattern*/
            if (gFollowing)
            {
                gFollowing = FALSE ;
                lTime = LedsGetLedFollowerRepeatTimeLeft( lLEDTask , lPattern ) ;    
            }
            else
            {
                /*do we have a led follower filter and are we indicating a state, if so use these parameters*/
                if (lLED->Type == IT_StateIndication)
                {
                    if( LedsCheckFiltersForLEDFollower( lLEDTask ) )
                    {
                        lTime = LedsGetLedFollowerStartDelay( lLEDTask ) ;       
                        gFollowing = TRUE ;
                    }
                }    
             }
            
        } 
        else /*otherwise set up for the next flash*/
        {
            lTime = lPattern->OffTime ;
        } 
            /*turn off both LEDS*/
     
        LED_DEBUG(("LED: Pair OFF\n")) ;   
        
        if ( (lTime == 0 ) && ( lPatternComplete == FALSE ) )
        {
                /*ie we are switching off for 0 time - do not use the overide led as this results in a tiny blip*/
            LedsTurnOffLEDPair ( lLEDTask , lPattern , FALSE) ;
        }
        else
        {
            LedsTurnOffLEDPair ( lLEDTask , lPattern , TRUE) ;
        }
    }
    
   
        /*handle the completion of the pattern or send the next update message*/
    if (lPatternComplete)
    {
        LED_DEBUG(("LM : P C [%x][%x]  [%x][%x]\n" , lLEDTask->gActiveLEDS[lPattern->LED_B].Index, lLED->Index , lLEDTask->gActiveLEDS[lPattern->LED_B].Type , lLED->Type    )) ;
        /*set the type of indication for both LEDs as undefined as we are now indicating nothing*/
        if ( lLEDTask->gActiveLEDS[id].Type == IT_EventIndication )
        {
                  /*signal the completion of an event*/
            LedsSendEventComplete ( lLED->Index + EVENTS_EVENT_BASE , TRUE ) ;
                /*now complete the event, and indicate a new state if required*/        
            LedsEventComplete ( lLEDTask, lLED , &lLEDTask->gActiveLEDS[lPattern->LED_B] ) ;
        }  
        else if (lLEDTask->gActiveLEDS[id].Type == IT_StateIndication )
        {
            /*then we have completed a state indication and the led pattern is now off*/    
            /*Indicate that we are now with LEDS disabled*/
           lLEDTask->gLEDSStateTimeout = TRUE ;
        }
    }
    else
    {       /*apply the filter in there is one  and schedule the next message to handle for this led pair*/
        lTime = LedsApplyFilterToTime ( lLEDTask , lTime ) ;
    
            LED_DEBUG(("LED: MSG [%d][%d]\n" , lTime ,id)) ;
        MessageSendLater (&lLEDTask->task , id , 0 , lTime ) ;
    } 
}


/****************************************************************************
NAME 
 LMTurnOnLEDPair

DESCRIPTION
    Fn to turn on the LED associated with the pattern / LEDs depending upon the 
    colour 
    
RETURNS
 void
*/
static void LedsTurnOnLEDPair ( LedTaskData * pLEDTask , LEDPattern_t * pPattern , LEDActivity_t * pLED )
{
    LEDColour_t lColour = LedsGetPatternColour ( pLEDTask , pPattern ) ;
    

    if (gFollowing )
    {
            /*turn of the pair of leds (and dont use an overide LED */
        LedsTurnOffLEDPair ( pLEDTask , pPattern , FALSE) ;
        PioSetLedPin ( pLEDTask , LedsGetFollowerPin(pLEDTask), LED_ON );
    }
    else
    {/*we are not following*/
        switch (lColour )
        {
        case LED_COL_LED_A:
    
            LED_DEBUG(("LED: A ON[%x][%x]\n", pPattern->LED_A , pPattern->LED_B)) ;
            
            PioSetLedPin ( pLEDTask,  pPattern->LED_A , LED_ON )  ;
            if (pPattern->LED_A != pPattern->LED_B)
            {
                PioSetLedPin ( pLEDTask , pPattern->LED_B , LED_OFF )  ;
            }
        
        break;
        case LED_COL_LED_B:
    
            LED_DEBUG(("LED: B ON[%x][%x]\n", pPattern->LED_A , pPattern->LED_B)) ;
            PioSetLedPin ( pLEDTask , pPattern->LED_B , LED_ON )  ;
            if (pPattern->LED_A != pPattern->LED_B)
            {
                PioSetLedPin ( pLEDTask , pPattern->LED_A , LED_OFF )  ;
            }
        break;
        case LED_COL_LED_ALT:
            if (pLED->NumFlashesComplete % 2 )
            {
        
                LED_DEBUG(("LED: A ALT[%x][%x]\n", pPattern->LED_A , pPattern->LED_B)) ;
                PioSetLedPin ( pLEDTask , pPattern->LED_B , LED_ON )  ;
                PioSetLedPin ( pLEDTask , pPattern->LED_A , LED_OFF )  ;
            }
            else
            {
        
                LED_DEBUG(("LED: B ALT[%x][%x]\n", pPattern->LED_A , pPattern->LED_B)) ;
                PioSetLedPin ( pLEDTask , pPattern->LED_A , LED_ON )  ;
                PioSetLedPin ( pLEDTask , pPattern->LED_B , LED_OFF )  ;
            }
        break;
        case LED_COL_LED_BOTH:
    
            LED_DEBUG(("LED: AB Both[%x][%x]\n", pPattern->LED_A , pPattern->LED_B)) ;
            PioSetLedPin ( pLEDTask , pPattern->LED_A , LED_ON )  ;
            PioSetLedPin ( pLEDTask , pPattern->LED_B , LED_ON )  ;
        break;
        default:
            LED_DEBUG(("LM : ?Col\n")) ;
        break;
        }
    }
    
        /*handle an overide LED if there is one will also dealit is different to one of the pattern LEDS)*/
    LedsHandleOverideLED (  pLEDTask , LED_OFF ) ;
    
    pLED->OnOrOff = TRUE ;
        
}


/****************************************************************************
NAME 
 LMTurnOffLEDPair

DESCRIPTION
    Fn to turn OFF the LEDs associated with the pattern
    
RETURNS
 void
*/
static void LedsTurnOffLEDPair ( LedTaskData * pLEDTask , LEDPattern_t * pPattern  , bool pUseOveride ) 
{    

        /*turn off bothe LEDS*/
    PioSetLedPin ( pLEDTask , pPattern->LED_A , LED_OFF )  ;
    PioSetLedPin ( pLEDTask , pPattern->LED_B , LED_OFF )  ;
   
        /*handle an overide LED if we want to use one*/
    if ( pUseOveride )
    {
        LedsHandleOverideLED (  pLEDTask , LED_ON ) ;
    }
    pLEDTask->gActiveLEDS [ pPattern->LED_A ].OnOrOff  = FALSE ;
        
}


/****************************************************************************
NAME 
 LedsHandleOverideLED

DESCRIPTION
    Enables / diables any overide LEDS if there are some    
RETURNS
*/
static void LedsHandleOverideLED ( LedTaskData * pLEDTask , bool pOnOrOff ) 
{   
    uint16 lFilterIndex = 0 ;
    
    for (lFilterIndex = 0 ; lFilterIndex< LM_NUM_FILTER_EVENTS ; lFilterIndex++ )
    {
        if ( LedsIsFilterEnabled(pLEDTask , lFilterIndex) )
        {
             if ( pLEDTask->gEventFilters[lFilterIndex].OverideLEDActive )
             {
                if ( pLEDTask->gEventFilters[lFilterIndex].OverideLED )
                {
                    /*Overide the Off LED with the Overide LED*/
                    LED_DEBUG(("LM: LEDOveride [%d] [%d]\n" , pLEDTask->gEventFilters[lFilterIndex].OverideLED , pOnOrOff)) ;    
                    PioSetLedPin ( pLEDTask , pLEDTask->gEventFilters[lFilterIndex].OverideLED , pOnOrOff) ;                
                }
            }    
        }
    }  
}


/****************************************************************************
NAME 
 LMGetPatternColour

DESCRIPTION
    Fn to determine the LEDColour_t of the LED pair we are currently playing
    takes into account whether or not a filter is currently active
    
RETURNS
 LEDColour_t
*/
static LEDColour_t LedsGetPatternColour (LedTaskData * pLEDTask , const  LEDPattern_t * pPattern )
{
    uint16 lFilterIndex = 0 ;
        /*sort out the colour of the LED we are interested in*/
    LEDColour_t lColour = pPattern->Colour ;
   
    for (lFilterIndex = 0 ; lFilterIndex< 10 ; lFilterIndex++ )
    {
        if ( LedsIsFilterEnabled(pLEDTask , lFilterIndex) )
        {
            if ( pLEDTask->gEventFilters[lFilterIndex].Colour != LED_COL_EITHER )
            {
                    /*Overide the Off LED with the Overide LED*/
                lColour = pLEDTask->gEventFilters[lFilterIndex].Colour;   

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -