当前位置:网站首页>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
边栏推荐
- 51 MCU tmod and timer configuration
- Design of charging pile mqtt transplantation based on 4G EC20 module
- 內存數據庫究竟是如何發揮內存優勢的?
- It is difficult to quantify the extent to which a single-chip computer can find a job
- 端午节快乐!—— canvas写的粽子~~~~~
- 干单片机这一行的时候根本没想过这么多,只想着先挣钱养活自己
- Stm32f407 key interrupt
- (1) What is a lambda expression
- Serial port programming
- [male nanny style] teach you to open the first wechat applet
猜你喜欢
Assignment to '*' form incompatible pointer type 'linkstack' {aka '*'} problem solving
CEF下载,编译工程
2021-10-27
端午节快乐!—— canvas写的粽子~~~~~
Not many people can finally bring their interests to college graduation
一个可执行的二进制文件包含的不仅仅是机器指令
[untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer
SSB Introduction (PbCH and DMRs need to be supplemented)
Interruption system of 51 single chip microcomputer
Happy Dragon Boat Festival—— Zongzi written by canvas~~~~~
随机推荐
The third paper of information system project manager in soft examination
I didn't think so much when I was in the field of single chip microcomputer. I just wanted to earn money to support myself first
Drive and control program of Dianchuan charging board for charging pile design
Raspberry pie installation SciPy
Project cost management__ Plan value_ Earned value_ Relationship among actual cost and Countermeasures
An executable binary file contains more than machine instructions
Quelle langue choisir pour programmer un micro - ordinateur à puce unique
Runtime. getRuntime(). GC () and runtime getRuntime(). The difference between runfinalization()
All processes of top ten management in project management
03 FastJson 解决循环引用
C language enumeration type
Notes on C language learning of migrant workers majoring in electronic information engineering
Stm32f04 clock configuration
自動裝箱與拆箱了解嗎?原理是什麼?
端午节快乐!—— canvas写的粽子~~~~~
Chromium Embedded Framework (CEF) 介绍
STM32 serial communication principle
Embedded systems are inherently flawed. Compared with the Internet, there are so many holes that it is simply difficult to walk away from
Successful graduation [2] - student health management system function development...
Schematic diagram and connection method of six pin self-locking switch