Skip to content

Commit 997496d

Browse files
authored
Merge pull request #10 from arduino/dev
merge dev to main for public release
2 parents 5029940 + f1d9916 commit 997496d

File tree

8 files changed

+250
-201
lines changed

8 files changed

+250
-201
lines changed

README.md

+161-108
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,66 @@
11
# Arduino Runtime for MicroPython
22

3-
A module to simplify and help writing MicroPython programs using the setup()/loop() paradigm.
3+
A module to simplify and help writing MicroPython programs using the `setup()`/`loop()` paradigm.
4+
5+
This module also wraps machine functions in easy-to-use methods.
6+
7+
## Installation
8+
9+
### mip (MicroPython Package Manager)
10+
This is the recommended method for boards which can connect to Internet.
11+
Run the following MicroPython script using your favourite editor:
12+
13+
```py
14+
import network
15+
import mip
16+
import time
17+
18+
SSID = 'YOUR WIFI NETWORK NAME (must be 2.4GHz)'
19+
PWD = 'YOUR WIFI PASSWORD'
20+
21+
interface = network.WLAN(network.STA_IF)
22+
interface.active(False)
23+
time.sleep(0.1)
24+
interface.active(True)
25+
26+
def connect(ssid, pwd):
27+
interface.connect(ssid, pwd)
28+
# Network Connection Progress
29+
max_dot_cols = 20
30+
dot_col = 0
31+
print()
32+
print(f"Connecting to {ssid}")
33+
while not interface.isconnected():
34+
if(dot_col % max_dot_cols == 0):
35+
print()
36+
print('.', end = '')
37+
dot_col +=1
38+
time.sleep_ms(100)
39+
print()
40+
print(f'{"C" if interface.isconnected() else "NOT c"}onnected to network')
41+
42+
connect(SSID, PWD)
43+
mip.install('github:arduino/arduino-runtime-mpy')
444

5-
## Commands
6-
7-
This module also wraps machine functions in easy-to-use methods
8-
9-
### pin_mode(PIN_NUMBER/ID, MODE)
10-
11-
This method is only provided as a mean to transition from Arduino C++ to MicroPython, but is redundant and unnecessary.
12-
Might be still used with no harm to slowly drop the habit.
13-
The following methods apply the required Pin direction on the fly.
14-
15-
```Python
16-
pin_mode(3, INPUT)
17-
pin_mode('D7', INPUT)
18-
```
19-
20-
Will set the direction of the specified Pin and allow writing to or reading from the physical pin.
21-
22-
### digital_read(PIN_NUMBER/ID)
23-
24-
Reads a digital value from the pin with Number or ID specified.
25-
For example:
26-
27-
```Python
28-
digital_read(12)
29-
digital_read('D7')
30-
digital_read('A3')
31-
```
32-
33-
return a value of `1` or `0` depending on the signal attached to the specified pins, for instance a button or a digital sensor.
34-
35-
### digital_write(PIN_NUMBER/ID, VALUE)
36-
37-
Writes the digital value (`HIGH|LOW|True|False|1|0`) to the pin with Number or ID specified.
38-
For example:
39-
40-
```Python
41-
digital_write(12, HIGH)
42-
digital_write('D7', 1)
43-
digital_write('A3', 0)
4445
```
46+
### mpremote mip
47+
You will need to have Python and `mpremote` installed on your system, [follow these instructions](https://docs.micropython.org/en/latest/reference/mpremote.html) to do so.
4548

46-
Will set the pin to the specified value.
49+
Open a shell and run the following command:
4750

48-
### analog_read(PIN_NUMBER/ID)
49-
50-
Reads the analog value for the Voltage applied to the pinpin with Number or ID specified.
51-
For example:
52-
53-
```Python
54-
analog_read('A3')
55-
analog_read('D18')
56-
analog_read('2')
51+
```shell
52+
mpremote mip install "github:arduino/arduino-runtime-mpy"
5753
```
5854

59-
return a value between `0` and the maximum allowed by the processor's ADC based on the Voltage applied to the specified Pin.
60-
Could be used to read the Voltage of a battery or any other analog source such as a potentiometer, light or moisture sensor to name a few.
61-
62-
### analog_write(PIN_NUMBER/ID, VALUE)
63-
64-
Writes an analog value (PWM) to the pin with Number or ID specified, if the Pin supports it.
65-
VALUE should be between 0 and the maximum allowed PWM value and it's highly platform specific.
66-
The method makes a conversion betwen the number and `frequency`/`duty_cycle`.
55+
### Manual Installation
56+
Copy the folder `arduino` and its content into your board's `lib` folder using your preferred method.
6757

