feat: support flashing of multiple connected mcus

This commit is contained in:
th33xitus
2021-01-08 13:56:36 +01:00
parent 65ec2e5e1a
commit 88afa55268
2 changed files with 112 additions and 131 deletions

View File

@@ -1,11 +1,3 @@
#install_klipper(){
# get_user_selections_klipper
# klipper_setup
# build_fw
# flash_mcu
# write_printer_usb
#}
### base variables ### base variables
SYSTEMDDIR="/etc/systemd/system" SYSTEMDDIR="/etc/systemd/system"
KLIPPY_ENV="${HOME}/klippy-env" KLIPPY_ENV="${HOME}/klippy-env"
@@ -205,77 +197,108 @@ create_multi_klipper_instance(){
} }
flash_routine(){ flash_routine(){
if [ "$FLASH_FIRMWARE" = "true" ]; then echo
echo top_border
echo -e "| ${red}~~~~~~~~~~~ [ ATTENTION! ] ~~~~~~~~~~~~${default} |"
hr
echo -e "| Flashing a Smoothie based board with this script will |"
echo -e "| certainly fail. This applies to boards like the BTT |"
echo -e "| SKR V1.3 or SKR V1.4(Turbo). You have to copy the |"
echo -e "| firmware file to the microSD card manually and rename |"
echo -e "| it to 'firmware.bin'. |"
hr
echo -e "| You can find the file in: ~/klipper/out/klipper.bin |"
bottom_border
while true; do
read -p "${cyan}###### Do you want to continue? (Y/n):${default} " yn
case "$yn" in
Y|y|Yes|yes|"")
echo -e "###### > Yes"
get_mcu_id
break;;
N|n|No|no)
echo -e "###### > No"
break;;
*)
print_unkown_cmd
print_msg && clear_msg;;
esac
done
}
flash_mcu(){
if [ ${#mcu_list[@]} -ge 1 ]; then
top_border top_border
echo -e "| ${red}~~~~~~~~~~~ [ ATTENTION! ] ~~~~~~~~~~~~${default} |" echo -e "| ${red}!!! IMPORTANT WARNING !!!${default} |"
hr hr
echo -e "| Flashing a Smoothie based board with this script will |" echo -e "| Make sure, that you select the correct ID for the MCU |"
echo -e "| certainly fail. This applies to boards like the BTT |" echo -e "| you have build the firmware for in the previous step! |"
echo -e "| SKR V1.3 or SKR V1.4(Turbo). You have to copy the |" blank_line
echo -e "| firmware file to the microSD card manually and rename |" echo -e "| This is especially important if you use different MCU |"
echo -e "| it to 'firmware.bin'. |" echo -e "| models which each require their own firmware! |"
hr blank_line
echo -e "| You can find the file in: ~/klipper/out/klipper.bin |" echo -e "| ${red}ONLY flash a firmware created for the respective MCU!${default} |"
bottom_border bottom_border
### list all mcus
i=1
for mcu in ${mcu_list[@]}; do
### rewrite mcu to cut off everything except the important stuff
mcu=$(echo $mcu | rev | cut -d"/" -f1 | rev)
echo -e "$i) ${cyan}$mcu${default}"
i=$(expr $i + 1)
done
while true; do while true; do
read -p "${cyan}###### Do you want to continue? (Y/n):${default} " yn echo
case "$yn" in read -p "${cyan}###### Please select the ID for flashing:${default} " selected_id
Y|y|Yes|yes|"") mcu_index=$(echo $((selected_id - 1)))
echo -e "###### > Yes" echo -e "\nYou have selected to flash:\n● MCU #$selected_id: ${mcu_list[$mcu_index]}\n"
CONFIRM_FLASHING="true" while true; do
CONFIRM_WRITE_PRINTER_USB="true" read -p "${cyan}###### Do you want to continue? (Y/n):${default} " yn
get_printer_usb case "$yn" in
break;; Y|y|Yes|yes|"")
N|n|No|no) echo -e "###### > Yes"
echo -e "###### > No" status_msg "Flashing ${mcu_list[$mcu_index]} ..."
CONFIRM_FLASHING="false" klipper_service "stop"
CONFIRM_WRITE_PRINTER_USB="false" if ! make flash FLASH_DEVICE="${mcu_list[$mcu_index]}" ; then
break;; warn_msg "Flashing failed!"
*) warn_msg "Please read the console output above!"
print_unkown_cmd else
print_msg && clear_msg;; ok_msg "Flashing successfull!"
esac fi
klipper_service "start"
break;;
N|n|No|no)
echo -e "###### > No"
break;;
*)
print_unkown_cmd
print_msg && clear_msg;;
esac
done
break
done done
fi fi
} }
flash_mcu(){
if [ "$CONFIRM_FLASHING" = "true" ] && [ ! -z "$PRINTER_USB" ]; then
klipper_service "stop"
if ! make flash FLASH_DEVICE="$PRINTER_USB" ; then
warn_msg "Flashing failed!"
warn_msg "Please read the console output above!"
else
ok_msg "Flashing successfull!"
fi
klipper_service "start"
fi
}
build_fw(){ build_fw(){
if [ "$BUILD_FIRMWARE" = "true" ]; then if [ -d $KLIPPER_DIR ]; then
if [ -d $KLIPPER_DIR ]; then cd $KLIPPER_DIR
cd $KLIPPER_DIR status_msg "Initializing firmware Setup ..."
status_msg "Initializing Firmware Setup ..." make menuconfig
make menuconfig status_msg "Building firmware ..."
status_msg "Building Firmware ..." make clean && make && ok_msg "Firmware built!"
make clean && make && ok_msg "Firmware built!" else
else warn_msg "Can not build firmware without a Klipper directory!"
warn_msg "Can not build Firmware without a Klipper directory!"
fi
fi fi
} }
### grab the printers id ### grab the mcu id
get_printer_usb(){ get_mcu_id(){
echo echo
top_border top_border
echo -e "| Please make sure your printer is connected to the Pi! |" echo -e "| Please make sure your MCU is connected to the Pi! |"
echo -e "| If the printer is not connected yet, connect it now. |" echo -e "| If the MCU is not connected yet, connect it now. |"
hr
echo -e "| Also make sure, that it is the only USB device |"
echo -e "| connected at for now! Otherwise this step may fail! |"
bottom_border bottom_border
while true; do while true; do
echo -e "${cyan}" echo -e "${cyan}"
@@ -283,70 +306,30 @@ get_printer_usb(){
echo -e "${default}" echo -e "${default}"
case "$yn" in case "$yn" in
*) *)
CONFIRM_PRINTER_USB="true"
break;; break;;
esac esac
done done
status_msg "Identifying the correct USB port ..." status_msg "Identifying the ID of your MCU ..."
sleep 2 sleep 2
unset PRINTER_USB unset MCU_ID
if [ -e /dev/serial/by-id/* ]; then ### if there are devices found, continue, else show warn message
if [ -e /dev/serial/by-id/* ]; then if ls /dev/serial/by-id/* 2>/dev/null 1>&2; then
PRINTER_USB=$(ls /dev/serial/by-id/*) mcu_count=1
status_msg "The ID of your printer is:" mcu_list=()
title_msg "$PRINTER_USB" status_msg "The ID of your printers MCU is:"
else ### loop over the IDs, write every ID as an item of the array 'mcu_list'
warn_msg "Could not retrieve ID!" for mcu in /dev/serial/by-id/*; do
fi declare "mcu_id_$mcu_count"="$mcu"
elif [ -e /dev/serial/by-path/* ]; then mcu_id="mcu_id_$mcu_count"
if [ -e /dev/serial/by-path/* ]; then mcu_list+=("${!mcu_id}")
PRINTER_USB=$(ls /dev/serial/by-path/*) ### rewrite mcu to cut off everything except the important stuff
status_msg "The path of your printer is:" mcu=$(echo $mcu | rev | cut -d"/" -f1 | rev)
title_msg "$PRINTER_USB" echo " ● MCU #$mcu_count: ${cyan}$mcu${default}"
else mcu_count=$(expr $mcu_count + 1)
warn_msg "Could not retrieve path!" done
fi unset mcu_count
else else
warn_msg "Could not retrieve ID!"
warn_msg "Printer not plugged in or not detectable!" warn_msg "Printer not plugged in or not detectable!"
fi
}
write_printer_usb(){
locate_printer_cfg
if [ ! -z "$PRINTER_CFG" ] && [ "$CONFIRM_WRITE_PRINTER_USB" = "true" ]; then
SERIAL_OLD=$(grep "serial" $PRINTER_CFG | tail -1 | cut -d" " -f2)
SERIAL_NEW=$PRINTER_USB
if [ "$SERIAL_OLD" != "$SERIAL_NEW" ]; then
unset write_entries
backup_printer_cfg
write_entries+=("[mcu]\nserial: $SERIAL_NEW")
write_entries+=("\\\n############################\n##### CREATED BY KIAUH #####\n############################")
write_entries=("############################\n" "${write_entries[@]}")
#check for a SAVE_CONFIG entry
SC="#*# <---------------------- SAVE_CONFIG ---------------------->"
if [[ $(grep "$SC" $PRINTER_CFG) ]]; then
SC_LINE=$(grep -n "$SC" $PRINTER_CFG | cut -d ":" -f1)
PRE_SC_LINE=$(expr $SC_LINE - 1)
SC_ENTRY="true"
else
SC_ENTRY="false"
fi
status_msg "Writing printer ID/path to printer.cfg ..."
if [ "$SC_ENTRY" = "true" ]; then
PRE_SC_LINE="$(expr $SC_LINE - 1)a"
for entry in "${write_entries[@]}"
do
sed -i "$PRE_SC_LINE $entry" $PRINTER_CFG
done
fi
if [ "$SC_ENTRY" = "false" ]; then
LINE_COUNT="$(wc -l < $PRINTER_CFG)a"
for entry in "${write_entries[@]}"
do
sed -i "$LINE_COUNT $entry" $PRINTER_CFG
done
fi
ok_msg "Done!"
fi
fi fi
} }

View File

@@ -12,7 +12,7 @@ advanced_ui(){
echo -e "| Firmware: | 7) [Shell Command] | " echo -e "| Firmware: | 7) [Shell Command] | "
echo -e "| 3) [Build only] | | " echo -e "| 3) [Build only] | | "
echo -e "| 4) [Build + Flash MCU] | | " echo -e "| 4) [Build + Flash MCU] | | "
echo -e "| 5) [Get Printer-USB] | | " echo -e "| 5) [Get MCU ID] | | "
echo -e "| | | " echo -e "| | | "
quit_footer quit_footer
} }
@@ -47,25 +47,23 @@ advanced_menu(){
3) 3)
clear clear
print_header print_header
unset BUILD_FIRMWARE && BUILD_FIRMWARE="true"
build_fw build_fw
print_msg && clear_msg print_msg && clear_msg
advanced_ui;; advanced_ui;;
4) 4)
clear clear
print_header print_header
unset FLASH_FIRMWARE && FLASH_FIRMWARE="true"
flash_routine flash_routine
unset BUILD_FIRMWARE && BUILD_FIRMWARE="true" ### build in a sleep to give the user a chance to have a look at the
build_fw ### MCU IDs before directly starting to build the klipper firmware
unset CONFIRM_FLASHING && CONFIRM_FLASHING="true" status_msg "Please wait..." && sleep 10 && build_fw
flash_mcu flash_mcu
print_msg && clear_msg print_msg && clear_msg
advanced_ui;; advanced_ui;;
5) 5)
clear clear
print_header print_header
get_printer_usb get_mcu_id
print_msg && clear_msg print_msg && clear_msg
advanced_ui;; advanced_ui;;
6) 6)