My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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