Skip to content

Instantly share code, notes, and snippets.

@andersevenrud
Last active November 1, 2023 20:43
Show Gist options
  • Save andersevenrud/4a7e7e9517c0e621c4cbf476e21e5f30 to your computer and use it in GitHub Desktop.
Save andersevenrud/4a7e7e9517c0e621c4cbf476e21e5f30 to your computer and use it in GitHub Desktop.
How to rotate (invert) Raspberry Pi 7" Touch LCD screen & input (RasPi 1, 2, 3)

Rotate Official Raspberry Pi 7" Touch Screen

Shows how to easily rotate (in this case invert to make orientation correct within a case) the Raspberry Pi 7" Touch Screen.

This will also update the calibration for the touch screen so it will work correctly.

References:

Personal notes

To make both touch and backlight controls work:

First enable I2C via raspi-config.

# /boot/config.txt
dtoverlay=rpi-backlight,i2c0,i2c1

And to control backlight via xscreensaver:

#!/bin/sh
#
# /usr/local/bin/xscreensaver-backlight
#
# Add this to the session autostart after launching xscreensaver
#

process() {
   while read line; do
       case "$line" in
           UNBLANK*)
               sh -c 'echo "0" > /sys/class/backlight/rpi_backlight/bl_power'
           ;;
           BLANK*)
               sh -c 'echo "1" > /sys/class/backlight/rpi_backlight/bl_power'
           ;;
       esac
   done
}

xscreensaver-command -watch | process

Which needs a udev rule to allow access to the system backlight:

# /etc/udev/rules.d/99-backlight.rules
SUBSYSTEM=="backlight",RUN+="/bin/chmod 666 /sys/class/backlight/%k/brightness /sys/class/backlight/%k/bl_power"
# /boot/config.txt
#
# Add the following contents to the bottom of the file:
# This will update rotation in kernel mode.
#
# 1 for 90deg left
# 2 for 180deg (inverted)
# 3 for 90deg right
lcd_rotate=2
# /boot/cmdline.txt
#
# Add the following to the end of the line of this file:
# fbcon=rotate:2
#
# This will rotate the console. Same value as in boot.txt
# You should end up with something looking like this:
#
console=serial0,115200 console=tty1 root=PARTUUID=556e127a-02 rootfstype=ext4 fsck.repair=yes rootwait cfg80211.ieee80211_regdom=GB fbcon=rotate:2
# /usr/share/X11/xorg.conf.d/99-custom.conf
#
# Create this file and insert content below.
# This will update rotation in Xorg.
#
Section "Monitor"
# This is the standard value for LCD display
Identifier "DSI-1"
# Inverts the screen (alternatively set to "left" or "right")
Option "Rotate" "inverted"
EndSection
Section "InputClass"
Identifier "libinput touchscreen catchall"
MatchIsTouchscreen "on"
MatchDevicePath "/dev/input/event*"
Driver "libinput"
# 180deg inversion calibration of touch input
Option "CalibrationMatrix" "-1 0 1 0 -1 1 0 0 1"
# 90deg left rotation of touch input
#Option "CalibrationMatrix" "0 -1 1 1 0 0 0 0 1"
# 90deg right rotation of touch input
#Option "CalibrationMatrix" "0 1 0 -1 0 1 0 0 1"
# This should be the default for this module
#Option "CalibrationMatrix" "1 0 0 0 1 0 0 0 1"
EndSection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment