My Marlin configs for Fabrikator Mini and CTC i3 Pro B
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.

mftest 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #!/usr/bin/env bash
  2. #
  3. # mftest Select a test to apply and build
  4. # mftest -b Build the auto-detected environment
  5. # mftest -u Upload the auto-detected environment
  6. # mftest [name] [index] [-y] Set config options and optionally build a test
  7. #
  8. MFINFO=$(mfinfo) || exit 1
  9. [[ -d Marlin/src ]] || { echo "Please 'cd' up to repo root." ; exit 1 ; }
  10. TESTPATH=buildroot/share/tests
  11. STATE_FILE=$( echo ./.pio/.mftestrc )
  12. SED=$(which gsed || which sed)
  13. shopt -s extglob nocasematch
  14. # Matching patterns
  15. ISNUM='^[0-9]+$'
  16. ISCMD='^(restore|opt|exec|use|pins|env)_'
  17. ISEXEC='^exec_'
  18. ISCONT='\\ *$'
  19. # Get the environment and test number from the command
  20. TESTENV=${1:-'-'}
  21. CHOICE=${2:-0}
  22. AUTOENV=0
  23. # Allow shorthand for test name
  24. case $TESTENV in
  25. tree) pio run -d . -e include_tree ; exit 1 ;;
  26. due) TESTENV='DUE' ;;
  27. esp) TESTENV='esp32' ;;
  28. lin*) TESTENV='linux_native' ;;
  29. lpc?(8)) TESTENV='LPC1768' ;;
  30. lpc9) TESTENV='LPC1769' ;;
  31. m128) TESTENV='megaatmega1280' ;;
  32. m256) TESTENV='megaatmega2560' ;;
  33. mega) TESTENV='megaatmega2560' ;;
  34. stm) TESTENV='STM32F103RE' ;;
  35. f1) TESTENV='STM32F103RE' ;;
  36. f4) TESTENV='STM32F4' ;;
  37. f7) TESTENV='STM32F7' ;;
  38. s6) TESTENV='FYSETC_S6' ;;
  39. teensy) TESTENV='teensy31' ;;
  40. t31) TESTENV='teensy31' ;;
  41. t32) TESTENV='teensy31' ;;
  42. t35) TESTENV='teensy35' ;;
  43. t36) TESTENV='teensy35' ;;
  44. # Build with the last-built env
  45. -r) [[ -f "$STATE_FILE" ]] || { echo "No previous (-r) build state found." ; exit 1 ; }
  46. read TESTENV <"$STATE_FILE"
  47. pio run -d . -e $TESTENV
  48. exit
  49. ;;
  50. -[bu]) MB=$( grep "define MOTHERBOARD" Marlin/Configuration.h | awk '{ print $3 }' | $SED 's/BOARD_//' )
  51. [[ -z $MB ]] && { echo "Error - Unable to read MOTHERBOARD setting." ; exit 1 ; }
  52. BLINE=$( grep "define BOARD_$MB" Marlin/src/core/boards.h )
  53. BNUM=$( $SED -E 's/^.+BOARD_[^ ]+ +([0-9]+).+$/\1/' <<<"$BLINE" )
  54. BDESC=$( $SED -E 's/^.+\/\/ *(.+)$/\1/' <<<"$BLINE" )
  55. [[ -z $BNUM ]] && { echo "Error - Unable to $MB in boards list." ; exit 1 ; }
  56. readarray -t ENVS <<< $( grep -A1 "MB($MB)" Marlin/src/pins/pins.h | $SED -n '2 p' | grep -oE 'env:[^ ]+' | $SED -E 's/env://' )
  57. [[ -z $ENVS ]] && { echo "Error - Unable to find target(s) for $MB ($BNUM)." ; exit 1 ; }
  58. ECOUNT=${#ENVS[*]}
  59. if [[ $ECOUNT == 1 ]]; then
  60. TARGET=$ENVS
  61. else
  62. #
  63. # List env names and numbers. Get selection.
  64. #
  65. if [[ $CHOICE == 0 ]]; then
  66. echo "Available targets for \"$BDESC\" | $MB ($BNUM):"
  67. IND=0 ; for ENV in "${ENVS[@]}"; do echo " $IND) $ENV" ; done
  68. if [[ $ECOUNT > 1 ]]; then
  69. for (( ; ; ))
  70. do
  71. read -p "Select a target for '$MB' (1-$ECOUNT) : " CHOICE
  72. [[ -z "$CHOICE" ]] && { echo '(canceled)' ; exit 1 ; }
  73. [[ $CHOICE =~ $ISNUM ]] && ((CHOICE >= 1 && CHOICE <= ECOUNT)) && break
  74. echo ">>> Invalid environment choice '$CHOICE'."
  75. done
  76. echo
  77. fi
  78. else
  79. echo "Detected \"$BDESC\" | $MB ($BNUM)."
  80. [[ $CHOICE > $ECOUNT ]] && { echo "Environment selection is out of range." ; exit 1 ; }
  81. fi
  82. TARGET="${ENVS[$CHOICE-1]}"
  83. echo "Selected $TARGET"
  84. fi
  85. echo "$TARGET" >"$STATE_FILE"
  86. if [[ $TESTENV == "-u" ]]; then
  87. echo "Build/Uploading environment $TARGET for board $MB ($BNUM)..." ; echo
  88. pio run -t upload -e $TARGET
  89. else
  90. echo "Building environment $TARGET for board $MB ($BNUM)..." ; echo
  91. pio run -e $TARGET
  92. fi
  93. exit
  94. ;;
  95. # The -y flag may come first
  96. -y) TESTENV=${2:-'-'} ; CHOICE=${3:-0} ;;
  97. -[a-z]) echo "Unknown flag $TESTENV" ; exit 1 ;;
  98. -) ;;
  99. esac
  100. #
  101. # List available tests and ask for selection
  102. #
  103. if [[ $TESTENV == '-' ]]; then
  104. IND=0
  105. NAMES=()
  106. for FILE in $( ls -1 $TESTPATH/*-tests )
  107. do
  108. let IND++
  109. TNAME=${FILE/-tests/}
  110. TNAME=${TNAME/$TESTPATH\//}
  111. NAMES+=($TNAME)
  112. (( IND < 10 )) && echo -n " "
  113. echo " $IND) $TNAME"
  114. done
  115. echo
  116. for (( ; ; ))
  117. do
  118. read -p "Select a test to apply (1-$IND) : " NAMEIND
  119. [[ -z "$NAMEIND" ]] && { echo '(canceled)' ; exit 1 ; }
  120. [[ $NAMEIND =~ $ISNUM ]] && ((NAMEIND >= 1 && NAMEIND <= IND)) && { TESTENV=${NAMES[$NAMEIND-1]} ; echo ; break ; }
  121. echo "Invalid selection."
  122. done
  123. fi
  124. # Get the contents of the test file
  125. OUT=$( cat $TESTPATH/$TESTENV-tests 2>/dev/null ) || { echo "Can't find test '$TESTENV'." ; exit 1 ; }
  126. # Count up the number of tests
  127. TESTCOUNT=$( awk "/$ISEXEC/{a++}END{print a}" <<<"$OUT" )
  128. # User entered a number?
  129. (( CHOICE && CHOICE > TESTCOUNT )) && { echo "Invalid test selection '$CHOICE' (1-$TESTCOUNT)." ; exit 1 ; }
  130. if [[ $CHOICE == 0 ]]; then
  131. #
  132. # List test descriptions with numbers and get selection
  133. #
  134. echo "Available '$TESTENV' tests:" ; echo "$OUT" | {
  135. IND=0
  136. while IFS= read -r LINE
  137. do
  138. if [[ $LINE =~ $ISEXEC ]]; then
  139. DESC=$( "$SED" -E 's/^.+"(.*)".*$/\1/g' <<<"$LINE" )
  140. (( ++IND < 10 )) && echo -n " "
  141. echo " $IND) $DESC"
  142. fi
  143. done
  144. }
  145. CHOICE=1
  146. if [[ $TESTCOUNT > 1 ]]; then
  147. for (( ; ; ))
  148. do
  149. read -p "Select a '$TESTENV' test (1-$TESTCOUNT) : " CHOICE
  150. [[ -z "$CHOICE" ]] && { echo '(canceled)' ; exit 1 ; }
  151. [[ $CHOICE =~ $ISNUM ]] && ((CHOICE >= 1 && CHOICE <= TESTCOUNT)) && break
  152. echo ">>> Invalid test selection '$CHOICE'."
  153. done
  154. fi
  155. fi
  156. #
  157. # Run the specified test lines
  158. #
  159. echo "$OUT" | {
  160. IND=0
  161. GOTX=0
  162. CMD=""
  163. while IFS= read -r LINE
  164. do
  165. if [[ $LINE =~ $ISCMD || $GOTX == 1 ]]; then
  166. ((!IND)) && let IND++
  167. if [[ $LINE =~ $ISEXEC ]]; then
  168. ((IND++ > CHOICE)) && break
  169. else
  170. ((!HEADER)) && {
  171. HEADER=1
  172. echo -e "\n#\n# Test $TESTENV ($CHOICE) $DESC\n#"
  173. }
  174. ((IND == CHOICE)) && {
  175. GOTX=1
  176. [[ $CMD == "" ]] && CMD="$LINE" || CMD=$( echo -e "$CMD$LINE" | $SED -e 's/\\//g' )
  177. [[ $LINE =~ $ISCONT ]] || { echo $CMD ; eval "$CMD" ; CMD="" ; }
  178. }
  179. fi
  180. fi
  181. done
  182. }
  183. # Get a -y parameter the lazy way
  184. [[ "$2" == "-y" || "$3" == "-y" ]] && BUILD_YES='Y'
  185. # Build the test too?
  186. if [[ $BUILD_YES != 'Y' ]]; then
  187. echo
  188. read -p "Build $TESTENV test #$CHOICE (y/N) ? " BUILD_YES
  189. fi
  190. [[ $BUILD_YES == 'Y' || $BUILD_YES == 'Yes' ]] && {
  191. pio run -d . -e $TESTENV
  192. echo "$TESTENV" >"$STATE_FILE"
  193. }