Skip to content

Commit 55bc9c9

Browse files
authored
Merge pull request #8 from arduino/dev
Dev
2 parents 34803f7 + ba921e0 commit 55bc9c9

File tree

8 files changed

+134
-46
lines changed

8 files changed

+134
-46
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.DS_Store
2+
*.mpy

README.md

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,18 @@ analog_write('A3', 64)
7474
Will generate a modulated signal on the specified Pin.
7575
Can be used to control small motors with low current needs as well as servo motors.
7676

77-
#### IMPORTANT
78-
79-
The numeric value for PIN_NUMBER is usually the processor's GPIO number, while values enclosed in quotes are "named pins" and are platform/implementation specific, not guaranteed to be valid.
80-
A `ValueError` exception with label "invalid pin" is thrown if the pin number or ID is not valid.
77+
> [!IMPORTANT]
78+
> The numeric value for PIN_NUMBER is usually the processor's GPIO number, while values enclosed in quotes are "named pins" and are platform/implementation specific, not guaranteed to be valid. A `ValueError` exception with label "invalid pin" is thrown if the pin number or ID is not valid.
8179
8280
### delay(MILLISECONDS)
8381

8482
Will halt the execution of your program for the amount of _milliseconds_ specified in the parameter.
8583
It is to be considered a code-blocking command.
8684

85+
```Python
86+
delay(1000) # Delay the execution for 1 second
87+
```
88+
8789
## Usage
8890

8991
The structure of an Arduino MicroPython program will look as follows:
@@ -137,7 +139,7 @@ Is run indefinitely until the program stops.
137139

138140
### cleanup()
139141

140-
Is run _once_ when the program stops.
142+
Is run _once_ when the program stops. This happen either when the user manually stops the execution of the program or if an error in the user code is thrown.
141143
It should contain code such as resetting the value of variables, stopping timers, causing threads to stop running.
142144

