How to read Map() function in micropython?

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
wael
Posts: 1
Joined: Thu May 27, 2021 5:39 am

How to read Map() function in micropython?

Post by wael » Fri May 28, 2021 6:57 am

I'm new to micropython.
How to write Map() function in micropython?
In arduino C:
int mappedValue;
int moistureValue = analogRead(A0);
mappedValue = map(moistureValue,AirValue,WaterValue, 0, 100);
How toconvert it into micropython?

How to use the library? I use Thonny but where I can see the examples Arduino ide?

fe2o3
Posts: 10
Joined: Fri Oct 23, 2015 4:47 pm

Re: How to read Map() function in micropython?

Post by fe2o3 » Fri May 28, 2021 10:02 am

It's really just simple math.

Code: Select all

def map(val, loval, hival, tolow, tohigh):
     if loval <= val <= hival:
         return (val - loval)/(hival-loval)*(tohigh-tolow) + tolow
     else:
         raise(ValueError)
You will NEED to verify that (loval < hival) and that (tolow < tohigh)
and return the result in the form you want (int vs float).

A Celsius to Fahrenheit conversion would be as follows:
>>> map(37, 0, 100, 32, 212)
98.6
>>>

hippy
Posts: 130
Joined: Sat Feb 20, 2021 2:46 pm
Location: UK

Re: How to read Map() function in micropython?

Post by hippy » Fri May 28, 2021 2:25 pm

For a simple proportional mapping a single function as given by 'fe203' will suffice.

This is my generic mapping solution ... https://www.raspberrypi.org/forums/view ... 3#p1866943

That handles multiple mappings depending on input value.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: How to read Map() function in micropython?

Post by pythoncoder » Fri May 28, 2021 5:26 pm

Bear in mind that map is a Python keyword (with different functionality).
Peter Hinch
Index to my micropython libraries.

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: How to read Map() function in micropython?

Post by SpotlightKid » Sun May 30, 2021 8:26 pm

It's actually a built-in function (or rather a class acting like a function), which means you can use "map" for identifiers without getting a syntax error. You will then of course shadow the builtin function.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: How to read Map() function in micropython?

Post by pythoncoder » Mon May 31, 2021 5:28 am

Indeed, it will work. But in my opinion it is not pretty and may confuse anyone reading the code.
Peter Hinch
Index to my micropython libraries.

fe2o3
Posts: 10
Joined: Fri Oct 23, 2015 4:47 pm

Re: How to read Map() function in micropython?

Post by fe2o3 » Mon May 31, 2021 8:38 am

I'd probably just rename map() to rescale(). The map(), as described
by the op, reminds me of the map function from Parallax BASIC for
their Stamp controllers.

-Rusty-

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: How to read Map() function in micropython?

Post by scruss » Mon May 31, 2021 7:20 pm

we already did this last year: map function

for floats, a modification of the old code from Processing that Wiring/Arduino inherited will do:

Code: Select all

def valmap(value, istart, istop, ostart, ostop):
  return ostart + (ostop - ostart) * ((value - istart) / (istop - istart))
fe2o3 wrote:
Fri May 28, 2021 10:02 am
You will NEED to verify that (loval < hival) and that (tolow < tohigh)
Not if you're extrapolating, which is often more useful than stopping with an exception.
For this reason, Arduino also provides the constrain() function if you want to keep the output range constrained

User avatar
OlivierLenoir
Posts: 126
Joined: Fri Dec 13, 2019 7:10 pm
Location: Picardie, FR

Re: How to read Map() function in micropython?

Post by OlivierLenoir » Tue Jun 01, 2021 4:55 am

You can also read this.

Jackli
Posts: 80
Joined: Thu Apr 29, 2021 9:11 am

Re: How to read Map() function in micropython?

Post by Jackli » Tue Jun 01, 2021 7:50 am

OlivierLenoir wrote:
Tue Jun 01, 2021 4:55 am
You can also read this.
This is very helpful, thanks a lot!

Post Reply