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.

285 lines
8.2 KiB

  1. #!/bin/bash
  2. # INSTALLER SCRIPT FOR ADAFRUIT RGB MATRIX BONNET OR HAT
  3. # hzeller/rpi-rgb-led-matrix sees lots of active development!
  4. # That's cool and all, BUT, to avoid tutorial breakage,
  5. # we reference a specific commit (update this as needed):
  6. GITUSER=https://github.com/hzeller
  7. REPO=rpi-rgb-led-matrix
  8. COMMIT=21410d2b0bac006b4a1661594926af347b3ce334
  9. # Previously: COMMIT=e3dd56dcc0408862f39cccc47c1d9dea1b0fb2d2
  10. if [ $(id -u) -ne 0 ]; then
  11. echo "Installer must be run as root."
  12. echo "Try 'sudo bash $0'"
  13. exit 1
  14. fi
  15. clear
  16. echo "This script installs software for the Adafruit"
  17. echo "RGB Matrix Bonnet or HAT for Raspberry Pi."
  18. echo "Steps include:"
  19. echo "- Update package index files (apt-get update)"
  20. echo "- Install prerequisite software"
  21. echo "- Install RGB matrix driver software and examples"
  22. echo "- Configure boot options"
  23. echo "Run time ~15 minutes. Some options require reboot."
  24. echo "EXISTING INSTALLATION, IF ANY, WILL BE OVERWRITTEN."
  25. echo
  26. echo -n "CONTINUE? [y/N] "
  27. read
  28. if [[ ! "$REPLY" =~ ^(yes|y|Y)$ ]]; then
  29. echo "Canceled."
  30. exit 0
  31. fi
  32. # FEATURE PROMPTS ----------------------------------------------------------
  33. # Installation doesn't begin until after all user input is taken.
  34. INTERFACE_TYPE=0
  35. INSTALL_RTC=0
  36. QUALITY_MOD=0
  37. #SLOWDOWN_GPIO=5
  38. #MATRIX_SIZE=3
  39. # Given a list of strings representing options, display each option
  40. # preceded by a number (1 to N), display a prompt, check input until
  41. # a valid number within the selection range is entered.
  42. selectN() {
  43. args=("${@}")
  44. if [[ ${args[0]} = "0" ]]; then
  45. OFFSET=0
  46. else
  47. OFFSET=1
  48. fi
  49. for ((i=0; i<$#; i++)); do
  50. echo $((i+$OFFSET)). ${args[$i]}
  51. done
  52. echo
  53. REPLY=""
  54. let LAST=$#+$OFFSET-1
  55. while :
  56. do
  57. echo -n "SELECT $OFFSET-$LAST: "
  58. read
  59. if [[ $REPLY -ge $OFFSET ]] && [[ $REPLY -le $LAST ]]; then
  60. let RESULT=$REPLY-$OFFSET
  61. return $RESULT
  62. fi
  63. done
  64. }
  65. OPTION_NAMES=(NO YES)
  66. INTERFACES=( \
  67. "Adafruit RGB Matrix Bonnet" \
  68. "Adafruit RGB Matrix HAT + RTC" \
  69. )
  70. QUALITY_OPTS=( \
  71. "Quality (disables sound, requires soldering)" \
  72. "Convenience (sound on, no soldering)" \
  73. )
  74. #SLOWDOWN_OPTS=( \
  75. # "0" \
  76. # "1" \
  77. # "2" \
  78. # "3" \
  79. # "4" \
  80. # "None -- specify at runtime with --led-slowdown-gpio" \
  81. #)
  82. # Default matrix dimensions are currently fixed at 32x32 in RGB matrix lib.
  83. # If that's compile-time configurable in the future, it'll happen here...
  84. #MATRIX_WIDTHS=(32 32 64)
  85. #MATRIX_HEIGHTS=(16 32 32)
  86. #SIZE_OPTS=( \
  87. # "${MATRIX_WIDTHS[0]} x ${MATRIX_HEIGHTS[0]}" \
  88. # "${MATRIX_WIDTHS[1]} x ${MATRIX_HEIGHTS[1]}" \
  89. # "${MATRIX_WIDTHS[2]} x ${MATRIX_HEIGHTS[2]}" \
  90. # "None/other -- specify at runtime with --led-cols and --led-rows" \
  91. #)
  92. echo
  93. echo "Select interface board type:"
  94. selectN "${INTERFACES[@]}"
  95. INTERFACE_TYPE=$?
  96. if [ $INTERFACE_TYPE -eq 1 ]; then
  97. # For matrix HAT, ask about RTC install
  98. echo
  99. echo -n "Install realtime clock support? [y/N] "
  100. read
  101. if [[ "$REPLY" =~ (yes|y|Y)$ ]]; then
  102. INSTALL_RTC=1
  103. fi
  104. fi
  105. #echo
  106. #echo "OPTIONAL: GPIO throttling can be compiled-in so"
  107. #echo "there's no need to specify this every time."
  108. #echo "For Raspberry Pi 4, it's usually 4, sometimes 3."
  109. #echo "Smaller values work for earlier, slower Pi models."
  110. #echo "If unsure, test different settings with the"
  111. #echo "--led-slowdown-gpio flag at runtime, then re-run"
  112. #echo "this installer, selecting the minimum slowdown value"
  113. #echo "that works reliably with your Pi and matrix."
  114. #echo "GPIO slowdown setting:"
  115. #selectN "${SLOWDOWN_OPTS[@]}"
  116. #SLOWDOWN_GPIO=$?
  117. #echo
  118. #echo "OPTIONAL: matrix size can be compiled-in so"
  119. #echo "there's no need to specify this every time."
  120. #echo "Some common Adafruit matrix sizes:"
  121. #selectN "${SIZE_OPTS[@]}"
  122. #MATRIX_SIZE=$?
  123. echo
  124. echo "Now you must choose between QUALITY and CONVENIENCE."
  125. echo
  126. echo "QUALITY: best output from the LED matrix requires"
  127. echo "commandeering hardware normally used for sound, plus"
  128. echo "some soldering. If you choose this option, there will"
  129. echo "be NO sound from the audio jack or HDMI (USB audio"
  130. echo "adapters will work and sound best anyway), AND you"
  131. echo "must SOLDER a wire between GPIO4 and GPIO18 on the"
  132. echo "Bonnet or HAT board."
  133. echo
  134. echo "CONVENIENCE: sound works normally, no extra soldering."
  135. echo "Images on the LED matrix are not quite as steady, but"
  136. echo "maybe OK for most uses. If eager to get started, use"
  137. echo "'CONVENIENCE' for now, you can make the change and"
  138. echo "reinstall using this script later!"
  139. echo
  140. echo "What is thy bidding?"
  141. selectN "${QUALITY_OPTS[@]}"
  142. QUALITY_MOD=$?
  143. # VERIFY SELECTIONS BEFORE CONTINUING --------------------------------------
  144. echo
  145. echo "Interface board type: ${INTERFACES[$INTERFACE_TYPE]}"
  146. if [ $INTERFACE_TYPE -eq 1 ]; then
  147. echo "Install RTC support: ${OPTION_NAMES[$INSTALL_RTC]}"
  148. fi
  149. #echo "GPIO slowdown: ${SLOWDOWN_OPTS[$SLOWDOWN_GPIO]}"
  150. #echo "Matrix size: ${SIZE_OPTS[$MATRIX_SIZE]}"
  151. echo "Optimize: ${QUALITY_OPTS[$QUALITY_MOD]}"
  152. if [ $QUALITY_MOD -eq 0 ]; then
  153. echo "Reminder: you must SOLDER a wire between GPIO4"
  154. echo "and GPIO18, and internal sound is DISABLED!"
  155. fi
  156. echo
  157. echo -n "CONTINUE? [y/N] "
  158. read
  159. if [[ ! "$REPLY" =~ ^(yes|y|Y)$ ]]; then
  160. echo "Canceled."
  161. exit 0
  162. fi
  163. # START INSTALL ------------------------------------------------------------
  164. # All selections are validated at this point...
  165. # Given a filename, a regex pattern to match and a replacement string,
  166. # perform replacement if found, else append replacement to end of file.
  167. # (# $1 = filename, $2 = pattern to match, $3 = replacement)
  168. reconfig() {
  169. grep $2 $1 >/dev/null
  170. if [ $? -eq 0 ]; then
  171. # Pattern found; replace in file
  172. sed -i "s/$2/$3/g" $1 >/dev/null
  173. else
  174. # Not found; append (silently)
  175. echo $3 | sudo tee -a $1 >/dev/null
  176. fi
  177. }
  178. echo
  179. echo "Starting installation..."
  180. echo "Updating package index files..."
  181. apt-get update
  182. echo "Downloading prerequisites..."
  183. apt-get install -y --force-yes python2.7-dev python-pillow python3-dev python3-pillow
  184. echo "Downloading RGB matrix software..."
  185. curl -L $GITUSER/$REPO/archive/$COMMIT.zip -o $REPO-$COMMIT.zip
  186. unzip -q $REPO-$COMMIT.zip
  187. rm $REPO-$COMMIT.zip
  188. mv $REPO-$COMMIT rpi-rgb-led-matrix
  189. echo "Building RGB matrix software..."
  190. cd rpi-rgb-led-matrix
  191. USER_DEFINES=""
  192. #if [ $SLOWDOWN_GPIO -lt 5 ]; then
  193. # USER_DEFINES+=" -DRGB_SLOWDOWN_GPIO=$SLOWDOWN_GPIO"
  194. #fi
  195. #if [ $MATRIX_SIZE --lt 3 ]; then
  196. # USER_DEFINES+=" -DLED_COLS=${MATRIX_WIDTHS[$MATRIX_SIZE]}"
  197. # USER_DEFINES+=" -DLED_ROWS=${MATRIX_HEIGHTS[$MATRIX_SIZE]}"
  198. #fi
  199. if [ $QUALITY_MOD -eq 0 ]; then
  200. # Build and install for Python 2.7...
  201. make clean
  202. make install-python HARDWARE_DESC=adafruit-hat-pwm USER_DEFINES="$USER_DEFINES" PYTHON=$(which python2)
  203. # Do over for Python 3...
  204. make clean
  205. make install-python HARDWARE_DESC=adafruit-hat-pwm USER_DEFINES="$USER_DEFINES" PYTHON=$(which python3)
  206. else
  207. # Build then install for Python 2.7...
  208. USER_DEFINES+=" -DDISABLE_HARDWARE_PULSES"
  209. make clean
  210. make install-python HARDWARE_DESC=adafruit-hat USER_DEFINES="$USER_DEFINES" PYTHON=$(which python2)
  211. # Do over for Python 3...
  212. make clean
  213. make install-python HARDWARE_DESC=adafruit-hat USER_DEFINES="$USER_DEFINES" PYTHON=$(which python3)
  214. fi
  215. # Change ownership to user calling sudo
  216. chown -R $SUDO_USER:$(id -g $SUDO_USER) `pwd`
  217. # CONFIG -------------------------------------------------------------------
  218. echo "Configuring system..."
  219. if [ $INSTALL_RTC -ne 0 ]; then
  220. # Enable I2C for RTC
  221. raspi-config nonint do_i2c 0
  222. # Do additional RTC setup for DS1307
  223. reconfig /boot/config.txt "^.*dtoverlay=i2c-rtc.*$" "dtoverlay=i2c-rtc,ds1307"
  224. apt-get -y remove fake-hwclock
  225. update-rc.d -f fake-hwclock remove
  226. sudo sed --in-place '/if \[ -e \/run\/systemd\/system \] ; then/,+2 s/^#*/#/' /lib/udev/hwclock-set
  227. fi
  228. if [ $QUALITY_MOD -eq 0 ]; then
  229. # Disable sound ('easy way' -- kernel module not blacklisted)
  230. reconfig /boot/config.txt "^.*dtparam=audio.*$" "dtparam=audio=off"
  231. else
  232. # Enable sound (ditto)
  233. reconfig /boot/config.txt "^.*dtparam=audio.*$" "dtparam=audio=on"
  234. fi
  235. # PROMPT FOR REBOOT --------------------------------------------------------
  236. echo "Done."
  237. echo
  238. echo "Settings take effect on next boot."
  239. if [ $INSTALL_RTC -ne 0 ]; then
  240. echo "RTC will be enabled then but time must be set"
  241. echo "up using the 'date' and 'hwclock' commands."
  242. fi
  243. echo
  244. echo -n "REBOOT NOW? [y/N] "
  245. read
  246. if [[ ! "$REPLY" =~ ^(yes|y|Y)$ ]]; then
  247. echo "Exiting without reboot."
  248. exit 0
  249. fi
  250. echo "Reboot started..."
  251. reboot
  252. sleep infinity