-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathrpi-display.sh
501 lines (419 loc) · 11.9 KB
/
rpi-display.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#!/usr/bin/env bash
# installation script for RPi-Display - https://github.com/watterott/RPi-Display
# run: sudo /bin/bash rpi-display.sh [0, 90, 180, 270]
# ask y/n function
function ask()
{
while true; do
read -p "$1 y/n " REPLY
case "$REPLY" in
Y*|y*) return 0 ;;
N*|n*) return 1 ;;
esac
done
}
# reboot system
function reboot_system()
{
echo "Rebooting now..."
reboot
exit 0
}
# update system
function update_system()
{
# run update
apt-get -y update
#apt-get -y upgrade
# install curl
if [ ! -f "/usr/bin/curl" ] && [ ! -f "/bin/curl" ]; then
apt-get install -y curl
fi
# install binutils
apt-get install -y binutils
# install rpi-update
if [ ! -f "/usr/bin/rpi-update" ] && [ ! -f "/bin/rpi-update" ]; then
curl -k -L -o /usr/bin/rpi-update https://raw.githubusercontent.com/Hexxeh/rpi-update/master/rpi-update
chmod +x /usr/bin/rpi-update
fi
# run rpi-update
# official RPi Kernel from 4.1 has DMA support for SPI
#REPO_URI=https://github.com/notro/rpi-firmware RPI_UPDATE_UNSUPPORTED=0 update
rpi-update
# ask for reboot
if ask "A reboot is needed. Do you want to reboot the system now?"; then
reboot_system
fi
}
# update config.txt
function update_configtxt()
{
echo "--- Updating /boot/config.txt ---"
echo
echo "Please reboot the system after the installation."
echo
# check blacklist
if [ -f "/etc/modprobe.d/raspi-blacklist.conf" ]; then
if ! grep -q "#blacklist spi-bcm2708" "/etc/modprobe.d/raspi-blacklist.conf"; then
sed -i 's/blacklist spi-bcm2708/#blacklist spi-bcm2708/g' "/etc/modprobe.d/raspi-blacklist.conf"
fi
fi
# no Device Tree -> use FBTFT module
if [ "${dt_found}" == "0" ]; then
if ! grep -q "dtparam=spi=on" "/boot/config.txt"; then
cat >> /boot/config.txt <<EOF
dtparam=spi=on
EOF
fi
echo "Updating /etc/modules"
if ! grep -q "spi-bcm2708" "/etc/modules"; then
cat >> /etc/modules <<EOF
spi-bcm2708
EOF
fi
if ! grep -q "fbtft_device" "/etc/modules"; then
cat >> /etc/modules <<EOF
fbtft_device name=rpi-display speed=32000000 rotate=$rotate
EOF
fi
# Device Tree -> use FBTFT DT-Overlay
else
if grep -q "dtoverlay=rpi-display" "/boot/config.txt"; then
sed -i 's/dtoverlay=rpi-display,speed=32000000,rotate=.*/dtoverlay=rpi-display,speed=32000000,rotate='$rotate'/g' "/boot/config.txt"
else
cat >> /boot/config.txt <<EOF
dtoverlay=rpi-display,speed=32000000,rotate=$rotate
EOF
fi
fi
}
# update xorg.conf
function update_xorg()
{
echo "--- Updating /etc/X11/xorg.conf.d ---"
echo
echo "startx -- -layout TFT"
echo "startx -- -layout HDMI"
echo "startx -- -layout HDMITFT"
echo "When -layout is not set, the first is used: TFT"
echo
mkdir -p /etc/X11/xorg.conf.d
if [ "${rotate}" == "0" ]; then
invertx="0"
inverty="0"
swapaxes="0"
tmatrix="1 0 0 0 1 0 0 0 1"
fi
if [ "${rotate}" == "90" ]; then
invertx="1"
inverty="0"
swapaxes="1"
tmatrix="0 -1 1 1 0 0 0 0 1"
fi
if [ "${rotate}" == "180" ]; then
invertx="1"
inverty="1"
swapaxes="0"
tmatrix="-1 0 1 0 -1 1 0 0 1"
fi
if [ "${rotate}" == "270" ]; then
invertx="0"
inverty="1"
swapaxes="1"
tmatrix="0 1 0 -1 0 1 0 0 1"
fi
cat > /etc/X11/xorg.conf.d/99-ads7846-cal.conf <<EOF
Section "InputClass"
Identifier "calibration"
MatchProduct "ADS7846 Touchscreen"
Option "EmulateThirdButton" "1"
Option "EmulateThirdButtonButton" "3"
Option "EmulateThirdButtonTimeout" "1500"
Option "EmulateThirdButtonMoveThreshold" "30"
Option "InvertX" "$invertx"
Option "InvertY" "$inverty"
Option "SwapAxes" "$swapaxes"
Option "TransformationMatrix" "$tmatrix"
EndSection
EOF
cat > /etc/X11/xorg.conf.d/50-fbtft.conf <<'EOF'
# FBTFT xorg config file
#
# startx -- -layout TFT
# startx -- -layout HDMI
# startx -- -layout HDMITFT
# When -layout is not set, the first is used: TFT
Section "ServerLayout"
Identifier "TFT"
Option "BlankTime" "10"
Screen 0 "ScreenTFT"
EndSection
Section "ServerLayout"
Identifier "HDMI"
Option "BlankTime" "10"
Screen 0 "ScreenHDMI"
EndSection
Section "ServerLayout"
Identifier "HDMITFT"
Option "BlankTime" "10"
Screen 0 "ScreenHDMI"
Screen 1 "ScreenTFT" RightOf "ScreenHDMI"
# Screen 1 "ScreenTFT" LeftOf "ScreenHDMI"
# Screen 1 "ScreenTFT" Above "ScreenHDMI"
# Screen 1 "ScreenTFT" Below "ScreenHDMI"
# Screen 1 "ScreenTFT" Relative "ScreenHDMI" x y
# Screen 1 "ScreenTFT" Absolute x y
EndSection
Section "Screen"
Identifier "ScreenHDMI"
Monitor "MonitorHDMI"
Device "DeviceHDMI"
Endsection
Section "Screen"
Identifier "ScreenTFT"
Monitor "MonitorTFT"
Device "DeviceTFT"
Endsection
Section "Monitor"
Identifier "MonitorHDMI"
Endsection
Section "Monitor"
Identifier "MonitorTFT"
Endsection
Section "Device"
Identifier "DeviceHDMI"
Driver "fbturbo"
Option "fbdev" "/dev/fb0"
Option "SwapbuffersWait" "true"
EndSection
Section "Device"
Identifier "DeviceTFT"
Driver "fbturbo"
Option "fbdev" "/dev/fb1"
EndSection
EOF
}
# create symlink for ads7846 device
function update_udev()
{
echo "--- Creating /dev/input/touchscreen ---"
cat > /etc/udev/rules.d/95-ads7846.rules <<'EOF'
SUBSYSTEM=="input", KERNEL=="event[0-9]*", ATTRS{name}=="ADS7846*", SYMLINK+="input/touchscreen"
EOF
}
# activate console on TFT display
function activate_console()
{
# install fbset
if [ ! -f "/usr/bin/fbset" ] && [ ! -f "/bin/fbset" ]; then
apt-get install -y fbset
fi
# set parameters
# fonts: MINI4x6, ProFont6x11, VGA8x8 - note: newer FBTFT has no built-in fonts
# https://github.com/watterott/RPi-Display/blob/master/docu/FAQ.md#how-to-change-the-console-font
if [ -f "/boot/cmdline.txt" ]; then
if ! grep -q "fbcon=map:10" "/boot/cmdline.txt"; then
sed -i 's/rootwait/rootwait fbcon=map:10 fbcon=font:VGA8x8 consoleblank=0/g' "/boot/cmdline.txt"
fi
else
echo "Run 'sudo nano /etc/rc.local' and add the line 'con2fbmap 1 1' before 'exit 0'."
fi
if [ -f "/etc/kbd/config" ]; then
sed -i 's/BLANK_TIME=.*/BLANK_TIME=0/g' "/etc/kbd/config"
else
echo "Run 'sudo nano /boot/cmdline.txt' and add the parameter 'consoleblank=0'."
fi
}
# deactivate console on TFT display
function deactivate_console()
{
if [ -f "/boot/cmdline.txt" ]; then
sed -i 's/rootwait fbcon=map:10 fbcon=font:VGA8x8 consoleblank=0/rootwait/g' "/boot/cmdline.txt"
else
echo "Run 'sudo nano /etc/rc.local' and remove the line 'con2fbmap 1 1'."
fi
if [ -f "/etc/kbd/config" ]; then
sed -i 's/BLANK_TIME=0/BLANK_TIME=10/g' "/etc/kbd/config"
else
echo "Run 'sudo nano /boot/cmdline.txt' and remove the parameter 'consoleblank=0'."
fi
echo
echo "Set screen blanking time to 10 minutes."
echo
}
# install fbcp
function install_fbcp()
{
echo "--- Installing fbcp ---"
cd /tmp
apt-get install -y git build-essential cmake
git clone --depth=1 https://github.com/tasanakorn/rpi-fbcp
mkdir -p rpi-fbcp/build
cd rpi-fbcp/build
cmake ..
make
install fbcp /usr/local/bin/fbcp
cd ../..
rm -r rpi-fbcp
curl -k -L -o /etc/init.d/fbcp https://github.com/watterott/RPi-Display/raw/master/software/fbcp
chmod +x /etc/init.d/fbcp
echo "To enable automatic startup of fbcp run:"
echo "sudo update-rc.d fbcp defaults"
echo "To disable automatic startup of fbcp run:"
echo "sudo update-rc.d fbcp remove"
# ask for automatic startup
if ask "Enable automatic startup of fbcp on boot?"; then
echo "Note: The console output on the TFT display will be disabled."
deactivate_console
update-rc.d fbcp defaults
fi
}
# download and install xinput-calibrator
function install_xinputcalibrator()
{
echo "--- Installing xinput-calibrator ---"
echo
echo "Run 'sudo startx' to calibrate the touchscreen for X."
echo
apt-get install -y xinput-calibrator
#cd /tmp
#curl -k -L -o xinput-calibrator_0.7.5-1_armhf.deb https://files.watterott.com/fbtft/xinput-calibrator_0.7.5-1_armhf.deb
#dpkg -i -B xinput-calibrator_0.7.5-1_armhf.deb
#rm xinput-calibrator_0.7.5-1_armhf.deb
cat > /etc/X11/Xsession.d/xinput_calibrator_pointercal <<'EOF'
#!/bin/sh
PATH="/usr/bin:$PATH"
BINARY="xinput_calibrator"
CALFILE="/etc/X11/xorg.conf.d/99-ads7846-cal.conf"
LOGFILE="/var/log/xinput_calibrator.pointercal.log"
CALDATA=`grep -o 'Option[[:space:]]*"MinX".*' $CALFILE`
if [ ! -z "$CALDATA" ] ; then
echo "Using calibration data stored in $CALFILE"
exit 0
fi
CALDATA=`DISPLAY=:0 $BINARY --output-type xorg.conf.d --device 'ADS7846 Touchscreen' | tee $LOGFILE | grep -i 'MinX\|MaxX\|MinY\|MaxY'`
if [ ! -z "$CALDATA" ] ; then
sed -i "/MinX/d;/MaxX/d;/MinY/d;/MaxY/d;/EndSection/d" "$CALFILE"
cat >> "$CALFILE" <<EOD
$CALDATA
EndSection
EOD
echo "Calibration data stored in $CALFILE (log in $LOGFILE)"
fi
EOF
# add touchpanel calibration to LXDE autostart
if [ -f "/etc/xdg/lxsession/LXDE-pi/autostart" ]; then
if grep -q "xinput_calibrator_pointercal" "/etc/xdg/lxsession/LXDE-pi/autostart"; then
echo "xinput_calibrator already in LXDE autostart"
else
cat >> /etc/xdg/lxsession/LXDE-pi/autostart <<'EOF'
@sh /etc/X11/Xsession.d/xinput_calibrator_pointercal
EOF
fi
fi
}
# download and install ts_lib
function install_tslib()
{
echo "--- Installing tslib ---"
echo
echo "Run the following command to calibrate the touchscreen for tslib."
echo "TSLIB_FBDEVICE=/dev/fb1 TSLIB_TSDEVICE=/dev/input/touchscreen sudo ts_calibrate"
echo
# install libts0 (for Buster) or tslib
apt-get install -y libts0 || apt-get install -y tslib
apt-get install -y libts-bin evtest
# tslib package does not exist on Buster, so compile from source
#apt-get install -y cmake git
#git clone https://github.com/libts/tslib
#cd tslib
#mkdir build && cd build
#cmake ../
#cmake --build .
#cmake -P cmake_install.cmake
# install ts_test with quit button
#curl -k -L -o /usr/bin/ts_test http://tronnes.org/downloads/ts_test
#chmod +x /usr/bin/ts_test
}
# calibrate touchpanel
function calibrate_touchpanel()
{
echo "--- Calibrating touchpanel ---"
TSLIB_FBDEVICE=/dev/fb1 TSLIB_TSDEVICE=/dev/input/touchscreen /usr/bin/ts_calibrate
#startx
#DISPLAY=:0 xinput_calibrator --device "ADS7846 Touchscreen" --output-type xinput
}
# main function
if [ $EUID -ne 0 ]; then
echo "The script must be run as root. Try: sudo $0"
exit 1
fi
rotate="$1"
dt_found="0"
fbtft_found="0"
if [ "${rotate}" != "0" ] && [ "${rotate}" != "90" ] && [ "${rotate}" != "180" ] && [ "${rotate}" != "270" ]; then
echo "Usage: sudo bash $0 [0, 90, 180 or 270]"
exit 1
fi
if [ -d "/proc/device-tree" ]; then
dt_found="1"
fi
if modinfo fbtft > /dev/null; then
fbtft_found="1"
fi
if [ "${dt_found}" == "0" ]; then
echo
echo "Info: No Device Tree Kernel found."
# if ask "Update the system now?"; then
# update_system
# fi
fi
if [ "${fbtft_found}" == "0" ]; then
echo
echo "FBTFT not found."
if ask "Update the system now?"; then
update_system
else
echo "Installation aborted."
exit 1
fi
fi
#if ! modinfo -F parm spi-bcm2708 | grep -q -i "dma"; then
# echo
# echo "No DMA support for spi-bcm2708 module found."
# if ask "Update the system now?"; then
# update_system
# fi
#fi
if ask "Enable TFT display driver and activate X windows on TFT display?"; then
update_configtxt
update_xorg
else
if [ -f "/etc/X11/xorg.conf.d/50-fbtft.conf" ]; then
echo "Removing /etc/X11/xorg.conf.d/50-fbtft.conf"
rm /etc/X11/xorg.conf.d/50-fbtft.conf
fi
fi
if ask "Activate the console on the TFT display?"; then
activate_console
else
deactivate_console
fi
if ask "Install fbcp (Framebuffer Copy)?"; then
install_fbcp
fi
if ask "Install xinput-calibrator?"; then
install_xinputcalibrator
fi
if ask "Install tslib (touchscreen library)?"; then
update_udev
install_tslib
fi
if [ -f "/usr/bin/ts_calibrate" ]; then
if ask "Calibrate the touchpanel now?"; then
calibrate_touchpanel
fi
fi
if ask "Reboot the system now?"; then
reboot_system
fi