Hi all,
I want convert some code from my Arduino libraries I wrote, to B4X enviroment, and I've encountered some problems.
This is a small part of Arduino code I want to convert:
As you can see, I pass to functions a pointer of variables I want to modify, then the functions modify it and variables changes it's values in my main code.
As you can see any function return a Boolean value to confirm values I passed are really writed or readed.
To read/write values in my main code, this can be do in a simple way, eg:
I think on B4X can be used global variables, but as you see "mySample" can be an unsigned byte, a short, float, double etc...
What is the right way to do this?
Many thanks
I want convert some code from my Arduino libraries I wrote, to B4X enviroment, and I've encountered some problems.
This is a small part of Arduino code I want to convert:
B4X:
bool WaveFile::ReadSample(uint8_t& sample) {
if (GetBitsPerChannel() != 8) {
error = "Read sample size mismatch (need uint8_t)";
return false;
}
return ReadRaw((uint8_t*) &sample, 1);
};
bool WaveFile::WriteSample(uint8_t sample) {
if (GetBitsPerChannel() != 8) {
error = "Write sample size mismatch (need uint8_t)";
return false;
}
return WriteRaw((uint8_t*) &sample, 1);
};
bool WaveFile::ReadSample(short & sample) {
if (GetBitsPerChannel() != 16) {
error = "Read sample size mismatch (need short)";
return false;
}
return ReadRaw((uint8_t*) &sample, 2);
};
bool WaveFile::WriteSample(short sample) {
if (GetBitsPerChannel() != 16) {
error = "Write sample size mismatch (need short)";
return false;
}
return WriteRaw((uint8_t*) &sample, 2);
};
As you can see, I pass to functions a pointer of variables I want to modify, then the functions modify it and variables changes it's values in my main code.
As you can see any function return a Boolean value to confirm values I passed are really writed or readed.
To read/write values in my main code, this can be do in a simple way, eg:
B4X:
uint8_t mySample
wave.ReadSample(mySample); ' here mySample contain a new read value of type uint8_t (unsigned byte)
'or
short mySample;
wave.ReadSample(mySample); ' here mySample contain a new read value of type short
'or
short sampleL, sampleR;
wave.ReadSample(sampleL); ' here sampleL contain a new read value of type short
wave.ReadSample(sampleR); ' here sampleR contain a new read value of type short
'or
uint8_t mySample
bool success = wave.ReadSample(mySample); ' here mySample contain a new read value of type uint8_t (unsigned byte)
if(success)
' process mySample
else
' show error
I think on B4X can be used global variables, but as you see "mySample" can be an unsigned byte, a short, float, double etc...
What is the right way to do this?
Many thanks
Last edited: