C语言开机后或第一次满足条件不再执行高级写法

当系统开机后,只需执行一次初始化设备,使用两个while循环的好处,当线程未收到退出消息时,将持续执行内循环,循环未发生异常时,一直执行,发生异常时,重新执行外循环初始化硬件,但不退出线程。

内循环发生异常,热插拔耳机时,内循环注销设备,退出循环,重新执行一次初始化设备打开设备。

g_char  = SX_FALSE;
#define 	MYFABS(x,y) (((x) > (y)) ? ((x)-(y)) : ((y)-(x)))

int main(int argc, char *argv[])
{
	HI_S32 s32Ret = 0;
	int fd0 = -1;
    char *filename0 = NULL;
    char buf[10]={0};
	int audioret = -1;
    filename0 = "/dev/tlv320aic31";
	
	SX_U32 ts0 = get_sys_ms();
	SX_U32 oldMs = ts0;
	SX_U32 currentMs = ts0;
	char reopen = 0,time = 0,detect_interrupt = 0;
	
	g_bQuit = SX_FALSE;
	while( g_bQuit == SX_FALSE)
	{
		//init system
		CONFIG_Open( handle );
		SYS_Open( handle );
		VI_Open( handle );
		// open dev
	    fd0 = open(filename0, O_RDWR);
	   	if (fd0 < 0) {
	        	printf("app open %s fail\n", filename0);
	        	return 0;
	    } else {
			printf("app open  %s success\r\n",filename0);
			read(fd0,buf,4);
	    }
		
		while( g_bQuit == SX_FALSE )
		{		
			currentMs = get_sys_ms();
			if( MYFABS(currentMs, oldMs) > 1000 )
			{
				oldMs = currentMs;
				SX_S32 second = MYFABS(currentMs, ts0) / 1000;
				if( second % 60 == 0 )
				{
					//TRACE( DL_INFO, "system run %ld hour %ld minutes\n", second/3600, (second%3600 ) / 60 );
				}
				
				if( 1 )
				{
					if(reopen == 1)
					{
						reopen = 0;
					    fd0 = open(filename0, O_RDWR);
					   	if (fd0 < 0) {
					        	printf("app open %s fail\n", filename0);
					        	return 0;
					    } else {
							printf("app open  %s success\r\n",filename0);
					    }
					}
					
					if(detect_interrupt == 1)
					{
						time ++;
						if(time < 2)
						{
							read(fd0,buf,4); //中断异常,重新读,才能关闭中断,这是bug
							continue;
						}
						else
							time = 0;
						detect_interrupt = 0;
					}
					
				    audioret = read(fd0,buf,4); 		//AudioStatus
					if(audioret < 0 ) {
				        	printf("app read audiostatus fail\n");
					} else {
						if(( (buf[0] & 0x10) == 0x10) && ( (buf[1] & 0x04) == 0x04)) // headset remmov then insert detect ,reset aicfg
						{
							close(fd0);
							reopen = 1;
							detect_interrupt = 1;
							mySAMPLE_AUDIO_AdecAo_exit();
							mySAMPLE_AUDIO_AiAenc_exit();
							system("/root/ko/reloadao.sh");  //rm and reload tl320 ko
							my_audio_init();
							mySAMPLE_AUDIO_AiAenc_start();
							mySAMPLE_AUDIO_AdecAo_start();
							mySAMPLE_AUDIO_SetAo_Volume(6);   
							continue;
						}
					}  
				}	
			}
			
		}		
	}
	
	TRACE(DL_INFO, "exit main ok\n");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值