Hi,
there is a function getServerData(), defined in a cpp-file.
byte getServerData(word rvon, byte anz, word* wmean, word* wmin, word* wmax, byte* morePeaks) {
/* computes the mean of a sequence of data in warray[] which is a global array.
warray[] is a ring buffer with length BUFFLEN. rbuf_idx is the index containing the latest value.
rvon is the start index of the sequence relative to rbuf_idx.
anz is the length of the sequence.
the function performs some more computations which are not relevant here. */
int j, i;
rvon=rbuf_idx-rvon+1;
if (rvon>BUFFLEN) rvon += BUFFLEN; // jetzt ist rvon der Startindex (geht auch mit word)
*wmean=*wmin=*wmax=warray[rvon];
j=rvon;
for (i=1; i<anz; i++) {
j++;
if (j>BUFFLEN-1) j=0; // Ueberlauf
*wmean += warray[j];
}
*wmean = (*wmean+(anz>>1))/anz;
...
This function once is called from a sketch, which works fine:
// global declarations for the sketch:
word wmean, wmin, wmax;
byte wdir, npeaks;
...
loop() {
...
wdir = getServerData(257, 19, &wmean, &wmin, &wmax, &npeaks);
...
}
The function also is called from within another function which fails:
void SdPlayClass::talkSequence(void) {
word wspeed, wsmin, wsmax,
byte wd, npeaks;
wd = getServerData(13, 13, &wspeed, &wsmin, &wsmax, &npeaks);
...
}
Shifting the local variables within void SdPlayClass::talkSequence(void) outside the function as global variables of the module works fine again:
word wspeed, wsmin, wsmax,
byte wd, npeaks;
...
void SdPlayClass::talkSequence(void) {
wd = getServerData(13, 13, &wspeed, &wsmin, &wsmax, &npeaks);
...
}
I don't understand why talkSequence() is delivering a totally wrong value for wspeed (and the other variables too) when the data transferred to the function are declared as local variables.
Can somebody explain it to me?
SupArdu