diff --git a/scripts/flash_klipper.sh b/scripts/flash_klipper.sh index 437fc8a..de363fc 100644 --- a/scripts/flash_klipper.sh +++ b/scripts/flash_klipper.sh @@ -11,48 +11,12 @@ set -e -function show_flash_method_help(){ - top_border - echo -e "| ~~~~~~~~ < ? > Help: Flash MCU < ? > ~~~~~~~~ |" - hr - echo -e "| ${cyan}Regular flashing method:${white} |" - echo -e "| The default method to flash controller boards which |" - echo -e "| are connected and updated over USB and not by placing |" - echo -e "| a compiled firmware file onto an internal SD-Card. |" - blank_line - echo -e "| Common controllers that get flashed that way are: |" - echo -e "| - Arduino Mega 2560 |" - echo -e "| - Fysetc F6 / S6 (used without a Display + SD-Slot) |" - blank_line - echo -e "| ${cyan}Updating via SD-Card Update:${white} |" - echo -e "| Many popular controller boards ship with a bootloader |" - echo -e "| capable of updating the firmware via SD-Card. |" - echo -e "| Choose this method if your controller board supports |" - echo -e "| this way of updating. This method ONLY works for up- |" - echo -e "| grading firmware. The initial flashing procedure must |" - echo -e "| be done manually per the instructions that apply to |" - echo -e "| your controller board. |" - blank_line - echo -e "| Common controllers that can be flashed that way are: |" - echo -e "| - BigTreeTech SKR 1.3 / 1.4 (Turbo) / E3 / Mini E3 |" - echo -e "| - Fysetc F6 / S6 (used with a Display + SD-Slot) |" - echo -e "| - Fysetc Spider |" - blank_line - back_footer - while true; do - read -p "${cyan}###### Please select:${white} " choice - case "${choice}" in - B|b) - clear && print_header - select_flash_method - break;; - *) - print_error "Invalid command!";; - esac - done -} +function init_flash_process(){ + local method + + ### step 1: check for required userhgroups (tty & dialout) + check_usergroups -function select_flash_method(){ top_border echo -e "| ~~~~~~~~~~~~ [ Flash MCU ] ~~~~~~~~~~~~ |" hr @@ -69,16 +33,12 @@ function select_flash_method(){ read -p "${cyan}###### Please select:${white} " choice case "${choice}" in 1) - echo -e "###### > Regular flashing method" - select_mcu_connection - select_mcu_id - [[ "${CONFIRM_FLASH}" == true ]] && flash_mcu + select_msg "Regular flashing method" + method="regular" break;; 2) - echo -e "###### > SD-Card Update" - select_mcu_connection - select_mcu_id - [[ "${CONFIRM_FLASH}" == true ]] && flash_mcu_sd + select_msg "SD-Card Update" + method="sdcard" break;; B|b) advanced_menu @@ -91,11 +51,61 @@ function select_flash_method(){ print_error "Invalid command!";; esac done + + ### step 2: select how the mcu is connected to the host + select_mcu_connection + ### step 3: select which detected mcu should be flashed + select_mcu_id "${method}" } +#================================================# +#=================== STEP 2 =====================# +#================================================# +function select_mcu_connection(){ + echo + top_border + echo -e "| ${yellow}Make sure to have the controller board connected now!${white} |" + blank_line + echo -e "| How is the controller board connected to the host? |" + echo -e "| 1) USB |" + echo -e "| 2) UART |" + bottom_border + while true; do + read -p "${cyan}###### Connection method:${white} " choice + case "${choice}" in + 1) + status_msg "Identifying MCU connected via USB ...\n" + get_usb_id + break;; + 2) + status_msg "Identifying MCU possibly connected via UART ...\n" + get_uart_id + break;; + *) + error_msg "Invalid input!\n";; + esac + done + + if [[ "${#mcu_list[@]}" -lt 1 ]]; then + warn_msg "No MCU found!" + warn_msg "MCU not plugged in or not detectable!" + echo + else + local i=1 + 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 +} + +#================================================# +#=================== STEP 3 =====================# +#================================================# function select_mcu_id(){ - local id=0 sel_index=0 - if [ ${#mcu_list[@]} -ge 1 ]; then + local i=1 sel_index=0 method=${1} top_border echo -e "| ${red}!!! ATTENTION !!!${white} |" hr @@ -105,17 +115,18 @@ function select_mcu_id(){ echo -e "${cyan}###### List of available MCU:${white}" ### list all mcus for mcu in "${mcu_list[@]}"; do - id=$((id+1)) - echo -e " ${id}) ${mcu}" + mcu=$(echo "${mcu}" | rev | cut -d"/" -f1 | rev) + echo -e " ● MCU #${i}: ${cyan}${mcu}${white}" + i=$((i+1)) done ### verify user input - while [[ ! (${sel_index} =~ ^[1-9]+$) ]] || [ "${sel_index}" -gt "${id}" ]; do + while [[ ! (${sel_index} =~ ^[1-9]+$) ]] || [ "${sel_index}" -gt "${i}" ]; do echo read -p "${cyan}###### Select MCU to flash:${white} " sel_index if [[ ! (${sel_index} =~ ^[1-9]+$) ]]; then error_msg "Invalid input!" - elif [ "${sel_index}" -lt 1 ] || [ "${sel_index}" -gt "${id}" ]; then - error_msg "Please select a number between 1 and ${id}!" + elif [ "${sel_index}" -lt 1 ] || [ "${sel_index}" -gt "${i}" ]; then + error_msg "Please select a number between 1 and ${i}!" fi mcu_index=$((sel_index - 1)) selected_mcu_id="${mcu_list[${mcu_index}]}" @@ -128,38 +139,45 @@ function select_mcu_id(){ Y|y|Yes|yes|"") select_msg "Yes" status_msg "Flashing ${selected_mcu_id} ..." - CONFIRM_FLASH=true + if [ "${method}" == "regular" ]; then + log_info "Flashing device '${selected_mcu_id}' with method '${method}'" + start_flash_mcu "${selected_mcu_id}" + elif [ "${method}" == "sdcard" ]; then + log_info "Flashing device '${selected_mcu_id}' with method '${method}'" + start_flash_mcu_sd "${selected_mcu_id}" + else + error_msg "No flash method set! Aborting..." + log_error "No flash method set!" + return 1 + fi break;; N|n|No|no) select_msg "No" - CONFIRM_FLASH=false break;; *) print_error "Invalid command!";; esac done - fi } -function flash_mcu(){ +function start_flash_mcu(){ + local device=${1} do_action_service "stop" "klipper" - make flash FLASH_DEVICE="${mcu_list[${mcu_index}]}" - ### evaluate exit code of make flash - if [ ! $? -eq 0 ]; then + if make flash FLASH_DEVICE="${device}"; then + ok_msg "Flashing successfull!" + else warn_msg "Flashing failed!" warn_msg "Please read the console output above!" - else - ok_msg "Flashing successfull!" fi do_action_service "start" "klipper" } -function flash_mcu_sd(){ - local i=0 board_list=() +function start_flash_mcu_sd(){ + local i=0 board_list=() device=${1} local flash_script="${HOME}/klipper/scripts/flash-sdcard.sh" ### write each supported board to the array to make it selectable - for board in $(/bin/bash "${flash_script}" -l | tail -n +2); do + for board in $("${flash_script}" -l | tail -n +2); do board_list+=("${board}") done @@ -214,13 +232,13 @@ function flash_mcu_sd(){ ###flash process do_action_service "stop" "klipper" - /bin/bash "${flash_script}" -b "${selected_baud_rate}" "${selected_mcu_id}" "${selected_board}" - ### evaluate exit code of flash-sdcard.sh execution - if [ ! $? -eq 0 ]; then + if "${flash_script}" -b "${selected_baud_rate}" "${device}" "${selected_board}"; then + ok_msg "Flashing successfull!" + log_info "Flash successfull!" + else warn_msg "Flashing failed!" warn_msg "Please read the console output above!" - else - ok_msg "Flashing successfull!" + log_error "Flash failed!" fi do_action_service "start" "klipper" } @@ -246,98 +264,65 @@ function build_fw(){ fi } -function select_mcu_connection(){ - echo +#================================================# +#=================== HELPERS ====================# +#================================================# + +function get_usb_id(){ + unset mcu_list + sleep 1 + mcus=$(find /dev/serial/by-id/*) + for mcu in ${mcus}; do + mcu_list+=("${mcu}") + done +} + +function get_uart_id() { + unset mcu_list + sleep 1 + mcus=$(find /dev -maxdepth 1 -regextype posix-extended -regex "^\/dev\/tty[^0-9]+([0-9]+)?$") + for mcu in ${mcus}; do + mcu_list+=("${mcu}") + done +} + +function show_flash_method_help(){ top_border - echo -e "| ${yellow}Make sure to have the controller board connected now!${white} |" + echo -e "| ~~~~~~~~ < ? > Help: Flash MCU < ? > ~~~~~~~~ |" + hr + echo -e "| ${cyan}Regular flashing method:${white} |" + echo -e "| The default method to flash controller boards which |" + echo -e "| are connected and updated over USB and not by placing |" + echo -e "| a compiled firmware file onto an internal SD-Card. |" blank_line - echo -e "| How is the controller board connected to the host? |" - echo -e "| 1) USB |" - echo -e "| 2) UART |" - bottom_border + echo -e "| Common controllers that get flashed that way are: |" + echo -e "| - Arduino Mega 2560 |" + echo -e "| - Fysetc F6 / S6 (used without a Display + SD-Slot) |" + blank_line + echo -e "| ${cyan}Updating via SD-Card Update:${white} |" + echo -e "| Many popular controller boards ship with a bootloader |" + echo -e "| capable of updating the firmware via SD-Card. |" + echo -e "| Choose this method if your controller board supports |" + echo -e "| this way of updating. This method ONLY works for up- |" + echo -e "| grading firmware. The initial flashing procedure must |" + echo -e "| be done manually per the instructions that apply to |" + echo -e "| your controller board. |" + blank_line + echo -e "| Common controllers that can be flashed that way are: |" + echo -e "| - BigTreeTech SKR 1.3 / 1.4 (Turbo) / E3 / Mini E3 |" + echo -e "| - Fysetc F6 / S6 (used with a Display + SD-Slot) |" + echo -e "| - Fysetc Spider |" + blank_line + back_footer while true; do - read -p "${cyan}###### Connection method:${white} " choice + read -p "${cyan}###### Please select:${white} " choice case "${choice}" in - 1) - retrieve_id "USB" - break;; - 2) - retrieve_id "UART" + B|b) + clear && print_header + init_flash_process break;; *) - print_unkown_cmd - print_msg && clear_msg;; + print_error "Invalid command!";; esac done - unset mcu_count - - if [[ "${#mcu_list[@]}" -lt 1 ]]; then - warn_msg "No MCU found!" - warn_msg "MCU not plugged in or not detectable!" - echo - fi -} - -function retrieve_id(){ - local mcu_list=() mcu_count=1 - status_msg "Identifying MCU ..." - sleep 1 - [ "${1}" = "USB" ] && path="/dev/serial/by-id/*" - [ "${1}" = "UART" ] && path="/dev/ttyAMA0" - if [[ "$(ls "${path}")" != "" ]] ; then - for mcu in ${path}; do - declare "mcu_id_${mcu_count}"="${mcu}" - mcu_id="mcu_id_${mcu_count}" - mcu_list+=("${!mcu_id}") - echo -e " ● ($1) MCU #${mcu_count}: ${cyan}${mcu}${white}\n" - mcu_count=$((mcu_count+1)) - done - fi 2>/dev/null -} - -function check_usergroups(){ - local group_dialout group_tty - if grep -q "dialout" /dev/null 1>&2; then status_msg "Restarting NGINX Service ..." sudo systemctl restart nginx && ok_msg "NGINX Service restarted!" @@ -362,7 +362,7 @@ restart_nginx(){ #================ DEPENDENCIES ==================# #================================================# -dependency_check(){ +function dependency_check(){ local dep=( "${@}" ) # dep: array status_msg "Checking for the following dependencies:" #check if package is installed, if not write name into array @@ -390,7 +390,7 @@ dependency_check(){ fi } -setup_gcode_shell_command(){ +function setup_gcode_shell_command(){ echo top_border echo -e "| You are about to install the G-Code Shell Command |" @@ -435,7 +435,7 @@ setup_gcode_shell_command(){ done } -install_gcode_shell_command(){ +function install_gcode_shell_command(){ do_action_service "stop" "klipper" status_msg "Copy 'gcode_shell_command.py' to '${KLIPPER_DIR}/klippy/extras' ..." cp "${SRCDIR}/kiauh/resources/gcode_shell_command.py" "${KLIPPER_DIR}/klippy/extras" @@ -553,3 +553,54 @@ function fetch_webui_ports(){ fi done } + +#================================================# +#================= USERGROUPS ===================# +#================================================# + +function check_usergroups(){ + local group_dialout group_tty + if grep -q "dialout"