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

📄 headset_volume.c

📁 bc5_stereo:bluetooth stereo Headset CODE 支持A2DP HSP 和 HSP 。可作为车载免提。BlueLab 2007环境下编译
💻 C
字号:
/****************************************************************************
Copyright (C) Cambridge Silicon Radio Ltd. 2004-2007

FILE NAME
    headset_volume.c
    
DESCRIPTION
    Module responsible for Vol control 
    
*/

#include "headset_debug.h"
#include "headset_LEDmanager.h"
#include "headset_volume.h"
#include "headset_statemanager.h"
#include "headset_tones.h"
#include "headset_configmanager.h"

#include <stdlib.h>
#include <audio.h>
#include <ps.h>

#ifdef DEBUG_VOLUME
#define VOL_DEBUG(x) DEBUG(x)
#else
#define VOL_DEBUG(x) 
#endif


#define MIC_VOLUME	(10)


typedef struct
{
	unsigned int hfpVol:8;
	unsigned int avVol:8;
} vol_table_t;

vol_table_t *gVolLevels = NULL;

/*****************************************************************************/
void VolumeInit ( hsTaskData * pApp ) 
{
	uint16 psVolume = 0;
	VOL_DEBUG(("VOL: Init Volume\n"));

	if (PsRetrieve(PSKEY_VOLUME_LEVELS, &psVolume, sizeof(uint16)))
	{
		pApp->audioData.gHfpVolumeLevel = psVolume & 0x7f; /* Field is 7 Bits long */
		pApp->audioData.gAvVolumeLevel = (psVolume >> 8) & 0x7f; /* Field is 7 Bits long */
		
		if (pApp->audioData.gHfpVolumeLevel > VOL_MAX_VOLUME_LEVEL)
		{
		    pApp->audioData.gHfpVolumeLevel = VOL_DEFAULT_VOLUME_LEVEL ; 
		}
		if (pApp->audioData.gAvVolumeLevel > VOL_MAX_VOLUME_LEVEL)
		{
		    pApp->audioData.gAvVolumeLevel = VOL_DEFAULT_VOLUME_LEVEL ; 
		}
	}
	else
	{
	    pApp->audioData.gHfpVolumeLevel = VOL_DEFAULT_VOLUME_LEVEL ; 
	    pApp->audioData.gAvVolumeLevel = VOL_DEFAULT_VOLUME_LEVEL ; 
	}
	pApp->audioData.gMuted = FALSE;
	
	gVolLevels = (vol_table_t*)PanicUnlessMalloc(sizeof(vol_table_t) * (VOL_MAX_VOLUME_LEVEL+1));
	configManagerSetupVolumeGains((uint16*)gVolLevels, VOL_MAX_VOLUME_LEVEL+1);
}


/*****************************************************************************/
void VolumeInitHfp ( hsTaskData * pApp ) 
{
	uint16 psVolume = 0;
	VOL_DEBUG(("VOL: Init HFP Volume\n"));

	if (PsRetrieve(PSKEY_VOLUME_LEVELS, &psVolume, sizeof(uint16)))
	{
		pApp->audioData.gHfpVolumeLevel = psVolume & 0x7f; /* Field is 7 Bits long */
		
		if (pApp->audioData.gHfpVolumeLevel > VOL_MAX_VOLUME_LEVEL)
		{
		    pApp->audioData.gHfpVolumeLevel = VOL_DEFAULT_VOLUME_LEVEL ; 
		}
	}
	else
	{
	    pApp->audioData.gHfpVolumeLevel = VOL_DEFAULT_VOLUME_LEVEL ; 
	}
	pApp->audioData.gMuted = FALSE;
}


/*****************************************************************************/
void VolumeToggleMute ( hsTaskData * pApp )
{
    VOL_DEBUG(("VOL: Mute T [%c]\n" , (pApp->audioData.gMuted ? 'F':'T') )) ;
    
        /*if then old state was muted*/
    if (pApp->audioData.gMuted )
    {
        MessageSend( &pApp->task , EventMuteOff , 0 ) ;
    }
    else
    {
        MessageSend( &pApp->task , EventMuteOn , 0 ) ;
    }
}


/*****************************************************************************/
void VolumeMuteOn ( hsTaskData * pApp )
{
    VOL_DEBUG(("VOL: Mute\n")) ;    
		
    AudioSetMode ( AUDIO_MODE_MUTE_MIC , NULL ) ;
    
    LEDManagerSetMicBias ( pApp , FALSE ) ; 
 
    pApp->audioData.gMuted = TRUE ;
    
    MessageSendLater( &pApp->task , EventMuteReminder , 0 ,D_SEC(pApp->Timeouts.MuteRemindTime_s ) ) ;
}


/*****************************************************************************/
void VolumeMuteOff ( hsTaskData * pApp )
{
	AUDIO_MODE_T lMode;
	
    VOL_DEBUG(("VOL: UnMute\n")) ;        
    pApp->audioData.gMuted = FALSE   ;
 
    MessageCancelAll( &pApp->task , EventMuteReminder ) ;
	
	if ( (stateManagerGetHfpState() == headsetActiveCall) || !pApp->cvcEnabled )
		lMode = AUDIO_MODE_CONNECTED ; 
	else
        lMode = AUDIO_MODE_MUTE_SPEAKER ;
    
    AudioSetMode ( lMode , NULL ) ;
    
    LEDManagerSetMicBias ( pApp , TRUE ) ;
}


