My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. [[ -d Marlin/src ]] || { echo "Please 'cd' to the Marlin repo root." ; exit 1 ; }
  9. perror() { echo -e "$0: \033[0;31m$1 -- $2\033[0m" ; }
  10. errout() { echo -e "\033[0;31m$1\033[0m" ; }
  11. bugout() { ((DEBUG)) && echo -e "\033[0;32m$1\033[0m" ; }
  12. usage() {
  13. echo "
  14. Usage: mftest [-t|--env=<env|index>] [-n|--num=<num>] [-m|--make] [-y|--build=<Y|n>]
  15. mftest [-a|--autobuild]
  16. mftest [-r|--rebuild]
  17. mftest [-u|--autoupload] [-n|--num=<num>]
  18. OPTIONS
  19. -t --env The environment to apply / run, or the menu index number.
  20. -n --num The index of the test to run. (In file order.)
  21. -m --make Use the make / Docker method for the build.
  22. -y --build Skip 'Do you want to build this test?' and assume YES.
  23. -h --help Print this help.
  24. -a --autobuild PIO Build using the MOTHERBOARD environment.
  25. -u --autoupload PIO Upload using the MOTHERBOARD environment.
  26. -v --verbose Extra output for debugging.
  27. -s --silent Silence build output from PlatformIO.
  28. env shortcuts: tree due esp lin lpc|lpc8 lpc9 m128 m256|mega stm|f1 f4 f7 s6 teensy|t31|t32 t35|t36 t40|t41
  29. "
  30. }
  31. TESTPATH=buildroot/tests
  32. STATE_FILE="./.pio/.mftestrc"
  33. SED=$(which gsed sed | head -n1)
  34. shopt -s extglob nocasematch
  35. # Matching patterns
  36. ISNUM='^[0-9]+$'
  37. ISCMD='^(restore|opt|exec|use|pins|env)_'
  38. ISEXEC='^exec_'
  39. ISCONT='\\ *$'
  40. # Get environment, test number, etc. from the command
  41. TESTENV='-'
  42. CHOICE=0
  43. DEBUG=0
  44. while getopts 'abhmruvyn:t:-:' OFLAG; do
  45. case "${OFLAG}" in
  46. a) AUTO_BUILD=1 ; bugout "Auto-Build target..." ;;
  47. h) EXIT_USAGE=1 ;;
  48. m) USE_MAKE=1 ; bugout "Using make with Docker..." ;;
  49. n) case "$OPTARG" in
  50. *[!0-9]*) perror "option requires a number" $OFLAG ; EXIT_USAGE=2 ;;
  51. *) CHOICE="$OPTARG" ; bugout "Got a number: $CHOICE" ;;
  52. esac
  53. ;;
  54. r) REBUILD=1 ; bugout "Rebuilding previous..." ;;
  55. s) SILENT_FLAG="-s" ;;
  56. t) TESTENV="$OPTARG" ; bugout "Got a target: $TESTENV" ;;
  57. u) AUTO_BUILD=2 ; bugout "Auto-Upload target..." ;;
  58. v) DEBUG=1 ; bugout "Debug ON" ;;
  59. y) BUILD_YES='Y' ; bugout "Build will initiate..." ;;
  60. -) IFS="=" read -r ONAM OVAL <<< "$OPTARG"
  61. case "$ONAM" in
  62. help) [[ -z "$OVAL" ]] || perror "option can't take value $OVAL" $ONAM ; EXIT_USAGE=1 ;;
  63. autobuild) AUTO_BUILD=1 ; bugout "Auto-Build target..." ;;
  64. autoupload) AUTO_BUILD=2 ; bugout "Auto-Upload target..." ;;
  65. env) case "$OVAL" in
  66. '') perror "option requires a value" $ONAM ; EXIT_USAGE=2 ;;
  67. *) TESTENV="$OVAL" ; bugout "Got a target: $TESTENV" ;;
  68. esac
  69. ;;
  70. num) case "$OVAL" in
  71. [0-9]+) CHOICE="$OVAL" ; bugout "Got a number: $CHOICE" ;;
  72. *) perror "option requires a value" $ONAM ; EXIT_USAGE=2 ;;
  73. esac
  74. ;;
  75. rebuild) REBUILD=1 ; bugout "Rebuilding previous..." ;;
  76. make) USE_MAKE=1 ; bugout "Using make with Docker..." ;;
  77. debug|verbose) DEBUG=1 ; bugout "Debug ON" ;;
  78. build) case "$OVAL" in
  79. ''|y|yes) BUILD_YES='Y' ;;
  80. n|no) BUILD_YES='N' ;;
  81. *) perror "option value must be y, n, yes, or no" $ONAM ; EXIT_USAGE=2 ;;
  82. esac
  83. bugout "Build will initiate? ($BUILD_YES)"
  84. ;;
  85. *) perror "Unknown flag" "$OPTARG" ; EXIT_USAGE=2 ;;
  86. esac
  87. ;;
  88. *) EXIT_USAGE=2 ;;
  89. esac
  90. done
  91. ((EXIT_USAGE)) && { usage ; let EXIT_USAGE-- ; exit $EXIT_USAGE ; }
  92. if ((REBUILD)); then
  93. bugout "Rebuilding previous..."
  94. # Build with the last-built env
  95. [[ -f "$STATE_FILE" ]] || { errout "No previous (-r) build state found." ; exit 1 ; }
  96. read TESTENV <"$STATE_FILE"
  97. pio run $SILENT_FLAG -d . -e $TESTENV
  98. exit 0
  99. fi
  100. case $TESTENV in
  101. tree) pio run -d . -e include_tree ; exit 1 ;;
  102. due) TESTENV='DUE' ;;
  103. esp) TESTENV='esp32' ;;
  104. lin*) TESTENV='linux_native' ;;
  105. lp8|lpc8) TESTENV='LPC1768' ;;
  106. lp9|lpc9) TESTENV='LPC1769' ;;
  107. m128) TESTENV='mega1280' ;;
  108. m256) TESTENV='mega2560' ;;
  109. mega) TESTENV='mega2560' ;;
  110. stm) TESTENV='STM32F103RE' ;;
  111. f1) TESTENV='STM32F103RE' ;;
  112. f4) TESTENV='STM32F4' ;;
  113. f7) TESTENV='STM32F7' ;;
  114. s6) TESTENV='FYSETC_S6' ;;
  115. teensy) TESTENV='teensy31' ;;
  116. t31) TESTENV='teensy31' ;;
  117. t32) TESTENV='teensy31' ;;
  118. t35) TESTENV='teensy35' ;;
  119. t36) TESTENV='teensy35' ;;
  120. t40) TESTENV='teensy41' ;;
  121. t41) TESTENV='teensy41' ;;
  122. [1-9][1-9]|[1-9]) TESTNUM=$TESTENV ; TESTENV=- ;;
  123. esac
  124. if ((AUTO_BUILD)); then
  125. #
  126. # List environments that apply to the current MOTHERBOARD.
  127. #
  128. case $(uname | tr '[:upper:]' '[:lower:]') in
  129. darwin) SYS='mac' ;;
  130. *linux) SYS='lin' ;;
  131. win*) SYS='win' ;;
  132. msys*) SYS='win' ;;
  133. cygwin*) SYS='win' ;;
  134. mingw*) SYS='win' ;;
  135. *) SYS='uni' ;;
  136. esac
  137. echo ; echo -n "Auto " ; ((AUTO_BUILD == 2)) && echo "Upload..." || echo "Build..."
  138. MB=$( grep -E "^\s*#define MOTHERBOARD" Marlin/Configuration.h | awk '{ print $3 }' | $SED 's/BOARD_//' )
  139. [[ -z $MB ]] && { echo "Error - Can't read MOTHERBOARD setting." ; exit 1 ; }
  140. BLINE=$( grep -E "define\s+BOARD_$MB\b" Marlin/src/core/boards.h )
  141. BNUM=$( $SED -E 's/^.+BOARD_[^ ]+ +([0-9]+).+$/\1/' <<<"$BLINE" )
  142. BDESC=$( $SED -E 's/^.+\/\/ *(.+)$/\1/' <<<"$BLINE" )
  143. [[ -z $BNUM ]] && { echo "Error - Can't find $MB in boards list." ; exit 1 ; }
  144. ENVS=( $( grep -EA1 "MB\(.*\b$MB\b.*\)" Marlin/src/pins/pins.h | grep -E "#include.+//.+(env|$SYS):[^ ]+" | grep -oE "(env|$SYS):[^ ]+" | $SED -E "s/(env|$SYS)://" ) )
  145. [[ -z $ENVS ]] && { errout "Error - Can't find target(s) for $MB ($BNUM)." ; exit 1 ; }
  146. ECOUNT=${#ENVS[*]}
  147. if [[ $ECOUNT == 1 ]]; then
  148. TARGET=$ENVS
  149. else
  150. if [[ $CHOICE == 0 ]]; then
  151. # List env names and numbers. Get selection.
  152. echo "Available targets for \"$BDESC\" | $MB ($BNUM):"
  153. IND=0 ; for ENV in "${ENVS[@]}"; do let IND++ ; echo " $IND) $ENV" ; done
  154. if [[ $ECOUNT > 1 ]]; then
  155. for (( ; ; ))
  156. do
  157. read -p "Select a target for '$MB' (1-$ECOUNT) : " CHOICE
  158. [[ -z "$CHOICE" ]] && { echo '(canceled)' ; exit 1 ; }
  159. [[ $CHOICE =~ $ISNUM ]] && ((CHOICE >= 1 && CHOICE <= ECOUNT)) && break
  160. errout ">>> Invalid environment choice '$CHOICE'."
  161. done
  162. echo
  163. fi
  164. else
  165. echo "Detected \"$BDESC\" | $MB ($BNUM)."
  166. [[ $CHOICE > $ECOUNT ]] && { echo "Environment selection out of range." ; exit 1 ; }
  167. fi
  168. TARGET="${ENVS[$CHOICE-1]}"
  169. echo "Selected $TARGET"
  170. fi
  171. echo "$TARGET" >"$STATE_FILE"
  172. if ((AUTO_BUILD == 2)); then
  173. echo "Uploading environment $TARGET for board $MB ($BNUM)..." ; echo
  174. pio run $SILENT_FLAG -t upload -e $TARGET
  175. else
  176. echo "Building environment $TARGET for board $MB ($BNUM)..." ; echo
  177. pio run $SILENT_FLAG -e $TARGET
  178. fi
  179. exit 0
  180. fi
  181. #
  182. # List available tests and ask for selection
  183. #
  184. if [[ $TESTENV == '-' ]]; then
  185. IND=0
  186. NAMES=()
  187. for FILE in $( ls -1 $TESTPATH/* )
  188. do
  189. let IND++
  190. TNAME=${FILE/$TESTPATH\//}
  191. NAMES+=($TNAME)
  192. (( IND < 10 )) && echo -n " "
  193. echo " $IND) $TNAME"
  194. done
  195. echo
  196. for (( ; ; ))
  197. do
  198. if [[ $TESTNUM -gt 0 ]]; then
  199. NAMEIND=$TESTNUM
  200. else
  201. read -p "Select a test to apply (1-$IND) : " NAMEIND
  202. fi
  203. [[ -z $NAMEIND ]] && { errout "(canceled)" ; exit 1 ; }
  204. TESTENV=${NAMES[$NAMEIND-1]}
  205. [[ $TESTNUM -gt 0 ]] && { echo "Preselected test $TESTNUM ... ($TESTENV)" ; TESTNUM='' ; }
  206. [[ $NAMEIND =~ $ISNUM ]] && ((NAMEIND >= 1 && NAMEIND <= IND)) && { TESTENV=${NAMES[$NAMEIND-1]} ; echo ; break ; }
  207. errout "Invalid selection."
  208. done
  209. fi
  210. # Get the contents of the test file
  211. OUT=$( cat $TESTPATH/$TESTENV 2>/dev/null ) || { errout "Can't find test '$TESTENV'." ; exit 1 ; }
  212. # Count up the number of tests
  213. TESTCOUNT=$( awk "/$ISEXEC/{a++}END{print a}" <<<"$OUT" )
  214. # User entered a number?
  215. (( CHOICE && CHOICE > TESTCOUNT )) && { errout "Invalid test selection '$CHOICE' (1-$TESTCOUNT)." ; exit 1 ; }
  216. if [[ $CHOICE == 0 ]]; then
  217. #
  218. # List test descriptions with numbers and get selection
  219. #
  220. echo "Available '$TESTENV' tests:" ; echo "$OUT" | {
  221. IND=0
  222. while IFS= read -r LINE
  223. do
  224. if [[ $LINE =~ $ISEXEC ]]; then
  225. DESC=$( "$SED" -E 's/^exec_test \$1 \$2 "([^"]+)".*$/\1/g' <<<"$LINE" )
  226. (( ++IND < 10 )) && echo -n " "
  227. echo " $IND) $DESC"
  228. fi
  229. done
  230. }
  231. CHOICE=1
  232. if [[ $TESTCOUNT > 1 ]]; then
  233. for (( ; ; ))
  234. do
  235. read -p "Select a '$TESTENV' test (1-$TESTCOUNT) : " CHOICE
  236. [[ -z "$CHOICE" ]] && { errout "(canceled)" ; exit 1 ; }
  237. [[ $CHOICE =~ $ISNUM ]] && ((CHOICE >= 1 && CHOICE <= TESTCOUNT)) && break
  238. errout ">>> Invalid test selection '$CHOICE'."
  239. done
  240. fi
  241. fi
  242. #
  243. # Run the specified test lines
  244. #
  245. echo -ne "\033[0;33m"
  246. echo "$OUT" | {
  247. IND=0
  248. GOTX=0
  249. CMD=""
  250. while IFS= read -r LINE
  251. do
  252. if [[ $LINE =~ $ISCMD || $GOTX == 1 ]]; then
  253. ((!IND)) && let IND++
  254. if [[ $LINE =~ $ISEXEC ]]; then
  255. ((IND++ > CHOICE)) && break
  256. else
  257. ((!HEADER)) && {
  258. HEADER=1
  259. echo -e "\n#\n# Test $TESTENV ($CHOICE) $DESC\n#"
  260. }
  261. ((IND == CHOICE)) && {
  262. GOTX=1
  263. [[ $CMD == "" ]] && CMD="$LINE" || CMD=$( echo -e "$CMD$LINE" | $SED -e 's/\\//g' | $SED -E 's/ +/ /g' )
  264. [[ $LINE =~ $ISCONT ]] || { echo "$CMD" ; eval "$CMD" ; CMD="" ; }
  265. }
  266. fi
  267. fi
  268. done
  269. }
  270. echo -ne "\033[0m"
  271. # Make clear it's a TEST
  272. opt_set CUSTOM_MACHINE_NAME "\"Test $TESTENV ($CHOICE)\""
  273. # Build the test too?
  274. if [[ -z "$BUILD_YES" ]]; then
  275. echo
  276. read -p "Build $TESTENV test #$CHOICE (y/N) ? " BUILD_YES
  277. fi
  278. [[ $BUILD_YES == 'Y' || $BUILD_YES == 'Yes' ]] && {
  279. ((USE_MAKE)) && make tests-single-local TEST_TARGET=$TESTENV ONLY_TEST=$CHOICE
  280. ((USE_MAKE)) || pio run $SILENT_FLAG -d . -e $TESTENV
  281. echo "$TESTENV" >"$STATE_FILE"
  282. }