Skip to content

Commit

Permalink
flasher will now pull from github into /tmp (#76)
Browse files Browse the repository at this point in the history
* flasher will now pull from github into /tmp

* fix: dfu mode will properly find new files

* delete temp directory before creating

Co-authored-by: Jonas Damtoft <[email protected]>
  • Loading branch information
krautech and Jomik authored Nov 20, 2024
1 parent 06a5678 commit 168181c
Showing 1 changed file with 78 additions and 54 deletions.
132 changes: 78 additions & 54 deletions firmware.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
#!/bin/bash

while getopts s:t:f: flag
while getopts s:t:f:b: flag
do
case "${flag}" in
s) switch=${OPTARG};;
t) ftype=${OPTARG};;
f) flash=${OPTARG};;
b) beta=${OPTARG};;
esac
done
# Define repository URLs
CARTOGRAPHER_KLIPPER_REPO="https://github.com/Cartographer3D/cartographer-klipper.git"
KATAPULT_REPO="https://github.com/Arksine/katapult.git"

# Define local paths where the repositories should be cloned
CARTOGRAPHER_KLIPPER_DIR="$HOME/cartographer-klipper"
if [[ $beta == "beta" ]]; then
TARBALL_URL="https://api.github.com/repos/Cartographer3D/cartographer-klipper/tarball/master"
else
TARBALL_URL="https://api.github.com/repos/Cartographer3D/cartographer-klipper/tarball/beta-firmware"
fi
TEMP_DIR="/tmp/cartographer-klipper"

KATAPULT_DIR="$HOME/katapult"

