A short example of using subtree for stuff.
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.

356 lines
9.3 KiB

  1. #!/usr/bin/env bash
  2. set -e
  3. function print_version() {
  4. echo "Adafruit PiTFT Helper v0.5.0"
  5. exit 1
  6. }
  7. function print_help() {
  8. echo "Usage: $0 -t [pitfttype]"
  9. echo " -h Print this help"
  10. echo " -v Print version information"
  11. echo " -u [homedir] Specify path of primary user's home directory (defaults to /home/pi)"
  12. echo " -t [type] Specify the type of PiTFT: '28r' (PID 1601) or '28c' (PID 1983) or '35r' or '22'"
  13. echo
  14. echo "You must specify a type of display."
  15. exit 1
  16. }
  17. group=ADAFRUIT
  18. function info() {
  19. system="$1"
  20. group="${system}"
  21. shift
  22. FG="1;32m"
  23. BG="40m"
  24. echo -e "[\033[${FG}\033[${BG}${system}\033[0m] $*"
  25. }
  26. function bail() {
  27. FG="1;31m"
  28. BG="40m"
  29. echo -en "[\033[${FG}\033[${BG}${group}\033[0m] "
  30. if [ -z "$1" ]; then
  31. echo "Exiting due to error"
  32. else
  33. echo "Exiting due to error: $*"
  34. fi
  35. exit 1
  36. }
  37. function ask() {
  38. # http://djm.me/ask
  39. while true; do
  40. if [ "${2:-}" = "Y" ]; then
  41. prompt="Y/n"
  42. default=Y
  43. elif [ "${2:-}" = "N" ]; then
  44. prompt="y/N"
  45. default=N
  46. else
  47. prompt="y/n"
  48. default=
  49. fi
  50. # Ask the question
  51. read -p "$1 [$prompt] " REPLY
  52. # Default?
  53. if [ -z "$REPLY" ]; then
  54. REPLY=$default
  55. fi
  56. # Check if the reply is valid
  57. case "$REPLY" in
  58. Y*|y*) return 0 ;;
  59. N*|n*) return 1 ;;
  60. esac
  61. done
  62. }
  63. # update /boot/config.txt with appropriate values
  64. function update_configtxt() {
  65. if grep -q "adafruit-pitft-helper" "/boot/config.txt"; then
  66. echo "Already have an adafruit-pitft-helper section in /boot/config.txt."
  67. echo "Adding new section, but please run:"
  68. echo "sudo nano /boot/config.txt"
  69. echo "...and remove any duplicate sections."
  70. fi
  71. if [ "${pitfttype}" == "22" ]; then
  72. # formerly: options fbtft_device name=adafruit22a gpios=dc:25 rotate=270 frequency=32000000
  73. overlay="dtoverlay=pitft22,rotate=270,speed=32000000,fps=20"
  74. fi
  75. if [ "${pitfttype}" == "28r" ]; then
  76. overlay="dtoverlay=pitft28r,rotate=90,speed=32000000,fps=20"
  77. fi
  78. if [ "${pitfttype}" == "28c" ]; then
  79. overlay="dtoverlay=pitft28c,rotate=90,speed=32000000,fps=20"
  80. fi
  81. if [ "${pitfttype}" == "35r" ]; then
  82. overlay="dtoverlay=pitft35r,rotate=90,speed=42000000,fps=20"
  83. fi
  84. date=`date`
  85. cat >> /boot/config.txt <<EOF
  86. # --- added by adafruit-pitft-helper $date ---
  87. [pi1]
  88. device_tree=bcm2708-rpi-b-plus.dtb
  89. [pi2]
  90. device_tree=bcm2709-rpi-2-b.dtb
  91. [all]
  92. dtparam=spi=on
  93. dtparam=i2c1=on
  94. dtparam=i2c_arm=on
  95. $overlay
  96. # --- end adafruit-pitft-helper $date ---
  97. EOF
  98. }
  99. # currently for '90' rotation only
  100. function update_xorg() {
  101. mkdir -p /etc/X11/xorg.conf.d
  102. if [ "${pitfttype}" == "28r" ]; then
  103. cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
  104. Section "InputClass"
  105. Identifier "calibration"
  106. MatchProduct "stmpe-ts"
  107. Option "Calibration" "3800 200 200 3800"
  108. Option "SwapAxes" "1"
  109. EndSection
  110. EOF
  111. fi
  112. if [ "${pitfttype}" == "35r" ]; then
  113. cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
  114. Section "InputClass"
  115. Identifier "calibration"
  116. MatchProduct "stmpe-ts"
  117. Option "Calibration" "3800 120 200 3900"
  118. Option "SwapAxes" "1"
  119. EndSection
  120. EOF
  121. fi
  122. if [ "${pitfttype}" == "28c" ]; then
  123. cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
  124. Section "InputClass"
  125. Identifier "captouch"
  126. MatchProduct "ft6x06_ts"
  127. Option "SwapAxes" "1"
  128. Option "InvertY" "1"
  129. Option "Calibration" "0 320 0 240"
  130. EndSection
  131. EOF
  132. fi
  133. }
  134. function update_x11profile() {
  135. fbturbo_path="/usr/share/X11/xorg.conf.d/99-fbturbo.conf"
  136. if [ -e $fbturbo_path ]; then
  137. echo "Moving ${fbturbo_path} to ${target_homedir}"
  138. mv "$fbturbo_path" "$target_homedir"
  139. fi
  140. if grep -xq "export FRAMEBUFFER=/dev/fb1" "${target_homedir}/.profile"; then
  141. echo "Already had 'export FRAMEBUFFER=/dev/fb1'"
  142. else
  143. echo "Adding 'export FRAMEBUFFER=/dev/fb1'"
  144. cat >> "${target_homedir}.profile" <<EOF
  145. export FRAMEBUFFER=/dev/fb1
  146. EOF
  147. fi
  148. }
  149. # currently for '90' rotation only
  150. function update_pointercal() {
  151. if [ "${pitfttype}" == "28r" ]; then
  152. cat > /etc/pointercal <<EOF
  153. -30 -5902 22077792 4360 -105 -1038814 65536
  154. EOF
  155. fi
  156. if [ "${pitfttype}" == "35r" ]; then
  157. cat > /etc/pointercal <<EOF
  158. 8 -8432 32432138 5699 -112 -965922 65536
  159. EOF
  160. fi
  161. if [ "${pitfttype}" == "28c" ]; then
  162. cat > /etc/pointercal <<EOF
  163. 320 65536 0 -65536 0 15728640 65536
  164. EOF
  165. fi
  166. }
  167. function update_udev() {
  168. if [ "${pitfttype}" == "28r" ] || [ "${pitfttype}" == "35r" ]; then
  169. cat > /etc/udev/rules.d/95-stmpe.rules <<EOF
  170. SUBSYSTEM=="input", ATTRS{name}=="stmpe-ts", ENV{DEVNAME}=="*event*", SYMLINK+="input/touchscreen"
  171. EOF
  172. fi
  173. if [ "${pitfttype}" == "28c" ]; then
  174. cat > /etc/udev/rules.d/95-ft6206.rules <<EOF
  175. SUBSYSTEM=="input", ATTRS{name}=="ft6x06_ts", ENV{DEVNAME}=="*event*", SYMLINK+="input/touchscreen"
  176. EOF
  177. fi
  178. }
  179. function install_console() {
  180. if ! grep -q 'fbcon=map:10 fbcon=font:VGA8x8' /boot/cmdline.txt; then
  181. echo "Updating /boot/cmdline.txt"
  182. sed -i 's/rootwait/rootwait fbcon=map:10 fbcon=font:VGA8x8/g' "/boot/cmdline.txt"
  183. else
  184. echo "/boot/cmdline.txt already updated"
  185. fi
  186. sed -i 's/BLANK_TIME=.*/BLANK_TIME=0/g' "/etc/kbd/config"
  187. }
  188. function uninstall_console() {
  189. sed -i 's/rootwait fbcon=map:10 fbcon=font:VGA8x8/rootwait/g' "/boot/cmdline.txt"
  190. sed -i 's/BLANK_TIME=0/BLANK_TIME=10/g' "/etc/kbd/config"
  191. echo "Screen blanking time reset to 10 minutes"
  192. }
  193. function update_etcmodules() {
  194. if [ "${pitfttype}" == "28c" ]; then
  195. ts_module="ft6x06_ts"
  196. elif [ "${pitfttype}" == "28r" ] || [ "${pitfttype}" == "35r" ]; then
  197. ts_module="stmpe_ts"
  198. else
  199. return 0
  200. fi
  201. if grep -xq "$ts_module" "/etc/modules"; then
  202. echo "Already had $ts_module"
  203. else
  204. echo "Adding $ts_module"
  205. echo "$ts_module" >> /etc/modules
  206. fi
  207. }
  208. function install_onoffbutton() {
  209. echo "Adding rpi_power_switch to /etc/modules"
  210. if grep -xq "rpi_power_switch" "${chr}/etc/modules"; then
  211. echo "Already had rpi_power_switch"
  212. else
  213. echo "Adding rpi_power_switch"
  214. cat >> /etc/modules <<EOF
  215. rpi_power_switch
  216. EOF
  217. fi
  218. echo "Adding rpi_power_switch config to /etc/modprobe.d/adafruit.conf"
  219. if grep -xq "options rpi_power_switch gpio_pin=23 mode=0" "${chr}/etc/modprobe.d/adafruit.conf"; then
  220. echo "Already had rpi_power_switch config"
  221. else
  222. echo "Adding rpi_power_switch"
  223. cat >> /etc/modprobe.d/adafruit.conf <<EOF
  224. options rpi_power_switch gpio_pin=23 mode=0
  225. EOF
  226. fi
  227. }
  228. # MAIN
  229. target_homedir="/home/pi"
  230. args=$(getopt -uo 'hvri:t:o:b:u:' -- $*)
  231. [ $? != 0 ] && print_help
  232. set -- $args
  233. for i
  234. do
  235. case "$i"
  236. in
  237. -h)
  238. print_help
  239. ;;
  240. -v)
  241. print_version
  242. ;;
  243. -u)
  244. target_homedir="$2"
  245. echo "Homedir = ${2}"
  246. shift
  247. shift
  248. ;;
  249. -t)
  250. pitfttype="$2"
  251. echo "Type = ${2}"
  252. shift
  253. shift
  254. ;;
  255. esac
  256. done
  257. if [[ $EUID -ne 0 ]]; then
  258. bail "adafruit-pitft-helper must be run as root. try: sudo adadfruit-pitft-helper"
  259. fi
  260. if grep -q boot /proc/mounts; then
  261. echo "/boot is mounted"
  262. else
  263. bail "/boot must be mounted. try: sudo mount /dev/mmcblk0p1 /boot"
  264. fi
  265. if [[ ! -e "$target_homedir" || ! -d "$target_homedir" ]]; then
  266. bail "$target_homedir must be an existing directory (use -u /home/foo to specify)"
  267. fi
  268. if [ "${pitfttype}" != "28r" ] && [ "${pitfttype}" != "28c" ] && [ "${pitfttype}" != "35r" ] && [ "${pitfttype}" != "22" ]; then
  269. echo "Type must be one of:"
  270. echo " '28r' (2.8\" resistive, PID 1601)"
  271. echo " '28c' (2.8\" capacitive, PID 1983)"
  272. echo " '35r' (3.5\" Resistive)"
  273. echo " '22' (2.2\" no touch)"
  274. echo
  275. print_help
  276. fi
  277. info PITFT "Updating X11 default calibration..."
  278. update_xorg || bail "Unable to update /etc/X11/xorg.conf.d/99-calibration.conf"
  279. info PITFT "Updating X11 setup tweaks..."
  280. update_x11profile || bail "Unable to update X11 setup"
  281. info PITFT "Updating TSLib default calibration..."
  282. update_pointercal || bail "Unable to update /etc/pointercal"
  283. info PITFT "Updating SysFS rules for Touchscreen..."
  284. update_udev || bail "Unable to update /etc/udev/rules.d"
  285. # ask for console access
  286. if ask "Would you like the console to appear on the PiTFT display?"; then
  287. info PITFT "Updating console to PiTFT..."
  288. install_console || bail "Unable to configure console"
  289. else
  290. info PITFT "Making sure console doesn't use PiTFT"
  291. uninstall_console || bail "Unable to configure console"
  292. fi
  293. info PITFT "Updating /etc/modules..."
  294. update_etcmodules || bail "Unable to update /etc/modules"
  295. if [ "${pitfttype}" != "35r" ]; then
  296. # ask for 'on/off' button
  297. if ask "Would you like GPIO #23 to act as a on/off button?"; then
  298. info PITFT "Adding GPIO #23 on/off to PiTFT..."
  299. install_onoffbutton || bail "Unable to add on/off button"
  300. fi
  301. fi
  302. info PITFT "Updating /boot/config.txt..."
  303. update_configtxt || bail "Unable to update /boot/config.txt"