Hey @all,
i would love to know how to set up a struct with some arrays.
the challenging is, that the array size is not know.
The Idea is as following:
there is this really cool MultiMap function (Arduino Playground - MultiMap)
and my idea was to build a struct that holds all needed things for a nice mapping:
struct sMultiMapStruct
{
word mm_In [];
word mm_Out [];
byte size;
};
typedef struct sMultiMapStruct tmm;
for testing:
/****************************************************************************************************
* structTest sketch
*
*
* Created: 22.06.2012
* copyright by Stefan Krueger
* written by Stefan Krueger code@s-light.eu
*
* Last modified: 22.06.2012 21:00
* By Stefan Krueger
*
* changelog / history :
*
*
****************************************************************************************************/
struct sMultiMapStruct
{
word mm_In [];
word mm_Out [];
byte size;
};
typedef struct sMultiMapStruct multiMap_type;
multiMap_type mmMyMap = {
{ 0, 20, 30},
{ 0, 80, 100},
3
};
/**
* MultiMap
* http://arduino.cc/playground/Main/MultiMap
* modified for 16bit word datatype
**/
word multiMap(word val, word* _in, word* _out, uint8_t size)
{
// take care the value is within range
// val = constrain(val, _in[0], _in[size-1]);
if (val <= _in[0]) return _out[0];
if (val >= _in[size-1]) return _out[size-1];
// search right interval
uint8_t pos = 1; // _in[0] allready tested
while(val > _in[pos]) pos++;
// this will handle all exact "points" in the _in array
if (val == _in[pos]) return _out[pos];
// interpolate in the right segment for the rest
return map(val, _in[pos-1], _in[pos], _out[pos-1], _out[pos]);
}
/** struct sMultiMapStruct used so you need no headerfile for struct definition **/
word mapIt(word wInputValue, struct sMultiMapStruct* mmTheMap)
{
// '(*mmTheMap).mm_In' could be also written as 'mmTheMap->mm_In'
return multiMap(wInputValue, (*mmTheMap).mm_In, (*mmTheMap).mm_Out, (*mmTheMap).size);
}
/***************************************************************************************************/
void setup()
{
/**************************************************/
/* Welcom @ This Sketch!! */
/**************************************************/
Serial.begin(115200);
Serial.println();
Serial.println(" ");
Serial.println(" oo ");
Serial.println(" ( ) ");
Serial.println(" ^^ ");
Serial.println();
Serial.println("Welcome to structTest Sketch!");
Serial.println("system is starting!");
Serial.println(":-)");
Serial.println("");
}
void loop()
{
for ( word x = 0; x < 30; x++)
{
Serial.print(x);
Serial.print("-->");
Serial.println(mapIt(x, &mmMyMap ));
//delay(10);
if ( x == 20 )
{
Serial.println("*** 20 ***");
delay(100);
}
}
Serial.println();
delay(500);
Serial.println("**************************************");
delay(500);
Serial.println();
delay(500);
Serial.println("**************************************");
delay(500);
Serial.println();
delay(500);
Serial.println("**************************************");
delay(500);
Serial.println();
}
(
now the problem is - i try to setup the size of the arrays at declaration of the variable.
error:
structTest:32: error: too many initializers for 'word [0]'
structTest:32: error: too many initializers for 'word [0]'
is there some easy possibilities to do this an other way?
sunny greetings
stefan