@@ -19,38 +19,90 @@ LIBDIR="lib"
1919# File "<stdin>", line 2, in <module>
2020# OSError: [Errno 17] EEXIST
2121
22- echo " Creating $LIBDIR on board"
23- mpremote mkdir " ${LIBDIR} "
24- echo " Creating $LIBDIR /$PKGDIR on board"
25- mpremote mkdir " /${LIBDIR} /${PKGDIR} "
22+ # Check if a directory exists
23+ # Returns 0 if directory exists, 1 if it does not
24+ function directory_exists {
25+ # Run mpremote and capture the error message
26+ error=$( mpremote fs ls $1 )
27+
28+ # Return error if error message contains "OSError: [Errno 2] ENOENT"
29+ if [[ $error == * " OSError: [Errno 2] ENOENT" * ]]; then
30+ return 1
31+ else
32+ return 0
33+ fi
34+ }
35+
36+ # Copies a file to the board using mpremote
37+ # Only produces output if an error occurs
38+ function copy_file {
39+ echo " Copying $1 to $2 "
40+ # Run mpremote and capture the error message
41+ error=$( mpremote cp $1 $2 )
42+
43+ # Print error message if return code is not 0
44+ if [ $? -ne 0 ]; then
45+ echo " Error: $error "
46+ fi
47+ }
48+
49+ # Deletes a file from the board using mpremote
50+ # Only produces output if an error occurs
51+ function delete_file {
52+ echo " Deleting $1 "
53+ # Run mpremote and capture the error message
54+ error=$( mpremote rm $1 )
55+
56+ # Print error message if return code is not 0
57+ if [ $? -ne 0 ]; then
58+ echo " Error: $error "
59+ fi
60+ }
61+
62+ echo " Installing Arduino Runtime for MicroPython"
63+
64+ # If directories do not exist, create them
65+ if ! directory_exists " /${LIBDIR} " ; then
66+ echo " Creating $LIBDIR on board"
67+ mpremote mkdir " ${LIBDIR} "
68+ fi
69+
70+ if ! directory_exists " /${LIBDIR} /${PKGDIR} " ; then
71+ echo " Creating $LIBDIR /$PKGDIR on board"
72+ mpremote mkdir " /${LIBDIR} /${PKGDIR} "
73+ fi
74+
2675
2776ext=" py"
2877if [ " $1 " = " mpy" ]; then
2978 ext=$1
30- echo " * * *"
3179 echo " .py files will be compiled to .mpy"
32- echo " * * *"
3380fi
3481
82+ existing_files=$( mpremote fs ls " :/${LIBDIR} /${PKGDIR} " )
83+
3584for py in $SRCDIR /* ; do
3685 f_name=` basename $py `
3786 if [ " $ext " = " mpy" ]; then
3887 echo " Compiling $SRCDIR /$f_name to $SRCDIR /${f_name% .* } .$ext "
3988 mpy-cross " $SRCDIR /$f_name "
4089 fi
4190
42- echo " Deleting files from board"
43- mpremote rm " :/${LIBDIR} /$PKGDIR /${f_name% .* } .py"
44- mpremote rm " :/${LIBDIR} /$PKGDIR /${f_name% .* } .mpy"
91+ if [[ $existing_files == * " ${f_name% .* } .py" * ]]; then
92+ delete_file " :/${LIBDIR} /$PKGDIR /${f_name% .* } .py"
93+ fi
94+
95+ if [[ $existing_files == * " ${f_name% .* } .mpy" * ]]; then
96+ delete_file " :/${LIBDIR} /$PKGDIR /${f_name% .* } .mpy"
97+ fi
4598
46- echo " Copying $SRCDIR /${f_name% .* } .$ext to :/${LIBDIR} /$PKGDIR /${f_name% .* } .$ext "
47- mpremote cp $SRCDIR /${f_name% .* } .$ext " :/${LIBDIR} /$PKGDIR /${f_name% .* } .$ext "
99+ copy_file $SRCDIR /${f_name% .* } .$ext " :/${LIBDIR} /$PKGDIR /${f_name% .* } .$ext "
48100done
49101
50102if [ " $ext " = " mpy" ]; then
51103 echo " cleaning up mpy files"
52104 rm $SRCDIR /* .mpy
53105fi
54106
55- echo " resetting target board"
107+ echo " Done. Resetting target board ... "
56108mpremote reset
0 commit comments