Skip to content

Commit f1d9916

Browse files
authored
Merge pull request #11 from arduino/tweaks
Various tweaks
2 parents 2274448 + c592303 commit f1d9916

File tree

4 files changed

+84
-108
lines changed

4 files changed

+84
-108
lines changed

Diff for: README.md

+55-57
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
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.
44

5-
This module also wraps machine functions in easy-to-use methods
5+
This module also wraps machine functions in easy-to-use methods.
66

7-
## Install
7+
## Installation
88

99
### mip (MicroPython Package Manager)
1010
This is the recommended method for boards which can connect to Internet.
11-
Run the following MicroPython script using your favourite editor
11+
Run the following MicroPython script using your favourite editor:
1212

13-
```Python
13+
```py
1414
import network
1515
import mip
1616
import time
@@ -44,23 +44,23 @@ mip.install('github:arduino/arduino-runtime-mpy')
4444

4545
```
4646
### 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).
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.
48+
4849
Open a shell and run the following command:
4950

5051
```shell
5152
mpremote mip install "github:arduino/arduino-runtime-mpy"
5253
```
5354

54-
### Manually
55-
Copy the folder `arduino` and its content into your board's `lib` folder using your preferred method
56-
55+
### Manual Installation
56+
Copy the folder `arduino` and its content into your board's `lib` folder using your preferred method.
5757

5858

5959
## Usage
6060

61-
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:
6262

63-
```Python
63+
```py
6464
from arduino import *
6565

6666
def setup():
@@ -75,46 +75,46 @@ start(setup, loop)
7575

7676
The program above will define two main methods: `setup()` and `loop()`.
7777
`setup()` will be invoked once at the execution of the program, while `loop()` will be invoked over and over until the program is stopped.
78-
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.
7980

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

83-
```Python
83+
```py
8484
from time import sleep_ms
8585

86-
print('starting my program)
86+
print('starting my program')
8787

8888
while True:
8989
print('loop')
9090
sleep_ms(1000)
9191
```
9292

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.
94-
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.
95-
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).
94+
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.
9696

97-
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.
98-
Please refer to the example "nano_esp32_advanced.py".
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).
9999

100-
This brings the implemented runtime commands to the three described below
100+
The runtime commands used in the example are explained in more detail below.
101101

102102
### setup()
103103

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

106106
### loop()
107107

108108
Is run indefinitely until the program stops.
109109

110110
### cleanup()
111111

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, 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.
114114

115-
A `cleanup()` enhanced 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:
116116

117-
```Python
117+
```py
118118
from arduino import *
119119

120120
def setup():
@@ -133,18 +133,16 @@ start(setup, loop, cleanup)
133133
> [!NOTE]
134134
> `cleanup()` is not invoked when the program stops because the hardware reset button on the board was pressed.
135135
136-
137136
## Commands
138137

139138
Here's a list of commands and wrappers that can be used in your Arduino MicroPython program.
140139

141140
### pin_mode(PIN_NUMBER/ID, MODE)
142141

143-
This method is only provided as a mean to transition from Arduino C++ to MicroPython, but is redundant and unnecessary.
144-
Might be still used with no harm to slowly drop the habit.
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.
145143
The following methods apply the required Pin direction on the fly.
146144

147-
```Python
145+
```py
148146
pin_mode(3, INPUT)
149147
pin_mode('D7', INPUT)
150148
```
@@ -153,68 +151,67 @@ Will set the direction of the specified Pin and allow writing to or reading from
153151

154152
### digital_read(PIN_NUMBER/ID)
155153

156-
Reads a digital value from the pin with Number or ID specified.
154+
Reads a digital value from the pin with the specified number or ID.
157155
For example:
158156

159-
```Python
157+
```py
160158
digital_read(12)
161159
digital_read('D7')
162160
digital_read('A3')
163161
```
164162

