MK404-build.sh 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. #!/bin/bash
  2. # This bash script is used to compile automatically and run the MK404 simulator
  3. #
  4. # Supported OS: Linux64 bit
  5. #
  6. # Linux:
  7. # Linux Ubuntu
  8. # 1. Follow these instructions
  9. # 2. Open Ubuntu bash and get latest updates with 'sudo apt-get update'
  10. # 3. Install latest updates with 'sudo apt-get upgrade'
  11. #
  12. #
  13. # Version: 1.0.0-Build_9
  14. # Change log:
  15. # 11 Feb 2021, 3d-gussner, Inital
  16. # 11 Feb 2021, 3d-gussner, Optional flags to check for updates
  17. # 12 Feb 2021, 3d-gussner, Update cmake
  18. # 13 Feb 2021, 3d-gussner, Auto build SD cards
  19. # 18 Jun 2021, 3d-gussner, Documentation and version number
  20. # 18 Jun 2021, 3d-gussner, Added some arguments and checks
  21. # 18 Jun 2021, 3d-gussner, Default extrusion graphics to line. Thanks @vintagepc point it out
  22. # 18 Jun 2021, 3d-gussner, Added -g 3 and 4 for more details extrusion lines
  23. # 18 Jun 2021, 3d-gussner, Check for updates is default. Fix update if internet connection is lost.
  24. # 21 Jun 2021, 3d-gussner, Change board_flash argument to 'y' and firmware_version to 'f'
  25. while getopts c:f:g:m:n:p:u:x:y:?h flag
  26. do
  27. case "${flag}" in
  28. c) check_flag=${OPTARG};;
  29. f) firmware_version_flag=${OPTARG};;
  30. g) graphics_flag=${OPTARG};;
  31. h) help_flag=1;;
  32. m) mk404_flag=${OPTARG};;
  33. n) new_build_flag=${OPTARG};;
  34. p) mk404_printer_flag=${OPTARG};;
  35. u) update_flag=${OPTARG};;
  36. x) board_mem_flag=${OPTARG};;
  37. y) board_flash_flag=${OPTARG};;
  38. ?) help_flag=1;;
  39. esac
  40. done
  41. #Debug echos
  42. #echo "c: $check_flag"
  43. #echo "f: $firmware_version_flag"
  44. #echo "g: $graphics_flag"
  45. #echo "m: $mk404_flag"
  46. #echo "n: $new_build_flag"
  47. #echo "p: $mk404_printer_flag"
  48. #echo "u: $update_flag"
  49. #echo "x: $board_mem_flag"
  50. #echo "y: $board_flash_flag"
  51. # '?' 'h' argument usage and help
  52. if [ "$help_flag" == "1" ] ; then
  53. echo "***************************************"
  54. echo "* MK404-build.sh Version: 1.0.0-Build_9 *"
  55. echo "***************************************"
  56. echo "Arguments:"
  57. echo "$(tput setaf 2)-c$(tput sgr0) Check for update"
  58. echo "$(tput setaf 2)-f$(tput sgr0) Prusa-Firmware version"
  59. echo "$(tput setaf 2)-g$(tput sgr0) Start MK404 graphics"
  60. echo "$(tput setaf 2)-h$(tput sgr0) Help"
  61. echo "$(tput setaf 2)-m$(tput sgr0) Start MK404 sim"
  62. echo "$(tput setaf 2)-n$(tput sgr0) Force new build"
  63. echo "$(tput setaf 2)-p$(tput sgr0) MK404 Printer"
  64. echo "$(tput setaf 2)-u$(tput sgr0) Update MK404"
  65. echo "$(tput setaf 2)-x$(tput sgr0) Board memory size"
  66. echo "$(tput setaf 2)-y$(tput sgr0) Board flash size"
  67. echo "$(tput setaf 2)-?$(tput sgr0) Help"
  68. echo
  69. echo "Brief USAGE:"
  70. echo " $(tput setaf 2)./MK404-build.sh$(tput sgr0) [-c] [-f] [-g] [-m] [-n] [-p] [-u] [-v] [-x] [-h] [-?]"
  71. echo
  72. echo " -c : '$(tput setaf 2)0$(tput sgr0)' no, '$(tput setaf 2)1$(tput sgr0)' yes"
  73. echo " -f : '$(tput setaf 2)path+file name$(tput sgr0)'"
  74. echo " -g : '$(tput setaf 2)0$(tput sgr0)' no, '$(tput setaf 2)1$(tput sgr0)' lite, '$(tput setaf 2)2$(tput sgr0)' fancy, '$(tput setaf 2)3$(tput sgr0)' lite with Quad_HR, '$(tput setaf 2)4$(tput sgr0)' fancy with Quad_HR"
  75. echo " -m : '$(tput setaf 2)0$(tput sgr0)' no, '$(tput setaf 2)1$(tput sgr0)' yes '$(tput setaf 2)2$(tput sgr0)' with MMU2"
  76. echo " -n : '$(tput setaf 2)0$(tput sgr0)' no, '$(tput setaf 2)1$(tput sgr0)' yes"
  77. echo " -p : '$(tput setaf 2)MK25$(tput sgr0)', '$(tput setaf 2)MK25S$(tput sgr0)', '$(tput setaf 2)MK3$(tput sgr0)' or '$(tput setaf 2)MK3S$(tput sgr0)'"
  78. echo " -u : '$(tput setaf 2)0$(tput sgr0)' no, '$(tput setaf 2)1$(tput sgr0)' yes '"
  79. echo " -x : '$(tput setaf 2)8$(tput sgr0)' or '$(tput setaf 2)64$(tput sgr0)' Kb."
  80. echo " -y : '$(tput setaf 2)256$(tput sgr0)','$(tput setaf 2)384$(tput sgr0)','$(tput setaf 2)512$(tput sgr0)','$(tput setaf 2)1024$(tput sgr0)''$(tput setaf 2)32M$(tput sgr0)'"
  81. echo
  82. echo "Example:"
  83. echo " $(tput setaf 2)./MK404-build.sh -f 1$(tput sgr0)"
  84. echo " Will force an update and rebuild the MK404 SIM"
  85. echo
  86. echo " $(tput setaf 2)./MK404-build.sh -m 1 -g 1 -v ../../../../Prusa-Firmware/PF-build-hex/FW3100-Build4481/BOAD_EINSY_1_0a/FW3100-Build4481-1_75mm_MK3S-EINSy10a-E3Dv6full.hex$(tput sgr0)"
  87. echo " Will start MK404 with Prusa_MK3S and Prusa-Firmware 3.10.0-Build4481"
  88. exit 1
  89. fi
  90. #### Start check if OSTYPE is supported
  91. OS_FOUND=$( command -v uname)
  92. case $( "${OS_FOUND}" | tr '[:upper:]' '[:lower:]') in
  93. linux*)
  94. TARGET_OS="linux"
  95. ;;
  96. *)
  97. TARGET_OS='unknown'
  98. ;;
  99. esac
  100. # Linux
  101. if [ $TARGET_OS == "linux" ]; then
  102. if [ $(uname -m) == "x86_64" ]; then
  103. echo "$(tput setaf 2)Linux 64-bit found$(tput sgr0)"
  104. Processor="64"
  105. #elif [[ $(uname -m) == "i386" || $(uname -m) == "i686" ]]; then
  106. # echo "$(tput setaf 2)Linux 32-bit found$(tput sgr0)"
  107. # Processor="32"
  108. else
  109. echo "$(tput setaf 1)Unsupported OS: Linux $(uname -m)"
  110. echo "Please refer to the notes of build.sh$(tput sgr0)"
  111. exit 2
  112. fi
  113. else
  114. echo "$(tput setaf 1)This script doesn't support your Operating system!"
  115. echo "Please use Linux 64-bit"
  116. echo "Read the notes of build.sh$(tput sgr0)"
  117. exit 2
  118. fi
  119. sleep 2
  120. #### End check if OSTYPE is supported
  121. #### Check MK404 dependencies
  122. packages=(
  123. "libelf-dev"
  124. "gcc-7"
  125. "gcc-avr"
  126. "libglew-dev"
  127. "freeglut3-dev"
  128. "libsdl-sound1.2-dev"
  129. "libpng-dev"
  130. "cmake"
  131. "zip"
  132. "wget"
  133. "git"
  134. "build-essential"
  135. "lcov"
  136. "mtools"
  137. )
  138. for check_package in ${packages[@]}; do
  139. if dpkg-query -W -f'${db:Status-Abbrev}\n' $check_package 2>/dev/null \
  140. | grep -q '^.i $'; then
  141. echo "$(tput setaf 2)$check_package: Installed$(tput sgr0)"
  142. else
  143. echo "$(tput setaf 1)$check_package: Not installed use $(tput setaf 3)'sudo apt install $check_package'$(tput setaf 1) to install missing package$(tput sgr0)"
  144. not_installed=1;
  145. fi
  146. done
  147. if [ "$not_installed" = "1" ]; then
  148. exit 3
  149. fi
  150. #### End Check MK404 dependencies
  151. #### Set build environment
  152. MK404_SCRIPT_PATH="$( cd "$(dirname "$0")" ; pwd -P )"
  153. MK404_URL="https://github.com/vintagepc/MK404.git"
  154. MK404_owner="vintagepc"
  155. MK404_project="MK404"
  156. MK404_PATH="$MK404_SCRIPT_PATH/../MK404/master"
  157. MK404_BUILD_PATH="$MK404_PATH/build"
  158. # List few useful data
  159. echo
  160. echo "Script path :" $MK404_SCRIPT_PATH
  161. echo "OS :" $TARGET_OS
  162. echo ""
  163. echo "MK404 path :" $MK404_PATH
  164. # Clone MK404 if needed
  165. if [ ! -d $MK404_PATH ]; then
  166. #release_url=$(curl -Ls -o /dev/null -w %{url_effective} https://github.com/$MK404_owner/$MK404_project/releases/latest)
  167. #release_tag=$(basename $release_url)
  168. #git clone -b $release_tag -- https://github.com/$MK404_owner/$MK404_project.git $MK404_PATH
  169. git clone $MK404_URL $MK404_PATH
  170. fi
  171. #
  172. cd $MK404_PATH
  173. #Check MK404 agruments
  174. #Set Check for updates as default
  175. check_flag=1
  176. #Check mk404_printer_flag
  177. if [ ! -z $mk404_printer_flag ]; then
  178. if [[ "$mk404_printer_flag" == "MK3" || "$mk404_printer_flag" == "MK3S" || "$mk404_printer_flag" == "MK25" || "$mk404_printer_flag" == "MK25S" ]]; then
  179. MK404_PRINTER_TEMP=$mk404_printer_flag
  180. else
  181. echo "Following Printers are supported: MK25, MK25S, MK3 and MK3S"
  182. exit 4
  183. fi
  184. fi
  185. #Check if Build is selected with argument '-f'
  186. if [ ! -z "$board_flash_flag" ] ; then
  187. if [ "$board_flash_flag" == "256" ] ; then
  188. BOARD_FLASH="0x3FFFF"
  189. echo "Board flash size : $board_flash_flag Kb, $BOARD_FLASH (hex)"
  190. elif [ "$board_flash_flag" == "384" ] ; then
  191. BOARD_FLASH="0x5FFFF"
  192. echo "Board flash size : $board_flash_flag Kb, $BOARD_FLASH (hex)"
  193. elif [ "$board_flash_flag" == "512" ] ; then
  194. BOARD_FLASH="0x7FFFF"
  195. echo "Board flash size : $board_flash_flag Kb, $BOARD_FLASH (hex)"
  196. elif [ "$board_flash_flag" == "1024" ] ; then
  197. BOARD_FLASH="0xFFFFF"
  198. echo "Board flash size : $board_flash_flag Kb, $BOARD_FLASH (hex)"
  199. elif [[ "$board_flash_flag" == "32M" || "$board_flash_flag" == "32768" ]] ; then
  200. BOARD_FLASH="0x1FFFFFF"
  201. echo "Board flash size : 32 Mb, $BOARD_FLASH (hex)"
  202. else
  203. echo "Unsupported board flash size chosen. Only '256', '384', '512', '1024' and '32M' are allowed."
  204. exit 5
  205. fi
  206. fi
  207. #Check if Build is selected with argument '-x'
  208. if [ ! -z "$board_mem_flag" ] ; then
  209. if [ "$board_mem_flag" == "8" ] ; then
  210. BOARD_MEM="0x21FF"
  211. echo "Board mem size : $board_mem_flag Kb, $BOARD_MEM (hex)"
  212. elif [ "$board_mem_flag" == "64" ] ; then
  213. BOARD_MEM="0xFFFF"
  214. echo "Board mem size : $board_mem_flag Kb, $BOARD_MEM (hex)"
  215. else
  216. echo "Unsupported board mem size chosen. Only '8', '64' are allowed."
  217. exit 6
  218. fi
  219. fi
  220. if [ "$new_build_flag" == "1" ]; then
  221. check_flag=1
  222. update_flag=1
  223. fi
  224. if [ "$update_flag" == "1" ]; then
  225. check_flag=1
  226. fi
  227. #End Check MK404 agruments
  228. #Check for updates
  229. if [ "$check_flag" == "1" ]; then
  230. if [ -d $MK404_BUILD_PATH ]; then
  231. cd $MK404_BUILD_PATH
  232. MK404_current_version=$( command ./MK404 --version | grep "MK404" | cut -f 4 -d " ")
  233. cd $MK404_PATH
  234. else
  235. echo "Cannot check current version as it has not been build."
  236. fi
  237. # Get local Commit_Hash
  238. MK404_local_GIT_COMMIT_HASH=$(git log --pretty=format:"%H" -1)
  239. # Get local Commit_Number
  240. MK404_local_GIT_COMMIT_NUMBER=$(git rev-list HEAD --count)
  241. # Get remote Commit_Hash
  242. MK404_remote_GIT_COMMIT_HASH=$(git ls-remote --heads $(git config --get remote.origin.url) | grep "refs/heads/master" | cut -f 1)
  243. # Get remote Commit_Number
  244. MK404_remote_GIT_COMMIT_NUMBER=$(git rev-list origin/master --count)
  245. # Output
  246. echo ""
  247. echo "Current version : $MK404_current_version"
  248. echo ""
  249. echo "Current local hash : $MK404_local_GIT_COMMIT_HASH"
  250. echo "Current local commit nr : $MK404_local_GIT_COMMIT_NUMBER"
  251. if [ "$MK404_local_GIT_COMMIT_HASH" != "$MK404_remote_GIT_COMMIT_HASH" ]; then
  252. echo "$(tput setaf 1)"
  253. else
  254. echo "$(tput setaf 2)"
  255. fi
  256. echo "Current remote hash : $MK404_remote_GIT_COMMIT_HASH"
  257. echo "Current remote commit nr: $MK404_remote_GIT_COMMIT_NUMBER"
  258. echo "$(tput sgr 0)"
  259. # Check for updates
  260. if [ ! -z $MK404_remote_GIT_COMMIT_HASH ]; then
  261. if [[ "$MK404_local_GIT_COMMIT_HASH" != "$MK404_remote_GIT_COMMIT_HASH" && -z "$update_flag" ]]; then
  262. echo "$(tput setaf 2)Update is availible.$(tput sgr 0)"
  263. read -t 10 -n 1 -p "$(tput setaf 3)Update now Y/n$(tput sgr 0)" update_answer
  264. if [ "$update_answer" == "Y" ]; then
  265. update_flag=1
  266. fi
  267. echo ""
  268. fi
  269. fi
  270. fi
  271. # Fetch updates and force new build
  272. if [ "$update_flag" == "1" ]; then
  273. if [ ! -z $MK404_remote_GIT_COMMIT_HASH ]; then
  274. if [ "$MK404_local_GIT_COMMIT_HASH" != "$MK404_remote_GIT_COMMIT_HASH" ]; then
  275. echo ""
  276. git fetch --all
  277. read -t 10 -p "$(tput setaf 2)Updating MK404 !$(tput sgr 0)"
  278. echo ""
  279. git reset --hard origin/master
  280. read -t 10 -p "$(tput setaf 2)Compiling MK404 !$(tput sgr 0)"
  281. echo ""
  282. new_build_flag=1
  283. fi
  284. fi
  285. fi
  286. # Prepare MK404
  287. mkdir -p $MK404_BUILD_PATH
  288. if [[ ! -f "$MK404_BUILD_PATH/Makefile" || "$new_build_flag" == "1" ]]; then
  289. # Init and update submodules
  290. git submodule init
  291. git submodule update
  292. cmake -B$MK404_BUILD_PATH -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE=Release -G "Unix Makefiles"
  293. fi
  294. # Make MK404
  295. cd $MK404_BUILD_PATH
  296. if [[ ! -f "$MK404_BUILD_PATH/MK404" || "$new_build_flag" == "1" ]]; then
  297. make
  298. fi
  299. # Make SDcards
  300. if [[ ! -f "$MK404_BUILD_PATH/Prusa_MK3S_SDcard.bin" || "$new_build_flag" == "1" ]]; then
  301. cmake --build $MK404_BUILD_PATH --config Release --target Prusa_MK3S_SDcard.bin
  302. cmake --build $MK404_BUILD_PATH --config Release --target Prusa_MK3_SDcard.bin
  303. cmake --build $MK404_BUILD_PATH --config Release --target Prusa_MK25_13_SDcard.bin
  304. cmake --build $MK404_BUILD_PATH --config Release --target Prusa_MK25S_13_SDcard.bin
  305. cmake --build $MK404_BUILD_PATH --config Release --target Prusa_MK3SMMU2_SDcard.bin
  306. cmake --build $MK404_BUILD_PATH --config Release --target Prusa_MK3MMU2_SDcard.bin
  307. fi
  308. # Prepare run MK404
  309. #Check MK404_Printer
  310. if [ ! -z $firmware_version_flag ]; then
  311. MK404_PRINTER_TEMP=$(echo $firmware_version_flag | sed 's/\(.*\)\///' | grep 'MK3')
  312. if [ ! -z $MK404_PRINTER_TEMP ]; then
  313. MK404_PRINTER=MK3
  314. fi
  315. MK404_PRINTER_TEMP=$(echo $firmware_version_flag | sed 's/\(.*\)\///' | grep 'MK3S')
  316. if [ ! -z $MK404_PRINTER_TEMP ]; then
  317. MK404_PRINTER=MK3S
  318. fi
  319. MK404_PRINTER_TEMP=$(echo $firmware_version_flag | sed 's/\(.*\)\///' | grep 'MK25')
  320. if [ ! -z $MK404_PRINTER_TEMP ]; then
  321. MK404_PRINTER=MK25
  322. fi
  323. MK404_PRINTER_TEMP=$(echo $firmware_version_flag | sed 's/\(.*\)\///' | grep 'MK25S')
  324. if [ ! -z $MK404_PRINTER_TEMP ]; then
  325. MK404_PRINTER=MK25S
  326. fi
  327. else
  328. echo "No firmware version file selected!"
  329. echo "Add argument -f with path and hex filename to start MK404"
  330. exit 7
  331. fi
  332. if [ -z "$MK404_PRINTER" ]; then
  333. echo "Tried to determine MK404 printer from hex file, but failed!"
  334. echo "Add argument -p with 'MK25', 'MK25S', 'MK3' or 'MK3S' to start MK404"
  335. exit 8
  336. fi
  337. if [ ! -z $mk404_printer_flag ]; then
  338. if [ "$mk404_printer_flag" != "$MK404_PRINTER" ]; then
  339. echo "$(tput setaf 3)You defined a different printer type than the firmware!"
  340. echo "This can cause unexpected issues.$(tput sgr 0)"
  341. echo
  342. PS3="Select $(tput setaf 2)printer$(tput sgr 0) you want to use."
  343. select which in "$(tput setaf 2)$MK404_PRINTER$(tput sgr 0)" "$mk404_printer_flag"; do
  344. case $which in
  345. $MK404_PRINTER)
  346. echo "Set $MK404_PRINTER as printer"
  347. break
  348. ;;
  349. $mk404_printer_flag)
  350. echo "Set $(tput setaf 3)$mk404_printer_flag$(tput sgr 0) as printer"
  351. echo "$(tput setaf 3)This firmware file isn't correct for this printer!!!$(tput sgr 0)"
  352. echo
  353. MK404_PRINTER=$mk404_printer_flag
  354. read -p "Press Enter to continue."
  355. break
  356. ;;
  357. *)
  358. break
  359. ;;
  360. esac
  361. done
  362. fi
  363. fi
  364. if [ -z $MK404_PRINTER ]; then
  365. exit 9
  366. fi
  367. if [[ "$MK404_PRINTER" == "MK25" || "$MK404_PRINTER" == "MK25S" ]]; then
  368. MK404_PRINTER="${MK404_PRINTER}_mR13"
  369. else
  370. if [[ "$mk404_flag" == "2" || "$mk404_flag" == "MMU2" || "$mk404_flag" == "MMU2S" ]]; then # Check if MMU2 is selected only for MK3/S
  371. MK404_PRINTER="${MK404_PRINTER}MMU2"
  372. fi
  373. fi
  374. # Run MK404 with 'debugcore' and/or 'bootloader-file'
  375. if [[ ! -z $MK404_DEBUG && "$MK404_DEBUG" == "atmega404" || ! -z $BOARD_MEM && "$BOARD_MEM" == "0xFFFF" ]]; then
  376. MK404_options="--debugcore"
  377. fi
  378. if [[ ! -z $MK404_DEBUG && "$MK404_DEBUG" == "atmega404_no_bootloader" || ! -z $BOARD_FLASH && "$BOARD_FLASH" != "0x3FFFF" ]]; then
  379. MK404_options='--debugcore --bootloader-file ""'
  380. fi
  381. # Run MK404 with grafics
  382. if [ ! -z "$graphics_flag" ]; then
  383. if [ ! -z "$MK404_options" ]; then
  384. MK404_options="${MK404_options} -g "
  385. else
  386. MK404_options=" -g "
  387. fi
  388. if [[ "$graphics_flag" == "1" || "$graphics_flag" == "lite" || "$graphics_flag" == "3" ]]; then
  389. MK404_options="${MK404_options}lite"
  390. elif [[ "$graphics_flag" == "2" || "$graphics_flag" == "fancy" || "$graphics_flag" == "4" ]]; then
  391. MK404_options="${MK404_options}fancy"
  392. else
  393. echo "$(tput setaf 1)Unsupported MK404 graphics option $graphics_flag$(tput sgr 0)"
  394. fi
  395. if [[ "$graphics_flag" == "3" || "$graphics_flag" == "4" ]]; then
  396. MK404_options="${MK404_options} --colour-extrusion --extrusion Quad_HR"
  397. else
  398. MK404_options="${MK404_options} --extrusion Line"
  399. fi
  400. fi
  401. if [ ! -z $firmware_version_flag ]; then
  402. MK404_firmware_file=" -f $firmware_version_flag"
  403. fi
  404. #Run MK404 SIM
  405. if [ ! -z $mk404_flag ]; then
  406. # Output some useful data
  407. echo "Printer : $MK404_PRINTER"
  408. echo "Options : $MK404_options"
  409. echo ""
  410. read -t 5 -p "Press $(tput setaf 2)Enter$(tput sgr 0) to start MK404"
  411. echo ""
  412. # Change to MK404 build folder
  413. cd $MK404_BUILD_PATH
  414. # Start MK404
  415. # default with serial output and terminal to manipulate it via terminal
  416. echo ""
  417. echo "./MK404 Prusa_$MK404_PRINTER -s --terminal $MK404_options $MK404_firmware_file"
  418. sleep 5
  419. ./MK404 Prusa_$MK404_PRINTER -s --terminal $MK404_options $MK404_firmware_file || exit 10
  420. fi
  421. #### End of MK404 Simulator