0.1.5 map2colour

This commit is contained in:
rob tillaart 2022-10-20 11:58:00 +02:00
parent 0050d7e9b9
commit 93deb0313a
19 changed files with 598 additions and 98 deletions

View File

@ -1,3 +1,18 @@
platforms:
rpipico:
board: rp2040:rp2040:rpipico
package: rp2040:rp2040
gcc:
features:
defines:
- ARDUINO_ARCH_RP2040
warnings:
flags:
packages:
rp2040:rp2040:
url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
compile:
# Choosing to run compilation tests on 2 different Arduino platforms
platforms:
@ -8,4 +23,5 @@ compile:
- m4
- esp32
# - esp8266
# - mega2560
# - mega2560
- rpipico

View File

@ -0,0 +1,34 @@
# Change Log map2colour
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.1.5] - 2022-10-19
- redo **begin()** of map2colourFast to always create divFactors.
- add compare example to compare map2colour and map2colourFast.
- extent unit test with compare map2colour and map2colourFast.
- update examples
- add RP2040 in build-CI
- add changelog.md
- fix keywords.txt
## [0.1.4] - 2021-12-08
- add derived class map2colourFast: faster + uses more MEMORY / RAM
## [0.1.3] - 2021-12-07
- improve performance map2RGB()
## [0.1.2] - 2021-12-06
- add map2_565(),
- add + improve examples.
- fix value > upper bug
## [0.1.1] - 2021-12-05
- add user defined colour-map.
- add unit tests, update readme.md.
## [0.1.0] - 2021-12-04
- initial version.

View File

@ -91,7 +91,8 @@ Colours are represented as 24 bit RGB values and have the pattern **0x00RRGGBB**
| M2C_AQUA | 0x0000FFFF |
More colour definitions can be found on the internet e.g. https://www.w3.org/wiki/CSS/Properties/color/keywords
More colour definitions can be found on the internet
e.g. https://www.w3.org/wiki/CSS/Properties/color/keywords
## Operation
@ -101,7 +102,22 @@ See examples.
By changing the colour map one can get different effects.
The minimum to implement is an intensity effect going from black towards a colour at max intensity.
More complex colour schemes are possible, up to 7 different colours.
This number is hardcoded (for now) and that might change in the future.
This number 7 is hardcoded (for now) and that might change in the future.
#### Experimental in 0.1.5
(was planned for 0.2.0)
If you create a non-decreasing array of values one can create a break in the gradient. See example.
```cpp
float values[7] = { -200, -90, 0, 45, 45, 150, 180 };
uint32_t colours[7] = { M2C_BLUE, M2C_AQUA, M2C_LIME, M2C_YELLOW, M2C_RED, M2C_YELLOW, M2C_BLUE};
```
with the double 45 in the values array there would be no gradient between the **M2C_YELLOW** and **M2C_RED**
effectively having 2 continuous gradients.
Note: **begin()** will report such array as false.
## Performance
@ -147,7 +163,8 @@ Note: UNO at 16 MHz, ESP32 at 240 MHz
One performance optimization (trade memory for speed) is replacing the float division
in map2RGB by a multiplication.
This requires 24 bytes RAM to hold the 6 factors and ~100 bytes of PROGMEM for the calculation of the dividers in begin().
This requires 24 bytes RAM to hold the 6 factors and ~100 bytes of PROGMEM for the
calculation of the dividers in begin().
This optimization is implemented as a derived class **map2colourFast** in version 0.1.4.
The **map2RGB()** call is about 40 % faster compared to the original 0.1.2.
Although the **begin()** call is ~300 us longer, it only takes a dozen **map2RGB()** calls to break even.
@ -157,14 +174,30 @@ Note: the gain for the ESP32 is less pronounced, but can still be interesting.
## Future
#### must
- update documentation
#### should
- redo **begin()** of map2colour to allow all values. (0.2.0)
- non-decreasing array (already experimental in 0.1.5, will still return false! )
- any array of numbers. (return value will always be true then)
- look for optimizations.
- cache last value?
#### development ?
- **void adjustColour(uint8_t index, uint32_t RGB)** // single colour adjust
- **uint32_t dumpColourMap()**
- PROGMEM for default array?
#### could
- make size configurable ?
- **void adjustColour(uint8_t index, uint32_t RGB)**
- single colour adjust
- faster than calling begin() again
- map2RGB variant that gives a colour to the delta with previous value
- user can do that fairly easy => example
- map2HSL() as extra colour space.
- add **reset()** for default array? (RAM)
#### wont
- **uint32_t dumpColourMap()** ==> not needed
- PROGMEM for default array? ==> slower, AVR specific.
- move up the test for non-increase in **begin()** ==> fail fast.
- conflicts with begin of fast version.

