How to enable PiShop.nz 3.5" Touch Screen Monitor in Raspbian

We recommend you do the below commands via a SSH connection to allow copy & paste
Backup any important files before proceeding

Enable SPI and Screen (Required)

Update package lists
sudo apt-get update
Install and force evdev (instead of libinput)
sudo apt-get install -y xserver-xorg-input-evdev
sudo cp -rf /usr/share/X11/xorg.conf.d/10-evdev.conf /usr/share/X11/xorg.conf.d/45-evdev.conf
Edit boot config
sudo cp /boot/config.txt /boot/config.txt.bu
sudo nano /boot/config.txt
Copy the below text to the bottom of the file
overscan_left=0
overscan_right=0
overscan_top=0
overscan_bottom=0
dtparam=spi=on
dtoverlay=piscreen,rotate=90,speed=24000000
Save the file (CTRL-X then Y then ENTER).

Show Raspbian GUI on Screen

Edit X11 config file to send GUI to 3.5" screen
sudo cp /usr/share/X11/xorg.conf.d/99-fbturbo.conf /usr/share/X11/xorg.conf.d/99-fbturbo.conf.bu
sudo sed -i 's/fb0/fb1/g' /usr/share/X11/xorg.conf.d/99-fbturbo.conf
Now reboot
sudo reboot
The Raspbian GUI should now show on the 3.5" screen

Raspbian Touch-screen Calibration

Run the below commands to calibrate the touch-screen.

Install the required packages
sudo apt-get install -y xinput-calibrator
Run the calibration program
xinput_calibrator
If you are doing the commands via SSH, then use
DISPLAY=:0.0 xinput_calibrator
Copy the output of the program.
It should look similiar to this:
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "ADS7846 Touchscreen"
        Option  "Calibration"   "3989 230 324 3976"
        Option  "SwapAxes"      "0"
EndSection
Now run the below command to create and edit the file for our persistent calibration.
sudo nano /usr/share/X11/xorg.conf.d/99-calibration.conf
Paste in the copied text and save the file (Ctrl-X, Y, ENTER).
Now reboot and the screen should be calibrated.
sudo reboot

Disable Screen Blanking (optional)

sudo sed -i 's/^BLANK_TIME=30/BLANK_TIME=0/' /etc/kbd/config
sudo sed -i 's/^POWERDOWN_TIME=30/POWERDOWN_TIME=0/' /etc/kbd/config
sudo reboot

Image Test (optional)

Install fbi image viewer and test
sudo apt-get -y install wget fbi
cd ~
wget http://pishop.nz/files/480x320.jpg
sudo fbi -noverbose -T 1 -a -d /dev/fb1 480x320.jpg
You should see the image on the 3.5" screen

Show Raspbian Boot Text on Screen (optional)

Edit boot config file to send console to 3.5" screen
sudo cp /boot/cmdline.txt /boot/cmdline.bu
sudo sed -i.bck '$s/$/ fbcon=map:10 fbcon=rotate:2 fbcon=font:ProFont6x11/' /boot/cmdline.txt
sudo reboot
The Raspbian console should now show on the 3.5" screen

Mirror HDMI Output to 3.5" Screen (optional)

Install cmake and build rpi-fbcp
sudo apt-get -y install cmake git
git clone https://github.com/tasanakorn/rpi-fbcp
cd rpi-fbcp/

mkdir build
cd build/
cmake ..
make

sudo install fbcp /usr/sbin/fbcp
Start mirroring
fbcp &
Stop mirroring
killall fbcp
Start mirroring on boot
sudo cp /etc/rc.local /etc/rc.local.bu
sudo sed -i 's/^exit 0/fbcp \&\nexit 0/g' /etc/rc.local
sudo reboot