I’m working on a project to create a writing device optimized for writing fiction. I figured one of the bigger reasons why I wasn’t writing anymore was the lack of a consistent writing platform. I’ve had some setbacks in the project, and compromises, and it’s not done yet, more on this project later. But the big hurdle I finally figured out, was how to get the Raspberry Pi 4 to project the screen in portrait mode.

In theory, according to most of the how-to’s on the internet (and there are quite few), this should be easy and accomplished using minimally invasive settings. However, in my case, I’ve got some odd hardware. I wanted a larger screen, to more accurately resemble a page of a book, and I wanted it touchscreen so I wouldn’t have to carry around a mouse when I was taking my bespoke word processor hardware out to the local coffee shop (once the world returns a little to normal). So I got a brandless 7-inch HDMI touchscreen off Amazon.

I quickly discovered what a host of forum posters before me complained about, that if you don’t use the Raspberry Pi official hardware, then all bets are off. Rotation won’t work through the GUI interface, and once you get it rotated by changing the config files, the touchscreen cursor shows up in a screen coordinate wildly different from where you touched. This is because the touchscreen device uses a separate driver than the screen itself, so it thinks the screen is still the orientation it used to be. Having a cursor appear several inches from where you touch the screen will drive a sane person to murder.

I ended up mostly using the system outlined in Instructables by GraysonL3, with a bunch of clues gleened from commenter grzbrz.

First step, rotate the screen. Open up the command prompt and edit the boot config file to change the rotation: sudo nano /boot/config.txt

I just added this line to swing it around 270 degrees: display_hdmi_rotate=3

The touchscreen hardware also required a bunch of new config lines to boost power to the HDMI port and such, so the boot config got a bit cluttered. Reboot and see if that works: sudo reboot

Presuming no problems with the rotation, next we need to work on the rotation of the touchscreen input. For that, we need to find the name of that input device. Typing into the command line: xinput list

This shows me that I’m using a mechanical keyboard and gaming mouse. By process of elimination, that gives me the name of the touchscreen device as: wch.cn USB2IIC_CTP_CONTROL

I know it’s a USB device, because a separate micro-USB port goes from the screen to the Raspberry Pi. At first I thought this was just for power, but using the HDMI cable as an input may not be possible, so of course the USB is the input, duh.

‘Xinput list’ will also give you the ID number (in my case 10), so you can drill down to the specific properties, including the current transformation matrix values with: xinput list-props 10

Now we find the Xorg calibration file and use the “TransformationMatrix” option to rotate that touch input:
cd /usr/share/X11/xorg.conf.d/

You may find multiple config files in this folder, but you want the one with the input sections. Mine was named 40-libinput.conf so to edit:
sudo nano 40*

Then add this to the file as the first section and reboot:
Section “InputClass”
        Identifier “callibration”
        MatchProduct “USB2IIC_CTP_CONTROL”
        MatchDevicePath “/dev/input/event*”
        Driver “libinput”
        Option “TransformationMatrix” “0 -1 1 1 0 0 0 0 1”

I intentionally misspelled “calibration” because the “Identifier” value is arbitrary and it allows me to verify in the logs that the system has implemented the new config settings. You buy that, right? The important part is the MatchProduct line. If it’s not clear from the xinput list which part is the product name (you’ll notice there’s an extra bit in the front for some reason that isn’t necessary in the config file), you may be able to use the whole string as “MatchDevice.” Or it might crash the system. Not sure which.

I’m not going to pretend to know how a transformation matrix works, if you end up using a different rotational direction, then consult the original Instructables article for other options. Also keep in mind that rotating the input the wrong way will look to you as if nothing happened.

In case of troubles, here’s some tools to help out. You can read the Xorg log, which has very verbose records of device setup and errors and will help you with the MatchProduct name.
cd /var/log/
nano xorg.0.log

That’s presuming that’s the correct log name.

You can also look at the boot text, looking for errors, by typing:
This is all that stuff that whips by really fast when your Raspberry Pi starts up. It’s actually saying some relevant stuff. To get out of that stream of text, just press ‘q’.

As usual, working with a Linux device is a little like having a pet that won’t take direction.