View File

@ -0,0 +1,78 @@
//
// FILE: map2colour_compare_fast.ino
// AUTHOR: Rob Tillaart
// PURPOSE: map2colour demo
// URL: https://github.com/RobTillaart/map2colour
#include "Arduino.h"
#include "map2colour.h"
map2colour mc;
map2colourFast mcf;
// should be in increasing order
float values[7] = { 0, 32, 64, 128, 256, 512, 1024 };
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("MAP2COLOUR_LIB_VERSION: ");
Serial.println(MAP2COLOUR_LIB_VERSION);
Serial.println();
// load the values array
mc.begin(values);
mcf.begin(values);
Serial.println();
Serial.println();
Serial.println("map2RGB");
// show the interpolating
for (float i = 0; i <= 1024; i++)
{
uint32_t rgb1 = mc.map2RGB(i);
uint32_t rgb2 = mcf.map2RGB(i);
Serial.print(i);
Serial.print("\t");
Serial.print(rgb1, HEX);
Serial.print("\t");
Serial.print(rgb2, HEX);
Serial.print("\t");
Serial.println((rgb1 == rgb2) ? "" : "<<<<"); // fails
}
Serial.println();
Serial.println();
Serial.println("map2_565");
// show the interpolating
for (float i = 0; i <= 1024; i++)
{
uint16_t rgb1 = mc.map2_565(i);
uint16_t rgb2 = mcf.map2_565(i);
Serial.print(i);
Serial.print("\t");
Serial.print(rgb1, HEX);
Serial.print("\t");
Serial.print(rgb2, HEX);
Serial.print("\t");
Serial.println((rgb1 == rgb2) ? "" : "<<<<"); // fails
}
Serial.println();
Serial.println();
Serial.println("Done...");
}
void loop()
{
}
// -- END OF FILE --

View File