68-
```Python
69-
analog_write(12, 255)
70-
analog_write('D7', 128)
71-
analog_write('A3', 64)
72-
```
73-
74-
Will generate a modulated signal on the specified Pin.
75-
Can be used to control small motors with low current needs as well as servo motors.
76-
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.
79-
80-
### delay(MILLISECONDS)
81-
82-
Will halt the execution of your program for the amount of _milliseconds_ specified in the parameter.
83-
It is to be considered a code-blocking command.
84-
85-
```Python
86-
delay(1000) # Delay the execution for 1 second
87-
```
8858

8959
## Usage
9060

91-
The structure of an Arduino MicroPython program will look as follows:
61+
The structure of a MicroPython program based on the Arduino Runtime for MicroPython will look as follows:
9262

93-
```Python
63+
```py
9464
from arduino import *
9565

9666
def setup():
@@ -105,46 +75,46 @@ start(setup, loop)
10575

10676
The program above will define two main methods: `setup()` and `loop()`.
10777
`setup()` will be invoked once at the execution of the program, while `loop()` will be invoked over and over until the program is stopped.
108-
The stop condition might be caused by a system error or by manual trigger from the user during development/test.
78+
The stop condition might be caused by a system error or by manual trigger from the user during development/testing.
79+
The `start()` command is what causes these functions to be run as described above.
10980

110-
The `start()` command is what causes the program to run, and is to be considered of high value in the MicroPython world.
111-
While traditionally the code above would be written as follows
81+
Traditionally the example code above would be written as follows in MicroPython:
11282

113-
```Python
83+
```py
11484
from time import sleep_ms
11585

