Abandoned, obsole. Macrule commented Apr 13, 2013. As the Raspberry PI has no proper standby mode from which it can be woken up again, it's so far impossible to turn off the attached display automatically. This change set allows the PI's video output to be powered down, and back on again (works for HDMI and SDTV).
I have some source code that was compiled on Windows. I am converting it to run on Red Hat Linux.
The source code has included the <windows.h>
header file and the programmer has used the Sleep()
function to wait for a period of milliseconds. This won't work on the Linux.
However, I can use the sleep(seconds)
function, but that uses integer in seconds. I don't want to convert milliseconds to seconds. Is there a alternative sleep function that I can use with gcc compiling on Linux?
6 Answers
Yes - older POSIX standards defined usleep()
, so this is available on Linux:
DESCRIPTION
The usleep() function suspends execution of the calling thread for (at least) usec microseconds. The sleep may be lengthened slightly by any system activity or by the time spent processing the call or by the granularity of system timers.
usleep()
takes microseconds, so you will have to multiply the input by 1000 in order to sleep in milliseconds.
usleep()
has since been deprecated and subsequently removed from POSIX; for new code, nanosleep()
is preferred:
DESCRIPTION
nanosleep()
suspends the execution of the calling thread until either at least the time specified in *req
has elapsed, or the delivery of a signal that triggers the invocation of a handler in the calling thread or that terminates the process.
The structure timespec is used to specify intervals of time with nanosecond precision. It is defined as follows:
![Sleep Sleep](https://spellfoundry.com/wp/wp-content/uploads/2016/05/Sleepy-Pi-2-Concept-Diagram-2-1000.png)
Alternatively to usleep()
, which is not defined in POSIX 2008 (though it was defined up to POSIX 2004, and it is evidently available on Linux and other platforms with a history of POSIX compliance), the POSIX 2008 standard defines nanosleep()
:
nanosleep
- high resolution sleep
The nanosleep()
function shall cause the current thread to be suspended from execution until either the time interval specified by the rqtp
argument has elapsed or a signal is delivered to the calling thread, and its action is to invoke a signal-catching function or to terminate the process. The suspension time may be longer than requested because the argument value is rounded up to an integer multiple of the sleep resolution or because of the scheduling of other activity by the system. But, except for the case of being interrupted by a signal, the suspension time shall not be less than the time specified by rqtp
, as measured by the system clock CLOCK_REALTIME.
The use of the nanosleep()
function has no effect on the action or blockage of any signal.
Beyond usleep, the humble select with NULL file descriptor sets will let you pause with microsecond precision, and without the risk of SIGALRM
complications.
sigtimedwait and sigwaitinfo offer similar behavior.
Not the answer you're looking for? Browse other questions tagged clinuxsleep or ask your own question.
is it possible to get the Rasperry PI into display sleep mode (After pressing the touchscreen reactivate)?
This code is only for disable the display
sudo sh -c 'echo 1 > /sys/class/backlight/rpi_backlight/bl_power'
Thankyou!
2 Answers
So for everyone with the same problem as me, I found a solution:
sudo XAUTHORITY=/home/pi/.Xauthority DISPLAY=:0.0 xset dpms force on
for turning on
sudo XAUTHORITY=/home/pi/.Xauthority DISPLAY=:0.0 xset dpms force off
for turning off the display in the ssh remote connection.
You can set the following in the .xinitrc file if you are running X:
To turn of the terminal screen, you could do:
More information on screen blanking on: http://www.cyberciti.biz/tips/linux-disable-screen-blanking-screen-going-blank.html