No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

adafruit-pitft.sh 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. #!/bin/bash
  2. # (C) Adafruit Industries, Creative Commons 3.0 - Attribution Share Alike
  3. #
  4. # Instructions!
  5. # cd ~
  6. # wget https://raw.githubusercontent.com/adafruit/Raspberry-Pi-Installer-Scripts/master/adafruit-pitft.sh
  7. # chmod +x adafruit-pitft.sh
  8. # sudo ./adafruit-pitft.sh
  9. if [ $(id -u) -ne 0 ]; then
  10. echo "Installer must be run as root."
  11. echo "Try 'sudo bash $0'"
  12. exit 1
  13. fi
  14. UPDATE_DB=false
  15. UNINSTALL=false
  16. ############################ CALIBRATIONS ############################
  17. # For TSLib
  18. POINTERCAL_28r0="4232 11 -879396 1 5786 -752768 65536"
  19. POINTERCAL_28r90="33 -5782 21364572 4221 35 -1006432 65536"
  20. POINTERCAL_28r180="-4273 61 16441290 4 -5772 21627524 65536"
  21. POINTERCAL_28r270="-9 5786 -784608 -4302 19 16620508 65536"
  22. POINTERCAL_35r0="5724 -6 -1330074 26 8427 -1034528 65536"
  23. POINTERCAL_35r90="5 8425 -978304 -5747 61 22119468 65536"
  24. POINTERCAL_35r180="-5682 -1 22069150 13 -8452 32437698 65536"
  25. POINTERCAL_35r270="3 -8466 32440206 5703 -1 -1308696 65536"
  26. POINTERCAL_28c="320 65536 0 -65536 0 15728640 65536"
  27. # for PIXEL desktop
  28. TRANSFORM_28r0="0.988809 -0.023645 0.060523 -0.028817 1.003935 0.034176 0 0 1"
  29. TRANSFORM_28r90="0.014773 -1.132874 1.033662 1.118701 0.009656 -0.065273 0 0 1"
  30. TRANSFORM_28r180="-1.115235 -0.010589 1.057967 -0.005964 -1.107968 1.025780 0 0 1"
  31. TRANSFORM_28r270="-0.033192 1.126869 -0.014114 -1.115846 0.006580 1.050030 0 0 1"
  32. TRANSFORM_35r0="-1.098388 0.003455 1.052099 0.005512 -1.093095 1.026309 0 0 1"
  33. TRANSFORM_35r90="-0.000087 1.094214 -0.028826 -1.091711 -0.004364 1.057821 0 0 1"
  34. TRANSFORM_35r180="1.102807 0.000030 -0.066352 0.001374 1.085417 -0.027208 0 0 1"
  35. TRANSFORM_35r270="0.003893 -1.087542 1.025913 1.084281 0.008762 -0.060700 0 0 1"
  36. TRANSFORM_28c0="-1 0 1 0 -1 1 0 0 1"
  37. TRANSFORM_28c90="0 1 0 -1 0 1 0 0 1"
  38. TRANSFORM_28c180="1 0 0 0 1 0 0 0 1"
  39. TRANSFORM_28c270="0 -1 1 1 0 0 0 0 1"
  40. ROTATE_28c0="rotate=0,touch-invx=true,touch-invy=true"
  41. ROTATE_28c90="rotate=90,touch-swapxy=true,touch-invx=true"
  42. ROTATE_28c180="rotate=180"
  43. ROTATE_28c270="rotate=270,touch-swapxy=true,touch-invy=true"
  44. MADCTL_st7789_240x2400="0x36,0x60,-1,0x37,0x00,0x00,-1"
  45. MADCTL_st7789_240x24090="0x36,0x00,-1,0x37,0x00,0x00,-1"
  46. MADCTL_st7789_240x240180="0x36,0xA0,-1,0x37,0x00,0x50,-1"
  47. MADCTL_st7789_240x240270="0x36,0xC0,-1,0x37,0x00,0x50,-1"
  48. MADCTL_st7789_240x32090="0x36,0x60,-1,0x37,0x00,0x00,-1"
  49. MADCTL_st7789_240x320180="0x36,0x00,-1,0x37,0x00,0x00,-1"
  50. MADCTL_st7789_240x320270="0x36,0xA0,-1,0x37,0x00,0x00,-1"
  51. MADCTL_st7789_240x3200="0x36,0xC0,-1,0x37,0x00,0x00,-1"
  52. warning() {
  53. echo WARNING : $1
  54. }
  55. ############################ Script assisters ############################
  56. # Given a list of strings representing options, display each option
  57. # preceded by a number (1 to N), display a prompt, check input until
  58. # a valid number within the selection range is entered.
  59. selectN() {
  60. for ((i=1; i<=$#; i++)); do
  61. echo $i. ${!i}
  62. done
  63. echo
  64. REPLY=""
  65. while :
  66. do
  67. echo -n "SELECT 1-$#: "
  68. read
  69. if [[ $REPLY -ge 1 ]] && [[ $REPLY -le $# ]]; then
  70. return $REPLY
  71. fi
  72. done
  73. }
  74. function print_version() {
  75. echo "Adafruit PiTFT Helper v2.1.0"
  76. exit 1
  77. }
  78. function print_help() {
  79. echo "Usage: $0 "
  80. echo " -h Print this help"
  81. echo " -v Print version information"
  82. echo " -u [homedir] Specify path of primary user's home directory (defaults to /home/pi)"
  83. exit 1
  84. }
  85. group=ADAFRUIT
  86. function info() {
  87. system="$1"
  88. group="${system}"
  89. shift
  90. FG="1;32m"
  91. BG="40m"
  92. echo -e "[\033[${FG}\033[${BG}${system}\033[0m] $*"
  93. }
  94. function bail() {
  95. FG="1;31m"
  96. BG="40m"
  97. echo -en "[\033[${FG}\033[${BG}${group}\033[0m] "
  98. if [ -z "$1" ]; then
  99. echo "Exiting due to error"
  100. else
  101. echo "Exiting due to error: $*"
  102. fi
  103. exit 1
  104. }
  105. function ask() {
  106. # http://djm.me/ask
  107. while true; do
  108. if [ "${2:-}" = "Y" ]; then
  109. prompt="Y/n"
  110. default=Y
  111. elif [ "${2:-}" = "N" ]; then
  112. prompt="y/N"
  113. default=N
  114. else
  115. prompt="y/n"
  116. default=
  117. fi
  118. # Ask the question
  119. read -p "$1 [$prompt] " REPLY
  120. # Default?
  121. if [ -z "$REPLY" ]; then
  122. REPLY=$default
  123. fi
  124. # Check if the reply is valid
  125. case "$REPLY" in
  126. Y*|y*) return 0 ;;
  127. N*|n*) return 1 ;;
  128. esac
  129. done
  130. }
  131. function has_repo() {
  132. # Checks for the right raspbian repository
  133. # http://mirrordirector.raspbian.org/raspbian/ stretch main contrib non-free rpi firmware
  134. if [[ $(grep -h ^deb /etc/apt/sources.list /etc/apt/sources.list.d/* | grep "mirrordirector.raspbian.org") ]]; then
  135. return 0
  136. else
  137. return 1
  138. fi
  139. }
  140. progress() {
  141. count=0
  142. until [ $count -eq $1 ]; do
  143. echo -n "..." && sleep 1
  144. ((count++))
  145. done
  146. echo
  147. }
  148. sysupdate() {
  149. if ! $UPDATE_DB; then
  150. # echo "Checking for correct software repositories..."
  151. # has_repo || { warning "Missing Apt repo, please add deb http://mirrordirector.raspbian.org/raspbian/ stretch main contrib non-free rpi firmware to /etc/apt/sources.list.d/raspi.list" && exit 1; }
  152. echo "Updating apt indexes..." && progress 3 &
  153. sudo apt update 1> /dev/null || { warning "Apt failed to update indexes!" && exit 1; }
  154. sudo apt-get update 1> /dev/null || { warning "Apt failed to update indexes!" && exit 1; }
  155. echo "Reading package lists..."
  156. progress 3 && UPDATE_DB=true
  157. fi
  158. }
  159. # Given a filename, a regex pattern to match and a replacement string,
  160. # perform replacement if found, else append replacement to end of file.
  161. # (# $1 = filename, $2 = pattern to match, $3 = replacement)
  162. reconfig() {
  163. grep $2 $1 >/dev/null
  164. if [ $? -eq 0 ]; then
  165. # Pattern found; replace in file
  166. sed -i "s/$2/$3/g" $1 >/dev/null
  167. else
  168. # Not found; append (silently)
  169. echo $3 | sudo tee -a $1 >/dev/null
  170. fi
  171. }
  172. ############################ Sub-Scripts ############################
  173. function softwareinstall() {
  174. echo "Installing Pre-requisite Software...This may take a few minutes!"
  175. apt-get install -y libts0 1> /dev/null 2>&1 || apt-get install -y tslib 1> /dev/null 2>&1 || { warning "Apt failed to install TSLIB!" && exit 1; }
  176. apt-get install -y bc fbi git python-dev python-pip python-smbus python-spidev evtest libts-bin device-tree-compiler 1> /dev/null || { warning "Apt failed to install software!" && exit 1; }
  177. pip install evdev 1> /dev/null || { warning "Pip failed to install software!" && exit 1; }
  178. }
  179. # Remove any old flexfb/fbtft stuff
  180. function uninstall_bootconfigtxt() {
  181. if grep -q "adafruit-pitft-helper" "/boot/config.txt"; then
  182. echo "Already have an adafruit-pitft-helper section in /boot/config.txt."
  183. echo "Removing old section..."
  184. cp /boot/config.txt /boot/configtxt.bak
  185. sed -i -e "/^# --- added by adafruit-pitft-helper/,/^# --- end adafruit-pitft-helper/d" /boot/config.txt
  186. fi
  187. }
  188. # Remove any old flexfb/fbtft stuff
  189. function uninstall_etc_modules() {
  190. rm -f /etc/modprobe.d/fbtft.conf
  191. sed -i 's/spi-bcm2835//g' "/etc/modules"
  192. sed -i 's/flexfb//g' "/etc/modules"
  193. sed -i 's/fbtft_device//g' "/etc/modules"
  194. }
  195. # update /boot/config.txt with appropriate values
  196. function update_configtxt() {
  197. uninstall_bootconfigtxt
  198. uninstall_etc_modules
  199. if [ "${pitfttype}" == "22" ]; then
  200. overlay="dtoverlay=pitft22,rotate=${pitftrot},speed=64000000,fps=30"
  201. fi
  202. if [ "${pitfttype}" == "28r" ]; then
  203. overlay="dtoverlay=pitft28-resistive,rotate=${pitftrot},speed=64000000,fps=30"
  204. fi
  205. if [ "${pitfttype}" == "28c" ]; then
  206. rotateparams=$(eval echo "\$ROTATE_$pitfttype$pitftrot")
  207. overlay=$(printf "dtoverlay=pitft28-capacitive,speed=64000000,fps=30\ndtoverlay=pitft28-capacitive,${rotateparams}")
  208. fi
  209. if [ "${pitfttype}" == "35r" ]; then
  210. overlay="dtoverlay=pitft35-resistive,rotate=${pitftrot},speed=20000000,fps=20"
  211. fi
  212. if [ "${pitfttype}" == "st7789_240x320" ]; then
  213. madctl=$(eval echo "\$MADCTL_$pitfttype$pitftrot")
  214. if [ "${pitftrot}" == "90" ] || [ "${pitftrot}" == "270" ]; then
  215. fbtftdevicerotate="rotate=90"
  216. else
  217. fbtftdevicerotate=""
  218. fi
  219. cat >> /etc/modprobe.d/fbtft.conf <<EOF
  220. # --- added by adafruit-pitft-helper $date ---
  221. options fbtft_device name=flexfb gpios=dc:25,cs:8,led:12 speed=40000000 bgr=1 fps=60 $fbtftdevicerotate
  222. options flexfb setaddrwin=0 width=240 height=320 init=-1,0x11,-2,120,-1,$madctl,0x3A,0x05,-1,0x26,0x04,-1,0xBA,0x01,-1,0xB2,0x0C,0x0C,0x00,0x33,0x33,-1,0xB7,0x35,-1,0xBB,0x1A,-1,0xC0,0x2C,-1,0xC2,0x01,-1,0xC3,0x0B,-1,0xC4,0x20,-1,0xC6,0x0F,-1,0xD0,0xA4,0xA1,-1,0x21,-1,0xE0,0x00,0x19,0x1E,0x0A,0x09,0x15,0x3D,0x44,0x51,0x12,0x03,0x00,0x3F,0x3F,-1,0xE1,0x00,0x18,0x1E,0x0A,0x09,0x25,0x3F,0x43,0x52,0x33,0x03,0x00,0x3F,0x3F,-1,0x29,-3
  223. # --- end adafruit-pitft-helper $date ---
  224. EOF
  225. echo "spi-bcm2835" >> /etc/modules
  226. echo "flexfb" >> /etc/modules
  227. echo "fbtft_device" >> /etc/modules
  228. overlay=""
  229. fi
  230. if [ "${pitfttype}" == "st7789_240x240" ]; then
  231. madctl=$(eval echo "\$MADCTL_$pitfttype$pitftrot")
  232. cat >> /etc/modprobe.d/fbtft.conf <<EOF
  233. # --- added by adafruit-pitft-helper $date ---
  234. options fbtft_device name=flexfb gpios=dc:25,cs:8,led:26 speed=40000000 bgr=1 fps=60
  235. options flexfb setaddrwin=0 width=240 height=240 init=-1,0x11,-2,120,-1,$madctl,0x3A,0x05,-1,0xB2,0x0C,0x0C,0x00,0x33,0x33,-1,0xB7,0x35,-1,0xBB,0x1A,-1,0xC0,0x2C,-1,0xC2,0x01,-1,0xC3,0x0B,-1,0xC4,0x20,-1,0xC6,0x0F,-1,0xD0,0xA4,0xA1,-1,0x21,-1,0xE0,0x00,0x19,0x1E,0x0A,0x09,0x15,0x3D,0x44,0x51,0x12,0x03,0x00,0x3F,0x3F,-1,0xE1,0x00,0x18,0x1E,0x0A,0x09,0x25,0x3F,0x43,0x52,0x33,0x03,0x00,0x3F,0x3F,-1,0x29,-3
  236. # --- end adafruit-pitft-helper $date ---
  237. EOF
  238. echo "spi-bcm2835" >> /etc/modules
  239. echo "flexfb" >> /etc/modules
  240. echo "fbtft_device" >> /etc/modules
  241. overlay=""
  242. fi
  243. if [ "${pitfttype}" == "st7789_240x135" ]; then
  244. dtc -@ -I dts -O dtb -o /boot/overlays/drm-minipitft114.dtbo overlays/minipitft114-overlay.dts
  245. echo "############# UPGRADING KERNEL ###############"
  246. sudo apt update || { warning "Apt failed to update itself!" && exit 1; }
  247. sudo apt-get upgrade || { warning "Apt failed to install software!" && exit 1; }
  248. apt-get install -y raspberrypi-kernel-headers 1> /dev/null || { warning "Apt failed to install software!" && exit 1; }
  249. [ -d /lib/modules/$(uname -r)/build ] || { warning "Kernel was updated, please reboot now and re-run script!" && exit 1; }
  250. cd st7789_module
  251. make -C /lib/modules/$(uname -r)/build M=$(pwd) modules || { warning "Apt failed to compile ST7789V driver!" && exit 1; }
  252. mv /lib/modules/$(uname -r)/kernel/drivers/gpu/drm/tinydrm/mi0283qt.ko /lib/modules/$(uname -r)/kernel/drivers/gpu/drm/tinydrm/mi0283qt.BACK
  253. mv st7789v_ada.ko /lib/modules/$(uname -r)/kernel/drivers/gpu/drm/tinydrm/mi0283qt.ko
  254. overlay="dtoverlay=drm-minipitft114,rotation=${pitftrot}"
  255. fi
  256. date=`date`
  257. cat >> /boot/config.txt <<EOF
  258. # --- added by adafruit-pitft-helper $date ---
  259. dtparam=spi=on
  260. dtparam=i2c1=on
  261. dtparam=i2c_arm=on
  262. $overlay
  263. # --- end adafruit-pitft-helper $date ---
  264. EOF
  265. }
  266. function update_udev() {
  267. cat > /etc/udev/rules.d/95-touchmouse.rules <<EOF
  268. SUBSYSTEM=="input", ATTRS{name}=="touchmouse", ENV{DEVNAME}=="*event*", SYMLINK+="input/touchscreen"
  269. EOF
  270. cat > /etc/udev/rules.d/95-ftcaptouch.rules <<EOF
  271. SUBSYSTEM=="input", ATTRS{name}=="EP0110M09", ENV{DEVNAME}=="*event*", SYMLINK+="input/touchscreen"
  272. SUBSYSTEM=="input", ATTRS{name}=="generic ft5x06*", ENV{DEVNAME}=="*event*", SYMLINK+="input/touchscreen"
  273. EOF
  274. cat > /etc/udev/rules.d/95-stmpe.rules <<EOF
  275. SUBSYSTEM=="input", ATTRS{name}=="*stmpe*", ENV{DEVNAME}=="*event*", SYMLINK+="input/touchscreen"
  276. EOF
  277. }
  278. function update_pointercal() {
  279. if [ "${pitfttype}" == "28r" ] || [ "${pitfttype}" == "35r" ]; then
  280. echo $(eval echo "\$POINTERCAL_$pitfttype$pitftrot") > /etc/pointercal
  281. fi
  282. if [ "${pitfttype}" == "28c" ]; then
  283. echo $(eval echo "\$POINTERCAL_$pitfttype") > /etc/pointercal
  284. fi
  285. }
  286. function install_console() {
  287. echo "Set up main console turn on"
  288. if ! grep -q 'fbcon=map:10 fbcon=font:VGA8x8' /boot/cmdline.txt; then
  289. echo "Updating /boot/cmdline.txt"
  290. sed -i 's/rootwait/rootwait fbcon=map:10 fbcon=font:VGA8x8/g' "/boot/cmdline.txt"
  291. else
  292. echo "/boot/cmdline.txt already updated"
  293. fi
  294. echo "Turning off console blanking"
  295. # pre-stretch this is what you'd do:
  296. if [ -e /etc/kbd/config ]; then
  297. sed -i 's/BLANK_TIME=.*/BLANK_TIME=0/g' "/etc/kbd/config"
  298. fi
  299. # as of stretch....
  300. # removing any old version
  301. sed -i -e '/^# disable console blanking.*/d' /etc/rc.local
  302. sed -i -e '/^sudo sh -c "TERM=linux setterm -blank.*/d' /etc/rc.local
  303. sed -i -e "s|^exit 0|# disable console blanking on PiTFT\\nsudo sh -c \"TERM=linux setterm -blank 0 >/dev/tty0\"\\nexit 0|" /etc/rc.local
  304. reconfig /etc/default/console-setup "^.*FONTFACE.*$" "FONTFACE=\"Terminus\""
  305. reconfig /etc/default/console-setup "^.*FONTSIZE.*$" "FONTSIZE=\"6x12\""
  306. echo "Setting raspi-config to boot to console w/o login..."
  307. (cd "$target_homedir" && raspi-config nonint do_boot_behaviour B2)
  308. # remove fbcp
  309. sed -i -e "/^.*fbcp.*$/d" /etc/rc.local
  310. }
  311. function uninstall_console() {
  312. echo "Removing console fbcon map from /boot/cmdline.txt"
  313. sed -i 's/rootwait fbcon=map:10 fbcon=font:VGA8x8/rootwait/g' "/boot/cmdline.txt"
  314. echo "Screen blanking time reset to 10 minutes"
  315. if [ -e "/etc/kbd/config" ]; then
  316. sed -i 's/BLANK_TIME=0/BLANK_TIME=10/g' "/etc/kbd/config"
  317. fi
  318. sed -i -e '/^# disable console blanking.*/d' /etc/rc.local
  319. sed -i -e '/^sudo sh -c "TERM=linux.*/d' /etc/rc.local
  320. }
  321. function install_fbcp() {
  322. echo "Installing cmake..."
  323. apt-get --yes --force-yes install cmake 1> /dev/null || { warning "Apt failed to install software!" && exit 1; }
  324. echo "Downloading rpi-fbcp..."
  325. cd /tmp
  326. #curl -sLO https://github.com/tasanakorn/rpi-fbcp/archive/master.zip
  327. curl -sLO https://github.com/adafruit/rpi-fbcp/archive/master.zip
  328. echo "Uncompressing rpi-fbcp..."
  329. rm -rf /tmp/rpi-fbcp-master
  330. unzip master.zip 1> /dev/null || { warning "Failed to uncompress fbcp!" && exit 1; }
  331. cd rpi-fbcp-master
  332. mkdir build
  333. cd build
  334. echo "Building rpi-fbcp..."
  335. echo -e "\nset (CMAKE_C_FLAGS \"-std=gnu99 ${CMAKE_C_FLAGS}\")" >> ../CMakeLists.txt
  336. cmake .. 1> /dev/null || { warning "Failed to cmake fbcp!" && exit 1; }
  337. make 1> /dev/null || { warning "Failed to make fbcp!" && exit 1; }
  338. echo "Installing rpi-fbcp..."
  339. install fbcp /usr/local/bin/fbcp
  340. cd ~
  341. rm -rf /tmp/rpi-fbcp-master
  342. # Start fbcp in the appropriate place, depending on init system:
  343. if [ "$SYSTEMD" == "0" ]; then
  344. # Add fbcp to /etc/rc.local:
  345. echo "We have sysvinit, so add fbcp to /etc/rc.local..."
  346. grep fbcp /etc/rc.local >/dev/null
  347. if [ $? -eq 0 ]; then
  348. # fbcp already in rc.local, but make sure correct:
  349. sed -i "s|^.*fbcp.*$|/usr/local/bin/fbcp \&|g" /etc/rc.local >/dev/null
  350. else
  351. # Insert fbcp into rc.local before final 'exit 0':
  352. sed -i "s|^exit 0|/usr/local/bin/fbcp \&\\nexit 0|g" /etc/rc.local >/dev/null
  353. fi
  354. else
  355. # Install fbcp systemd unit, first making sure it's not in rc.local:
  356. uninstall_fbcp_rclocal
  357. echo "We have systemd, so install fbcp systemd unit..."
  358. install_fbcp_unit || bail "Unable to install fbcp unit file"
  359. sudo systemctl enable fbcp.service
  360. fi
  361. # if there's X11 installed...
  362. if [ -e /etc/lightdm ]; then
  363. echo "Setting raspi-config to boot to desktop w/o login..."
  364. raspi-config nonint do_boot_behaviour B4
  365. fi
  366. # Disable overscan compensation (use full screen):
  367. raspi-config nonint do_overscan 1
  368. # Set up HDMI parameters:
  369. echo "Configuring boot/config.txt for forced HDMI"
  370. reconfig /boot/config.txt "^.*hdmi_force_hotplug.*$" "hdmi_force_hotplug=1"
  371. reconfig /boot/config.txt "^.*hdmi_group.*$" "hdmi_group=2"
  372. reconfig /boot/config.txt "^.*hdmi_mode.*$" "hdmi_mode=87"
  373. # if there's X11 installed...
  374. if [ -e /etc/lightdm ]; then
  375. if [ "${pitfttype}" == "35r" ]; then
  376. echo "Using x1.5 resolution"
  377. SCALE=1.5
  378. else
  379. echo "Using x2 resolution"
  380. SCALE=2.0
  381. fi
  382. else
  383. echo "Using native resolution"
  384. SCALE=1
  385. fi
  386. if [[ "${pitfttype}" == "st7789_240x320" && ( "${pitftrot}" == "180" || "${pitftrot}" == "0" ) ]]; then
  387. # swap width/height for portrait display rotation when using st7789_240x320
  388. WIDTH=`python -c "print(int(${HEIGHT_VALUES[PITFT_SELECT-1]} * ${SCALE}))"`
  389. HEIGHT=`python -c "print(int(${WIDTH_VALUES[PITFT_SELECT-1]} * ${SCALE}))"`
  390. else
  391. WIDTH=`python -c "print(int(${WIDTH_VALUES[PITFT_SELECT-1]} * ${SCALE}))"`
  392. HEIGHT=`python -c "print(int(${HEIGHT_VALUES[PITFT_SELECT-1]} * ${SCALE}))"`
  393. fi
  394. reconfig /boot/config.txt "^.*hdmi_cvt.*$" "hdmi_cvt=${WIDTH} ${HEIGHT} 60 1 0 0 0"
  395. if [ "${pitftrot}" == "90" ] || [ "${pitftrot}" == "270" ] || [ "${pitfttype}" == "st7789_240x320" ]; then
  396. # dont rotate HDMI on 90 or 270
  397. reconfig /boot/config.txt "^.*display_hdmi_rotate.*$" ""
  398. fi
  399. if [ "${pitftrot}" == "0" ]; then
  400. reconfig /boot/config.txt "^.*display_hdmi_rotate.*$" "display_hdmi_rotate=1"
  401. # this is a hack but because we rotate HDMI we have to 'unrotate' the TFT!
  402. pitftrot=90
  403. update_configtxt || bail "Unable to update /boot/config.txt"
  404. pitftrot=0
  405. fi
  406. if [ "${pitftrot}" == "180" ]; then
  407. reconfig /boot/config.txt "^.*display_hdmi_rotate.*$" "display_hdmi_rotate=3"
  408. # this is a hack but because we rotate HDMI we have to 'unrotate' the TFT!
  409. pitftrot=90
  410. update_configtxt || bail "Unable to update /boot/config.txt"
  411. pitftrot=180
  412. fi
  413. }
  414. function install_fbcp_unit() {
  415. cat > /etc/systemd/system/fbcp.service <<EOF
  416. [Unit]
  417. Description=Framebuffer copy utility for PiTFT
  418. After=network.target
  419. [Service]
  420. Type=simple
  421. ExecStartPre=/bin/sleep 10
  422. ExecStart=/usr/local/bin/fbcp
  423. [Install]
  424. WantedBy=multi-user.target
  425. EOF
  426. }
  427. function uninstall_fbcp() {
  428. uninstall_fbcp_rclocal
  429. # Enable overscan compensation
  430. raspi-config nonint do_overscan 0
  431. # Set up HDMI parameters:
  432. echo "Configuring boot/config.txt for default HDMI"
  433. reconfig /boot/config.txt "^.*hdmi_force_hotplug.*$" "hdmi_force_hotplug=0"
  434. sed -i -e '/^hdmi_group=2.*$/d' /boot/config.txt
  435. sed -i -e '/^hdmi_mode=87.*$/d' /boot/config.txt
  436. sed -i -e '/^hdmi_cvt=.*$/d' /boot/config.txt
  437. }
  438. function uninstall_fbcp_rclocal() {
  439. # Remove fbcp from /etc/rc.local:
  440. echo "Remove fbcp from /etc/rc.local, if it's there..."
  441. sed -i -e '/^.*fbcp.*$/d' /etc/rc.local
  442. }
  443. function update_xorg() {
  444. if [ "${pitfttype}" == "28r" ] || [ "${pitfttype}" == "35r" ]; then
  445. matrix=$(eval echo "\$TRANSFORM_$pitfttype$pitftrot")
  446. transform="Option \"TransformationMatrix\" \"${matrix}\""
  447. cat > /usr/share/X11/xorg.conf.d/20-calibration.conf <<EOF
  448. Section "InputClass"
  449. Identifier "STMPE Touchscreen Calibration"
  450. MatchProduct "stmpe"
  451. MatchDevicePath "/dev/input/event*"
  452. Driver "libinput"
  453. ${transform}
  454. EndSection
  455. EOF
  456. fi
  457. if [ "${pitfttype}" == "28c" ]; then
  458. matrix=$(eval echo "\$TRANSFORM_$pitfttype$pitftrot")
  459. transform="Option \"TransformationMatrix\" \"${matrix}\""
  460. cat > /usr/share/X11/xorg.conf.d/20-calibration.conf <<EOF
  461. Section "InputClass"
  462. Identifier "FocalTech Touchscreen Calibration"
  463. MatchProduct "EP0110M09"
  464. MatchDevicePath "/dev/input/event*"
  465. Driver "libinput"
  466. ${transform}
  467. EndSection
  468. EOF
  469. fi
  470. }
  471. ####################################################### MAIN
  472. target_homedir="/home/pi"
  473. clear
  474. echo "This script downloads and installs"
  475. echo "PiTFT Support using userspace touch"
  476. echo "controls and a DTO for display drawing."
  477. echo "one of several configuration files."
  478. echo "Run time of up to 5 minutes. Reboot required!"
  479. echo
  480. echo "Select configuration:"
  481. selectN "PiTFT 2.4\", 2.8\" or 3.2\" resistive (240x320)" \
  482. "PiTFT 2.0\" no touch (240x320)" \
  483. "PiTFT 2.2\" no touch (240x320)" \
  484. "PiTFT 2.8\" capacitive touch (240x320)" \
  485. "PiTFT 3.5\" resistive touch (320x480)" \
  486. "PiTFT Mini 1.3\" or 1.54\" display (240x240)" \
  487. "MiniPiTFT 1.14\" display (240x135) - WARNING! CUTTING EDGE! WILL UPGRADE YOUR KERNEL TO LATEST" \
  488. "Uninstall PiTFT" \
  489. "Quit without installing"
  490. PITFT_SELECT=$?
  491. if [ $PITFT_SELECT -gt 8 ]; then
  492. exit 1
  493. fi
  494. if [ $PITFT_SELECT -eq 8 ]; then
  495. UNINSTALL=true
  496. fi
  497. if ! $UNINSTALL; then
  498. echo "Select rotation:"
  499. selectN "90 degrees (landscape)" \
  500. "180 degrees (portait)" \
  501. "270 degrees (landscape)" \
  502. "0 degrees (portait)"
  503. PITFT_ROTATE=$?
  504. if [ $PITFT_ROTATE -gt 4 ]; then
  505. exit 1
  506. fi
  507. fi
  508. PITFT_ROTATIONS=("90" "180" "270" "0")
  509. PITFT_TYPES=("28r" "st7789_240x320" "22" "28c" "35r" "st7789_240x240" "st7789_240x135")
  510. WIDTH_VALUES=(320 320 320 320 480 240)
  511. HEIGHT_VALUES=(240 240 240 240 320 240)
  512. HZ_VALUES=(64000000 64000000 64000000 64000000 32000000 64000000)
  513. args=$(getopt -uo 'hvri:o:b:u:' -- $*)
  514. [ $? != 0 ] && print_help
  515. set -- $args
  516. for i
  517. do
  518. case "$i"
  519. in
  520. -h)
  521. print_help
  522. ;;
  523. -v)
  524. print_version
  525. ;;
  526. -u)
  527. target_homedir="$2"
  528. echo "Homedir = ${2}"
  529. shift
  530. shift
  531. ;;
  532. esac
  533. done
  534. # check init system (technique borrowed from raspi-config):
  535. info PITFT 'Checking init system...'
  536. if command -v systemctl > /dev/null && systemctl | grep -q '\-\.mount'; then
  537. echo "Found systemd"
  538. SYSTEMD=1
  539. elif [ -f /etc/init.d/cron ] && [ ! -h /etc/init.d/cron ]; then
  540. echo "Found sysvinit"
  541. SYSTEMD=0
  542. else
  543. bail "Unrecognised init system"
  544. fi
  545. if grep -q boot /proc/mounts; then
  546. echo "/boot is mounted"
  547. else
  548. echo "/boot must be mounted. if you think it's not, quit here and try: sudo mount /dev/mmcblk0p1 /boot"
  549. if ask "Continue?"; then
  550. echo "Proceeding."
  551. else
  552. bail "Aborting."
  553. fi
  554. fi
  555. if [[ ! -e "$target_homedir" || ! -d "$target_homedir" ]]; then
  556. bail "$target_homedir must be an existing directory (use -u /home/foo to specify)"
  557. fi
  558. if ! $UNINSTALL;
  559. then
  560. pitfttype=${PITFT_TYPES[$PITFT_SELECT-1]}
  561. pitftrot=${PITFT_ROTATIONS[$PITFT_ROTATE-1]}
  562. if [ "${pitfttype}" != "28r" ] && [ "${pitfttype}" != "28c" ] && [ "${pitfttype}" != "35r" ] && [ "${pitfttype}" != "22" ] && [ "${pitfttype}" != "st7789_240x240" ] && [ "${pitfttype}" != "st7789_240x320" ] && [ "${pitfttype}" != "st7789_240x135" ]; then
  563. echo "Type must be one of:"
  564. echo " '28r' (2.8\" resistive, PID 1601)"
  565. echo " '28c' (2.8\" capacitive, PID 1983)"
  566. echo " '35r' (3.5\" Resistive)"
  567. echo " '22' (2.2\" no touch)"
  568. echo " 'st7789_240x240' (1.54\" or 1.3\" no touch)"
  569. echo " 'st7789_320x240' (2.0\" no touch)"
  570. echo " 'st7789_240x135' 1.14\" no touch)"
  571. echo
  572. print_help
  573. fi
  574. info PITFT "System update"
  575. sysupdate || bail "Unable to apt-get update"
  576. info PITFT "Installing Python libraries & Software..."
  577. softwareinstall || bail "Unable to install software"
  578. info PITFT "Updating /boot/config.txt..."
  579. update_configtxt || bail "Unable to update /boot/config.txt"
  580. if [ "${pitfttype}" == "28r" ] || [ "${pitfttype}" == "35r" ] || [ "${pitfttype}" == "28c" ] ; then
  581. info PITFT "Updating SysFS rules for Touchscreen..."
  582. update_udev || bail "Unable to update /etc/udev/rules.d"
  583. info PITFT "Updating TSLib default calibration..."
  584. update_pointercal || bail "Unable to update /etc/pointercal"
  585. fi
  586. # ask for console access
  587. if ask "Would you like the console to appear on the PiTFT display?"; then
  588. info PITFT "Updating console to PiTFT..."
  589. uninstall_fbcp || bail "Unable to uninstall fbcp"
  590. install_console || bail "Unable to configure console"
  591. else
  592. info PITFT "Making sure console doesn't use PiTFT"
  593. uninstall_console || bail "Unable to configure console"
  594. if ask "Would you like the HDMI display to mirror to the PiTFT display?"; then
  595. info PITFT "Adding FBCP support..."
  596. install_fbcp || bail "Unable to configure fbcp"
  597. if [ -e /etc/lightdm ]; then
  598. info PITFT "Updating X11 default calibration..."
  599. update_xorg || bail "Unable to update calibration"
  600. fi
  601. fi
  602. fi
  603. else
  604. info PITFT "Uninstalling PiTFT"
  605. uninstall_bootconfigtxt
  606. uninstall_console
  607. uninstall_fbcp
  608. uninstall_fbcp_rclocal
  609. uninstall_etc_modules
  610. fi
  611. #info PITFT "Updating X11 setup tweaks..."
  612. #update_x11profile || bail "Unable to update X11 setup"
  613. #if [ "${pitfttype}" != "35r" ]; then
  614. # # ask for 'on/off' button
  615. # if ask "Would you like GPIO #23 to act as a on/off button?"; then
  616. # info PITFT "Adding GPIO #23 on/off to PiTFT..."
  617. # install_onoffbutton || bail "Unable to add on/off button"
  618. # fi
  619. #fi
  620. # update_bootprefs || bail "Unable to set boot preferences"
  621. info PITFT "Success!"
  622. echo
  623. echo "Settings take effect on next boot."
  624. echo
  625. echo -n "REBOOT NOW? [y/N] "
  626. read
  627. if [[ ! "$REPLY" =~ ^(yes|y|Y)$ ]]; then
  628. echo "Exiting without reboot."
  629. exit 0
  630. fi
  631. echo "Reboot started..."
  632. reboot
  633. exit 0