Closed fermino closed 1 year ago
Memory usage change @ 8ec190d8507df5408e6d8adbb40ce1140fd74545
Board | flash | % | RAM for global variables | % |
---|---|---|---|---|
arduino:avr:LilyPadUSB | 0 - 0 | 0.0 - 0.0 | 0 - 0 | 0.0 - 0.0 |
arduino:avr:atmegang:cpu=atmega168 | 0 - 0 | 0.0 - 0.0 | 0 - 0 | 0.0 - 0.0 |
arduino:avr:atmegang:cpu=atmega8 | 0 - 0 | 0.0 - 0.0 | 0 - 0 | 0.0 - 0.0 |
arduino:avr:bt:cpu=atmega168 | 0 - 0 | 0.0 - 0.0 | 0 - 0 | 0.0 - 0.0 |
arduino:avr:bt:cpu=atmega328 | 0 - 0 | 0.0 - 0.0 | 0 - 0 | 0.0 - 0.0 |
arduino:avr:chiwawa | 0 - 0 | 0.0 - 0.0 | 0 - 0 | 0.0 - 0.0 |
arduino:avr:circuitplay32u4cat | 0 - 0 | 0.0 - 0.0 | 0 - 0 | N/A |
arduino:avr:diecimila:cpu=atmega168 | 0 - 0 | 0.0 - 0.0 | 0 - 0 | 0.0 - 0.0 |
arduino:avr:diecimila:cpu=atmega328 | 0 - 0 | 0.0 - 0.0 | 0 - 0 | 0.0 - 0.0 |
arduino:avr:esplora | 0 - 0 | 0.0 - 0.0 | 0 - 0 | 0.0 - 0.0 |
arduino:avr:ethernet | 0 - 0 | 0.0 - 0.0 | 0 - 0 | 0.0 - 0.0 |
arduino:avr:fio | 0 - 0 | 0.0 - 0.0 | 0 - 0 | 0.0 - 0.0 |
arduino:avr:gemma | 0 - 0 | 0.0 - 0.0 | 0 - 0 | N/A |
arduino:avr:leonardo | 0 - 0 | 0.0 - 0.0 | 0 - 0 | 0.0 - 0.0 |
arduino:avr:leonardoeth | 0 - 0 | 0.0 - 0.0 | 0 - 0 | 0.0 - 0.0 |
arduino:avr:lilypad:cpu=atmega168 | 0 - 0 | 0.0 - 0.0 | 0 - 0 | 0.0 - 0.0 |
arduino:avr:lilypad:cpu=atmega328 | 0 - 0 | 0.0 - 0.0 | 0 - 0 | 0.0 - 0.0 |
arduino:avr:mega:cpu=atmega1280 | 0 - 0 | 0.0 - 0.0 | 0 - 0 | 0.0 - 0.0 |
arduino:avr:mega:cpu=atmega2560 | 0 - 0 | 0.0 - 0.0 | 0 - 0 | 0.0 - 0.0 |
arduino:avr:megaADK | 0 - 0 | 0.0 - 0.0 | 0 - 0 | 0.0 - 0.0 |
arduino:avr:micro | 0 - 0 | 0.0 - 0.0 | 0 - 0 | 0.0 - 0.0 |
arduino:avr:mini:cpu=atmega168 | 0 - 0 | 0.0 - 0.0 | 0 - 0 | 0.0 - 0.0 |
arduino:avr:mini:cpu=atmega328 | 0 - 0 | 0.0 - 0.0 | 0 - 0 | 0.0 - 0.0 |
arduino:avr:nano:cpu=atmega168 | 0 - 0 | 0.0 - 0.0 | 0 - 0 | 0.0 - 0.0 |
arduino:avr:nano:cpu=atmega328 | 0 - 0 | 0.0 - 0.0 | 0 - 0 | 0.0 - 0.0 |
arduino:avr:nano:cpu=atmega328old | 0 - 0 | 0.0 - 0.0 | 0 - 0 | 0.0 - 0.0 |
arduino:avr:one | 0 - 0 | 0.0 - 0.0 | 0 - 0 | 0.0 - 0.0 |
arduino:avr:pro:cpu=16MHzatmega168 | 0 - 0 | 0.0 - 0.0 | 0 - 0 | 0.0 - 0.0 |
arduino:avr:pro:cpu=16MHzatmega328 | 0 - 0 | 0.0 - 0.0 | 0 - 0 | 0.0 - 0.0 |
arduino:avr:pro:cpu=8MHzatmega168 | 0 - 0 | 0.0 - 0.0 | 0 - 0 | 0.0 - 0.0 |
arduino:avr:pro:cpu=8MHzatmega328 | 0 - 0 | 0.0 - 0.0 | 0 - 0 | 0.0 - 0.0 |
arduino:avr:robotControl | 0 - 0 | 0.0 - 0.0 | 0 - 0 | 0.0 - 0.0 |
arduino:avr:robotMotor | 0 - 0 | 0.0 - 0.0 | 0 - 0 | 0.0 - 0.0 |
arduino:avr:uno | 0 - 0 | 0.0 - 0.0 | 0 - 0 | 0.0 - 0.0 |
arduino:avr:unowifi | 0 - 0 | 0.0 - 0.0 | 0 - 0 | 0.0 - 0.0 |
arduino:avr:yun | 0 - 0 | 0.0 - 0.0 | 0 - 0 | 0.0 - 0.0 |
arduino:avr:yunmini | 0 - 0 | 0.0 - 0.0 | 0 - 0 | 0.0 - 0.0 |
It is not that simple. On AVR this will easily give incorrect results due to 16-bit int
overflow.
A common example: scale ADC value to 0–100.
template <typename T>
T tmap(T x, T in_min, T in_max, T out_min, T out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
void setup() {
Serial.begin(115200);
for (int i = 0; i < 1024; i += 33) {
int x1 = map(i, 0, 1023, 0, 100);
int x2 = tmap(i, 0, 1023, 0, 100);
Serial.print(i);
Serial.print('\t');
Serial.print(x1);
Serial.print('\t');
Serial.println(x2);
}
}
void loop() { }
Output
0 0 0
33 3 3
66 6 6
99 9 9
132 12 12
165 16 16
198 19 19
231 22 22
264 25 25
297 29 29
330 32 -31
363 35 -28
396 38 -25
429 41 -22
462 45 -18
495 48 -15
528 51 -12
561 54 -9
594 58 -5
627 61 -2
660 64 0
693 67 3
726 70 6
759 74 10
792 77 13
825 80 16
858 83 19
891 87 23
924 90 26
957 93 29
990 96 -31
1023 100 -28
Change type of i
to unsigned int
sketch.ino: In function 'void setup()':
sketch.ino:11:37: error: no matching function for call to 'tmap(unsigned int&, int, int, int, int)'
int x2 = tmap(i, 0, 1023, 0, 100);
^
sketch.ino:2:3: note: candidate: template<class T> T tmap(T, T, T, T, T)
T tmap(T x, T in_min, T in_max, T out_min, T out_max)
^~~~
sketch.ino:2:3: note: template argument deduction/substitution failed:
sketch.ino:11:37: note: deduced conflicting types for parameter 'T' ('unsigned int' and 'int')
int x2 = tmap(i, 0, 1023, 0, 100);
^
Error during build: exit status 1
Let's try
int x2 = tmap(i, 0u, 1023u, 0u, 100u);
Nope.
0 0 0
33 3 3
66 6 6
99 9 9
132 12 12
165 16 16
198 19 19
231 22 22
264 25 25
297 29 29
330 32 32
363 35 35
396 38 38
429 41 41
462 45 45
495 48 48
528 51 51
561 54 54
594 58 58
627 61 61
660 64 0
693 67 3
726 70 6
759 74 10
792 77 13
825 80 16
858 83 19
891 87 23
924 90 26
957 93 29
990 96 32
1023 100 35
I have seen how many other PR's and issues are related to the map() function, but well, hope it is of any use.
In any case, it should be backwards compatible with any C++ project out there.