How to get value of optarg without using command line? - c++

I used getopt() function to get value of optarg parameters in Visual Studio. However, following to document of GNU, getopt() is a function that used to parse command-line options of the Unix/POSIX style and I can not use commandline in Visual Studio. So, how can I get the optarg in VS without using command line or getopt().
Here is my code:
int ch;
extern char* optarg;/*
extern int optind, opterr;*/
char* testfn;
bool testflag = false;
double prd = 60 * 24; // minutes of a day
int maxstep = 100; // # em steps
int Z = 2; // # mixtures
double initscale = 0.25 * prd; // 6 hours
while (optind < argc) {
if ((ch = getopt(argc, argv, "t:m:z:s:")) != -1) {
switch (ch) {
case 't':
testflag = true;
testfn = optarg;
break;
case 'm':
maxstep = atoi(optarg);
break;
case 'z':
Z = atoi(optarg);
break;
case 's':
initscale = atof(optarg);
break;
default:
usage();
}
}
else {
// Regular argument
//code to handle the argument
optind++; // Skip to the next argument
}
}

Related

key and mouse input

i have a question:
(i use winows, visual studio)
i making a program and i need to read keyboard and mouse input istantly so like getch() (because if i use cin i need to press fullstop new line evry time).
i have this:
char p;
while (running) {
if (_kbhit()) {
p = _getch();
switch (p) {
case 'w':
mx = -1;
map(player, mx, 0);
break;
case 'a':
my = -1;
map(player, 0, my);
break;
case 's':
mx = 1;
map(player, mx, 0);
break;
case 'd':
my = 1;
map(player, 0, my);
break;
case 27:
running = false;
break;
}
}
}
the problem is with _getch() i can't read mouse imput.
so how i can get this?

Why spi.recieve() function always return zero in SPI of mbed?

