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