Skip to content

Instantly share code, notes, and snippets.

@jmichalicek
Last active July 21, 2017 13:33
Show Gist options
  • Save jmichalicek/94e3176cf14c820c6c8f2aaebb836929 to your computer and use it in GitHub Desktop.
Save jmichalicek/94e3176cf14c820c6c8f2aaebb836929 to your computer and use it in GitHub Desktop.
Tap sleeping/backlight powered down raspberry pi touchscreen even in console to wake it
"""
Raspberry pi touchscreens get blanked and then later the backlight turned off.
When in X of some sort, taps on the screen can turn it back on. When in a console
taps on the touchscreen are not seen and the display stays asleep.
This script polls mouse inputs in /dev/input for any action on the raspberry pi touchscreen
if there is action, update the file which controls the backlight power to
turn the backlight back on.
This should be tweaked more to not be constantly writing to the bl_power file, possibly monitor
to see if it has changed.
Also need to deal with screen blanking by sending signal to tty
https://raspberrypi.stackexchange.com/a/12475
"""
import serial
mouse = file('/dev/input/mouse0')
TOUCHSCREEN_POWER_FILE = '/sys/class/backlight/rpi_backlight/bl_power'
while True:
status, dx, dy = tuple(ord(c) for c in mouse.read(3))
if status:
# taking a guess at the serial bit here
# This is to unblank the screen
with serial.Serial('/dev/tty1'):
ser.write(b'\033[13]')
# This is to give power back to the touchscreen backlight
with open(TOUCHSCREEN_POWER_FILE, 'w') as f:
f.write('0')
# A systemd unit file to run the mouse monitor
# update execstart to point to wherever the script is actually saved
# Put this into /lib/systemd/system/ or possibly /etc/systemd/user/ to only start when
# your user is logged in
[Unit]
Description=Touchscreen Mouse Monitor
After=multi-user.target
[Service]
Type=idle
ExecStart=python /home/pi/mousemon.py
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment