当前位置:网站首页>getopt_ Typical use of long function
getopt_ Typical use of long function
2022-07-03 09:59:00 【Wukong is so timid】
getopt_long Typical use of functions
In actual development We need to develop some test programs by ourselves . Reference resources glibc etc. LINUX Standard commands can be implemented .
int main(int argc, char const *argv[])
{
int ret = 0,i;
char buffer[1024*2];
int len_read,len_write,offset,chunk_bytes;
fd_set rfds,wfds;
int io_select = -1;
char file_name[100]={
0,};
struct fh81_audio_cfg_arg audio_cfg;
int option_index, c;
static const char short_options[] = "hC:P:r:v:s:b:p:";
static const struct option long_options[] = {
{
"help",no_argument,0,'h'},
{
"capture",required_argument,0,'C'},
{
"playback",required_argument,0,'P'},
{
"rate",required_argument,0,'r'},
{
"volume",required_argument,0,'v'},
{
"select_io",required_argument,0,'s'},
{
"buffer_size",required_argument,0,'b'},
{
"period_size",required_argument,0,'p'},
{
0, 0, 0, 0}
};
/*default config*/
audio_cfg.volume = 85;
audio_cfg.rate = 8000;
audio_cfg.frame_bit = 16;
audio_cfg.channels = 1;
audio_cfg.period_size = 1024;
audio_cfg.buffer_size = audio_cfg.period_size << 3;
while ((c = getopt_long(argc, argv, short_options, long_options, &option_index)) != -1) {
switch(c) {
case 'h':
print_usage();
return 0;
case 'C':
strcpy(file_name,optarg);
stream = 0;
break;
case 'P':
strcpy(file_name,optarg);
stream = 1;
break;
case 'r':
audio_cfg.rate = strtol(optarg, NULL, 0);
break;
case 'v':
audio_cfg.volume = strtol(optarg, NULL, 0);
break;
case 's':
break;
case 'b':
audio_cfg.buffer_size = strtol(optarg, NULL, 0);
break;
case 'p':
audio_cfg.period_size = strtol(optarg, NULL, 0);
break;
default:
printf("unsupport cmd,try -h for help\n");
return 1;
}
}
if (-1 == stream) {
printf("please select capture or playback\n");
return 0;
}
if (strlen(file_name) < 1) {
printf("please input the file_name to capture or playback\n");
return 0;
}
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
signal(SIGABRT, signal_handler);
audio_fd = open("/dev/fh_linbao_i2s", O_RDWR);
if (audio_fd == -1) {
perror("open /dev/fh_audio");
return -1;
}
if (0 == stream) {
/*capture test*/
ret = ioctl(audio_fd, I2S_INIT_CAPTURE_MEM, &audio_cfg);
if (ret < 0) {
printf("[ERROR]: ioctl AC_INIT_CAPTURE_MEM failed ret=%d\n", ret);
return ret;
}
chunk_bytes = frames_to_bytes(audio_cfg.frame_bit,audio_cfg.period_size);
fd_file = open(file_name,O_RDWR|O_CREAT|O_TRUNC);
ret = ioctl(audio_fd, I2S_AI_EN, NULL);
if (ret < 0) {
printf("[ERROR]: ioctl AC_AI_EN failed ret=%d\n", ret);
return ret;
}
while(1) {
FD_ZERO(&rfds);
FD_SET(audio_fd,&rfds);
select(audio_fd+1,&rfds,NULL,NULL,NULL);
if (FD_ISSET(audio_fd,&rfds)) {
len_read = read(audio_fd,buffer,chunk_bytes);
write(fd_file,buffer,len_read);
}
}
ret = ioctl(audio_fd, I2S_AI_DISABLE, NULL);
} else {
/*playback test*/
fd_file = open(file_name,O_RDONLY);
if (fd_file < 0) {
printf("playback file not exits\n");
return -1;
}
ret = ioctl(audio_fd, I2S_INIT_PLAYBACK_MEM, &audio_cfg);
if (ret < 0) {
printf("[ERROR]: ioctl AC_INIT_PLAYBACK_MEM failed ret=%d\n", ret);
return ret;
}
ret = ioctl(audio_fd, I2S_AO_EN, NULL);
if (ret < 0) {
printf("[ERROR]: ioctl AC_AO_EN failed ret=%d\n", ret);
return ret;
}
chunk_bytes = frames_to_bytes(audio_cfg.frame_bit,audio_cfg.period_size);
while(1) {
len_read = read(fd_file,buffer,chunk_bytes);
if (len_read <= 0) {
printf("playback finished %d\n",len_read);
break;
}
if (len_read < chunk_bytes) {
fill_silence_data(buffer,chunk_bytes, chunk_bytes - len_read);
}
FD_ZERO(&wfds);
FD_SET(audio_fd,&wfds);
select(audio_fd+1,NULL,&wfds,NULL,NULL);
if (FD_ISSET(audio_fd,&wfds)) {
len_write = write(audio_fd,buffer,chunk_bytes);
//printf("write len = %d\n",len_write);
}
}
ret = ioctl(audio_fd, I2S_AO_DISABLE, NULL);
}
close(fd_file);
fd_file = -1;
close(audio_fd);
audio_fd = -1;
return 0;
}
The above is I2S A data capture test of DEMO
arm-linux# ./i2s_test -h
-h :help
-C file_name :capture
-P file_name :playback
-r n :n=sample rate(8000/16000/32000/44100/48000)
-v n :n=input volume(0~100)
-s n :n=0/1/2/3(0=mic_in;1=line_in;2=speaker_out;3=line_out)
-b n :n=buffer size
-p n :n=period_size
capture example: ./i2s_test -C test.dat -r 44100 -v 80 -s 1 -p 1024 -b 8196
playback example: ./i2s_test -P test.dat -r 44100 -s 2 -p 1024 -b 8196
getopt_long Parameter acquisition of For details, please refer to the above code implementation
边栏推荐
猜你喜欢
NR PUCCH format0 sequence generation and detection mechanism
学历是一张通行证,门票,你有了它,可以踏入更高层次的环境里
Not many people can finally bring their interests to college graduation
单片机现在可谓是铺天盖地,种类繁多,让开发者们应接不暇
【順利畢業】[1]-遊覽 [學生管理信息系統]
Notes on C language learning of migrant workers majoring in electronic information engineering
Exception handling of arm
Oracle数据库 SQL语句执行计划、语句跟踪与优化实例
Runtime.getRuntime().gc() 和 Runtime.getRuntime().runFinalization() 的区别
Project cost management__ Topic of comprehensive calculation
随机推荐
Working mode of 80C51 Serial Port
Fundamentals of Electronic Technology (III)_ Integrated operational amplifier and its application__ Basic arithmetic circuit
[untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer
STM32 external interrupt experiment
Successful graduation [2] - student health management system function development...
我想各位朋友都应该知道学习的基本规律就是:从易到难
学历是一张通行证,门票,你有了它,可以踏入更高层次的环境里
All processes of top ten management in project management
STM32 serial communication principle
2020-08-23
开学实验里要用到mysql,忘记基本的select语句怎么玩啦?补救来啦~
Emballage automatique et déballage compris? Quel est le principe?
Serial communication based on 51 single chip microcomputer
2020-08-23
How does the memory database give full play to the advantages of memory?
当你需要使用STM32某些功能,而51实现不了时, 那32自然不需要学
UCI and data multiplexing are transmitted on Pusch - Part I
Serial port programming
CEF download, compile project
[successful graduation] [1] - visit [student management information system]