I want to set up a SPI connection between two mbed boards and I found that the spi.recieve() function which is used to detect the data transmission from the master always return zero and the connection can't be established.
I try to remove 'if' in my code, and it works, but I still don't understand why the function returns zero.
here is some code:
while (1)
{
transmit_value = 100 * voltage;
spislave.reply(transmit_value);
if (spislave.receive())
{
receive_value = spislave.read();
}
here is my completed code:
#include <mbed.h>
Serial pc(USBTX, USBRX);
SPISlave spislave(p5, p6, p7, p8);
AnalogIn voltage(p20);
PwmOut led(p21);
BusOut display(p9, p10, p11, p12, p13, p14, p15, p16);
DigitalOut GroundLeft(p27);
DigitalOut GroundRight(p28);
int main()
{
char transmit_value = 50;
char receive_value = 60;
spislave.format(8,0);
spislave.frequency(1000000);
while (1)
{
transmit_value = 100 * voltage;
spislave.reply(transmit_value);
if (!spislave.receive())
{
receive_value = spislave.read();
}
led.write(receive_value / 100.0);
int leftnum = receive_value / 10;
int rightnum = receive_value % 10;
pc.printf("%d\n", receive_value);
GroundLeft = 0;
GroundRight = 1;
switch (leftnum)
{
case 0:
display = 0x3F;
break;
case 1:
display = 0x06;
break;
case 2:
display = 0x5B;
break;
case 3:
display = 0x4F;
break;
case 4:
display = 0x66;
break;
case 5:
display = 0x6D;
break;
case 6:
display = 0x7D;
break;
case 7:
display = 0x07;
break;
case 8:
display = 0x7F;
break;
case 9:
display = 0x6F;
break;
}
wait(0.01);
GroundLeft = 1;
GroundRight = 0;
switch (rightnum)
{
case 0:
display = 0x3F;
break;
case 1:
display = 0x06;
break;
case 2:
display = 0x5B;
break;
case 3:
display = 0x4F;
break;
case 4:
display = 0x66;
break;
case 5:
display = 0x6D;
break;
case 6:
display = 0x7D;
break;
case 7:
display = 0x07;
break;
case 8:
display = 0x7F;
break;
case 9:
display = 0x6F;
break;
}
wait(0.01);
}
}
Why are you not following the reference code? https://os.mbed.com/docs/mbed-os/v5.9/reference/spislave.html
With SPI you have to understand that RX and TX happen at the same time and that the operation will take some amount of time.
.reply() sets up what to send, .receive() checks if something has been received (which probably won't be immediately), and .read() gets data from SPI controller.

How to enable audio of two ffplay windows simultaneously?(How can I hear audio of the video files at the same time?)

When more than one ffplay-window is running only the audio of one of them is enable , But I need to hear all of them at the same time.
For example I run two command-lines separately like this:
//command line 1
ffplay -i video1.avi
//command line 2
ffplay -i video2.avi
How can I hear audio of the video files at the same time?
I did not find anything about that in the ffplay documents, So I want to change the ffplay source code.
this is event_loop function (in ffplay.c):
static void event_loop(VideoState *cur_stream)
{
SDL_Event event;
double incr, pos, frac;
for (;;) {
double x;
refresh_loop_wait_event(cur_stream, &event);
switch (event.type) {
case SDL_KEYDOWN:
if (exit_on_keydown) {
do_exit(cur_stream);
break;
}
switch (event.key.keysym.sym) {
case SDLK_ESCAPE:
case SDLK_q:
do_exit(cur_stream);
break;
case SDLK_f:
toggle_full_screen(cur_stream);
cur_stream->force_refresh = 1;
break;
case SDLK_p:
case SDLK_SPACE:
toggle_pause(cur_stream);
break;
case SDLK_m:
toggle_mute(cur_stream);
break;
case SDLK_KP_MULTIPLY:
case SDLK_0:
update_volume(cur_stream, 1, SDL_VOLUME_STEP);
break;
case SDLK_KP_DIVIDE:
case SDLK_9:
update_volume(cur_stream, -1, SDL_VOLUME_STEP);
break;
case SDLK_s: // S: Step to next frame
step_to_next_frame(cur_stream);
break;
case SDLK_a:
stream_cycle_channel(cur_stream, AVMEDIA_TYPE_AUDIO);
break;
case SDLK_v:
stream_cycle_channel(cur_stream, AVMEDIA_TYPE_VIDEO);
break;
case SDLK_c:
stream_cycle_channel(cur_stream, AVMEDIA_TYPE_VIDEO);
stream_cycle_channel(cur_stream, AVMEDIA_TYPE_AUDIO);
stream_cycle_channel(cur_stream, AVMEDIA_TYPE_SUBTITLE);
break;
case SDLK_t:
stream_cycle_channel(cur_stream, AVMEDIA_TYPE_SUBTITLE);
break;
case SDLK_w:
#if CONFIG_AVFILTER
if (cur_stream->show_mode == SHOW_MODE_VIDEO && cur_stream->vfilter_idx < nb_vfilters - 1) {
if (++cur_stream->vfilter_idx >= nb_vfilters)
cur_stream->vfilter_idx = 0;
} else {
cur_stream->vfilter_idx = 0;
toggle_audio_display(cur_stream);
}
#else
toggle_audio_display(cur_stream);
#endif
break;
case SDLK_PAGEUP:
if (cur_stream->ic->nb_chapters <= 1) {
incr = 600.0;
goto do_seek;
}
seek_chapter(cur_stream, 1);
break;
case SDLK_PAGEDOWN:
if (cur_stream->ic->nb_chapters <= 1) {
incr = -600.0;
goto do_seek;
}
seek_chapter(cur_stream, -1);
break;
case SDLK_LEFT:
incr = -10.0;
goto do_seek;
case SDLK_RIGHT:
incr = 10.0;
goto do_seek;
case SDLK_UP:
incr = 60.0;
goto do_seek;
case SDLK_DOWN:
incr = -60.0;
do_seek:
if (seek_by_bytes) {
pos = -1;
if (pos < 0 && cur_stream->video_stream >= 0)
pos = frame_queue_last_pos(&cur_stream->pictq);
if (pos < 0 && cur_stream->audio_stream >= 0)
pos = frame_queue_last_pos(&cur_stream->sampq);
if (pos < 0)
pos = avio_tell(cur_stream->ic->pb);
if (cur_stream->ic->bit_rate)
incr *= cur_stream->ic->bit_rate / 8.0;
else
incr *= 180000.0;
pos += incr;
stream_seek(cur_stream, pos, incr, 1);
} else {
pos = get_master_clock(cur_stream);
if (isnan(pos))
pos = (double)cur_stream->seek_pos / AV_TIME_BASE;
pos += incr;
if (cur_stream->ic->start_time != AV_NOPTS_VALUE && pos < cur_stream->ic->start_time / (double)AV_TIME_BASE)
pos = cur_stream->ic->start_time / (double)AV_TIME_BASE;
stream_seek(cur_stream, (int64_t)(pos * AV_TIME_BASE), (int64_t)(incr * AV_TIME_BASE), 0);
}
break;
default:
break;
}
break;
case SDL_VIDEOEXPOSE:
cur_stream->force_refresh = 1;
break;
case SDL_MOUSEBUTTONDOWN:
if (exit_on_mousedown) {
do_exit(cur_stream);
break;
}
case SDL_MOUSEMOTION:
if (cursor_hidden) {
SDL_ShowCursor(1);
cursor_hidden = 0;
}
cursor_last_shown = av_gettime_relative();
if (event.type == SDL_MOUSEBUTTONDOWN) {
x = event.button.x;
} else {
if (event.motion.state != SDL_PRESSED)
break;
x = event.motion.x;
}
if (seek_by_bytes || cur_stream->ic->duration <= 0) {
uint64_t size = avio_size(cur_stream->ic->pb);
stream_seek(cur_stream, size*x/cur_stream->width, 0, 1);
} else {
int64_t ts;
int ns, hh, mm, ss;
int tns, thh, tmm, tss;
tns = cur_stream->ic->duration / 1000000LL;
thh = tns / 3600;
tmm = (tns % 3600) / 60;
tss = (tns % 60);
frac = x / cur_stream->width;
ns = frac * tns;
hh = ns / 3600;
mm = (ns % 3600) / 60;
ss = (ns % 60);
av_log(NULL, AV_LOG_INFO,
"Seek to %2.0f%% (%2d:%02d:%02d) of total duration (%2d:%02d:%02d) \n", frac*100,
hh, mm, ss, thh, tmm, tss);
ts = frac * cur_stream->ic->duration;
if (cur_stream->ic->start_time != AV_NOPTS_VALUE)
ts += cur_stream->ic->start_time;
stream_seek(cur_stream, ts, 0, 0);
}
break;
case SDL_VIDEORESIZE:
screen = SDL_SetVideoMode(FFMIN(16383, event.resize.w), event.resize.h, 0,
SDL_HWSURFACE|(is_full_screen?SDL_FULLSCREEN:SDL_RESIZABLE)|SDL_ASYNCBLIT|SDL_HWACCEL);
if (!screen) {
av_log(NULL, AV_LOG_FATAL, "Failed to set video mode\n");
do_exit(cur_stream);
}
screen_width = cur_stream->width = screen->w;
screen_height = cur_stream->height = screen->h;
cur_stream->force_refresh = 1;
break;
case SDL_QUIT:
case FF_QUIT_EVENT:
do_exit(cur_stream);
break;
case FF_ALLOC_EVENT:
alloc_picture(event.user.data1);
break;
default:
break;
}
}
}
But I can not find the command that cause disable audio when change active window or lost focus in event_loop function.
You can add multiple input files for ffplay and then "map" different streams together, but if you have alsa mixer as default audio device the system should mix the audio automatically for you. Modifying ffplay is not a trivial task
this is how you select output device
https://ffmpeg.org/ffmpeg-devices.html#Examples-7
this is how you "concatinate" mutiple inputs:
https://trac.ffmpeg.org/wiki/Concatenate

GetFileAttributes Function

#include "stdafx.h"
#include <Windows.h>
#include <conio.h>
int _tmain(int argc, _TCHAR* argv[])
{
DWORD d = GetFileAttributes(argv[0]);
_TCHAR* temp;
printf("%d\n", d);
switch(d)
{
case 2048: temp = L"Compressed"; break;
case 32: temp = L"Archive"; break;
case 16: temp = L"Directory"; break;
case 16384: temp = L"Encrypted"; break;
case 2: temp = L"Hidden"; break;
case 128: temp = L"Normal"; break;
case 1: temp = L"Readonly"; break;
case 4: temp = L"System"; break;
case 256: temp = L"Temporary"; break;
default: temp = L"Error or unsupported attribute"; break;
}
_tprintf(temp);
getch();
return 0;
}
what's wrong with this code? I always get 32 in d, even when I launch it with no attributes?
I'm using visual studio 2010.
Thank you!
argv[0] is the name of your executable program. Simply set the index to 1 (ensure it exists). You may also want to use a bitwise AND operation to determine if a flag is set.

Segmentation fault - Core Dumped error while using getopt

I know this queston has been asked multiple times, but still I am unable to figure this out
#include<stdio.h>
#include<getopt.h>
int ch;
int queue_time=60;
int thread_num=4;
char *scheduling_algo="FCFS";
extern char *optarg;
int port=8080;
int debug_flag,h_flag,l_flag;
int main(int argc,char *argv[])
{
while ((ch = getopt(argc, argv, "dhlprtns")) != -1)
switch(ch)
{
case 'd':
debug_flag=atoi(optarg); /* print address in output */
break;
case 'h':
h_flag=atoi(optarg);
break;
case 'l':
l_flag=atoi(optarg);;
break;
case 'p':
port = atoi(optarg);
break;
case 'r':
printf("%s",optarg);
break;
case 't':
queue_time = atoi(optarg);
break;
case 'n':
thread_num = atoi(optarg);
break;
case 's':
scheduling_algo = optarg;
break;
default:
printf("nothing was passed");
}
printf("%d",queue_time);
printf("%d",debug_flag);
printf("%d",h_flag);
printf("%d",l_flag);
}
I am executing my program using the following command
./a.out -d -h -l -t 55
I am getting the core dumped error . I read a few examples on google, but still I am facing this problem. Can anyone please help?
You need to read the man page for getopt()
while ((ch = getopt(argc, argv, "dhlprtns")) != -1)
^^^^^^^^
This does not correspond to the way you are using the arguments. You
want colons ":" after the flags which expect arguments. In your code
"d" is not followed by a colon and yet you seem to want an value for it:
case 'd':
debug_flag=atoi(optarg); /* print address in output */
break;
So what is happening is you are calling atoi(0) and this is seg faulting.
Here's the example from the man page, note how "b" is not followed by a
colon while "f" is.
#include <unistd.h>
int bflag, ch, fd;
bflag = 0;
while ((ch = getopt(argc, argv, "bf:")) != -1) {
switch (ch) {
case 'b':
bflag = 1;
break;
case 'f':
if ((fd = open(optarg, O_RDONLY, 0)) < 0) {
(void)fprintf(stderr,
"myname: %s: %s\n", optarg, strerror(errno));
exit(1);
}
break;
case '?':
default:
usage();
}
}
argc -= optind;
argv += optind;
This may be of use to others: You will also get a segfault if you specify an option letter as both without colon, and with colon eg "dabcd:e" - in this case "d" occurs with and without colon.... and then use that option letter.
It appears getopt and its variants do not check for this conflict and return an error!