@ -1,9 +1,7 @@
//
// FILE: map2colour_demo01.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: map2colour demo
// DATE: 2021-12-04
// URL: https://github.com/RobTillaart/map2colour
@ -13,7 +11,7 @@
map2colour mc;
// should be in increasing order
// should be in increasing order
float values[7] = { 0, 32, 64, 128, 256, 512, 1024 };
@ -21,11 +19,14 @@ void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("MAP2COLOUR_LIB_VERSION: ");
Serial.println(MAP2COLOUR_LIB_VERSION);
Serial.println();
// load the values array
// load the values array
mc.begin(values);
// show the interpolating
// show the interpolating
for (float i = 0; i < 1024; i += 10)
{
uint32_t rgb = mc.map2RGB(i);
@ -39,7 +40,7 @@ void setup()
void loop()
{
int x = analogRead(0); // UNO returns between 0..1023; adapt if needed.
int x = analogRead(0); // UNO returns between 0..1023; adapt if needed.
uint32_t rgb = mc.map2RGB(x);
Serial.print(x);
Serial.print("\t");
@ -49,3 +50,4 @@ void loop()
// -- END OF FILE --

View File

@ -1,14 +1,12 @@
//
// FILE: map2colour_faucet.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: map2colour demo
// DATE: 2021-12-06
// URL: https://github.com/RobTillaart/map2colour
// demo simulates a temperature of a faucet mapped upon 2 colours.
// blue = cold, black is middle, red is hot
// demo simulates a temperature of a faucet mapped upon 2 colours.
// blue = cold, black is middle, red is hot
#include "Arduino.h"
@ -17,7 +15,7 @@
map2colour mct;
// values must be 7 elements in increasing order.
// values must be 7 elements in increasing order.
float values[7] = { 0, 10, 15, 25, 40, 100, 125 };
uint32_t faucet[7] =
{
@ -29,6 +27,9 @@ void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("MAP2COLOUR_LIB_VERSION: ");
Serial.println(MAP2COLOUR_LIB_VERSION);
Serial.println();
mct.begin(values, faucet);

View File

@ -0,0 +1,46 @@
//
// FILE: map2colour_gradient_break.ino
// AUTHOR: Rob Tillaart
// PURPOSE: map2colour demo
// URL: https://github.com/RobTillaart/map2colour
#include "Arduino.h"
#include "map2colour.h"
map2colour mc;
// note that 128 appears twice.
float values[7] = { 0, 32, 64, 128, 128, 256, 512 };
uint32_t colours[7] = { M2C_BLUE, M2C_AQUA, M2C_LIME, M2C_BLUE, M2C_RED, M2C_YELLOW, M2C_GREEN};
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("MAP2COLOUR_LIB_VERSION: ");
Serial.println(MAP2COLOUR_LIB_VERSION);
Serial.println();
// load the values array and colours array
mc.begin(values, colours);
// show the interpolating - note the break between 128 and 129.
for (float i = 0; i < 512; i++)
{
uint32_t rgb = mc.map2RGB(i);
Serial.print(i);
Serial.print("\t");
Serial.println(rgb, HEX);
}
Serial.println();
}
void loop()
{
}
// -- END OF FILE --

View File

@ -1,14 +1,13 @@
//
// FILE: map2colour_intensity.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: map2colour demo
// DATE: 2021-12-06
// URL: https://github.com/RobTillaart/map2colour
// demo simulates a temperature mapped upon 1 colour intensity.
// low temperature = no colour, higher is more intense RED
// demo simulates a temperature mapped upon 1 colour intensity.
// low temperature = no colour, higher is more intense RED
// above 100 is just RED
#include "Arduino.h"
@ -17,7 +16,7 @@
map2colour mct;
// values must be 7 elements in increasing order
// values must be 7 elements in increasing order
float values[7] = { 20, 100, 125, 150, 200, 250, 300 };
uint32_t colours[7] =
{
@ -29,6 +28,9 @@ void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("MAP2COLOUR_LIB_VERSION: ");
Serial.println(MAP2COLOUR_LIB_VERSION);
Serial.println();
mct.begin(values, colours);
@ -49,3 +51,4 @@ void loop()
// -- END OF FILE --

View File

@ -1,9 +1,7 @@
//
// FILE: map2colour_map2_565.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: map2colour demo
// DATE: 2021-12-05
// URL: https://github.com/RobTillaart/map2colour
@ -13,7 +11,7 @@
map2colour mc;
// should be in increasing order
// should be in increasing order
float values[7] = { 0, 32, 64, 128, 256, 512, 1024 };
@ -21,11 +19,14 @@ void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("MAP2COLOUR_LIB_VERSION: ");
Serial.println(MAP2COLOUR_LIB_VERSION);
Serial.println();
// load the values array
// load the values array
mc.begin(values);
// show the interpolating
// show the interpolating
for (float i = 0; i < 1024; i += 10)
{
uint16_t rgb = mc.map2_565(i);
@ -39,7 +40,7 @@ void setup()
void loop()
{
int x = analogRead(0); // UNO returns between 0..1023; adapt if needed.
int x = analogRead(0); // UNO returns between 0..1023; adapt if needed.
uint32_t rgb = mc.map2_565(x);
Serial.print(x);
Serial.print("\t");

View File

@ -15,7 +15,7 @@ map2colour mc;
// map2colourFast mc;
// should be in increasing order; and 7 elements
// should be in increasing order; and 7 elements
float values[7] = { 0, 32, 64, 128, 256, 512, 1024 };
uint32_t colours[7] =
{
@ -23,17 +23,21 @@ uint32_t colours[7] =
};
uint32_t start, stop;
uint32_t total = 0;
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("MAP2COLOUR_LIB_VERSION: ");
Serial.println(MAP2COLOUR_LIB_VERSION);
Serial.println();
delay(1000);
Serial.println("begin()");
delay(10);
// use the default colour map.
// use the default colour map.
start = micros();
mc.begin(values);
stop = micros();
@ -41,7 +45,7 @@ void setup()
delay(10);
// use the default colour map.
// use the default colour map.
start = micros();
mc.begin(values, colours);
stop = micros();
@ -51,12 +55,14 @@ void setup()
Serial.println("map2RGB()");
total = 0;
delay(10);
for (float i = 0; i < 1024; i += 10)
{
start = micros();
uint32_t rgb = mc.map2RGB(i);
stop = micros();
total += (stop - start);
Serial.print(stop - start); // 120 - 172 us
Serial.print("\t");
Serial.print(i);
@ -65,16 +71,21 @@ void setup()
delay(10);
}
Serial.println();
Serial.print("AVG:\t");
Serial.println(total / 103.0);
Serial.println();
delay(1000);
Serial.println("map2_565()");
total = 0;
delay(10);
for (float i = 0; i < 1024; i += 10)
{
uint32_t start = micros();
uint32_t rgb = mc.map2_565(i);
uint32_t stop = micros();
total += (stop - start);
Serial.print(stop - start); // 120 - 172 us
Serial.print("\t");
Serial.print(i);
@ -83,6 +94,10 @@ void setup()
delay(10);
}
Serial.println();
Serial.print("AVG:\t");
Serial.println(total / 103.0);
Serial.println();
delay(1000);
Serial.println("done...");

View File

@ -0,0 +1,223 @@
map2colour_performance.ino - map2colourFast
MAP2COLOUR_LIB_VERSION: 0.1.5
begin()
284
304
map2RGB()
16 0.00 FF0000
40 10.00 FF0000
40 20.00 FF0000
44 30.00 FF0000
68 40.00 FF3F00
68 50.00 FF8F00
68 60.00 FFDF00
100 70.00 E8FD00
108 80.00 C3FB00
100 90.00 9DF800
100 100.00 78F600
100 110.00 52F400
104 120.00 2DF100
100 130.00 12F000
100 140.00 25F100
104 150.00 38F200
100 160.00 4BF300
100 170.00 5DF400
100 180.00 70F600
104 190.00 83F700
100 200.00 96F800
100 210.00 A8F900
100 220.00 BBFA00
100 230.00 CEFB00
104 240.00 E1FD00
100 250.00 F3FE00
76 260.00 FFFB00
84 270.00 FFF100
80 280.00 FFE700
80 290.00 FFDD00
80 300.00 FFD300
80 310.00 FFC900
80 320.00 FFBF00
80 330.00 FFB500
80 340.00 FFAB00
80 350.00 FFA100
80 360.00 FF9700
80 370.00 FF8D00
80 380.00 FF8300
80 390.00 FF7900
80 400.00 FF6F00
80 410.00 FF6500
88 420.00 FF5B00
80 430.00 FF5100
80 440.00 FF4700
84 450.00 FF3D00
80 460.00 FF3300
80 470.00 FF2900
84 480.00 FF1F00
84 490.00 FF1500
84 500.00 FF0B00
88 510.00 FF0100
64 520.00 FF0000
64 530.00 FF0000
60 540.00 FF0000
64 550.00 FF0000
64 560.00 FF0000
60 570.00 FF0000
60 580.00 FF0000
60 590.00 FF0000
60 600.00 FF0000
60 610.00 FF0000
60 620.00 FF0000
60 630.00 FF0000
60 640.00 FF0000
64 650.00 FF0000
64 660.00 FF0000
64 670.00 FF0000
60 680.00 FF0000
60 690.00 FF0000
60 700.00 FF0000
64 710.00 FF0000
60 720.00 FF0000
60 730.00 FF0000
60 740.00 FF0000
64 750.00 FF0000
64 760.00 FF0000
60 770.00 FF0000
64 780.00 FF0000
64 790.00 FF0000
60 800.00 FF0000
60 810.00 FF0000
64 820.00 FF0000
64 830.00 FF0000
60 840.00 FF0000
64 850.00 FF0000
64 860.00 FF0000
60 870.00 FF0000
60 880.00 FF0000
64 890.00 FF0000
60 900.00 FF0000
64 910.00 FF0000
64 920.00 FF0000
64 930.00 FF0000
60 940.00 FF0000
64 950.00 FF0000
64 960.00 FF0000
64 970.00 FF0000
64 980.00 FF0000
60 990.00 FF0000
64 1000.00 FF0000
64 1010.00 FF0000
68 1020.00 FF0000
AVG: 73.32
map2_565()
16 0.00 F800
44 10.00 F800
44 20.00 F800
44 30.00 F800
76 40.00 F9E0
72 50.00 FC60
76 60.00 FEE0
104 70.00 EFE0
104 80.00 C7C0
104 90.00 9FC0
104 100.00 7FA0
104 110.00 57A0
104 120.00 2F80
104 130.00 1780
104 140.00 2780
104 150.00 3F80
108 160.00 4F80
104 170.00 5FA0
100 180.00 77A0
108 190.00 87A0
104 200.00 97C0
104 210.00 AFC0
104 220.00 BFC0
104 230.00 CFC0
108 240.00 E7E0
104 250.00 F7E0
84 260.00 FFC0
84 270.00 FF80
84 280.00 FF20
88 290.00 FEE0
84 300.00 FE80
84 310.00 FE40
80 320.00 FDE0
84 330.00 FDA0
84 340.00 FD40
80 350.00 FD00
80 360.00 FCA0
80 370.00 FC60
84 380.00 FC00
84 390.00 FBC0
92 400.00 FB60
80 410.00 FB20
84 420.00 FAC0
84 430.00 FA80
84 440.00 FA20
84 450.00 F9E0
84 460.00 F980
84 470.00 F940
88 480.00 F8E0
88 490.00 F8A0
84 500.00 F840
96 510.00 F800
64 520.00 F800
68 530.00 F800
64 540.00 F800
68 550.00 F800
68 560.00 F800
64 570.00 F800
68 580.00 F800
68 590.00 F800
68 600.00 F800
64 610.00 F800
68 620.00 F800
64 630.00 F800
64 640.00 F800
68 650.00 F800
64 660.00 F800
64 670.00 F800
64 680.00 F800
68 690.00 F800
64 700.00 F800
64 710.00 F800
72 720.00 F800
64 730.00 F800
68 740.00 F800
64 750.00 F800
68 760.00 F800
68 770.00 F800
64 780.00 F800
64 790.00 F800
72 800.00 F800
68 810.00 F800
64 820.00 F800
68 830.00 F800
64 840.00 F800
64 850.00 F800
68 860.00 F800
68 870.00 F800
68 880.00 F800
68 890.00 F800
68 900.00 F800
64 910.00 F800
68 920.00 F800
68 930.00 F800
64 940.00 F800
64 950.00 F800
68 960.00 F800
68 970.00 F800
68 980.00 F800
68 990.00 F800
68 1000.00 F800
68 1010.00 F800
68 1020.00 F800
AVG: 77.13
done...

View File

@ -1,15 +1,13 @@
//
// FILE: map2colour_pressure.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: map2colour demo
// DATE: 2021-12-05
// URL: https://github.com/RobTillaart/map2colour
// demo simulates a pressure sensor and its mapping on three colours
// pressure 950, 990, 1010, 1030 1050, 2000, 2000
// RED YELLOW GREEN YELLOW RED WHITE WHITE
// demo simulates a pressure sensor and its mapping on three colours
// pressure 950, 990, 1010, 1030 1050, 2000, 2000
// RED YELLOW GREEN YELLOW RED WHITE WHITE
#include "Arduino.h"
@ -19,8 +17,8 @@
map2colour mcp;
// should be in increasing order; and 7 elements
// pressure in mbar
// should be in increasing order; and 7 elements
// pressure in mbar
float values[7] = { 950, 990, 1010, 1030, 1050, 2000, 2500 };
uint32_t colours[7] =
{
@ -32,6 +30,9 @@ void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("MAP2COLOUR_LIB_VERSION: ");
Serial.println(MAP2COLOUR_LIB_VERSION);
Serial.println();
mcp.begin(values, colours);
for (int i = 950; i < 1050; i++)
@ -47,10 +48,10 @@ void setup()
void loop()
{
float pressure = 950 + random(100); // add your sensor
float pressure = 950 + random(100); // add your sensor
uint32_t rgb = mcp.map2RGB(pressure);
Serial.print("P: ");
Serial.println(rgb, HEX); // replace with PWM-RGB led
Serial.println(rgb, HEX); // replace with PWM-RGB led
delay(1000);
}

View File

@ -1,16 +1,14 @@
//
// FILE: map2colour_temperature.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: map2colour demo
// DATE: 2021-12-04
// URL: https://github.com/RobTillaart/map2colour
// demo simulates a temperature sensor and its mapping upon colours
// temp -20, 5, 10, 30, 35, 50, 200
// Red yellow black black yellow red red
// note thart the middle area 10..30 has no colour.
// demo simulates a temperature sensor and its mapping upon colours
// temp -20, 5, 10, 30, 35, 50, 200
// Red yellow black black yellow red red
// note that the middle area 10..30 has no colour (black).
#include "Arduino.h"
@ -20,8 +18,8 @@
map2colour mct;
map2colour mch;
// should be in increasing order; and 7 elements
// temperature
// should be in increasing order; and 7 elements
// temperature
float Tvalues[7] = { -20, 5, 10, 30, 35, 50, 200 };
uint32_t Tcolours[7] =
{
@ -30,7 +28,7 @@ uint32_t Tcolours[7] =
0x00FF0000
};
// humidity around 35 is perfect
// humidity around 35 is perfect
float Hvalues[7] = { 0, 20, 30, 35, 40, 50, 100 };
uint32_t Hcolours[7] =
{
@ -40,11 +38,13 @@ uint32_t Hcolours[7] =
};
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("MAP2COLOUR_LIB_VERSION: ");
Serial.println(MAP2COLOUR_LIB_VERSION);
Serial.println();
mct.begin(Tvalues, Tcolours);
mch.begin(Hvalues, Hcolours);
@ -72,18 +72,19 @@ void setup()
void loop()
{
float temperature = analogRead(A0)/10; // add your sensor
float temperature = analogRead(A0)/10; // add your sensor
uint32_t rgb = mct.map2RGB(temperature);
Serial.print("T: ");
Serial.print(rgb, HEX); // replace with PWM-RGB led
Serial.print(rgb, HEX); // replace with PWM-RGB led
Serial.print("\t\t");
float humidity = analogRead(A1)/10; // add your sensor
float humidity = analogRead(A1)/10; // add your sensor
rgb = mch.map2RGB(humidity);
Serial.print("H: ");
Serial.println(rgb, HEX); // replace with PWM-RGB led
Serial.println(rgb, HEX); // replace with PWM-RGB led
delay(1000);
}
// -- END OF FILE --

View File

@ -2,6 +2,7 @@
# Data types (KEYWORD1)
map2colour KEYWORD1
map2colourFast KEYWORD1
# Methods and Functions (KEYWORD2)

View File

@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/map2colour.git"
},
"version": "0.1.4",
"version": "0.1.5",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",

View File

@ -1,5 +1,5 @@
name=map2colour
version=0.1.4
version=0.1.5
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for mapping a float to colour spectrum

View File

@ -1,20 +1,11 @@
//
// FILE: map2colour.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.4
// VERSION: 0.1.5
// PURPOSE: Arduino library for mapping a float to colour spectrum
// URL: https://github.com/RobTillaart/map2colour
//
// HISTORY:
// 0.1.0 2021-12-04 initial version.
// 0.1.1 2021-12-05 add user defined colour-map.
// add unit tests, update readme.md.
// 0.1.2 2021-12-06 add map2_565(),
// add + improve examples.
// fix value > upper bug
// 0.1.3 2021-12-07 improve performance map2RGB
// 0.1.4 2021-12-08 add derived class that is fast and uses more MEMORY / RAM
//
// HISTORY see changelog.md
#include "map2colour.h"
@ -43,7 +34,7 @@ bool map2colour::begin(float * values, uint32_t * colourMap)
_values = values;
for (int index = 1; index < 7; index++)
{
// catch non increasing values.
// catch non increasing values.
float den = _values[index] - _values[index - 1];
if (den <= 0.0) return false;
}
@ -59,7 +50,7 @@ uint32_t map2colour::map2RGB(float value)
uint8_t G = _Green[0];
uint8_t B = _Blue[0];
if (_values[0] < value)
if (_values[0] < value)
{
if (value <= _values[6] )
{
@ -71,10 +62,10 @@ uint32_t map2colour::map2RGB(float value)
G = _Green[index];
B = _Blue[index];
// calculate the interpolation factor
// OPTIMIZE USE PRECALCULATED DIVIDERS (costs 24 bytes extra RAM).
// map2colourFast uses pre calculated dividers (costs 24 bytes extra RAM).
float factor = (_values[index] - value) / (_values[index] - _values[index - 1]);
// interpolate if delta <> 0
// interpolate only if delta != 0
int delta = _Red[index] - _Red[index - 1];
if (delta != 0 ) R -= factor * delta;
@ -127,16 +118,22 @@ map2colourFast::map2colourFast() : map2colour()
bool map2colourFast::begin(float * values, uint32_t * colourMap)
{
if (map2colour::begin(values, colourMap) == false)
{
return false; // non increasing values.
}
// calculate dividers
// load the colour-map and check non-decreasing order.
bool OK = map2colour::begin(values, colourMap);
// pre-calculate dividers
for (int index = 1; index < 7; index++)
{
divFactor[index - 1] = 1.0 / (_values[index] - _values[index - 1]);
float divider = _values[index] - _values[index - 1];
if (divider > 0)
{
divFactor[index - 1] = 1.0 / divider;
}
else
{
divFactor[index - 1] = 0;
}
}
return true;
return OK;
}
@ -148,7 +145,7 @@ uint32_t map2colourFast::map2RGB(float value)
uint8_t G = _Green[0];
uint8_t B = _Blue[0];
if (_values[0] < value)
if (_values[0] < value)
{
if (value <= _values[6] )
{

View File

@ -2,7 +2,7 @@
//
// FILE: map2colour.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.4
// VERSION: 0.1.5
// PURPOSE: Arduino library for mapping a float to colour spectrum
// URL: https://github.com/RobTillaart/map2colour
//
@ -10,10 +10,10 @@
#include "Arduino.h"
#define MAP2COLOUR_LIB_VERSION (F("0.1.4"))
#define MAP2COLOUR_LIB_VERSION (F("0.1.5"))
// https://www.w3.org/wiki/CSS/Properties/color/keywords
// https://www.w3.org/wiki/CSS/Properties/color/keywords
#define M2C_BLACK 0x00000000
#define M2C_SILVER 0x00C0C0C0
#define M2C_GRAY 0x00808080
@ -37,7 +37,7 @@ class map2colour
public:
map2colour();
// values is an array of 7 values in ascending order
// values is an array of 7 values in ascending order
bool begin(float * values, uint32_t * colourMap = NULL);
uint32_t map2RGB(float value);
uint16_t map2_565(float value);
@ -46,7 +46,7 @@ class map2colour
uint8_t _size = 7;
float * _values;
// default colours, split up per channel for performance.
// default colours, split up per channel for performance.
uint8_t _Red[7] = { 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0xFF };
uint8_t _Green[7] = { 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0xFF };
uint8_t _Blue[7] = { 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF };
@ -55,15 +55,15 @@ class map2colour
/////////////////////////////////////////////////////////////////////////
//
// DERIVED CLASS
// DERIVED CLASS
//
// FASTER + MORE RAM
// FASTER + MORE RAM
//
class map2colourFast : public map2colour
{
public:
map2colourFast();
// values is an array of 7 values in ascending order
// values is an array of 7 values in ascending order
bool begin(float * values, uint32_t * colourMap = NULL);
uint32_t map2RGB(float value);
uint16_t map2_565(float value);

View File

@ -28,6 +28,7 @@
unittest_setup()
{
fprintf(stderr, "MAP2COLOUR_LIB_VERSION: %s\n", (char *) MAP2COLOUR_LIB_VERSION);
}
unittest_teardown()
@ -37,8 +38,6 @@ unittest_teardown()
unittest(test_constructor_I)
{
fprintf(stderr, "MAP2COLOUR_LIB_VERSION: %s\n", (char *) MAP2COLOUR_LIB_VERSION);
map2colour mc;
float values[7] = { 1, 2, 3, 4, 5, 6, 7 };
@ -53,10 +52,9 @@ unittest(test_constructor_I)
assertEqual(0x00FFFFFF, mc.map2RGB(7));
}
unittest(test_constructor_II)
{
fprintf(stderr, "MAP2COLOUR_LIB_VERSION: %s\n", (char *) MAP2COLOUR_LIB_VERSION);
map2colour mc;
float values[7] = { 1, 2, 3, 4, 5, 6, 7 };
@ -76,11 +74,8 @@ unittest(test_constructor_II)
}
unittest(test_constants)
{
fprintf(stderr, "MAP2COLOUR_LIB_VERSION: %s\n", (char *) MAP2COLOUR_LIB_VERSION);
assertEqual(M2C_BLACK ,0x00000000);
assertEqual(M2C_SILVER ,0x00C0C0C0);
assertEqual(M2C_GRAY ,0x00808080);
@ -101,6 +96,59 @@ unittest(test_constants)
unittest(test_compare_RGB)
{
map2colour mc;
map2colourFast mcf;
float values[7] = { 0, 32, 64, 128, 256, 512, 1024 };
mc.begin(values);
mcf.begin(values);
int fails = 0;
for (float i = 0; i <= 1024; i++)
{
uint32_t rgb1 = mc.map2RGB(i);
uint32_t rgb2 = mcf.map2RGB(i);
// assertEqual(rgb1, rgb2); // do not want 1024 output lines.
if (rgb1 != rgb2)
{
fprintf(stderr, "%d\n", i);
fails++;
}
}
assertEqual(0, fails);
}
unittest(test_compare_RGB565)
{
map2colour mc;
map2colourFast mcf;
float values[7] = { 0, 32, 64, 128, 256, 512, 1024 };
mc.begin(values);
mcf.begin(values);
int fails = 0;
for (float i = 0; i <= 1024; i++)
{
uint16_t rgb1 = mc.map2_565(i);
uint16_t rgb2 = mcf.map2_565(i);
// assertEqual(rgb1, rgb2); // do not want 1024 output lines.
if (rgb1 != rgb2)
{
fprintf(stderr, "%d\n", i);
fails++;
}
}
assertEqual(0, fails);
}
unittest_main()