diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..0a04128 --- /dev/null +++ b/LICENSE @@ -0,0 +1,165 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. diff --git a/README.md b/README.md new file mode 100644 index 0000000..267586a --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# Map2D3D diff --git a/library.properties b/library.properties new file mode 100644 index 0000000..c6f28ce --- /dev/null +++ b/library.properties @@ -0,0 +1,10 @@ +name=Map2D3D +version=0.1 +author=Arend Lammertink +maintainer=Arend Lammertink +sentence=2D and 3D Maps c.q. lookup tables with interpolation +paragraph= +category=Data Processing +url= +architectures=avr + diff --git a/src/LookUpTable2D3D.h b/src/LookUpTable2D3D.h new file mode 100644 index 0000000..fc15202 --- /dev/null +++ b/src/LookUpTable2D3D.h @@ -0,0 +1,188 @@ +//----------------------------------------------------------------------------- +// +// Copyright (C) 2018 Arend Lammertink +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, version 3. +// +// This library is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public +// License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this library. If not, see . +// +//----------------------------------------------------------------------------- +// Multi-include protection +//----------------------------------------------------------------------------- + +#ifndef _LOOKUP_TABLE_2D3D +#define _LOOKUP_TABLE_2D3D + +//----------------------------------------------------------------------------- +// Includes +//----------------------------------------------------------------------------- + +#include + +//----------------------------------------------------------------------------- +// Debug stuff +//----------------------------------------------------------------------------- + +//#define DEBUG 1 + +#if DEBUG == 1 + #define dprint(expression) Serial.print(F("# ")); Serial.print( F(#expression) ); Serial.print( F(": ") ); Serial.println( expression ) + #define dshow(expression) Serial.println( expression ) +#else + #define dprint(expression) + #define dshow(expression) +#endif + +//----------------------------------------------------------------------------- +// +// Based on: +// https://stackoverflow.com/questions/7091294/how-to-build-a-lookup-table-in-c-sdcc-compiler-with-linear-int16_terpolation +// http://www.cplusplus.com/forum/general/114406/ +// +//----------------------------------------------------------------------------- + + + +//----------------------------------------------------------------------------- +// Linear interpolation with specialization for integer arithmetic +//----------------------------------------------------------------------------- + +template +T interpolate( uint16_t x, uint16_t x1, uint16_t x2, T y1, T y2 ) +{ + uint16_t dx = x2-x1; + int32_t dy = y2-y1; + + return y1 + (x-x1) * (dy/dx); +} + + +// For integer type lookup tables, we incorporate fractions by +// casting into an int32 and shift by 6 bits. + +template<> +uint8_t interpolate( uint16_t x, uint16_t x1, uint16_t x2, + uint8_t y1, uint8_t y2 ) +{ + int32_t _y1 = (static_cast(y1))<<6; + int32_t _y2 = (static_cast(y2))<<6; + + uint16_t dx = x2-x1; + int32_t dy = _y2-_y1; + + dprint(F("uint8_t")); + + return y1 + (( (x-x1) * (dy/dx) )>>6); +} + + +template<> +int8_t interpolate( uint16_t x, uint16_t x1, uint16_t x2, + int8_t y1, int8_t y2 ) +{ + int32_t _y1 = (static_cast(y1))<<6; + int32_t _y2 = (static_cast(y2))<<6; + + uint16_t dx = x2-x1; + int32_t dy = _y2-_y1; + + dprint(F("int8_t")); + + return y1 + (( (x-x1) * (dy/dx) )>>6); +} + + +template<> +uint16_t interpolate( uint16_t x, uint16_t x1, uint16_t x2, + uint16_t y1, uint16_t y2 ) +{ + int32_t _y1 = (static_cast(y1))<<6; + int32_t _y2 = (static_cast(y2))<<6; + + uint16_t dx = x2-x1; + int32_t dy = _y2-_y1; + + dprint(F("uint16_t")); + + return y1 + (( (x-x1) * (dy/dx) )>>6); +} + + +template<> +int16_t interpolate( uint16_t x, uint16_t x1, uint16_t x2, + int16_t y1, int16_t y2 ) +{ + int32_t _y1 = (static_cast(y1))<<6; + int32_t _y2 = (static_cast(y2))<<6; + + uint16_t dx = x2-x1; + int32_t dy = _y2-_y1; + + dprint(F("int16_t")); + + return y1 + (( (x-x1) * (dy/dx) )>>6); +} + + + +template // S: determine size at compile time, T: data type +class Table2D +{ +public: + + int size() { return S; } + + void setXs( const uint16_t* xss ) + { memcpy( xs, xss, S*sizeof(uint16_t) ); } + + void setYs( const T* yss ) + { memcpy( ys, yss, S*sizeof(T) ); } + +#ifdef ARDUINO // Initialize from array in PROGMEM + void setXs_P( const uint16_t* xss ) + { memcpy_P( xs, xss, S*sizeof(uint16_t) ); } + + void setYs_P( const T* yss ) + { memcpy_P( ys, yss, S*sizeof(T) ); } +#endif + + T getValue(uint16_t x) + { + int i; + + if (x < xs[0]) { return ys[0]; } // minimum + if (x > xs[S-1]) { return ys[S-1]; } // maximum + + /* find i, such that xs[i] <= x < xs[i+1] */ + for (i = 0; i < S-1; i++) { + if (xs[i+1] > x) { + break; + } + } + + return interpolate( x, xs[i], xs[i+1], ys[i], ys[i+1] ); + } + +private: + + uint16_t xs[S]; + T ys[S]; + +}; + + + + +//----------------------------------------------------------------------------- +// +//----------------------------------------------------------------------------- + +#endif // End multi-include protection diff --git a/src/src.ino b/src/src.ino new file mode 100644 index 0000000..32bc3a6 --- /dev/null +++ b/src/src.ino @@ -0,0 +1,100 @@ + +#include "LookUpTable2D3D.h" + +#define BAUDRATE 115200 + +/* http://www.gammon.com.au/progmem + * + * AVR Libc, and therefore Arduino provides several standard string and memory + * manipulation functions in versions that address program memory rather than RAM. + * The AVR Libc convention is to append "_P" to the name of the standard function, + * thus we have strcpy_P() and memcpy_P() which are functions used in the above + * examples to copy strings or memory from PROGMEM to RAM. The "source" argument + * is assumed to be in PROGMEM and the destination argument in RAM. Otherwise + * they function like the standard library functions strcpy() and memcpy(). + * Some useful functions of this kind are: + + strcpy_P (dest, PROGMEM src) + strcmp_P (s1, PROGMEM s2) + strncmp_P (s1, PROGMEM s2, len) + memcpy_P (dest, PROGMEM src, len) + sprintf_P (dest, PROGMEM format, item, ... ) + * +*/ + +/* NOTE: xs MUST be sorted */ +const uint16_t xs[] PROGMEM = { 300, 700, 800, 900, 1500, 1800, 2100, 2500 }; +const uint8_t ys[] PROGMEM = { 10, 89, 126, 0, 225, 230, 220, 10 }; +const int8_t yss[] PROGMEM = {-127, -50, 127, 0, 10, -30, -50, 10 }; + +// Some test data from Miata Brain ECU +// https://sourceforge.net/projects/miatabrain/ +const int RPMSteps[16] PROGMEM = + { 256, 512, 1024, 1536, 2048, 2560, 3072, 3584, 4086, 4598, 5120, 5632, 6144, 6656, 7168, 7680}; + +const float tuningMap[256] PROGMEM = + { 2.0, 10.0, 10.0, 28.0, 30.4, 30.4, 30.4, 36.9, 37.6, 40.9, 39.6, 31.3, 32.2, 30.0, 30.0, 30.0, + 2.0, 10.0, 10.0, 28.0, 30.4, 30.4, 30.4, 36.9, 37.6, 40.9, 39.6, 31.3, 32.2, 30.0, 30.0, 30.0, + 2.0, 10.0, 10.0, 28.0, 30.4, 30.4, 30.4, 36.9, 37.6, 40.9, 39.6, 31.3, 32.2, 30.0, 30.0, 30.0, + 2.0, 10.0, 13.1, 24.2, 29.6, 29.8, 30.7, 36.0, 37.6, 39.8, 38.7, 31.3, 32.2, 30.0, 30.0, 30.0, + 2.0, 14.0, 15.3, 22.0, 26.2, 27.3, 27.8, 31.6, 33.1, 34.4, 34.4, 32.4, 32.2, 30.0, 30.0, 30.0, + 2.0, 12.2, 13.3, 19.8, 23.8, 24.9, 25.3, 28.9, 30.9, 32.0, 32.4, 30.2, 31.6, 30.0, 30.0, 30.0, + 2.0, 8.7, 12.0, 18.7, 20.9, 23.3, 24.0, 27.6, 29.1, 30.2, 31.3, 28.4, 29.1, 27.6, 27.8, 27.8, + 2.0, 5.1, 7.6, 16.9, 20.2, 21.8, 22.7, 26.0, 27.8, 29.6, 29.1, 26.2, 27.1, 26.4, 26.7, 26.7, + 2.0, 3.3, 5.6, 15.6, 20.0, 21.3, 22.0, 24.0, 25.6, 27.6, 26.9, 25.1, 26.4, 25.3, 25.3, 25.3, + 2.0, 1.6, 2.7, 12.2, 19.1, 20.9, 21.3, 23.3, 24.2, 25.8, 26.0, 24.0, 25.3, 24.7, 24.9, 24.9, + 2.0, -1.3, 1.1, 8.9, 14.7, 20.2, 20.4, 22.4, 22.2, 23.1, 25.1, 23.3, 24.7, 23.8, 24.2, 24.2, + 2.0, -2.0, -2.0, 4.9, 9.1, 11.8, 17.6, 19.1, 19.1, 22.7, 24.7, 22.7, 23.8, 24.0, 22.9, 22.9, + 2.0, -2.0, -2.0, 4.9, 9.1, 11.8, 14.4, 19.1, 19.1, 19.6, 23.6, 21.3, 22.9, 22.9, 22.9, 22.9, + 2.0, -2.0, -2.0, 4.9, 9.1, 11.8, 14.4, 19.1, 19.1, 19.6, 22.4, 21.3, 22.9, 22.9, 22.9, 22.9, + 2.0, -2.0, -2.0, 4.9, 9.1, 11.8, 14.4, 19.1, 19.1, 19.6, 22.4, 21.3, 22.9, 22.9, 22.9, 22.9, + 2.0, -2.0, -2.0, 4.9, 9.1, 11.8, 14.4, 19.1, 19.1, 19.6, 22.4, 21.3, 22.9, 22.9, 22.9, 22.9}; + + + +void initSerial() +{ + // Open serial port with a baud rate of BAUDRATE b/s + Serial.begin(BAUDRATE); +} + + +void setup() +{ + initSerial(); + + Serial.println( F("Hi there") ); + + Table2D<8, uint8_t> test; + test.setXs_P(xs); + test.setYs_P(ys); + + for( int idx=250; idx<2550; idx+=50) + { + uint8_t val = test.getValue(idx); + Serial.print(idx); + Serial.print( F(": ") ); + Serial.println( (int)val ); + } + + Table2D<8, int8_t> test2; + test2.setXs_P(xs); + test2.setYs_P(yss); + + for( int idx=250; idx<2550; idx+=50) + { + int8_t val = test2.getValue(idx); + Serial.print(idx); + Serial.print( F(": ") ); + Serial.println( (int)val ); + } + +} + + +void loop() +{ + + +} +