165-
return a value of `1` or `0` depending on the signal attached to the specified pins, for instance a button or a digital sensor.
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.
166164

167165
### digital_write(PIN_NUMBER/ID, VALUE)
168166

169167
Writes the digital value (`HIGH|LOW|True|False|1|0`) to the pin with Number or ID specified.
170168
For example:
171169

172-
```Python
170+
```py
173171
digital_write(12, HIGH)
174172
digital_write('D7', 1)
175173
digital_write('A3', 0)
176174
```
177175

178-
Will set the pin to the specified value.
176+
Sets the pin to the specified value.
179177

180178
### analog_read(PIN_NUMBER/ID)
181179

182-
Reads the analog value for the Voltage applied to the pinpin with Number or ID specified.
180+
Reads the analog value for the Voltage applied to the pin with the specified number or ID.
183181
For example:
184182

185-
```Python
183+
```py
186184
analog_read('A3')
187185
analog_read('D18')
188186
analog_read('2')
189187
```
190188

191-
return a value between `0` and the maximum allowed by the processor's ADC based on the Voltage applied to the specified Pin.
189+
returns a value between `0` and the maximum resolution of the processor's ADC based on the Voltage applied to the specified Pin.
192190
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.
193191

194192
### analog_write(PIN_NUMBER/ID, VALUE)
195193

196194
Writes an analog value (PWM) to the pin with Number or ID specified, if the Pin supports it.
197-
VALUE should be between 0 and the maximum allowed PWM value and it's highly platform specific.
198-
The method makes a conversion betwen the number and `frequency`/`duty_cycle`.
195+
VALUE should be between 0 and the maximum allowed PWM value 255.
199196

200-
```Python
197+
```py
201198
analog_write(12, 255)
202199
analog_write('D7', 128)
203200
analog_write('A3', 64)
204201
```
205202

206-
Will generate a modulated signal on the specified Pin.
207-
Can be used to control small motors with low current needs as well as servo motors.
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.
208205

209206
> [!IMPORTANT]
210-
> 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.
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.
211208
212209
### delay(MILLISECONDS)
213210

214-
Will halt the execution of your program for the amount of _milliseconds_ specified in the parameter.
211+
Will halt the execution of your program for the amount of **milliseconds** specified in the parameter.
215212
It is to be considered a code-blocking command.
216213

217-
```Python
214+
```py
218215
delay(1000) # Delay the execution for 1 second
219216
```
220217

@@ -223,26 +220,27 @@ delay(1000) # Delay the execution for 1 second
223220
Some utility methods are provided and are still in development:
224221

225222
* `map(x, in_min, in_max, out_min, out_max)`
226-
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
227224
* `mapi(x, in_min, in_max, out_min, out_max)`
228225
same as `map` but always returns an integer
229226
* `random(low, high=None)`
230-
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
231228
* `constrain(val, min_val, max_val)`
232-
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`
233230
* `lerp(start, stop, amount)`
234-
will return a linear interpolation (percentage) of `amount` between `start` and `stop`
231+
Returns a linear interpolation (percentage) of `amount` between `start` and `stop`
235232

236233
## Convenience and scaffolding methods
237234

238235
### create_sketch(sketch_name = None, destination_path = '.', overwrite = False, source = None)
239236

240-
Will create a new Python file (`.py`) with the specified name at the provided path.
241-
By default if a file with the same name is found, it will append a timestamp, but overwrite can be forced to True.
242-
Providing a source path it will use that file's content, effectively copying the code from one file to the newly created one.
243-
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:
244242

245-
```Python
243+
```py
246244
create_sketch('my_arduino_sketch')
247245
create_sketch('my_arduino_sketch', 'tmp')
248246
create_sketch('main')
@@ -252,4 +250,4 @@ The method returns the Python file's full path.
252250

253251
### copy_sketch(source_path = '', destination_path = '.', name = None, overwrite = False):
254252

255-
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.

Diff for: 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)