RED='\033[0;31m'
Expand Down Expand Up @@ -42,8 +48,6 @@ if systemctl is-active --quiet "klipper.service" ; then
else
sudo service klipper stop
fi
cd ~/cartographer-klipper
git pull > /dev/null 2>&1
##
# Color Variables
##
Expand Down Expand Up @@ -77,7 +81,7 @@ printf "${BLUE}
\____| \__,_| |_| \__| \___/ \__, | |_| \__,_| | .__/ |_| |_| \___| |_|
|___/ |_|
${NC}"
printf "${RED}Firmware Script ${NC} v1.0.10\n"
printf "${RED}Firmware Script ${NC} v1.1.1\n"
printf "Created by ${GREEN}KrauTech${NC} ${BLUE}(https://github.com/krautech)${NC}\n"
echo
echo
Expand Down Expand Up @@ -141,7 +145,7 @@ menu(){
if [[ $canbootID == "" ]] && [[ $katapultID == "" ]] && [[ $dfuID == "" ]] && [[ $usbID == "" ]] && [[ $queryID == "" ]]; then
echo -ne "$(ColorRed 'No Device Found in Flashing Mode')\n"
fi
if [ ! -d ~/katapult ] || [ ! -d ~/cartographer-klipper ]; then
if [ ! -d ~/katapult ] || [ ! -d $CARTOGRAPHER_KLIPPER_DIR ]; then
echo -ne ""
else
if [[ $flash == "dfu" ]] || [[ $flash == "" ]]; then
Expand Down Expand Up @@ -207,8 +211,8 @@ menu(){
"lsusb")
lsusb
read -p "Press enter to return to main menu"; menu ;;
"q") sudo service klipper start; exit;;
"r") sudo reboot; exit;;
"q") sudo service klipper start; delete_temp;;
"r") sudo reboot; delete_temp;;
*) echo -e $red"Wrong option."$clear;;
esac
}
Expand Down Expand Up @@ -248,8 +252,15 @@ initialChecks(){
echo "This can take a few moments.. please wait."
echo
installPre
cd ~/cartographer-klipper/
git pull > /dev/null 2>&1

# Clean out temp_dir
rm -rf "$TEMP_DIR"
mkdir -p "$TEMP_DIR"
curl -L "$TARBALL_URL" | tar -xz -C "$TEMP_DIR"
# Find the extracted folder name (GitHub includes commit hash in the folder name)
CARTOGRAPHER_KLIPPER_DIR=$(find "$TEMP_DIR" -mindepth 1 -maxdepth 1 -type d)


# Check for Device in DFU Mode Instead
if [[ $flash == "dfu" ]] || [[ $flash == "" ]]; then
dfuCheck=$(lsusb | grep -oP "DFU Mode")
Expand Down Expand Up @@ -325,7 +336,6 @@ installPre(){
# Installs all needed files FUNCTION
check_git_installed
check_dfu_util_installed
check_cartographer_klipper
check_katapult
}
# Function to check if Git is installed
Expand Down Expand Up @@ -364,10 +374,6 @@ check_repo_pulled() {
fi
fi
}
# Check if cartographer-klipper is pulled
check_cartographer_klipper() {
check_repo_pulled "$CARTOGRAPHER_KLIPPER_DIR" "$CARTOGRAPHER_KLIPPER_REPO"
}

# Check if katapult is pulled
check_katapult() {
Expand Down Expand Up @@ -550,12 +556,11 @@ flashFirmware(){
printf "${BLUE}Flashing ${canbootID}${katapultID} via ${GREEN}CANBUS - KATAPULT${NC}\n\n"

# Update repository
cd ~/cartographer-klipper/ || exit
git pull > /dev/null 2>&1
cd "$CARTOGRAPHER_KLIPPER_DIR" || exit

# Determine flashing type
if [[ $ftype == "katapult" ]]; then
cd ~/cartographer-klipper/firmware/v2-v3/katapult-deployer || exit
cd "$CARTOGRAPHER_KLIPPER_DIR/firmware/v2-v3/katapult-deployer/" || exit

# Set search parameters based on switch
if [[ $switch == "canbus" ]]; then
Expand All @@ -577,9 +582,9 @@ flashFirmware(){
else
# Set directory based on parameter
if [[ $1 == 1 ]]; then
cd ~/cartographer-klipper/firmware/v2-v3/survey || exit
cd "$CARTOGRAPHER_KLIPPER_DIR/firmware/v2-v3/survey" || exit
else
cd ~/cartographer-klipper/firmware/v2-v3/ || exit
cd "$CARTOGRAPHER_KLIPPER_DIR/cartographer-klipper/firmware/v2-v3/" || exit
fi

# Set search pattern based on bitrate
Expand All @@ -590,7 +595,7 @@ flashFirmware(){
# find . -maxdepth 1 -type f ! -name "*.md" -name "$search_pattern" -printf "%f\n" | sort_firmware_files
# )

archive_dir="$HOME/cartographer-klipper/firmware/v2-v3/survey" # Corrected path
archive_dir="$CARTOGRAPHER_KLIPPER_DIR/firmware/v2-v3/survey" # Corrected path
if [[ -d $archive_dir ]]; then
# Get the folder names sorted from largest to smallest version
folders=($(ls -d "$archive_dir"/*/ | sort -rV))
Expand Down Expand Up @@ -639,12 +644,11 @@ flashFirmware(){
printf "${BLUE}Flashing ${queryID} via ${GREEN}CANBUS - KATAPULT${NC}\n\n"

# Update repository
cd ~/cartographer-klipper/ || exit
git pull > /dev/null 2>&1
cd "$CARTOGRAPHER_KLIPPER_DIR" || exit

# Determine flashing type
if [[ $ftype == "katapult" ]]; then
cd ~/cartographer-klipper/firmware/v2-v3/katapult-deployer || exit
cd "$CARTOGRAPHER_KLIPPER_DIR/firmware/v2-v3/katapult-deployer" || exit

# Set search parameters based on switch
if [[ $switch == "canbus" ]]; then
Expand All @@ -666,9 +670,9 @@ flashFirmware(){
else
# Set directory based on parameter
if [[ $1 == 1 ]]; then
cd ~/cartographer-klipper/firmware/v2-v3/survey || exit
cd "$CARTOGRAPHER_KLIPPER_DIR/firmware/v2-v3/survey" || exit
else
cd ~/cartographer-klipper/firmware/v2-v3 || exit
cd "$CARTOGRAPHER_KLIPPER_DIR/firmware/v2-v3" || exit
fi

# Set search pattern based on bitrate
Expand All @@ -679,7 +683,7 @@ flashFirmware(){
# find . -maxdepth 1 -type f ! -name "*.md" -name "$search_pattern" -printf "%f\n" | sort_firmware_files
# )

archive_dir="$HOME/cartographer-klipper/firmware/v2-v3/survey" # Corrected path
archive_dir="$CARTOGRAPHER_KLIPPER_DIR/cartographer-klipper/firmware/v2-v3/survey" # Corrected path
if [[ -d $archive_dir ]]; then
# Get the folder names sorted from largest to smallest version
folders=($(ls -d "$archive_dir"/*/ | sort -rV))
Expand Down Expand Up @@ -724,33 +728,46 @@ flashFirmware(){
# If found device is DFU
if [[ $dfuID != "" ]] && [[ $2 == 2 ]]; then
printf "${BLUE}Flashing via ${GREEN}DFU${NC}\n\n"
cd ~/cartographer-klipper/
git pull > /dev/null 2>&1
cd ~/cartographer-klipper/firmware/v2-v3/combined-firmware
if [[ $1 == 1 ]]; then
findFiles="Full_Survey_*"
else
findFiles="Full_Cartographer_*"
search_pattern="Full_Survey_*"

archive_dir="$CARTOGRAPHER_KLIPPER_DIR/firmware/v2-v3/combined-firmware" # Corrected path
if [[ -d $archive_dir ]]; then
# Get the folder names sorted from largest to smallest version
cd $archive_dir
folders=($(ls -d "$archive_dir"/*/ | sort -rV))

for folder in "${folders[@]}"; do
if [[ -d "$folder" ]]; then
folder_name=$(basename "$folder")
# List the files inside each folder using the same search pattern
for file in "$folder"/$search_pattern; do
if [[ -f "$file" ]]; then
options+=("${folder_name}/$(basename "$file")")
fi
done
fi
done
fi
DIRECTORY=.
unset options i
declare -A arr
while IFS= read -r -d $'\0' f; do
options[i++]="$f"
done < <(find $DIRECTORY -maxdepth 1 -type f \( -name "${findFiles}" \) -print0)

# Add "Back" option
options+=("Back")

#done < <(find $DIRECTORY -maxdepth 1 -type f \( -name 'katapult_and_carto_can_1m_beta.bin' \) -print0)
COLUMNS=12
select opt in "${options[@]}" "Back"; do
PS3="Please select a firmware to flash: "
select opt in "${options[@]}"; do
case $opt in
*.bin)
flashing $opt $1 "dfu";
flashing "$opt" "$1" "dfu"
break
;;
"Back")
menu ; break
menu
break
;;
*)
echo "This is not a number"
;;
echo "Invalid selection. Please try again."
;;
esac
done
fi
Expand All @@ -760,12 +777,11 @@ flashFirmware(){
printf "${BLUE}Flashing via ${GREEN}USB - KATAPULT${NC}\n\n"

# Update repository
cd ~/cartographer-klipper/ || exit
git pull > /dev/null 2>&1
cd ~/cartographer-klipper/firmware/v2-v3/katapult-deployer || exit

cd "$CARTOGRAPHER_KLIPPER_DIR/firmware/v2-v3/katapult-deployer" || exit

if [[ $ftype == "katapult" ]]; then
cd ~/cartographer-klipper/firmware/v2-v3/katapult-deployer || exit
cd "$CARTOGRAPHER_KLIPPER_DIR/firmware/v2-v3/katapult-deployer" || exit

# Set search parameters based on switch
if [[ $switch == "canbus" ]]; then
Expand All @@ -787,9 +803,9 @@ flashFirmware(){
else
# Set directory based on parameter
if [[ $1 == 1 ]]; then
cd ~/cartographer-klipper/firmware/v2-v3/survey || exit
cd "$CARTOGRAPHER_KLIPPER_DIR/firmware/v2-v3/survey" || exit
else
cd ~/cartographer-klipper/firmware/v2-v3/ || exit
cd "$CARTOGRAPHER_KLIPPER_DIR/firmware/v2-v3/" || exit
fi

# Set search pattern based on USB
Expand All @@ -800,7 +816,7 @@ flashFirmware(){
# find . -maxdepth 1 -type f ! -name "*.md" -name "$search_pattern" -printf "%f\n" | sort_firmware_files
# )

archive_dir="$HOME/cartographer-klipper/firmware/v2-v3/survey" # Corrected path
archive_dir="$CARTOGRAPHER_KLIPPER_DIR/firmware/v2-v3/survey" # Corrected path
if [[ -d $archive_dir ]]; then
# Get the folder names sorted from largest to smallest version
folders=($(ls -d "$archive_dir"/*/ | sort -rV))
Expand Down Expand Up @@ -943,7 +959,7 @@ display_flashed(){
method=$2
survey=$3

printf "Your device has been flash.
printf "Your device has been flashed.
Note, after you exit this script you need to replace the serial path or UUID with your probes serial path or UUID, this can be found by running the following commands
Expand All @@ -953,6 +969,14 @@ For USB based probes\n\n"
printf "${RED}~/klippy-env/bin/python ~/klipper/scripts/canbus_query.py can0${NC}-\n"
printf "Take note of either the Serial ID or the UUID.\n\n"
read -p "Press enter to continue"
delete_temp;
}
delete_temp(){
if [[ -d "$TEMP_DIR" ]]; then
echo "Cleaning up temporary directory..."
rm -rf "$temp_dir"
echo "Temporary directory deleted."
fi
exit;
}
disclaimer;
Expand Down

0 comments on commit 168181c

Please sign in to comment.