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.

273 lines
8.1 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. #!/bin/bash
  2. # INSTALLER SCRIPT FOR ADAFRUIT SPECTRO PROJECT
  3. if [ $(id -u) -ne 0 ]; then
  4. echo "Installer must be run as root."
  5. echo "Try 'sudo bash $0'"
  6. exit 1
  7. fi
  8. clear
  9. echo "This script installs software for the Adafruit"
  10. echo "Spectro project for Raspberry Pi."
  11. echo "Steps include:"
  12. echo "- Update package index files (apt-get update)"
  13. echo "- Install prerequisite software"
  14. echo "- Install Spectro software"
  15. echo "- Configure hardware and boot options"
  16. echo "Run time ~10 minutes."
  17. echo
  18. echo "EXISTING INSTALLATION, IF ANY, WILL BE OVERWRITTEN."
  19. echo "If you've edited any Spectro-related files, cancel"
  20. echo "the installation and back up those files first."
  21. echo
  22. echo -n "CONTINUE? [y/N] "
  23. read
  24. if [[ ! "$REPLY" =~ ^(yes|y|Y)$ ]]; then
  25. echo "Canceled."
  26. exit 0
  27. fi
  28. # FEATURE PROMPTS ----------------------------------------------------------
  29. # Installation doesn't begin until after all user input is taken.
  30. MATRIX_SIZE=0
  31. SLOWDOWN_GPIO=0
  32. ENABLE_MIC=0
  33. ENABLE_ACCEL=0
  34. HDMI_SIZE=0
  35. OPTION_NAMES=(NO YES)
  36. MATRIX_WIDTHS=(64 32)
  37. MATRIX_HEIGHTS=(32 16)
  38. SIZE_OPTS=( \
  39. "${MATRIX_WIDTHS[0]} x ${MATRIX_HEIGHTS[0]}" \
  40. "${MATRIX_WIDTHS[1]} x ${MATRIX_HEIGHTS[1]}" \
  41. )
  42. SLOWDOWN_OPTS=( \
  43. "0" \
  44. "1" \
  45. "2" \
  46. "3" \
  47. "4" \
  48. )
  49. HDMI_OPTS=( \
  50. "640x480" \
  51. "320x240" \
  52. )
  53. # Given a list of strings representing options, display each option
  54. # preceded by a number (1 to N), display a prompt, check input until
  55. # a valid number within the selection range is entered.
  56. selectN() {
  57. args=("${@}")
  58. # If first item in list is the literal number '0', make the list
  59. # indexed from 0 rather than 1. This is to avoid confusion when
  60. # entering the GPIO slowdown setting (e.g. entering '1' for a
  61. # value of '0' is awkward). In all other cases, list is indexed
  62. # from 1 as this is more human.
  63. if [[ ${args[0]} = "0" ]]; then
  64. OFFSET=0
  65. else
  66. OFFSET=1
  67. fi
  68. for ((i=0; i<$#; i++)); do
  69. echo $((i+$OFFSET)). ${args[$i]}
  70. done
  71. echo
  72. REPLY=""
  73. let LAST=$#+$OFFSET-1
  74. while :
  75. do
  76. echo -n "SELECT $OFFSET-$LAST: "
  77. read
  78. if [[ $REPLY -ge $OFFSET ]] && [[ $REPLY -le $LAST ]]; then
  79. let RESULT=$REPLY-$OFFSET
  80. return $RESULT
  81. fi
  82. done
  83. }
  84. echo
  85. echo "What size LED matrix are you using with Spectro?"
  86. selectN "${SIZE_OPTS[@]}"
  87. MATRIX_SIZE=$?
  88. echo
  89. echo "Faster Pi boards require dialing back GPIO speed"
  90. echo "to work with the LED matrix. For Raspberry Pi 4,"
  91. echo "this usually means the max '4' slowdown setting."
  92. echo "For Pi 2 or 3, try the '1' or '2' settings. There"
  93. echo "is no hard-set rule to this, it can vary with"
  94. echo "matrix and cabling as well. If the Spectro display"
  95. echo "is glitchy, just re-run this installer, selecting"
  96. echo "a higher setting until you find a stable value."
  97. echo "GPIO slowdown setting:"
  98. selectN "${SLOWDOWN_OPTS[@]}"
  99. SLOWDOWN_GPIO=$?
  100. echo
  101. echo -n "OPTIONAL: Enable USB microphone support? [y/N] "
  102. read
  103. if [[ "$REPLY" =~ (yes|y|Y)$ ]]; then
  104. ENABLE_MIC=1
  105. fi
  106. echo
  107. echo -n "OPTIONAL: Enable LIS3DH accelerometer support? [y/N] "
  108. read
  109. if [[ "$REPLY" =~ (yes|y|Y)$ ]]; then
  110. ENABLE_ACCEL=1
  111. fi
  112. # HDMI resolution selection might be handled later; right now nothing
  113. # requires it. But in the future if anything relies on fb2matrix.py,
  114. # HDMI resolution ultimately determines the frame rate that's possible,
  115. # flipside being that some monitors can't handle extremely low resolutions.
  116. # So an option might be presented here, set to 320x240 or 640x480 (the
  117. # latter being the minimum resolution some displays can support).
  118. # VERIFY SELECTIONS BEFORE CONTINUING --------------------------------------
  119. echo
  120. echo "LED matrix size: ${SIZE_OPTS[$MATRIX_SIZE]}"
  121. echo "GPIO slowdown: ${SLOWDOWN_OPTS[$SLOWDOWN_GPIO]}"
  122. echo "Enable USB microphone support: ${OPTION_NAMES[$ENABLE_MIC]}"
  123. echo "Enable LIS3DH support: ${OPTION_NAMES[$ENABLE_ACCEL]}"
  124. #echo "HDMI resolution: ${HDMI_OPTS[$HDMI_SIZE]}"
  125. echo
  126. echo -n "CONTINUE? [y/N] "
  127. read
  128. if [[ ! "$REPLY" =~ ^(yes|y|Y)$ ]]; then
  129. echo "Canceled."
  130. exit 0
  131. fi
  132. # Check whether RGB matrix library is present.
  133. # If not, offer to download and run that script first (then return here).
  134. echo
  135. echo "Updating package index files..."
  136. apt-get update
  137. apt-get -qq install python3-pip python-pip
  138. echo -n "Checking for RGB matrix library..."
  139. pip3 freeze | grep rgbmatrix > /dev/null
  140. if [ $? -eq 0 ]; then
  141. echo "OK."
  142. else
  143. echo "not present."
  144. echo "Would you like to download and install the RGB matrix"
  145. echo "library (required by Spectro) first? If so, DO NOT REBOOT"
  146. echo "when prompted. You’ll return to this script for more"
  147. echo "Spectro configuration."
  148. echo -n "Run RGB matrix installer? [y/N] "
  149. read
  150. if [[ "$REPLY" =~ (yes|y|Y)$ ]]; then
  151. wget https://raw.githubusercontent.com/adafruit/Raspberry-Pi-Installer-Scripts/master/rgb-matrix.sh -O rgb-matrix.sh
  152. bash rgb-matrix.sh
  153. echo
  154. echo "You are now back in the main Spectro installer script."
  155. echo "When prompted about reboot again, now it's OK!"
  156. fi
  157. fi
  158. # START INSTALL ------------------------------------------------------------
  159. # All selections are validated at this point...
  160. # Given a filename, a regex pattern to match and a replacement string,
  161. # perform replacement if found, else append replacement to end of file.
  162. # (# $1 = filename, $2 = pattern to match, $3 = replacement)
  163. reconfig() {
  164. grep $2 $1 >/dev/null
  165. if [ $? -eq 0 ]; then
  166. # Pattern found; replace in file
  167. sed -i "s/$2/$3/g" $1 >/dev/null
  168. else
  169. # Not found; append (silently)
  170. echo $3 | sudo tee -a $1 >/dev/null
  171. fi
  172. }
  173. # Same as above, but skips if pattern not found
  174. reconfig2() {
  175. grep $2 $1 >/dev/null
  176. if [ $? -eq 0 ]; then
  177. # Pattern found; replace in file
  178. sed -i "s/$2/$3/g" $1 >/dev/null
  179. fi
  180. }
  181. echo
  182. echo "Starting installation..."
  183. # Although Spectro is all Python3-ready, user additions might rely on
  184. # Python2, so we'll install the prerequisite libraries for both 2 and 3...
  185. echo "Downloading prerequisites..."
  186. pip3 install psutil RPi.GPIO
  187. pip install psutil RPi.GPIO
  188. apt-get install -y --allow-unauthenticated python3-dev python3-pillow python2.7-dev python-pillow
  189. if [ $ENABLE_MIC -ne 0 ]; then
  190. apt-get install -y --allow-unauthenticated python3-pyaudio python3-numpy python-pyaudio python-numpy
  191. fi
  192. if [ $ENABLE_ACCEL -ne 0 ]; then
  193. pip3 install adafruit-circuitpython-busdevice adafruit-circuitpython-lis3dh
  194. pip install adafruit-circuitpython-busdevice adafruit-circuitpython-lis3dh
  195. fi
  196. echo "Downloading Spectro software..."
  197. curl -L https://github.com/adafruit/Adafruit_Spectro_Pi/archive/master.zip -o Adafruit_Spectro_Pi.zip
  198. unzip -q -o Adafruit_Spectro_Pi.zip
  199. rm Adafruit_Spectro_Pi.zip
  200. mv Adafruit_Spectro_Pi-master Adafruit_Spectro_Pi
  201. chown -R pi:pi Adafruit_Spectro_Pi
  202. # CONFIG -------------------------------------------------------------------
  203. echo "Configuring system..."
  204. if [ $ENABLE_MIC -ne 0 ]; then
  205. # Change ALSA settings to allow USB mic use
  206. reconfig2 /usr/share/alsa/alsa.conf "^defaults.ctl.card.*0" "defaults.ctl.card 1"
  207. reconfig2 /usr/share/alsa/alsa.conf "^defaults.pcm.card.*0" "defaults.pcm.card 1"
  208. fi
  209. if [ $ENABLE_ACCEL -ne 0 ]; then
  210. # Enable I2C for accelerometer
  211. raspi-config nonint do_i2c 0
  212. fi
  213. # Make default GIFs directory
  214. mkdir /boot/gifs
  215. # Set up LED columns, rows and slowdown in selector.py script
  216. reconfig2 ./Adafruit_Spectro_Pi/selector.py "^FLAGS.*$" "FLAGS\ =\ [\"--led-cols=${MATRIX_WIDTHS[$MATRIX_SIZE]}\",\ \"--led-rows=${MATRIX_HEIGHTS[$MATRIX_SIZE]}\",\ \"--led-slowdown-gpio=${SLOWDOWN_OPTS[$SLOWDOWN_GPIO]}\"]"
  217. # Force HDMI out so /dev/fb0 exists
  218. reconfig /boot/config.txt "^.*hdmi_force_hotplug.*$" "hdmi_force_hotplug=1"
  219. #reconfig /boot/config.txt "^.*hdmi_group.*$" "hdmi_group=2"
  220. #reconfig /boot/config.txt "^.*hdmi_mode.*$" "hdmi_mode=87"
  221. # Auto-start selector.py on boot
  222. grep selector.py /etc/rc.local >/dev/null
  223. if [ $? -ne 0 ]; then
  224. # Insert selector.py into rc.local before final 'exit 0'
  225. sed -i "s/^exit 0/cd \/home\/pi\/Adafruit_Spectro_Pi\;python3 selector.py \&\\nexit 0/g" /etc/rc.local >/dev/null
  226. fi
  227. # PROMPT FOR REBOOT --------------------------------------------------------
  228. echo "Done."
  229. echo
  230. echo "Settings take effect on next boot."
  231. echo "For proper clock time, set the time zone with raspi-config."
  232. echo
  233. echo -n "REBOOT NOW? [y/N] "
  234. read
  235. if [[ ! "$REPLY" =~ ^(yes|y|Y)$ ]]; then
  236. echo "Exiting without reboot."
  237. exit 0
  238. fi
  239. echo "Reboot started..."
  240. reboot
  241. sleep infinity