当前位置:网站首页>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
边栏推荐
- STM32 port multiplexing and remapping
- 单片机职业发展:能做下去的都成牛人了,熬不动就辞职或者改行了
- The third paper of information system project manager in soft examination
- Happy Dragon Boat Festival—— Zongzi written by canvas~~~~~
- MYSQL数据库底层基础专栏
- An executable binary file contains more than machine instructions
- 01仿B站项目业务架构
- Serial port programming
- STM32 interrupt priority management
- 01 business structure of imitation station B project
猜你喜欢

There is no shortcut to learning and development, and there is almost no situation that you can learn faster by leading the way

学历是一张通行证,门票,你有了它,可以踏入更高层次的环境里

Pymssql controls SQL for Chinese queries

IDEA远程断点调试jar包项目

CEF下载,编译工程

There is no specific definition of embedded system

Happy Dragon Boat Festival—— Zongzi written by canvas~~~~~

SSB Introduction (PbCH and DMRs need to be supplemented)

Windows下MySQL的安装和删除
![[successful graduation] [1] - visit [student management information system]](/img/91/72cdea3eb3f61315595330d2c9016d.png)
[successful graduation] [1] - visit [student management information system]
随机推荐
How does the memory database give full play to the advantages of memory?
Quelle langue choisir pour programmer un micro - ordinateur à puce unique
内存数据库究竟是如何发挥内存优势的?
Learn the contents of 5g toolbox supporting NR through the NR resources provided by MATLAB
Fundamentals of Electronic Technology (III)__ Logic gate symbols in Chapter 5
CEF download, compile project
Stm32f407 key interrupt
uniapp 实现微信小程序全局分享及自定义分享按钮样式
You need to use MySQL in the opening experiment. How can you forget the basic select statement? Remedy is coming~
01 business structure of imitation station B project
自动装箱与拆箱了解吗?原理是什么?
SCM is now overwhelming, a wide variety, so that developers are overwhelmed
Raspberry pie installation SciPy
2020-08-23
Hal library sets STM32 clock
MySQL的简单使用(增删改查)
UCI and data multiplexing are transmitted on Pusch - determine the bit number of harqack, csi1 and csi2 (Part II)
Which language should I choose to program for single chip microcomputer
All processes of top ten management in project management
Pymssql controls SQL for Chinese queries