Skip to content

Commit 06d1a45

Browse files
committed
Final draft
- added all examples (and tested) - added API
1 parent d403ec5 commit 06d1a45

File tree

3 files changed

+194
-26
lines changed

3 files changed

+194
-26
lines changed
Loading
Loading

content/micropython/00.first-steps/03.runtime-package/runtime-package.md

Lines changed: 194 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
---
2-
title: 'Arduino Style MicroPython'
3-
description: 'Learn how to use the runtime package, which allows you to write MicroPython code, Arduino style.'
4-
author: 'Karl Söderby'
2+
title: "Arduino MicroPython Runtime"
3+
description: "Learn how to use the Arduino MicroPython runtime package, which allows you to write MicroPython code in a familiar Arduino style."
4+
author: "Karl Söderby"
55
tags: [MicroPython, Runtime]
66
micropython_type: test
77
---
88

9-
The [Arduino Runtime Package]() is a MicroPython package that allows you to write and program your board using the classic `setup()` and `loop()` constructs.
9+
The [Arduino Runtime Package](https://github.com/arduino/arduino-runtime-mpy/tree/main) is a MicroPython package that allows you to write and program your board using the classic `setup()` and `loop()` constructs.
1010

1111
The package was designed to make it easier to create programs, particularly for those familiar with the Arduino C++ environment.
1212

@@ -28,19 +28,19 @@ To follow this tutorial, you will need to have the following requirements ticked
2828

2929
## Installation
3030

31-
To use the runtime library, we will need to install it first.
31+
To use the runtime package, we will need to install it first.
3232

33-
1. Download and install the [Arduino MicroPython Package Installer](https://labs.arduino.cc/en/labs/micropython-package-installer).
33+
1. Download and install the [Arduino MicroPython Package Installer](https://labs.arduino.cc/en/labs/micropython-package-installer).
3434
2. Connect your board to your computer.
3535
3. Run the tool. In the tool, you should now see your board connected.
3636

37-
![Board connected.]()
37+
![Board connected.](assets/connect-device.png)
3838

3939
4. After verifying that your board is connected, click on the search field, and search for **runtime**. Install the package.
4040

41-
![Install the package.]()
41+
![Install the package.](assets/install-package.png)
4242

43-
5. When the installation is complete, we are ready to use the library.
43+
5. When the installation is complete, we are ready to use the package.
4444

4545
## Basic Example
4646

@@ -64,6 +64,7 @@ start(setup, loop)
6464
```
6565

6666
This program has two main functions: `setup()` and `loop()`. If you are unfamiliar with this concept, here's how it works:
67+
6768
- `setup()` - this function will run just once, at the start of a program. Like in this example, we use `print('starting my program')`.
6869
- `loop()` - this function will continue to run, until you disrupt the program by disconnecting the board or stopping the script.
6970

@@ -75,16 +76,23 @@ At the bottom of the program, we have something called `start()`. This function
7576

7677
Arduino Runtime was created to simplify the code creation when programming in MicroPython, providing a more user-friendly syntax that allows you to understand the programs you create a bit better.
7778

78-
Now that we have everything installed, and our basic example tested out, let's take a look at some of the more common examples.
79+
Now that we have everything installed, and our basic example tested out, let's take a look at some of the more common examples.
7980

80-
***A list of commands are listed [later in this article](). You can also view the [source code on Github]() for further understanding.***
81+
***The API is listed at [the end of this article](#runtime-api). You can also view the [source code on GitHub](https://github.com/arduino/arduino-runtime-mpy/tree/main) for further understanding.***
8182

8283
### Pin Mode
8384

8485
- `pin_mode(pin, mode)`
8586

8687
Configures a pin as an input or an output.
8788

89+
```python
90+
pin = "D6"
91+
92+
def setup():
93+
pin_mode(pin, OUTPUT)
94+
```
95+
8896
### Analog Read
8997

9098
- `analog_read(pin)`
@@ -113,7 +121,7 @@ To write an analog signal (using PWM), we can use the `analog_write()` method. T
113121
The example below sets the pin to "half capacity", and if you connect an LED to this pin, it will shine at half brightness.
114122

115123
```python
116-
pin = "D6"
124+
pin = "LED_BUILTIN"
117125
brightness = 127 #half brightness
118126

119127
def setup():
@@ -132,11 +140,13 @@ start(setup, loop)
132140
Reads a digital pin and returns a HIGH (1) or LOW (0) value.
133141

134142
```python
143+
pin = "D2"
144+
135145
def setup():
136146
print("Digital Read Example")
137147

138148
def loop():
139-
value = digital_read("D2")
149+
value = digital_read(pin)
140150
print(value)
141151

142152
start(setup, loop)
@@ -146,15 +156,16 @@ start(setup, loop)
146156

147157
- `digital_write(pin)`
148158

149-
Writes a HIGH (1) or LOW (0) value to a digital pin.
159+
Writes a **HIGH (1)** or **LOW (0)** value to a digital pin.
150160

151161
```python
162+
pin = "LED_BUILTIN"
163+
152164
def setup():
153165
print("Digital Write Example")
154166

155167
def loop():
156-
digital_write("D2")
157-
168+
digital_write(pin, HIGH)
158169

159170
start(setup, loop)
160171
```
@@ -163,12 +174,12 @@ start(setup, loop)
163174

164175
- `delay(time)`
165176

166-
Freezes the program for the duration specified in *microseconds*.
177+
Freezes the program for the duration specified in _microseconds_.
167178

168179
Below is a demonstration of the classic blink example:
169180

170181
```python
171-
led = "D13"
182+
led = "LED_BUILTIN"
172183

173184
def setup():
174185
print("Delay Example")
@@ -179,21 +190,178 @@ def loop():
179190
delay(1000)
180191
digital_write(led, LOW)
181192
delay(1000)
182-
193+
183194
start(setup, loop)
184195
```
185196

186-
## Runtime Specific Examples
197+
## Runtime API
198+
199+
The API for the runtime package can be found below. See the table for a quick overview:
200+
201+
| Function | Description | Parameters | Returns | Alias |
202+
| ------------------- | ---------------------------------------------- | ------------------------------------------------------------- | ------------------------ | ---------------- |
203+
| `start` | Begins main loop with hooks | `preload`, `setup`, `loop`, `cleanup` | None | None |
204+
| `map_float` | Maps a value from one range to another (float) | `x`, `in_min`, `in_max`, `out_min`, `out_max` | Mapped value (float/int) | None |
205+
| `map_int` | Maps a value from one range to another (int) | Same as `map_float` | Mapped value (int) | None |
206+
| `random` | Generates a pseudo-random integer | `low`, `high` (optional) | Random integer | None |
207+
| `constrain` | Clamps value within min and max | `val`, `min_val`, `max_val` | Clamped value | None |
208+
| `lerp` | Linear interpolation between two values | `start`, `stop`, `amount` | Interpolated value | None |
209+
| `pin_mode` | Sets GPIO pin mode | `pin`, `mode` | Configured `Pin` object | `pinMode()` |
210+
| `digital_write` | Writes digital value to a pin | `pin`, `state` | None | `digitalWrite()` |
211+
| `digital_read` | Reads digital state from a pin | `pin` | `0` or `1` | `digitalRead()` |
212+
| `analog_read` | Reads analog value from a pin | `pin` | 0–65535 | `analogRead()` |
213+
| `analog_write` | Writes PWM duty cycle to a pin | `pin`, `duty_cycle` (0–255) | None | `analogWrite()` |
214+
| `delay` | Pauses execution in milliseconds | `ms` | None | None |
215+
| `get_template_path` | Gets path to default sketch template | None | Template path (string) | None |
216+
| `create_sketch` | Creates new sketch from template | `sketch_name`, `destination_path`, `overwrite`, `source_path` | Created sketch path | None |
217+
| `copy_sketch` | Copies existing sketch to new location | `source_path`, `destination_path`, `name`, `overwrite` | Copied sketch path | None |
218+
219+
### start(setup=None, loop=None, cleanup=None, preload=None)
220+
221+
Begins the main execution loop, calling user-defined hooks.
222+
223+
- **Parameters:**
224+
- `preload`: Function run once before setup.
225+
- `setup`: Initialization function.
226+
- `loop`: Function called repeatedly in a loop.
227+
- `cleanup`: Function called on exception or interrupt.
228+
- **Returns:** None.
229+
- **Alias:** None.
230+
231+
### map_float(x, in_min, in_max, out_min, out_max)
232+
233+
Maps a numeric value from one range to another, allowing floating-point output.
234+
235+
- **Parameters:**
236+
- `x`: Input value to map.
237+
- `in_min`: Lower bound of input range.
238+
- `in_max`: Upper bound of input range.
239+
- `out_min`: Lower bound of output range.
240+
- `out_max`: Upper bound of output range.
241+
- **Returns:** The mapped value (float or int).
242+
- **Alias:** None.
243+
244+
### map_int(x, in_min, in_max, out_min, out_max)
245+
246+
Maps a numeric value from one range to another and returns an integer.
247+
248+
- **Parameters:** same as `map_float`.
249+
- **Returns:** The mapped value as int.
250+
- **Alias:** None.
251+
252+
### random(low, high=None)
253+
254+
Generates a pseudo-random integer within a specified range.
255+
256+
- **Parameters:**
257+
- `low`: If `high` is `None`, acts as upper bound (exclusive); otherwise, lower bound (inclusive).
258+
- `high` (optional): Upper bound (exclusive).
259+
- **Returns:** A random integer in the computed range.
260+
- **Alias:** None.
261+
262+
### constrain(val, min_val, max_val)
263+
264+
Clamps a value to lie within a specified minimum and maximum.
265+
266+
- **Parameters:**
267+
- `val`: Value to clamp.
268+
- `min_val`: Minimum allowable value.
269+
- `max_val`: Maximum allowable value.
270+
- **Returns:** The clamped value.
271+
- **Alias:** None.
272+
273+
### lerp(start, stop, amount)
274+
275+
Performs linear interpolation between two numeric values.
276+
277+
- **Parameters:**
278+
- `start`: Start value.
279+
- `stop`: End value.
280+
- `amount`: Interpolation factor (0.0 = `start`, 1.0 = `stop`).
281+
- **Returns:** The interpolated value.
282+
- **Alias:** None.
283+
284+
### pin_mode(pin, mode)
285+
286+
Configures a GPIO pin mode.
287+
288+
- **Parameters:**
289+
- `pin`: Pin identifier or number.
290+
- `mode`: `INPUT` or `OUTPUT`.
291+
- **Returns:** Configured `Pin` object.
292+
- **Alias:** `pinMode()`.
293+
294+
### digital_write(pin, state)
295+
296+
Writes a digital state to a pin configured as output.
297+
298+
- **Parameters:**
299+
- `pin`: Pin number.
300+
- `state`: `HIGH` or `LOW`.
301+
- **Returns:** None.
302+
- **Alias:** `digitalWrite()`.
303+
304+
### digital_read(pin)
305+
306+
Reads a digital state from a pin configured as input.
307+
308+
- **Parameters:**
309+
- `pin`: Pin number.
310+
- **Returns:** `0` or `1`.
311+
- **Alias:** `digitalRead()`.
312+
313+
### analog_read(pin)
314+
315+
Reads a 16-bit ADC value from a pin.
316+
317+
- **Parameters:**
318+
- `pin`: Pin number.
319+
- **Returns:** Integer between 0 and 65535.
320+
- **Alias:** `analogRead()`.
321+
322+
### analog_write(pin, duty_cycle)
323+
324+
Writes a PWM duty cycle (0–255) to a pin.
325+
326+
- **Parameters:**
327+
- `pin`: Pin number.
328+
- `duty_cycle`: 0–255 duty value.
329+
- **Returns:** None.
330+
- **Alias:** `analogWrite()`.
331+
332+
### delay(ms)
333+
334+
Pauses execution for a specified number of milliseconds.
335+
336+
- **Parameters:**
337+
- `ms`: Milliseconds to sleep.
338+
- **Returns:** None.
339+
- **Alias:** None.
340+
341+
### get_template_path()
342+
343+
Retrieves the filesystem path to the default sketch template.
187344

188-
Below are some methods that are introduced with the runtime package.
345+
- **Parameters:** none.
346+
- **Returns:** Template file path (string).
347+
- **Alias:** None.
189348

190-
### Start
349+
### create_sketch(sketch_name=None, destination_path='.', overwrite=False, source_path=None)
191350

192-
- `start(setup, loop, cleanup)`
351+
Generates a new sketch file from a template.
193352

194-
Starts the program, first by running the `setup()` function, the `loop()` function and finally the `cleanup()` function.
353+
- **Parameters:**
354+
- `sketch_name` (optional): Desired filename without extension.
355+
- `destination_path` (optional): Directory to create the sketch in.
356+
- `overwrite` (optional): Overwrite existing file if `True`.
357+
- `source_path` (optional): Custom template path.
358+
- **Returns:** Path to the created sketch file.
359+
- **Alias:** None.
195360

196-
The `cleanup()` function is optional.
361+
### copy_sketch(source_path='', destination_path='.', name=None, overwrite=False)
197362

198-
### Cleanup
363+
Duplicates an existing sketch file to a new location/name.
199364

365+
- **Parameters:** same as `create_sketch` except uses existing source file.
366+
- **Returns:** Path to the copied sketch.
367+
- **Alias:** None.

0 commit comments

Comments
 (0)