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

📄 headset_power.c

📁 针对bluelab3.42的handsfree车载蓝牙的参考
💻 C
字号:
/****************************************************************************
Copyright (C) Cambridge Silicon Radio Ltd. 2004-2006
Part of BlueLab 3.4.2-release

FILE NAME
    headset_power.c        

DESCRIPTION
    Deals with powering the headset.

*/


/****************************************************************************
    Header files
*/
#include "headset_private.h"
#include "av_stream_control.h"
#include "headset_common.h"
#include "headset_led.h"
#include "headset_power.h"
#include "hfp_slc.h"

#include <stdlib.h>
#include <string.h>
#include <panic.h>
#include <pio.h>
#include <codec.h>
#include <pcm.h>
#include <ps.h>


#ifdef DEBUG_POWER
#define POWER_DEBUG(x) DEBUG(x)
#else
#define POWER_DEBUG(x) 
#endif


/**************************************************************************/
void headsetSetPowerState(headsetTaskData *app, power_state state)
{
    POWER_DEBUG(("POWER: Set power state old = %d new = %d\n",app->headset_power_state, state));
    app->headset_power_state = state;
}

/**************************************************************************/
void headsetPowerOn(headsetTaskData *app)
{
    /* Hold power on */
    PioSetDir(POWER_HOLD, POWER_HOLD);
    PioSet(POWER_HOLD, POWER_HOLD);
	PioSetDir(AUDIO_AMP_LINE, AUDIO_AMP_LINE);
    headsetPowerAmp(app, TRUE);
	app->led_state |= LED_INIT;
	app->led_state &= ~LED_POWER_DOWN;
	app->headset_logically_on = 1;
	
    /* Start the LED flashing sequence */
    startBlueLed(app->led_state);
	if (app->headset_power_state != power_state_off_charging)
		startRedLed();
    

}


/**************************************************************************/
void headsetPowerOnInit(headsetTaskData *app)
{
    /*Power on and send a message to check that a power-on event has been seen*/
	POWER_DEBUG(("POWER: Charger Plugged in\n"));
	PioSetDir(POWER_HOLD, POWER_HOLD);
    PioSet(POWER_HOLD, POWER_HOLD);
		/*Send a message to check if we have seen a power on event, if this isn't seen
		  the headset will power down*/
	MessageSendLater(getAppTask(), APP_POWER_ON_EVENT_SEEN, 0, (uint32) 15000);

#ifdef WOLFSON_CODEC
    /* Init the Wolfson codec with default params. */
   CodecInitWolfson(&app->task, 0);
#else
    /* Initialise the Codec Library */
    CodecInitCsrInternal(&app->task);
#endif
}


/**************************************************************************/
void headsetStartPowerDown(headsetTaskData* app)
{
	if (app->headset_power_state == power_state_powering_down)
		return;
	
    headsetSetPowerState(app, power_state_powering_down);
    
    /* Close down any active AV link */
    if ((app->a2dp_state == avHeadsetA2dpConnected) || (app->a2dp_state == avHeadsetA2dpStreaming))
    {
        A2dpCloseAll(app->a2dp);
    }
    /* Close down any active AVRCP link */
    if (app->avrcp_state == avHeadsetAvrcpConnected)
        AvrcpDisconnect(app->avrcp);
  
    /* Close down any active AG link */
    if ((app->hfp_state != headsetInitialising) && (app->hfp_state != headsetReady))
        hfpHeadsetDisconnectSlc(app);
    
    MessageCancelAll(getAppTask(), APP_AUTO_SWITCH_OFF_IND);        
    MessageSendLater(getAppTask(), APP_POWER_OFF_IND, 0, (uint32) 5000);
    
    headsetCheckPowerDownStatus(app);
        
}


/**************************************************************************/
void headsetCheckPowerDownStatus(const headsetTaskData *app)
{
    if (app->headset_power_state != power_state_powering_down)
        return;
    
    if ((app->a2dp_state == avHeadsetA2dpReady) && (app->avrcp_state == avHeadsetAvrcpReady) && (app->hfp_state == headsetReady)
		&& MessageCancelAll(getAppTask(), APP_POWER_OFF_IND))
    {
            POWER_DEBUG(("POWER: Power down early\n"));
		
        (void) MessageCancelAll(getAppTask(), APP_POWER_OFF_IND);
		(void) MessageCancelAll(getAppTask(), APP_POWER_DOWN_LED_UPDATE_IND);
        MessageSend(getAppTask(), APP_POWER_OFF_IND, 0);
    }
}


