A little Python program to follow the 20-20-20 rule and prevent eye strain
I have recently started to complain some eye strain and headache, after remaining many hours in front of my laptop screen. Hence I discovered the 20-20-20 rule and wrote a script in Python to automatic show a black fullscreen every 20 minutes for 20 seconds in order to force me to look away at something that is 20 feet (about 6,10 meters) from me. Besides the program makes a sound when the screen turns on again.
When I am working on PC, it’s difficult for me to relax my eye and take a break above all if I have many things to do, so a simple recurrent timer or the sedentary tracker alert of my smart bracelet don’t work at all. I have the necessity to force me to make a break like the 20-20-20 rule says.
The 20-20-20 rule
Here there is a complete article on this matter: How Does the 20-20-20 Rule Prevent Eye Strain?, the rule consists every 20 minutes spent using a screen, you should try to look away at something that is 20 feet away from you for a total of 20 seconds.
I don’t know if the researches reported there are true, but I found some symptoms that happen even to me (highlighted in bold):
- dry eyes;
- watery eyes;
- blurred vision;
- doubled vision;
- soreness in the neck, shoulders, or back;
- sensitivity to light;
- trouble concentrating;
- difficulty keeping eyes open.
And using this method I have almost solved them!
I want a program that every 20 minutes turn off the screen of my computer, then turns on it and makes a sound when 20 seconds have passed, with the ability to deactivate the rule if my audio card is busy (useful if I am watching videos).
First I tried with Bash and cron using:
- xrand (too parameters to set up, little portability to other computers)
xrandr --output HDMI2 --off sleep 20 xrandr --output HDMI2 --on xrandr --output HDMI2 --mode 1920x1080
- chvt (necessity to change the permission at the binary in order to execute it and little suited)
sudo chmod u+s /bin/chvt #to allow the user to execute it PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin export DISPLAY=:0.0 status=`/usr/bin/pacmd list-sink-inputs | grep -c "state: RUNNING"` /usr/bin/pacmd list-sink-inputs >> /home/nicola/prova.txt echo $status >> /home/nicola/prova.txt if [ "`/usr/bin/pacmd list-sink-inputs | grep -c 'state: RUNNING'`" -lt "1" ]; then chvt 3; sleep 20; chvt 7; fi
Second I tried with Python and I was very satisfied with the solution.
I wrote it in Python 3.6 and the following packages1 are required:
- pacmd (i.e. Pulseaudio2 and strictly connected to the OS in use).
At the beginning of the file there are three items to adapt the program at your needs:
# SETTINGS: size = (1920, 1080) # size of your monitor check_audio = ‘ON’ # with 'ON' the script doesn't turn off the screen if your audio card is working duration = 1200 # the time interval in seconds
You can find and download my script on GitHub at the following link:
- GitHub: ../eye_strain_reducer
How it works
Simple execute it from a terminal or create a launcher in this manner:
Then it prints in the terminal:
- the time when it starts;
- the time and the correct action every the duration setted
- --> OKAY!! (if turns off the screen)
- --> NO WAY (if doesn't turn off the screen because check_audio option is ON and the number of the sink is greater than or equal to 1).
Here is an example:
$ python3.6 eye_strain_reducer.py 19:35:11–> Starting… b’0\n’ 19:55:11–> OKAY!! b’1\n’ 20:15:34–> NO WAY!! ...