Closed matatata closed 2 years ago
I have taken over for Carsten as the head of development for FlyWithLua.
I built your fork to test out your pull request and do have a question. Since it looks to me that we are adding in wait states so then even keystrokes are delayed what is the use case?
Thanks Bill
Hi Bill,
I'm not sure about all consequences of usleep but when used without care it indeed could cause all sorts of delays. My usecase is this: I have a MacBookPro i9 and when running X-Plane the fans are running at fullspeed and rendering is at least 60FPS or more (depending on the airplane, Vsync on or off and the refresh rate of the display...). I was looking for a possibility to literally slow it down in order to keep the cpu/gpu cooler and the fans quiter. So I wrote the script fps_limit.lua script
When toggled via a command it will adaptively introduce delays if the FPS is larger than 25 which is enough for me. Turns out I do not notice any bad behaviour but indeed the system runs cooler and quieter now.
I'd totally understand if you think my addition is not a good fit for FlyWithLua.. I'm unsure about it myself now. I rewrote my little program in C anyway and I might publish it for other users like me.
Hi Bill,
I looked into the topic again and the sleep method I used usleep(nanos)
is neither portable nor up to date. The best code I could find on the internet is this:
#ifdef WIN32
#include <windows.h>
#elif _POSIX_C_SOURCE >= 199309L
#include <time.h> // for nanosleep
#else
#include <unistd.h> // for usleep
#endif
void sleep_ms(int milliseconds) // cross-platform sleep function
{
#ifdef WIN32
Sleep(milliseconds);
#elif _POSIX_C_SOURCE >= 199309L
struct timespec ts;
ts.tv_sec = milliseconds / 1000;
ts.tv_nsec = (milliseconds % 1000) * 1000000;
nanosleep(&ts, NULL);
#else
usleep(milliseconds * 1000);
#endif
}
I did not try it yet, but I will. By now I think FlyWithLua does not really need a sleep(ms) function, but on the other hand if it was portable and reliable then why not. What do you think? Feel free to close the pull request. Best regards, Matteo
Did you know you could do this to do the same thing with no extra programs?
X-Plane.exe --lock_fr=030
Did you know you could do this to do the same thing with no extra programs?
X-Plane.exe --lock_fr=030
Oh that's interesting thank you. No I did not know that. I just tried it (I'm on macOS) and indeed the FPS are locked. However CPU and GPU temperatures still rise to quite high temperatures. Whereas when I throttle the flight loop using usleep(microseconds) the whole X-Plane process is slowed down and the temperatures really go down. I'm not sure what side effects my approach has in terms of flight model etc., but I'll keep playing around with it. I really enjoy flying with my fanless MacBook Air M1, but on the intel machine it is no joy at all. I don't blame X-Plane for it, but maybe I'll dare asking a X-Plane developer if they have an idea.
I'll close this pull request as FlyWithLua has really nothing to do with and I guess there's no need for a sleep function. Thank you for your time and hints.
Hi Carsten, First of all thank you for the wonderful FylWithLua!
I've added a sleep function using usleep(nanos). I needed it for my Lua script "fps_limit.lua", that limits the FPS to 25 in order to save energy and save me from loud fan noise. I did not add it to the documentation yet because I'm not even sure what you think about it. I just thought you may like the idea. I have also implemented my FPS limit function as a plugin written in C, but I loved how I could prototype it with FlyWithLua. Obviously it's a kind of dangerous function, because it suspends threads... so feel free to ignore my pull request.