143145
A `cleanup()` enchanced version of our initial program would look like this
@@ -158,6 +160,9 @@ def cleanup():
158160
start(setup, loop)
159161
```
160162

163+
> [!NOTE]
164+
> `cleanup()` does not get called when the program stops because the hardware button on the board was pressed.
165+
161166
## Utilities
162167

163168
Some utility methods are provided and are still in development:
@@ -175,9 +180,11 @@ Some utility methods are provided and are still in development:
175180

176181
## Convenience and scaffolding methods
177182

178-
### create_sketch(SKETCH_NAME, PATH)
183+
### create_sketch(sketch_name = None, destination_path = '.', overwrite = False, source = None)
179184

180185
Will create a new Python file (`.py`) with the specified name at the provided path.
186+
By default if a file with the same name is found, it will append a timestamp, but overwrite can be forced to True.
187+
Providing a source path it will use that file's content, effectively copying the code from one file to the newly created one.
181188
Example:
182189

183190
```Python
@@ -186,5 +193,8 @@ create_sketch('my_arduino_sketch', 'tmp')
186193
create_sketch('main')
187194
```
188195

189-
If the destination `.py` file exists, a timestamp in _microseconds_ will be appended to the name.
190196
The method returns the Python file's full path.
197+
198+
### dcopy_sketch(source_path = '', destination_path = '.', name = None, overwrite = False):
199+
200+
Wraps create_sketch() and provides a shortcut to copy a file onto another file

arduino/arduino.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,26 @@ def delay(_ms):
8282

8383

8484
# HELPERS
85-
def create_sketch(sketch_name = None, path = '.', overwrite = False):
85+
86+
def get_template():
87+
return '/'.join(__file__.split('/')[:-1]) + '/template.tpl'
88+
89+
90+
def create_sketch(sketch_name = None, destination_path = '.', overwrite = False, source_path = None):
8691

8792
if sketch_name is None:
8893
sketch_name = 'main'
89-
new_sketch_path = f'{path}/{sketch_name}.py'
94+
new_sketch_path = f'{destination_path}/{sketch_name}.py'
9095
try:
9196
open(new_sketch_path, 'r')
9297
if not overwrite:
9398
sketch_name = f'{sketch_name}_{ticks_us()}'
9499
except OSError:
95100
pass
96101

97-
template_path = '/'.join(__file__.split('/')[:-1]) + '/template.py'
102+
template_path = get_template() if source is None else source
98103
template_sketch = open(template_path, 'r')
99-
new_sketch_path = f'{path}/{sketch_name}.py'
104+
new_sketch_path = f'{destination_path}/{sketch_name}.py'
100105

101106
with open(new_sketch_path, 'w') as f:
102107
sketch_line = None
@@ -107,6 +112,10 @@ def create_sketch(sketch_name = None, path = '.', overwrite = False):
107112
template_sketch.close()
108113
return new_sketch_path
109114

115+
def copy_sketch(source_path = '', destination_path = '.', name = None, overwrite = False):
116+
name = name or 'main'
117+
return create_sketch(sketch_name = name, destination_path = destination_path, overwrite = overwrite, source_path = source_path)
118+
110119
# the following methods are just for testing
111120
# will produce output when this module is run as __main__
112121
def preload():

arduino/template.py

Lines changed: 0 additions & 11 deletions
This file was deleted.

arduino/template.tpl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Code generated by Arduino Runtime for MicroPython
2+
from arduino import *
3+
4+
def setup():
5+
# your initialization code goes here
6+
# this code only runs once at start
7+
print('setup')
8+
9+
def loop():
10+
# code that needs to run repeatedly goes here
11+
print('loop')
12+
delay(1000)
13+
14+
def cleanup():
15+
# code to cleanup or reset things when the program stops goes here
16+
print('cleanup')
17+
18+
# start your program
19+
start(setup, loop, cleanup)

examples/arduino_basic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ def setup():
1717

1818
def loop():
1919
print('loopy loop')
20-
digitalWrite(led, HIGH)
20+
digital_write(led, HIGH)
2121
delay(500)
22-
digitalWrite(led, LOW)
22+
digital_write(led, LOW)
2323
delay(500)
2424

2525
start(setup, loop)

examples/nano_esp32_advanced.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ def blink_me(_timer):
3434
print('blink')
3535
led_builtin_state = not led_builtin_state
3636
if led_builtin_state:
37-
digitalWrite(led_builtin, HIGH)
37+
digital_write(led_builtin, HIGH)
3838
else:
39-
digitalWrite(led_builtin, LOW)
39+
digital_write(led_builtin, LOW)
4040

4141
def timed_message(_timer):
4242
print('hello')
@@ -47,16 +47,16 @@ def setup():
4747
message_timer.init(period = 2000, mode = Timer.PERIODIC, callback = timed_message)
4848

4949
def loop():
50-
if digitalRead(button):
51-
digitalWrite(led_red, LOW)
50+
if digital_read(button):
51+
digital_write(led_red, LOW)
5252
else:
53-
digitalWrite(led_red, HIGH)
53+
digital_write(led_red, HIGH)
5454

5555
def cleanup():
5656
print("*** cleaning up ***")
5757
blink_timer.deinit()
5858
message_timer.deinit()
59-
digitalWrite(led_builtin, LOW)
60-
digitalWrite(led_red, HIGH)
59+
digital_write(led_builtin, LOW)
60+
digital_write(led_red, HIGH)
6161

6262
start(setup, loop, cleanup)

install.sh

Lines changed: 74 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2776
ext="py"
2877
if [ "$1" = "mpy" ]; then
2978
ext=$1
30-
echo "* * *"
3179
echo ".py files will be compiled to .mpy"
32-
echo "* * *"
3380
fi
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_extension" == "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"
48107
done
49108

50109
if [ "$ext" = "mpy" ]; then
51110
echo "cleaning up mpy files"
52111
rm $SRCDIR/*.mpy
53112
fi
54113

55-
echo "resetting target board"
114+
echo "Done. Resetting target board ..."
56115
mpremote reset

0 commit comments

Comments
 (0)