MK404-build.sh 16 KB

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