/**************************************************************************/
void headsetPowerDown(const headsetTaskData *app)
{
	
    APP_ON_EVENT_LED_UPDATE_IND_T* message;
	message = malloc(sizeof(APP_ON_EVENT_LED_UPDATE_IND_T));
	message->count=0;
	message->id = APP_POWER_DOWN_LED_UPDATE_IND;
	POWER_DEBUG(("POWER: PowerDown and Panic\n"));
 	(void) MessageCancelAll(getAppTask(), APP_LED_UPDATE_IND);
    /* Power down the codec */
    CodecPowerDown(app->codec_task);

    /* Store the current volume */
    (void) PsStore(VOLUME_LEVEL, &app->speaker_volume, sizeof(app->speaker_volume));
	
    /* call timer function ASAP */
	if (app->headset_logically_on)
	    MessageSendLater(getAppTask(), APP_POWER_DOWN_LED_UPDATE_IND, message, 0);
	else
	{
		headsetPowerDownLEDs((headsetTaskData *) app);	
	}

    /* Cancel messages we may have queued */
	POWER_DEBUG(("POWER: Cancel messages\n"));
    (void) MessageCancelAll(getAppTask(), APP_MUSIC_RESTART_IND);
    (void) MessageCancelAll(getAppTask(), APP_HFP_SLC_REQ);
    (void) MessageCancelAll(getAppTask(), APP_HFP_SLC_TIMEOUT_IND);

}
/**************************************************************************/
void headsetPowerDownLEDs(headsetTaskData *app)
{  
	if ((app->led_state & LED_CHARGING) || (app->led_state & LED_BATTERY_FULL))
	{
    	PioSet(LED_BLUE, 0);
		PioSetDir(LED_BLUE, 0);
		app->headset_logically_on = 0;
		MessageSend(getAppTask(), APP_POWER_CHARGING_OFF, 0);
        POWER_DEBUG(("POWER: Power off charging\n"));
	}
	else
	{
	    /* Disable power hold and LED */
		MessageSend(getAppTask(), APP_POWER_OFF, 0);
        POWER_DEBUG(("POWER: Power off\n"));
		PioSet(POWER_HOLD | LED_BLUE |LED_RED, 0);
	}

    /* Call the function that will disable page scanning*/
	updateHeadsetScanning(app);

    /* Finally turn off audio supply if it's still active */    
    headsetPowerAmp(app, FALSE);
}		


/**************************************************************************/
void headsetPowerCheckAutoOff(const headsetTaskData *app)
{
    if (app->features.auto_switch_off_enabled && 
        (app->features.auto_switch_off_timeout != 0) &&
        ((app->headset_power_state == power_state_on) || (app->headset_power_state == power_state_on_charging)))
    {
        POWER_DEBUG(("POWER: Cancel auto power off\n"));
        MessageCancelAll(getAppTask(), APP_AUTO_SWITCH_OFF_IND);        
        if ((app->hfp_state == headsetReady) && (app->a2dp_state == avHeadsetA2dpReady))
        {
            POWER_DEBUG(("POWER: Auto power off in %d secs\n",app->features.auto_switch_off_timeout));
            MessageSendLater(getAppTask(), APP_AUTO_SWITCH_OFF_IND, 0, D_SEC(app->features.auto_switch_off_timeout));            
        }
    }
}


/**************************************************************************/
bool headsetIsAmpPowered(const headsetTaskData *app) 
{
    return app->amp.amp_enabled;
}
    

/**************************************************************************/
void headsetPowerAmp(headsetTaskData *app, bool power)
{
    if (power && !app->amp.amp_enabled)
    {
        POWER_DEBUG(("POWER: Enable Audio Amp\n"));
        PioSet(AUDIO_AMP_LINE, AUDIO_AMP_LINE);
        app->amp.amp_enabled = 1;
    }
    else
    if (!power && app->amp.amp_enabled)
    {
        POWER_DEBUG(("POWER: Disable Audio Amp\n"));
        PioSet(AUDIO_AMP_LINE, 0);
        StreamDisconnect(StreamPcmSource(0), StreamPcmSink(0)); 
        StreamDisconnect(StreamPcmSource(1), StreamPcmSink(1));
        app->amp.amp_enabled = 0;
    }
}

/**************************************************************************/
void headsetPowerAmpOnOnly(headsetTaskData *app)
{
    POWER_DEBUG(("POWER: Enable Amp Only\n"));
    PioSet(AUDIO_AMP_LINE, AUDIO_AMP_LINE);
    MessageCancelAll(getAppTask(), APP_AMP_IDLE_IND);
    if ((app->headset_power_state == power_state_off) || (app->headset_power_state == power_state_off_charging))
    {
        MessageSendLater(getAppTask(), APP_AMP_IDLE_IND, 0, AMP_IDLE_TIMER);
    }
}

/**************************************************************************/
void headsetPowerUpdateAmpOnFlag(headsetTaskData *app)
{
    app->amp.amp_enabled = 1;    
}

⌨️ 快捷键说明

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