/*****************************************************************************/
void VolumeUp ( hsTaskData * pApp )
{
	uint16 actVol = 0;
	bool avAudio = FALSE;
	
    VOL_DEBUG(("VOL: VolUp\n"));

    if (!VolumeGetHeadsetVolume( pApp, &actVol, &avAudio ))
	    return;
        
    if (actVol < VOL_MAX_VOLUME_LEVEL)
    {
        actVol++;
	
	    VolumeSetHeadsetVolume(  pApp, actVol, avAudio, TRUE );
	}
    else
    {
		MessageSend ( &pApp->task , EventVolumeMax , 0 );	
	    VOL_DEBUG(("VOL:      Max. volume reached\n"));
    }
}


/*****************************************************************************/
void VolumeDown ( hsTaskData * pApp )
{
	uint16 actVol = 0;
	bool avAudio = FALSE;
	
    VOL_DEBUG(("VOL: VolDwn\n"))  ;
    
    if (!VolumeGetHeadsetVolume( pApp, &actVol, &avAudio ))
	    return;
        
    if (actVol > 0)
    {
        actVol--;
	
	    VolumeSetHeadsetVolume(  pApp, actVol, avAudio, TRUE );
	}
    else
    {
	    MessageSend ( &pApp->task , EventVolumeMin , 0 );
	    VOL_DEBUG(("VOL:      Min. Volume reached\n"));
    }
}

/*****************************************************************************/
void VolumeStoreLevels ( hsTaskData * pApp )
{
	uint16 psVolume = 0;
	
	VOL_DEBUG(("VOL: Store Volume Levels\n"));
	
	psVolume |= pApp->audioData.gHfpVolumeLevel; /* Field is 7 Bits long */
	psVolume |= pApp->audioData.gAvVolumeLevel << 8; /* Field is 7 Bits long */
	
	if (!PsStore(PSKEY_VOLUME_LEVELS, &psVolume, sizeof(uint16)))
	{
		VOL_DEBUG(("VOL:    Can not store volume\n"));
	}
}


/*****************************************************************************/
void VolumeSendSettingsToAG(hsTaskData * pApp, bool send_speaker, bool send_mic)
{
	if (pApp->local_hfp_features & HFP_REMOTE_VOL_CONTROL)
	{
		if (send_speaker)
		{
			if (pApp->profile_connected == hfp_handsfree_profile)
				HfpSendSpeakerVolume(pApp->hfp, pApp->audioData.gHfpVolumeLevel);
			else
				HfpSendSpeakerVolume(pApp->hsp, pApp->audioData.gHfpVolumeLevel);
		}

		if (send_mic)
		{
			if (pApp->profile_connected == hfp_handsfree_profile)
				HfpSendMicrophoneVolume(pApp->hfp, MIC_VOLUME);
			else
				HfpSendMicrophoneVolume(pApp->hsp, MIC_VOLUME);
		}
	}
}


/*****************************************************************************/
bool VolumeGetHeadsetVolume(hsTaskData * pApp, uint16 * actVol, bool * avAudio)
{
	if (stateManagerIsA2dpStreaming())
    {
	    *actVol = pApp->audioData.gAvVolumeLevel;
	    *avAudio = TRUE;
    }
    else if (stateManagerIsHfpConnected())
    {
	    *actVol = pApp->audioData.gHfpVolumeLevel;
    }
    else
    {
	    VOL_DEBUG(("VOL:      No Active audio\n"));
	    return FALSE;
    }
	
	return TRUE;
}


/*****************************************************************************/
void VolumeSetHeadsetVolume(hsTaskData * pApp, uint16 actVol, bool avAudio, bool sendVolToAg)
{
	if (avAudio)
	{
	    pApp->audioData.gAvVolumeLevel = actVol;
		AudioSetVolume(	gVolLevels[actVol].avVol , pApp->theCodecTask ) ;
		VOL_DEBUG(("VOL:      Set A2DP vol index[%d] gain[%d]\n", actVol,gVolLevels[actVol].avVol));
	}
	else
	{
	    pApp->audioData.gHfpVolumeLevel = actVol;

		VOL_DEBUG(("VOL:      Set HFP vol index[%d] gain[%d]\n", actVol, gVolLevels[actVol].hfpVol));
		AudioSetVolume(	gVolLevels[actVol].hfpVol , pApp->theCodecTask ) ;

		if (sendVolToAg)
		{
			/* Send new local volume to AG */
			VolumeSendSettingsToAG(pApp, TRUE, FALSE);	
			VOL_DEBUG(("VOL:      Update AG vol\n"));
		}
	}
	
}


/*****************************************************************************/
uint16 VolumeRetrieveGain( uint16 index , bool avAudio )
{
	if (avAudio)
		return gVolLevels[index].avVol;
	else
		return gVolLevels[index].hfpVol;
		
}


⌨️ 快捷键说明

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