aci.sh
1 #!/usr/bin/env bash 2 3 BOARD_NAME="" 4 FQBN_SELECTED="" 5 SERIAL_PORT="" 6 7 editors=("nano" "micro" "gedit" "vim" "nvim" "hx" "code" "codium") 8 installed_editors=() 9 sketch_file="" 10 11 # Get script directory (handles symlinks correctly) 12 DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" 13 14 serial_monitor() { 15 local baud_rate=$(gum input --placeholder "Baud Rate") 16 arduino-cli monitor -p $SERIAL_PORT -b $FQBN_SELECTED --config $baud_rate 17 trap 'gum confirm "Return to Homepage ?" && main || exit' SIGINT 18 19 echo "Press Ctrl+C again" 20 while true; do 21 sleep 1 22 done 23 } 24 25 check_for_updates() { 26 local current_version="v1.1.0" 27 28 # Check if the system is online by pinging Google 29 if ! ping -q -c 1 -W 1 8.8.8.8 >/dev/null 2>&1; then 30 echo "No internet connection. Skipping update check." 31 return 0 # Proceed to main program 32 fi 33 34 # First check if GitHub token is set 35 if [ -z "$ACI_GITHUB_TOKEN" ]; then 36 echo "Warning: ACI_GITHUB_TOKEN is not set" >&2 37 echo "Please set your GitHub token with: export ACI_GITHUB_TOKEN='your_token_here'" >&2 38 return 1 39 fi 40 41 # Get the latest version with error checking and store full response 42 # Added -k to skip SSL verification 43 local api_response=$(curl -k -s -H "Authorization: token $ACI_GITHUB_TOKEN" \ 44 -H "Accept: application/vnd.github.v3+json" \ 45 "https://api.github.com/repos/Vaishnav-Sabari-Girish/arduino-cli-interactive/releases/latest") 46 47 # Store the curl exit code 48 local curl_exit_code=$? 49 50 # Check if curl command succeeded 51 if [ $curl_exit_code -ne 0 ]; then 52 echo "Error: Failed to fetch latest version information (Exit code: $curl_exit_code)" >&2 53 return 1 54 fi 55 56 # Check if response is empty 57 if [ -z "$api_response" ]; then 58 echo "Error: Empty response from GitHub API" >&2 59 return 1 60 fi 61 62 # Check if response is valid JSON 63 if ! echo "$api_response" | jq empty >/dev/null 2>&1; then 64 echo "Error: Invalid JSON response from GitHub API" >&2 65 echo "Raw response: $api_response" >&2 66 return 1 67 fi 68 69 # Try to extract the version 70 local latest_version=$(echo "$api_response" | jq -r '.tag_name') 71 72 # Check if version was extracted successfully 73 if [ -z "$latest_version" ] || [ "$latest_version" = "null" ]; then 74 echo "Error: Could not find version tag in response" >&2 75 echo "Raw response: $api_response" >&2 76 return 1 77 fi 78 79 if [ "$current_version" != "$latest_version" ]; then 80 cat >&2 <<-EOF 81 📦 Update available! 82 Current version: $current_version 83 Latest version: $latest_version 84 To upgrade, run: 85 brew upgrade vaishnav-sabari-girish/arduino-cli-interactive 86 EOF 87 return 2 # Return code 2 indicates update available 88 fi 89 90 return 0 # Return code 0 indicates up to date 91 } 92 93 list_libraries() { 94 echo "These are the libraries installed in your system" 95 printf "\n" 96 arduino-cli lib list 97 98 gum confirm "Return to homepage ?" && main || : 99 } 100 101 lib_examples() { 102 installed_editors=() 103 104 local lib_example=$(arduino-cli lib list | tail -n +2 | awk '{print $1, $2}' | 105 sed 's/ *[0-9][^ ]*//g' | 106 sed 's/-//g' | gum filter --placeholder "Input Library name to get example" --indicator=">") 107 local example_chosen=$(arduino-cli lib examples $lib_example | tail -n +2 | awk 'NF' | gum choose) 108 109 local lib_dir=$(echo $example_chosen | sed 's/^[[:space:]-]*//') 110 111 local ex_file=$(gum file $lib_dir) 112 113 gum pager <$ex_file 114 sketch_file=("${lib_dir}${ex_file}") 115 116 main 117 118 } 119 120 edit_config_file() { 121 if [ -e $HOME/.arduino15/arduino-cli.yaml ]; then 122 arduino-cli config init 123 fi 124 125 installed_editors=() 126 127 echo "Choose your preferred editor." 128 echo "Note that these editors are already installed in your system" 129 130 timer 1s 131 132 for editor in "${editors[@]}"; do 133 if command -v $editor &>/dev/null; then 134 installed_editors+=("$editor") 135 fi 136 done 137 138 chosen_editor=$(printf "%s\n" "${installed_editors[@]}" | gum choose) 139 timer 0.5s 140 141 $chosen_editor "$HOME/.arduino15/arduino-cli.yaml" 142 143 gum confirm "Check file contents" && gum pager <$HOME/.arduino15/arduino-cli.yaml || main 144 145 } 146 147 edit_sketch() { 148 installed_editors=() 149 150 echo "Choose file to edit : " 151 sketch_file=$(gum file --height 6) 152 timer 0.5s 153 echo "Choose your preferred editor." 154 echo "Note that these editors are already installed in your system" 155 156 timer 1s 157 158 for editor in "${editors[@]}"; do 159 if command -v $editor &>/dev/null; then 160 installed_editors+=("$editor") 161 fi 162 done 163 164 chosen_editor=$(printf "%s\n" "${installed_editors[@]}" | gum choose) 165 timer 0.5 166 167 "$chosen_editor" "$sketch_file" 168 169 gum confirm "Check file contents" && gum pager <"$sketch_file" || main 170 171 } 172 173 create_new_sketch() { 174 arduino-cli sketch new $1 175 echo "New Sketch Created" 176 echo "At path ${PWD}" | gum style --foreground 47 177 178 timer 2s 179 clear 180 main 181 } 182 183 upload_code() { 184 #local file_u=$(gum file --height 5) 185 echo "Select Serial port to which the board is connected" 186 SERIAL_PORT=$(arduino-cli board list | awk '/\/dev\/tty/ {print $1}' | gum choose) 187 echo "Is your bootloader old (mostly for Nano) or the latest one" 188 local booltoader_old_new=$(gum choose "Old Bootloader" "New Bootloader") 189 190 case $booltoader_old_new in 191 "New Bootloader") 192 arduino-cli upload --fqbn $FQBN_SELECTED -p $SERIAL_PORT $sketch_file 193 ;; 194 "Old Bootloader") 195 arduino-cli upload --fqbn "${FQBN_SELECTED}:cpu=atmega328old" -p $SERIAL_PORT $sketch_file 196 ;; 197 *) 198 echo "Invalid Option" 199 ;; 200 esac 201 echo "Uploaded Sketch" $sketch_file 202 gum style --foreground 47 $sketch_file 203 204 timer 2.5s 205 clear 206 main 207 } 208 209 compile_code() { 210 echo "Select file to be compiled" 211 #local file_c=$(gum file --height 5) 212 gum spin --spinner moon --title "Compiling for $BOARD_NAME" -- arduino-cli compile --fqbn $FQBN_SELECTED $sketch_file -v 213 echo "Compiled Sketch at path" 214 gum style --foreground 47 $sketch_file 215 216 gum confirm "Return to Home?" && main || exit 217 } 218 219 list_installed_boards() { 220 local boards=$(arduino-cli board listall) 221 local selected_line=$(echo "$boards" | gum filter --placeholder "Select a board") 222 223 BOARD_NAME=$(echo "$selected_line" | awk '{print substr($0, 1, index($0, $NF)-1)}' | xargs) 224 FQBN_SELECTED=$(echo "$selected_line" | awk '{print $NF}') 225 226 echo "Board Name:" | gum style --foreground 46 227 echo "$BOARD_NAME" | gum style --foreground 47 228 echo "FQBN:" | gum style --foreground 46 229 echo "$FQBN_SELECTED" | gum style --foreground 47 230 231 confirm_board_selection 232 233 timer 2s 234 clear 235 main 236 } 237 238 confirm_board_selection() { 239 gum confirm "Current Selected Board : $BOARD_NAME" && main || list_installed_boards 240 } 241 242 install_libraries() { 243 local lib_name=$(gum input --placeholder "Enter Library Name to install") 244 local lib_chosen=$(arduino-cli lib search $lib_name | awk ' 245 /Name/ {name=$2} 246 /Author/ { 247 author=""; 248 for(i=2; i<=NF; i++){ 249 if($i ~ /</) 250 break; 251 author=author " " $i; 252 } 253 print name, " : ", author; 254 } 255 ' | gum choose) 256 257 local lib_to_install=$(echo "$lib_chosen" | awk -F' : ' '{print $1}' | tr -d '"') 258 arduino-cli lib install $lib_to_install 259 timer 2s 260 main 261 } 262 263 main() { 264 clear 265 266 check_for_updates 267 local intro="Welcome to arduino-cli-interactive" 268 echo "$intro" | gum style --foreground 47 --border-foreground 217 --border double --align center --width 50 269 echo "You have chosen the board : $BOARD_NAME 270 with FQBN : $FQBN_SELECTED 271 Sketch file : $sketch_file 272 Serial Port : $SERIAL_PORT" 273 274 timer 1s 275 local choice=$(gum choose --height 12 "Select Board" "Create New Sketch" "Edit the Sketch" \ 276 "Compile Code" "Upload Code" "Serial Monitor" "Install Libraries" "Display Installed Libraries" \ 277 "View Examples" "Edit Configurations" "Exit") 278 279 case $choice in 280 "Create New Sketch") 281 local sketch_name=$(gum input --placeholder "Enter Name of Sketch") 282 create_new_sketch $sketch_name 283 ;; 284 "Edit the Sketch") 285 edit_sketch 286 ;; 287 288 "Edit Configurations") 289 edit_config_file 290 ;; 291 "Compile Code") 292 compile_code 293 ;; 294 "Upload Code") 295 upload_code 296 ;; 297 "Serial Monitor") 298 serial_monitor 299 ;; 300 "Install Libraries") 301 install_libraries 302 ;; 303 "Display Installed Libraries") 304 list_libraries 305 ;; 306 "View Examples") 307 lib_examples 308 ;; 309 "Select Board") 310 list_installed_boards 311 ;; 312 "Exit") 313 echo "See you again !!!" | gum style --foreground 47 314 exit 315 ;; 316 *) 317 echo "Unknown option" 318 ;; 319 esac 320 } 321 322 main