refactor: improve flash methods

Signed-off-by: Dominik Willner th33xitus@gmail.com
This commit is contained in:
th33xitus
2022-04-17 13:47:30 +02:00
parent 421dacf317
commit 5f78228eb0
2 changed files with 139 additions and 83 deletions

View File

@@ -48,7 +48,7 @@ function init_flash_process(){
show_flash_method_help show_flash_method_help
break;; break;;
*) *)
print_error "Invalid command!";; error_msg "Invalid command!";;
esac esac
done done
@@ -62,43 +62,56 @@ function init_flash_process(){
#=================== STEP 2 =====================# #=================== STEP 2 =====================#
#================================================# #================================================#
function select_mcu_connection(){ function select_mcu_connection(){
echo
top_border top_border
echo -e "| ${yellow}Make sure to have the controller board connected now!${white} |" echo -e "| ${yellow}Make sure that the controller board is connected now!${white} |"
hr
blank_line blank_line
echo -e "| How is the controller board connected to the host? |" echo -e "| How is the controller board connected to the host? |"
echo -e "| 1) USB |" echo -e "| 1) USB |"
echo -e "| 2) UART |" echo -e "| 2) UART |"
bottom_border blank_line
back_help_footer
while true; do while true; do
read -p "${cyan}###### Connection method:${white} " choice read -p "${cyan}###### Connection method:${white} " choice
case "${choice}" in case "${choice}" in
### continue to execute script even if get_usb_id or get_uart_id returns with exit code 1
### this is not critical, as the mcu_list array will be empty and that specific
### case is handled later during execution of the script
1) 1)
status_msg "Identifying MCU connected via USB ...\n" status_msg "Identifying MCU connected via USB ...\n"
get_usb_id get_usb_id || true # continue even after exit code 1
break;; break;;
2) 2)
status_msg "Identifying MCU possibly connected via UART ...\n" status_msg "Identifying MCU possibly connected via UART ...\n"
get_uart_id get_uart_id || true # continue even after exit code 1
break;;
B|b)
advanced_menu
break;;
H|h)
clear && print_header
show_mcu_connection_help
break;; break;;
*) *)
error_msg "Invalid input!\n";; error_msg "Invalid command!";;
esac esac
done done
}
if [[ "${#mcu_list[@]}" -lt 1 ]]; then function print_detected_mcu_to_screen(){
warn_msg "No MCU found!" local i=1
warn_msg "MCU not plugged in or not detectable!"
echo if [ "${#mcu_list[@]}" -lt 1 ]; then
else print_error "No MCU found!\n MCU eihter not connected or not detected!"
local i=1 return
for mcu in "${mcu_list[@]}"; do
mcu=$(echo "${mcu}" | rev | cut -d"/" -f1 | rev)
echo -e " ● MCU #${i}: ${cyan}${mcu}${white}"
i=$((i+1))
done
echo
fi fi
for mcu in "${mcu_list[@]}"; do
mcu=$(echo "${mcu}" | rev | cut -d"/" -f1 | rev)
echo -e " ● MCU #${i}: ${cyan}${mcu}${white}"
i=$((i+1))
done
echo
} }
#================================================# #================================================#
@@ -106,58 +119,64 @@ function select_mcu_connection(){
#================================================# #================================================#
function select_mcu_id(){ function select_mcu_id(){
local i=0 sel_index=0 method=${1} local i=0 sel_index=0 method=${1}
top_border
echo -e "| ${red}!!! ATTENTION !!!${white} |" if [ "${#mcu_list[@]}" -lt 1 ]; then
hr print_error "No MCU found!\n MCU eihter not connected or not detected!"
echo -e "| Make sure, to select the correct MCU! |" return
echo -e "| ${red}ONLY flash a firmware created for the respective MCU!${white} |" fi
bottom_border
echo -e "${cyan}###### List of available MCU:${white}" top_border
### list all mcus echo -e "| ${red}!!! ATTENTION !!!${white} |"
for mcu in "${mcu_list[@]}"; do hr
i=$((i+1)) echo -e "| Make sure, to select the correct MCU! |"
mcu=$(echo "${mcu}" | rev | cut -d"/" -f1 | rev) echo -e "| ${red}ONLY flash a firmware created for the respective MCU!${white} |"
echo -e " ● MCU #${i}: ${cyan}${mcu}${white}" bottom_border
done echo -e "${cyan}###### List of available MCU:${white}"
### verify user input ### list all mcus
while [[ ! (${sel_index} =~ ^[1-9]+$) ]] || [ "${sel_index}" -gt "${i}" ]; do for mcu in "${mcu_list[@]}"; do
echo i=$((i+1))
read -p "${cyan}###### Select MCU to flash:${white} " sel_index mcu=$(echo "${mcu}" | rev | cut -d"/" -f1 | rev)
if [[ ! (${sel_index} =~ ^[1-9]+$) ]]; then echo -e " ● MCU #${i}: ${cyan}${mcu}${white}"
error_msg "Invalid input!" done
elif [ "${sel_index}" -lt 1 ] || [ "${sel_index}" -gt "${i}" ]; then ### verify user input
error_msg "Please select a number between 1 and ${i}!" while [[ ! (${sel_index} =~ ^[1-9]+$) ]] || [ "${sel_index}" -gt "${i}" ]; do
fi echo
mcu_index=$((sel_index - 1)) read -p "${cyan}###### Select MCU to flash:${white} " sel_index
selected_mcu_id="${mcu_list[${mcu_index}]}" if [[ ! (${sel_index} =~ ^[1-9]+$) ]]; then
done error_msg "Invalid input!"
### confirm selection elif [ "${sel_index}" -lt 1 ] || [ "${sel_index}" -gt "${i}" ]; then
while true; do error_msg "Please select a number between 1 and ${i}!"
echo -e "\n###### You selected:\n ● MCU #${sel_index}: ${selected_mcu_id}\n" fi
read -p "${cyan}###### Continue? (Y/n):${white} " yn mcu_index=$((sel_index - 1))
case "${yn}" in selected_mcu_id="${mcu_list[${mcu_index}]}"
Y|y|Yes|yes|"") done
select_msg "Yes" ### confirm selection
status_msg "Flashing ${selected_mcu_id} ..." while true; do
if [ "${method}" == "regular" ]; then echo -e "\n###### You selected:\n ● MCU #${sel_index}: ${selected_mcu_id}\n"
log_info "Flashing device '${selected_mcu_id}' with method '${method}'" read -p "${cyan}###### Continue? (Y/n):${white} " yn
start_flash_mcu "${selected_mcu_id}" case "${yn}" in
elif [ "${method}" == "sdcard" ]; then Y|y|Yes|yes|"")
log_info "Flashing device '${selected_mcu_id}' with method '${method}'" select_msg "Yes"
start_flash_sd "${selected_mcu_id}" status_msg "Flashing ${selected_mcu_id} ..."
else if [ "${method}" == "regular" ]; then
error_msg "No flash method set! Aborting..." log_info "Flashing device '${selected_mcu_id}' with method '${method}'"
log_error "No flash method set!" start_flash_mcu "${selected_mcu_id}"
return 1 elif [ "${method}" == "sdcard" ]; then
fi log_info "Flashing device '${selected_mcu_id}' with method '${method}'"
break;; start_flash_sd "${selected_mcu_id}"
N|n|No|no) else
select_msg "No" print_error "No flash method set! Aborting..."
break;; log_error "No flash method set!"
*) return
print_error "Invalid command!";; fi
esac break;;
done N|n|No|no)
select_msg "No"
break;;
*)
error_msg "Invalid command!";;
esac
done
} }
function start_flash_mcu(){ function start_flash_mcu(){
@@ -204,16 +223,17 @@ function start_flash_sd(){
if [ "${choice}" = "q" ] || [ "${choice}" = "Q" ]; then if [ "${choice}" = "q" ] || [ "${choice}" = "Q" ]; then
clear && advanced_menu && break clear && advanced_menu && break
elif [ "${choice}" -le ${#board_list[@]} ]; then elif [ "${choice}" -le ${#board_list[@]} ]; then
selected_board="${board_list[${choice}]}" local selected_board="${board_list[${choice}]}"
break break
else else
clear && print_header clear && print_header
print_error "Invalid choice!" error_msg "Invalid choice!"
flash_mcu_sd flash_mcu_sd
fi fi
done done
while true; do while true; do
echo
top_border top_border
echo -e "| If your board is flashed with firmware that connects |" echo -e "| If your board is flashed with firmware that connects |"
echo -e "| at a custom baud rate, please change it now. |" echo -e "| at a custom baud rate, please change it now. |"
@@ -221,10 +241,9 @@ function start_flash_sd(){
echo -e "| If you are unsure, stick to the default 250000! |" echo -e "| If you are unsure, stick to the default 250000! |"
bottom_border bottom_border
echo -e "${cyan}###### Please set the baud rate:${white} " echo -e "${cyan}###### Please set the baud rate:${white} "
unset baud_rate
while [[ ! ${baud_rate} =~ ^[0-9]+$ ]]; do while [[ ! ${baud_rate} =~ ^[0-9]+$ ]]; do
read -e -i "250000" -e baud_rate read -e -i "250000" -e baud_rate
selected_baud_rate=${baud_rate} local selected_baud_rate=${baud_rate}
break break
done done
break break
@@ -233,11 +252,10 @@ function start_flash_sd(){
###flash process ###flash process
do_action_service "stop" "klipper" do_action_service "stop" "klipper"
if "${flash_script}" -b "${selected_baud_rate}" "${device}" "${selected_board}"; then if "${flash_script}" -b "${selected_baud_rate}" "${device}" "${selected_board}"; then
ok_msg "Flashing successfull!" print_confirm "Flashing successfull!"
log_info "Flash successfull!" log_info "Flash successfull!"
else else
warn_msg "Flashing failed!" print_error "Flashing failed!\n Please read the console output above!"
warn_msg "Please read the console output above!"
log_error "Flash failed!" log_error "Flash failed!"
fi fi
do_action_service "start" "klipper" do_action_service "start" "klipper"
@@ -247,7 +265,7 @@ function build_fw(){
local python_version local python_version
if [ ! -d "${KLIPPER_DIR}" ] || [ ! -d "${KLIPPY_ENV}" ]; then if [ ! -d "${KLIPPER_DIR}" ] || [ ! -d "${KLIPPY_ENV}" ]; then
print_error "Klipper not found!\n Cannot build firmware without Klipper!" print_error "Klipper not found!\n Cannot build firmware without Klipper!"
return 1 return
else else
cd "${KLIPPER_DIR}" cd "${KLIPPER_DIR}"
status_msg "Initializing firmware build ..." status_msg "Initializing firmware build ..."
@@ -271,7 +289,7 @@ function build_fw(){
function get_usb_id(){ function get_usb_id(){
unset mcu_list unset mcu_list
sleep 1 sleep 1
mcus=$(find /dev/serial/by-id/*) mcus=$(find /dev/serial/by-id/* 2>/dev/null)
for mcu in ${mcus}; do for mcu in ${mcus}; do
mcu_list+=("${mcu}") mcu_list+=("${mcu}")
done done
@@ -280,7 +298,7 @@ function get_usb_id(){
function get_uart_id() { function get_uart_id() {
unset mcu_list unset mcu_list
sleep 1 sleep 1
mcus=$(find /dev -maxdepth 1 -regextype posix-extended -regex "^\/dev\/tty[^0-9]+([0-9]+)?$") mcus=$(find /dev -maxdepth 1 -regextype posix-extended -regex "^\/dev\/tty[^0-9]+([0-9]+)?$" 2>/dev/null)
for mcu in ${mcus}; do for mcu in ${mcus}; do
mcu_list+=("${mcu}") mcu_list+=("${mcu}")
done done
@@ -322,7 +340,41 @@ function show_flash_method_help(){
init_flash_process init_flash_process
break;; break;;
*) *)
print_error "Invalid command!";; error_msg "Invalid command!";;
esac
done
}
function show_mcu_connection_help(){
top_border
echo -e "| ~~~~~~~~ < ? > Help: Flash MCU < ? > ~~~~~~~~ |"
hr
echo -e "| ${cyan}USB:${white} |"
echo -e "| Selecting USB as the connection method will scan the |"
echo -e "| USB ports for connected controller boards. This will |"
echo -e "| be similar to the 'ls /dev/serial/by-id/*' command |"
echo -e "| suggested by the official Klipper documentation for |"
echo -e "| determining successfull USB connections! |"
blank_line
echo -e "| ${cyan}UART:${white} |"
echo -e "| Selecting UART as the connection method will list all |"
echo -e "| possible UART serial ports. Note: This method ALWAYS |"
echo -e "| returns something as it seems impossible to determine |"
echo -e "| if a valid Klipper controller board is connected or |"
echo -e "| not. Because of that, you ${red}MUST${white} know which UART serial |"
echo -e "| port your controller board is connected to when using |"
echo -e "| this connection method. |"
blank_line
back_footer
while true; do
read -p "${cyan}###### Please select:${white} " choice
case "${choice}" in
B|b)
clear && print_header
select_mcu_connection
break;;
*)
error_msg "Invalid command!";;
esac esac
done done
} }

View File

@@ -14,6 +14,7 @@ set -e
advanced_ui(){ advanced_ui(){
top_border top_border
echo -e "| ${yellow}~~~~~~~~~~~~~ [ Advanced Menu ] ~~~~~~~~~~~~~${white} | " echo -e "| ${yellow}~~~~~~~~~~~~~ [ Advanced Menu ] ~~~~~~~~~~~~~${white} | "
hr
echo -e "| Klipper: | Mainsail: | " echo -e "| Klipper: | Mainsail: | "
echo -e "| 1) [Switch Branch] | 7) [Theme installer] | " echo -e "| 1) [Switch Branch] | 7) [Theme installer] | "
echo -e "| 2) [Rollback] | | " echo -e "| 2) [Rollback] | | "
@@ -46,7 +47,10 @@ advanced_menu(){
build_fw && init_flash_process build_fw && init_flash_process
advanced_ui;; advanced_ui;;
6) 6)
do_action "select_mcu_connection" "advanced_ui";; clear && print_header
select_mcu_connection
print_detected_mcu_to_screen
advanced_ui;;
7) 7)
do_action "ms_theme_menu";; do_action "ms_theme_menu";;
8) 8)