You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
+
defconnect(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
+
whilenot 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')
4
44
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)
44
45
```
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.
45
48
46
-
Will set the pin to the specified value.
49
+
Open a shell and run the following command:
47
50
48
-
### analog_read(PIN_NUMBER/ID)
49
-
50
-
Reads the analog value for the Voltage applied to the pinpin with Number or ID specified.
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.
67
57
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
-
```
88
58
89
59
## Usage
90
60
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:
92
62
93
-
```Python
63
+
```py
94
64
from arduino import*
95
65
96
66
defsetup():
@@ -105,46 +75,46 @@ start(setup, loop)
105
75
106
76
The program above will define two main methods: `setup()` and `loop()`.
107
77
`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.
109
80
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:
112
82
113
-
```Python
83
+
```py
114
84
from time import sleep_ms
115
85
116
-
print('starting my program)
86
+
print('starting my program')
117
87
118
88
whileTrue:
119
89
print('loop')
120
90
sleep_ms(1000)
121
91
```
122
92
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 orobjectin the MicroPython board and allow the user to simply change a variable, set the property of an objectand 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).
126
94
127
-
We also introduce a new way of cleaning up andor 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.
129
96
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.
131
101
132
102
### setup()
133
103
134
-
Is run _once_and should contain initialisation code.
104
+
Is run **once** and should contain initialisation code.
135
105
136
106
### loop()
137
107
138
108
Is run indefinitely until the program stops.
139
109
140
110
### cleanup()
141
111
142
-
Is run _once_ when the program stops. This happen either when the user manually stops the execution of the program orif 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.
144
114
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:
146
116
147
-
```Python
117
+
```py
148
118
from arduino import*
149
119
150
120
defsetup():
@@ -157,37 +127,120 @@ def loop():
157
127
defcleanup():
158
128
print('cleanup')
159
129
160
-
start(setup, loop)
130
+
start(setup, loop, cleanup)
161
131
```
162
132
163
133
> [!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
+
```
165
217
166
218
## Utilities
167
219
168
220
Some utility methods are provided and are still in development:
169
221
170
222
*`map(x, in_min, in_max, out_min, out_max)`
171
-
will remap the value `x`from its inputrange to an output range
223
+
Remaps the value `x` from its input range to an output range
172
224
*`mapi(x, in_min, in_max, out_min, out_max)`
173
225
same as `map` but always returns an integer
174
226
*`random(low, high=None)`
175
-
will returna random number between `0`and`low`-1if no `high`isprovide, 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
176
228
*`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`
178
230
*`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`
Will create a new Python file (`.py`) with the specified name at the provided path.
186
-
By default if a filewith 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:
189
242
190
-
```Python
243
+
```py
191
244
create_sketch('my_arduino_sketch')
192
245
create_sketch('my_arduino_sketch', 'tmp')
193
246
create_sketch('main')
@@ -197,4 +250,4 @@ The method returns the Python file's full path.
0 commit comments