@@ -19,38 +19,97 @@ 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
35- for py in $SRCDIR /* ; do
36- f_name=` basename $py `
37- if [ " $ext " = " mpy" ]; then
82+ existing_files=$( mpremote fs ls " :/${LIBDIR} /${PKGDIR} " )
83+
84+ for filename in $SRCDIR /* ; do
85+ f_name=` basename $filename `
86+ source_extension=" ${f_name##* .} "
87+ destination_extension=$source_extension
88+
89+ if [ " $ext " = " mpy" ] && [ " $source_extention " = " py" ]; then
3890 echo " Compiling $SRCDIR /$f_name to $SRCDIR /${f_name% .* } .$ext "
3991 mpy-cross " $SRCDIR /$f_name "
92+ destination_extension=$ext
4093 fi
4194
42- echo " Deleting files from board"
43- mpremote rm " :/${LIBDIR} /$PKGDIR /${f_name% .* } .py"
44- mpremote rm " :/${LIBDIR} /$PKGDIR /${f_name% .* } .mpy"
95+ # Make sure previous versions of the given file are deleted.
96+ if [[ $existing_files == * " ${f_name% .* } .$source_extension " * ]]; then
97+ delete_file " :/${LIBDIR} /$PKGDIR /${f_name% .* } .$source_extension "
98+ fi
99+
100+ # Check if source file has a .py extension and if a .mpy file exists on the board
101+ if [ " $source_extension " = " py" ] && [[ $existing_files == * " ${f_name% .* } .mpy" * ]]; then
102+ delete_file " :/${LIBDIR} /$PKGDIR /${f_name% .* } .mpy"
103+ fi
45104
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 "
105+ # Copy either the .py or .mpy file to the board depending on the chosen option
106+ copy_file $SRCDIR /${f_name% .* } .$destination_extension " :/${LIBDIR} /$PKGDIR /${f_name% .* } .$destination_extension "
48107done
49108
50109if [ " $ext " = " mpy" ]; then
51110 echo " cleaning up mpy files"
52111 rm $SRCDIR /* .mpy
53112fi
54113
55- echo " resetting target board"
114+ echo " Done. Resetting target board ... "
56115mpremote reset
0 commit comments