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.

joy-bonnet.sh 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #!/bin/bash
  2. if [ $(id -u) -ne 0 ]; then
  3. echo "Installer must be run as root."
  4. echo "Try 'sudo bash $0'"
  5. exit 1
  6. fi
  7. clear
  8. echo "This script installs software for the Adafruit"
  9. echo "Joy Bonnet for Raspberry Pi. Steps include:"
  10. echo "- Update package index files (apt-get update)."
  11. echo "- Install Python libraries: smbus, evdev."
  12. echo "- Install joyBonnet.py in /boot and"
  13. echo " configure /etc/rc.local to auto-start script."
  14. echo "- Enable I2C bus."
  15. echo "- OPTIONAL: disable overscan."
  16. echo "Run time ~10 minutes. Reboot required."
  17. echo "EXISTING INSTALLATION, IF ANY, WILL BE OVERWRITTEN."
  18. echo
  19. echo -n "CONTINUE? [y/N] "
  20. read
  21. if [[ ! "$REPLY" =~ ^(yes|y|Y)$ ]]; then
  22. echo "Canceled."
  23. exit 0
  24. fi
  25. # FEATURE PROMPTS ----------------------------------------------------------
  26. # Installation doesn't begin until after all user input is taken.
  27. DISABLE_OVERSCAN=0
  28. INSTALL_HALT=0
  29. # Given a list of strings representing options, display each option
  30. # preceded by a number (1 to N), display a prompt, check input until
  31. # a valid number within the selection range is entered.
  32. selectN() {
  33. for ((i=1; i<=$#; i++)); do
  34. echo $i. ${!i}
  35. done
  36. echo
  37. REPLY=""
  38. while :
  39. do
  40. echo -n "SELECT 1-$#: "
  41. read
  42. if [[ $REPLY -ge 1 ]] && [[ $REPLY -le $# ]]; then
  43. return $REPLY
  44. fi
  45. done
  46. }
  47. echo -n "Disable overscan? [y/N] "
  48. read
  49. if [[ "$REPLY" =~ (yes|y|Y)$ ]]; then
  50. DISABLE_OVERSCAN=1
  51. fi
  52. echo -n "Install GPIO-halt utility? [y/N] "
  53. read
  54. if [[ "$REPLY" =~ (yes|y|Y)$ ]]; then
  55. INSTALL_HALT=1
  56. echo -n "GPIO pin for halt: "
  57. read
  58. HALT_PIN=$REPLY
  59. fi
  60. echo
  61. if [ $DISABLE_OVERSCAN -eq 1 ]; then
  62. echo "Overscan: disable."
  63. else
  64. echo "Overscan: keep current setting."
  65. fi
  66. if [ $INSTALL_HALT -eq 1 ]; then
  67. echo "Install GPIO-halt: YES (GPIO$HALT_PIN)"
  68. else
  69. echo "Install GPIO-halt: NO"
  70. fi
  71. echo
  72. echo -n "CONTINUE? [y/N] "
  73. read
  74. if [[ ! "$REPLY" =~ ^(yes|y|Y)$ ]]; then
  75. echo "Canceled."
  76. exit 0
  77. fi
  78. # START INSTALL ------------------------------------------------------------
  79. # All selections are validated at this point...
  80. # Given a filename, a regex pattern to match and a replacement string,
  81. # perform replacement if found, else append replacement to end of file.
  82. # (# $1 = filename, $2 = pattern to match, $3 = replacement)
  83. reconfig() {
  84. grep $2 $1 >/dev/null
  85. if [ $? -eq 0 ]; then
  86. # Pattern found; replace in file
  87. sed -i "s/$2/$3/g" $1 >/dev/null
  88. else
  89. # Not found; append (silently)
  90. echo $3 | sudo tee -a $1 >/dev/null
  91. fi
  92. }
  93. echo
  94. echo "Starting installation..."
  95. echo "Updating package index files..."
  96. apt-get update
  97. echo "Installing Python libraries..."
  98. apt-get install -y --force-yes python-pip python-dev python-smbus
  99. pip install evdev --upgrade
  100. echo "Installing Adafruit code in /boot..."
  101. cd /tmp
  102. curl -LO https://raw.githubusercontent.com/adafruit/Adafruit-Retrogame/master/joyBonnet.py
  103. # Moving between filesystems requires copy-and-delete:
  104. cp -r joyBonnet.py /boot
  105. rm -f joyBonnet.py
  106. if [ $INSTALL_HALT -ne 0 ]; then
  107. echo "Installing gpio-halt in /usr/local/bin..."
  108. curl -LO https://github.com/adafruit/Adafruit-GPIO-Halt/archive/master.zip
  109. unzip master.zip
  110. cd Adafruit-GPIO-Halt-master
  111. make
  112. mv gpio-halt /usr/local/bin
  113. cd ..
  114. rm -rf Adafruit-GPIO-Halt-master
  115. fi
  116. # CONFIG -------------------------------------------------------------------
  117. echo "Configuring system..."
  118. # Enable I2C using raspi-config
  119. raspi-config nonint do_i2c 0
  120. # Disable overscan compensation (use full screen):
  121. if [ $DISABLE_OVERSCAN -eq 1 ]; then
  122. raspi-config nonint do_overscan 1
  123. fi
  124. if [ $INSTALL_HALT -ne 0 ]; then
  125. # Add gpio-halt to /rc.local:
  126. grep gpio-halt /etc/rc.local >/dev/null
  127. if [ $? -eq 0 ]; then
  128. # gpio-halt already in rc.local, but make sure correct:
  129. sed -i "s/^.*gpio-halt.*$/\/usr\/local\/bin\/gpio-halt $HALT_PIN \&/g" /etc/rc.local >/dev/null
  130. else
  131. # Insert gpio-halt into rc.local before final 'exit 0'
  132. sed -i "s/^exit 0/\/usr\/local\/bin\/gpio-halt $HALT_PIN \&\\nexit 0/g" /etc/rc.local >/dev/null
  133. fi
  134. fi
  135. # Auto-start joyBonnet.py on boot
  136. grep joyBonnet.py /etc/rc.local >/dev/null
  137. if [ $? -eq 0 ]; then
  138. # joyBonnet.py already in rc.local, but make sure correct:
  139. sed -i "s/^.*joyBonnet.py.*$/cd \/boot;python joyBonnet.py \&/g" /etc/rc.local >/dev/null
  140. else
  141. # Insert joyBonnet.py into rc.local before final 'exit 0'
  142. sed -i "s/^exit 0/cd \/boot;python joyBonnet.py \&\\nexit 0/g" /etc/rc.local >/dev/null
  143. fi
  144. # Add udev rule (will overwrite if present)
  145. echo "SUBSYSTEM==\"input\", ATTRS{name}==\"retrogame\", ENV{ID_INPUT_KEYBOARD}=\"1\"" > /etc/udev/rules.d/10-retrogame.rules
  146. # PROMPT FOR REBOOT --------------------------------------------------------
  147. echo "Done."
  148. echo
  149. echo "Settings take effect on next boot."
  150. echo
  151. echo -n "REBOOT NOW? [y/N] "
  152. read
  153. if [[ ! "$REPLY" =~ ^(yes|y|Y)$ ]]; then
  154. echo "Exiting without reboot."
  155. exit 0
  156. fi
  157. echo "Reboot started..."
  158. reboot
  159. exit 0