Skip to content

Commit 8b64a5c

Browse files
committed
Reworked API for map/mapi into map_float/map_int.
Signed-off-by: ubi de feo <[email protected]>
1 parent bce3c0e commit 8b64a5c

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

README.md

+10-7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This module also wraps machine functions in easy-to-use methods.
77
## Installation
88

99
### mip (MicroPython Package Manager)
10+
1011
This is the recommended method for boards which can connect to Internet.
1112
Run the following MicroPython script using your favourite editor:
1213

@@ -43,7 +44,9 @@ connect(SSID, PWD)
4344
mip.install('github:arduino/arduino-runtime-mpy')
4445

4546
```
47+
4648
### mpremote mip
49+
4750
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.
4851

4952
Open a shell and run the following command:
@@ -53,8 +56,8 @@ mpremote mip install "github:arduino/arduino-runtime-mpy"
5356
```
5457

5558
### Manual Installation
56-
Copy the folder `arduino` and its content into your board's `lib` folder using your preferred method.
5759

60+
Copy the folder `arduino` and its content into your board's `lib` folder using your preferred method.
5861

5962
## Usage
6063

@@ -219,10 +222,10 @@ delay(1000) # Delay the execution for 1 second
219222

220223
Some utility methods are provided and are still in development:
221224

222-
* `map(x, in_min, in_max, out_min, out_max)`
223-
Remaps the value `x` from its input range to an output range
224-
* `mapi(x, in_min, in_max, out_min, out_max)`
225-
same as `map` but always returns an integer
225+
* `map_float(x, in_min, in_max, out_min, out_max)`
226+
Remaps the value `x` from its input range to an output range as a float
227+
* `map_int(x, in_min, in_max, out_min, out_max)`
228+
same as `map_float` but always returns an integer
226229
* `random(low, high=None)`
227230
Returns a random number between `0` and `low` - 1 if no `high` is provided, otherwise a value between `low` and `high` - 1
228231
* `constrain(val, min_val, max_val)`
@@ -248,6 +251,6 @@ create_sketch('main')
248251

249252
The method returns the Python file's full path.
250253

251-
### copy_sketch(source_path = '', destination_path = '.', name = None, overwrite = False):
254+
### copy_sketch(source_path = '', destination_path = '.', name = None, overwrite = False)
252255

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

arduino/arduino.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
LOW = 0 # Voltage level LOW
1616

1717
# UTILITY
18-
def map(x, in_min, in_max, out_min, out_max) -> int | float:
18+
def map_float(x, in_min, in_max, out_min, out_max) -> int | float:
1919
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
2020

21-
def mapi(x, in_min, in_max, out_min, out_max) -> int:
22-
return int(map(x, in_min, in_max, out_min, out_max))
21+
def map_int(x, in_min, in_max, out_min, out_max) -> int:
22+
return int(map_float(x, in_min, in_max, out_min, out_max))
2323

2424
def random(low, high=None) -> int:
2525
if high == None:
@@ -63,7 +63,7 @@ def analogRead(_pin) -> int:
6363

6464
def analog_write(_pin, _duty_cycle) -> None:
6565
p = PWM(Pin(_pin))
66-
duty = mapi(_duty_cycle, 0, 255, 0, 65535)
66+
duty = map_int(_duty_cycle, 0, 255, 0, 65535)
6767
p.duty_u16(floor(duty))
6868

6969
if(_duty_cycle == 0):

0 commit comments

Comments
 (0)