116-
print('starting my program)
86+
print('starting my program')
11787

11888
while True:
11989
print('loop')
12090
sleep_ms(1000)
12191
```
12292

123-
Using the Arduino Runtime for MicroPython introduces some nice features, like the possibility to wrap user code in functions which can be tested during learning/development using the REPL.
124-
Running the Arduino formatted code, omitting the `start()` command, would create the functions and every variable or object in the MicroPython board and allow the user to simply change a variable, set the property of an object and simply call `loop()` to see the results of their changes.
125-
A more interactive approach to learning/testing/debugging.
93+
Using the Arduino Runtime for MicroPython introduces some nice features, like the possibility to wrap user code in functions which can be tested during learning/development using the [REPL](https://docs.micropython.org/en/latest/reference/repl.html).
12694

127-
We also introduce a new way of cleaning up and or resetting variables, objects, timers, leveraging a `cleanup()` method which will be called when the program is stopped or a system error happens which stops the execution of the program.
128-
Please refer to the example "nano_esp32_advanced.py".
95+
To debug your application interactively you can use an approach in which you define `setup()` and `loop()` but omit the `start()` command. That way you can change variables or set the property of an object through the REPL and simply call `loop()` to see the results of their changes without "restarting" the complete script.
12996

130-
This brings the implemented runtime commands to the three described below
97+
We also introduced a new way of cleaning up and or resetting variables, objects, timers, leveraging a `cleanup()` method which will be called when the program is stopped or a system error occurs which stops the execution of the program.
98+
Please refer to the example ["nano_esp32_advanced.py"](./examples/02_nano_esp32_advanced.py).
99+
100+
The runtime commands used in the example are explained in more detail below.
131101

132102
### setup()
133103

134-
Is run _once_ and should contain initialisation code.
104+
Is run **once** and should contain initialisation code.
135105

136106
### loop()
137107

138108
Is run indefinitely until the program stops.
139109

140110
### cleanup()
141111

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.
143-
It should contain code such as resetting the value of variables, stopping timers, causing threads to stop running.
112+
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.
113+
It should contain code such as resetting the value of variables, resetting peripherals, stopping timers, causing threads to stop running.
144114

145-
A `cleanup()` enchanced version of our initial program would look like this
115+
A version of our initial program that leverages the `cleanup()` function would look like this:
146116

147-
```Python
117+
```py
148118
from arduino import *
149119

150120
def setup():
@@ -157,37 +127,120 @@ def loop():
157127
def cleanup():
158128
print('cleanup')
159129

160-
start(setup, loop)
130+
start(setup, loop, cleanup)
161131
```
162132

163133
> [!NOTE]
164-
> `cleanup()` does not get called when the program stops because the hardware button on the board was pressed.
134+
> `cleanup()` is not invoked when the program stops because the hardware reset button on the board was pressed.
135+
136+
## Commands
137+
138+
Here's a list of commands and wrappers that can be used in your Arduino MicroPython program.
139+
140+
### pin_mode(PIN_NUMBER/ID, MODE)
141+
142+
This method is only provided as a mean to transition from Arduino C++ to MicroPython. In MicroPython a Pin object can be used directly instead.
143+
The following methods apply the required Pin direction on the fly.
144+
145+
```py
146+
pin_mode(3, INPUT)
147+
pin_mode('D7', INPUT)
148+
```
149+
150+
Will set the direction of the specified Pin and allow writing to or reading from the physical pin.
151+
152+
### digital_read(PIN_NUMBER/ID)
153+
154+
Reads a digital value from the pin with the specified number or ID.
155+
For example:
156+
157+
```py
158+
digital_read(12)
159+
digital_read('D7')
160+
digital_read('A3')
161+
```
162+
163+
returns a value of `1` or `0` depending on the signal attached to the specified pins, for instance a button or a digital sensor.
164+
165+
### digital_write(PIN_NUMBER/ID, VALUE)
166+
167+
Writes the digital value (`HIGH|LOW|True|False|1|0`) to the pin with Number or ID specified.
168+
For example:
169+
170+
```py
171+
digital_write(12, HIGH)
172+
digital_write('D7', 1)
173+
digital_write('A3', 0)
174+
```
175+
176+
Sets the pin to the specified value.
177+
178+
### analog_read(PIN_NUMBER/ID)
179+
180+
Reads the analog value for the Voltage applied to the pin with the specified number or ID.
181+
For example:
182+
183+
```py
184+
analog_read('A3')
185+
analog_read('D18')
186+
analog_read('2')
187+
```
188+
189+
returns a value between `0` and the maximum resolution of the processor's ADC based on the Voltage applied to the specified Pin.
190+
Could be used to read the Voltage of a battery or any other analog source such as a potentiometer, light or moisture sensor to name a few.
191+
192+
### analog_write(PIN_NUMBER/ID, VALUE)
193+
194+
Writes an analog value (PWM) to the pin with Number or ID specified, if the Pin supports it.
195+
VALUE should be between 0 and the maximum allowed PWM value 255.
196+
197+
```py
198+
analog_write(12, 255)
199+
analog_write('D7', 128)
200+
analog_write('A3', 64)
201+
```
202+
203+
Will generate a pulse width modulated signal on the specified Pin.
204+
Can be used to control small motors with low current needs, servo motors or an LED's brightness.
205+
206+
> [!IMPORTANT]
207+
> 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. A `ValueError` exception with label "invalid pin" is thrown if the pin number or ID is not valid.
208+
209+
### delay(MILLISECONDS)
210+
211+
Will halt the execution of your program for the amount of **milliseconds** specified in the parameter.
212+
It is to be considered a code-blocking command.
213+
214+
```py
215+
delay(1000) # Delay the execution for 1 second
216+
```
165217

166218
## Utilities
167219

168220
Some utility methods are provided and are still in development:
169221

170222
* `map(x, in_min, in_max, out_min, out_max)`
171-
will remap the value `x` from its input range to an output range
223+
Remaps the value `x` from its input range to an output range
172224
* `mapi(x, in_min, in_max, out_min, out_max)`
173225
same as `map` but always returns an integer
174226
* `random(low, high=None)`
175-
will return a random number between `0` and `low` - 1 if no `high` is provide, otherwise a value between `low` and `high` - 1
227+
Returns a random number between `0` and `low` - 1 if no `high` is provided, otherwise a value between `low` and `high` - 1
176228
* `constrain(val, min_val, max_val)`
177-
will return a capped value of the number `val` between `min_val` and `max_val`
229+
Returns a capped value of the number `val` between `min_val` and `max_val`
178230
* `lerp(start, stop, amount)`
179-
will return a linear interpolation (percentage) of `amount` between `start` and `stop`
231+
Returns a linear interpolation (percentage) of `amount` between `start` and `stop`
180232

181233
## Convenience and scaffolding methods
182234

183235
### create_sketch(sketch_name = None, destination_path = '.', overwrite = False, source = None)
184236

185-
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.
188-
Example:
237+
Creates a new Python file (`.py`) with the specified name at the provided path.
238+
By default if a file with the same name is found, it will append a timestamp to the filename, but overwrite can be forced by setting that parameter to True.
239+
Providing a source path it will use that file's content, effectively copying the code from the source file to the newly created one.
240+
241+
Examples:
189242

190-
```Python
243+
```py
191244
create_sketch('my_arduino_sketch')
192245
create_sketch('my_arduino_sketch', 'tmp')
193246
create_sketch('main')
@@ -197,4 +250,4 @@ The method returns the Python file's full path.
197250

198251
### copy_sketch(source_path = '', destination_path = '.', name = None, overwrite = False):
199252

200-
Wraps create_sketch() and provides a shortcut to copy a file onto another file
253+
Wraps `create_sketch()` and provides a shortcut to copy a file to another path.

arduino/__init__.py

+5
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1+
__author__ = "ubi de feo / murilo polese"
2+
__license__ = "MPL 2.0"
3+
__version__ = "0.1.0"
4+
__maintainer__ = "Arduino"
5+
16
from .arduino import *

0 commit comments

Comments
 (0)