当前位置:网站首页>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 running lantern experiment - library function version
- Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction
- 01 business structure of imitation station B project
- 内存数据库究竟是如何发挥内存优势的?
- Timer and counter of 51 single chip microcomputer
- Serial port programming
- Code word in NR
- Stm32f04 clock configuration
- UCI and data multiplexing are transmitted on Pusch - placement of data and UCI positions (Part III)
- UCI and data multiplexing are transmitted on Pusch (Part 4) --small block lengths
猜你喜欢

UCI and data multiplexing are transmitted on Pusch (Part V) -- polar coding

You need to use MySQL in the opening experiment. How can you forget the basic select statement? Remedy is coming~

2.Elment Ui 日期选择器 格式化问题

Serial communication based on 51 single chip microcomputer

Fundamentals of Electronic Technology (III)__ Fundamentals of circuit analysis__ Basic amplifier operating principle

没有多少人能够最终把自己的兴趣带到大学毕业上

Mysql database underlying foundation column

yocto 技术分享第四期:自定义增加软件包支持

SCM career development: those who can continue to do it have become great people. If they can't endure it, they will resign or change their careers

Mobile phones are a kind of MCU, but the hardware it uses is not 51 chip
随机推荐
2021-10-27
El table X-axis direction (horizontal) scroll bar slides to the right by default
Eight working modes of stm32gpio and chip naming rules
2021-10-28
It is difficult to quantify the extent to which a single-chip computer can find a job
Application of 51 single chip microcomputer timer
Fundamentals of Electronic Technology (III)__ Chapter 1 resistance of parallel circuit
Seven sorting of ten thousand words by hand (code + dynamic diagram demonstration)
You need to use MySQL in the opening experiment. How can you forget the basic select statement? Remedy is coming~
Design of charging pile mqtt transplantation based on 4G EC20 module
Blue Bridge Cup for migrant workers majoring in electronic information engineering
UCI and data multiplexing are transmitted on Pusch (Part V) -- polar coding
自动装箱与拆箱了解吗?原理是什么?
Fundamentals of Electronic Technology (III)__ Logic gate symbols in Chapter 5
[Li Kou brush question notes (II)] special skills, module breakthroughs, classification and summary of 45 classic questions, and refinement in continuous consolidation
ADS simulation design of class AB RF power amplifier
Which language should I choose to program for single chip microcomputer
一个可执行的二进制文件包含的不仅仅是机器指令
UCI and data multiplexing are transmitted on Pusch - Part I
03 FastJson 解决循环引用