map() warning re arguments

Status
Not open for further replies.

rfresh737

Well-known member
I'm using a Teensy 3.5 and the touch functions. I'm getting a warning about my map() method argument list not being overloaded.

I think the problem is with my argument data types? But I'm not sure how to correct this.

I'm using Visual Studio 2017 with the Visual Micro plug-in.

Please see attachment.

Thank you...


2018-06-21_100554.png
 
p is of type TS_Point.

The cast didn't remove the warning.

Code:
TS_Point p;

p.x = map((int16_t) p.x,     200,     3700,     0, 319);
 
I was looking at the map() function.

I tried casting all the arguments to (long) but that didn't work.

https://www.arduino.cc/reference/en/language/functions/math/map/

Code:
long map(long x, long in_min, long in_max, long out_min, long out_max)
{
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

That is the traditional Arduino implementation, but that is not the implementation used on the Teensy:

Code:
#include <type_traits>
// when the input number is an integer type, do all math as 32 bit signed long
template <class T, class A, class B, class C, class D>
long map(T _x, A _in_min, B _in_max, C _out_min, D _out_max, typename std::enable_if<std::is_integral<T>::value >::type* = 0)
{
        long x = _x, in_min = _in_min, in_max = _in_max, out_min = _out_min, out_max = _out_max;
        // Arduino's traditional algorithm
        //return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
        // st42's suggestion: https://github.com/arduino/Arduino/issues/2466#issuecomment-69873889
        // more conversation:
        // https://forum.pjrc.com/threads/44503-map()-function-improvements
        if ((in_max - in_min) > (out_max - out_min)) {
                return (x - in_min) * (out_max - out_min+1) / (in_max - in_min+1) + out_min;
        } else {
                return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
        }
}
// when the input is a float or double, do all math using the input's type
template <class T, class A, class B, class C, class D>
T map(T x, A in_min, B in_max, C out_min, D out_max, typename std::enable_if<std::is_floating_point<T>::value >::type* = 0)
{
        return (x - (T)in_min) * ((T)out_max - (T)out_min) / ((T)in_max - (T)in_min) + (T)out_min;
}
 
Please show us your code. Even better, reduce it down to the smallest complete program (i.e., a sketch) that shows the issue.

Does it work in the Arduino IDE?

What version of Arduino IDE and Teensyduino are you using?
 
@MichaelMeissner, I looked at both the Arduino and Teensyduino/Teensy3 implementation but saw no obvious conflict. I the Arduino version (mine is 1.8.4), all arguments will be automatically converted to long. In the Teensyduino version (mine is 1.38), because of the template definitions, there should be overloaded version of map() for _any_ limit types so long as the mapped type (1st arg) is either a internal type or a floating point type. It looks to me like it should work, or at least find an map() function for args: uint16_t, long, long, long, long.

This code builds for me without error for both Arduino Uno and Teensy 3.5:
Code:
#include <Arduino.h>

void setup()
{
    const int16_t mapped = 123;
    int16_t value = map(mapped, 200, 3700, 0, 319);
}

void loop()
{
    
}

I'm starting to suspect that the code might not be the problem.

@rfresh737, Does your code or a trimmed down version of it work in the Arduino IDE?
 
I'm using the latest Ardunio IDE and Teensydunio 1.42 (the latest also).

Here is the small test code from Mark in my VS 2017 with the Visual Micro plug-in. I'm starting to thinking maybe this is a vmicro issue.

UPDATE: I've posted this issue on the vmicro support forum. I'll see what they have to say about it and update this post accordingly.

2018-06-22_085015.png

Here is the same test code in the Ardunio IDE...only a warning about 'value' not being used so I think that's ok.

2018-06-22_085230.png
 
Last edited:
Status
Not open for further replies.
Back
Top