I need to write an easy function that only return bool answer.
if there is joystick plugged in the computer - true,else - false.
how can i do this?
I have tried to write this code, but i only get the num of deviced that plug by usb to my computer, i need to know that this usb is joystick.
UINT pNum = 0;
RAWINPUTDEVICELIST* arr = new RAWINPUTDEVICELIST;
PRAWINPUTDEVICELIST pRawInputDeviceList = arr;
GetRawInputDeviceList(pRawInputDeviceList, &pNum, sizeof(RAWINPUTDEVICELIST));
std::cout << "Num entered: " << pNum << std::endl;
Use the joyGetPos function to determine whether a given joystick is physically attached to the system. If the specified joystick is not connected, joyGetPos returns a JOYERR_UNPLUGGED error value.
The joyGetNumDevs function returns the number of joysticks supported by the current driver or zero if no driver is installed.
Related
I have written a small c++ program that receives data from the USRP. The program can receive the I/Q data and show it on a spectrum analyzer. The receiver LED is not always green though. It sorts of blinking and dimming. I suspect there is a rate mismatch between the computer and the USRP. Could this be the case? How does one make sure that the computer consumes the samples at the same rate as the USRP is acquiring them? Below is a thread function I use for the I/Q signal acquisition.
void
USRPDriver::RxEventLoop()
{
uhd::rx_metadata_t md;
uhd::stream_cmd_t stream_cmd(uhd::stream_cmd_t::STREAM_MODE_NUM_SAMPS_AND_DONE);
stream_cmd.stream_now = true;
stream_cmd.num_samps = 1024;
//std::cout << "Maximum num samps = " << rx_stream->get_max_num_samps() << std::endl;
std::vector<std::complex<float> > fcpxIQ;
fcpxIQ.resize(1024);
usrp->issue_stream_cmd(stream_cmd);
while(true)
{
usrp->issue_stream_cmd(stream_cmd);
size_t num_rx_samps = rx_stream->recv(&fcpxIQ[0], 1024, md);
emit ReceiveIQ(fcpxIQ);
//std::cout << "Rx rate = " << usrp->get_rx_rate(0) << std::endl;
//fcpxIQ.clear();
}
}
you should not use NUM_SAMPS_AND_DONE if you want continuous streaming. That's exactly not the use case it's for: It tells the USRP to stop receiving once 1024 samples have been received.
Simply don't use that mode.
I prepare the input buffer like this
...
buffer->mi.dx = x;
buffer->mi.dy = y;
buffer->mi.mouseData = 0;
buffer->mi.time = 0;
buffer->mi.dwExtraInfo = 0;
buffer->mi.dwFlags = (MOUSEEVENTF_MOVE);
SendInput(1, buffer, sizeof(INPUT));
std::cout << "moving " << buffer->mi.dx << "," << buffer->mi.dy << " relative to current position" << std::endl;
...
Here's the output of this code:
Current Mouse position: 755,286
moving 0,10 relative to current position
Result: 755,294
I need to avoid MOUSEEVENTF_ABSOLUTE dwflag if possible.
Any ides on why this is happening and how can I fix it?
Any ides on why this is happening and how can I fix it?
I'm guessing it's mouse-acceleration. This is mentioned in the documentation:
https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-mouse_event
Relative mouse motion is subject to the settings for mouse speed and acceleration level. An end user sets these values using the Mouse application in Control Panel. An application obtains and sets these values with the SystemParametersInfo function.
Obviously a workaround is to disable mouse-acceleration, but that's not something you should be doing (and if I ever catch any programs on my computer messing with my mouse settings, they find themselves uninstalled very quickly).
A better idea is to use MOUSEEVENTF_ABSOLUTE and apply the (0,+10) offset to the current position in your own code - I don't know why you're so opposed to using MOUSEEVENTF_ABSOLUTE, it just means you need to get the current coordinates first, which is hardly much work.
I'm trying to change a code written in C++ and run it on Raspberry Pi. It is an opensource code called freelss (https://github.com/hairu/freelss) and it's a laser scanner software. I tried to change some parts. For example, I expanded the system with some i2c chips and some buttons. Now I have changed the main.cpp and the main.h to scan the buttons. Everything works fine but now I wanted to have a button that starts the scanning process if I push it.
Scanner (*scanner_Max);
std::cout << "before the first function" << std::endl;
scanner_Max->setTask(Scanner::GENERATE_SCAN);
std::cout << "after the first function" << std::endl;
Now, if I push the button, It says "before the first function" goes into the setTask function and stays there. It never comes back. So I never get the message "after the first function".
void Scanner::setTask(Scanner::Task task)
{
std::cout << "setTask starts" << std::endl;
m_task = task;
}
This is the function in scanner.cpp. I always get the "setTask starts" but it won't come back to the main programm. Can please someone help me with the code?
Greets, Max
In the code you have shown you have not created an instance of Scanner, just a pointer.
Are you missing a:
scanner_Max = new Scanner ();
Or have you just not shown this?
I am writing a simple program (my 1st program) to display the laptop battery, however, I would like to keep it active to monitor the battery %.:
using namespace std;
int main(int argc, char *argv[]) {
id:
SYSTEM_POWER_STATUS spsPwr;
if (GetSystemPowerStatus(&spsPwr)) {
cout << "\nAC Status : " << static_cast<double>(spsPwr.ACLineStatus)
<< "\nBattery Status : " << static_cast<double>(spsPwr.BatteryFlag)
<< "\nBattery Life % : " << static_cast<double>(spsPwr.BatteryLifePercent)
<< endl;
system("CLS");
goto id;
return 0;
}
else return 1;
}
using goto seems to be a bad idea as the CPU utilization jump to 99% ! :(, I am sure this is not the right way to do it.
Any suggestion?
Thanks
while (true) {
// do the stuff
::Sleep(2000); // suspend thread to 2 sec
}
(you are on Windows according to the API function)
see: Sleep
First of all, the issue you are asking about: of course you get 100% CPU usage, since you're asking the computer to try and get and print the power status of the computer as fast it possibly can. And since computers will happily do what you tell them to, well... you know what happens next.
As others have said, the solution is to use an API that will instruct your application to go to sleep. In Windows, which appears to be your platform of choice, that API is Sleep:
// Sleep for around 1000 milliseconds - it may be slightly more since Windows
// is not a hard real-time operating system.
Sleep(1000);
Second, please do not use goto. There are looping constructs in C and you should use them. I'm not fundamentally opposed to goto (in fact, in my kernel-driver programming days I used it quite frequently) but I am opposed to seeing it used when better alternatives are available. In this case the better alternative is a while loop.
Before I show you that let me point out another issue: DO NOT USE THE system function.
Why? The system function executes the command passed to it; on Windows it happens to execute inside the context of the command interpreter (cmd.exe) which supports and internal command called cls which happens to clear the screen. At least on your system. But yours isn't the only system in the world. On some other system, there might be a program called cls.exe which would get executed instead, and who knows what that would do? It could clear the screen, or it could format the hard drive. So please, don't use the system function. It's almost always the wrong thing to do. If you find yourself looking for that command stop and think about what you're doing and whether you need to do it.
So, you may ask, how do I clear the screen if I can't use system("cls")? There's a way to do it which should be portable across various operating systems:
int main(int, char **)
{
SYSTEM_POWER_STATUS spsPwr;
while (GetSystemPowerStatus(&spsPwr))
{
std::string status = "unknown";
if (spsPwr.ACLineStatus == 0)
status = "offline";
else if (spsPwr.ACLineStatus == 1)
status = "online";
// The percent of battery life left is returned as a value
// between 0 and 255 so we normalize it by multiplying it
// by 100.0 and dividing by 255.0 which is ~0.39.
std::cout << "Current Status: " << status << " ("
<< static_cast<int>(spsPwr.BatteryFlag) << "): "
<< 0.39 * static_cast<int>(spsPwr.BatteryLifePercent)
<< "% of battery remaining.\r" << std::flush;
// Sleep for around 1000 milliseconds - it may be slightly more
// since Windows is not a hard real-time operating system.
Sleep(1000);
}
// Print a new line before exiting.
std::cout << std::endl;
return 0;
}
What this does is print the information in a single line, then move back to the beginning of that line, sleep for around one second and then write the next line, overwriting what was previously there.
If the new line you write is shorter than the previous line, you may see some visual artifacts. Removing them should not be difficult but I'll leave it for you as an exercise. Here's a hint: what happens if you output a space where a letter used to be?
In order to do this across lines, you will need to use more advanced techniques to manipulate the console, and this exercise becomes a lot trickier.
You are having 100% CPU usage because your program is always running.
I don't want to get into details, and given that this is your first program, I'll recommend to put a call to usleep before the goto.
And, of course, avoid goto, use a proper loop instead.
int milliseconds2wait = 3000;
while (!flag_exit) {
// code
usleep( 1000 * milliseconds2wait )
}
Update: This is windows, use Sleep instead of usleep:
Sleep( milliseconds2wait );
I am doing a benchmark project between two graphical libraries (SDL, SFML) for my final cs project. I got it almost finished, however when I benchmark the speed of playing sounds, it always returns time taken 0, no matter how many loops he does. Do you know whats wrong with my code? The sound actually plays, however I should probably do some other algorithm.
void playSound()
{
Mix_PlayChannel(-1, sound, 0);
}
void soundBenchmark(int numOfCycles)
{
int time = SDL_GetTicks(), timeRequired;
for(int i = 0; i < numOfCycles; i++) playSound();
timeRequired = SDL_GetTicks() - time;
cout << "Time required for " << numOfCycles << " cycles: " << timeRequired << " seconds.\n";
}
The function Mix_PlayChannel() does not block the execution of the code. The function just send the data to the sound card( or equivalent) and returns.
You are going to have to remember the channel you used with Mix_PlayChannel() and then check periodically with Mix_Playing() whether that channel is playing or not and look at the time.