C/C++ Question how to pass float variables

Michael1968

Active Member
Licensed User
Longtime User
Hi,
I'm working on a wrapper for a i2c gyro.
in the *.ccp file i have this function:
Objective-C:
void M5_MPU6886::getGyro(float* gx, float* gy, float* gz) {
  float gRes = 2000.0 / 32768.0;
  *gx = (int16_t)((readByte(0x43) << 8) | readByte(0x44)) * gRes;
  *gy = (int16_t)((readByte(0x45) << 8) | readByte(0x46)) * gRes;
  *gz = (int16_t)((readByte(0x47) << 8) | readByte(0x48)) * gRes;
}

is there a way to pass the float variables to b4r
or is it better to pass a int array and do the calculation in b4r ?

best regards
Michael
 

candide

Active Member
Licensed User
in B4X documentation:
Double (4 bytes) 4 bytes floating point. Similar to Float in other B4x tools.
Float is the same as Double. Short is the same as Int.
in arduino:
Floating-point numbers can be as large as 3.4028235E+38 and as low as -3.4028235E+38. They are stored as 32 bits (4 bytes) of information.

if my understanding is correct, in your case Double should work...
 

Michael1968

Active Member
Licensed User
Longtime User
yes i read this....but it does not work
 

candide

Active Member
Licensed User
in fact it should work if we use variable and not pointers :

in arduino cpp file :
B4X:
void I2C_MPU6886::getAccel(float ax, float ay, float az) {
  float aRes = 8.0 / 32768.0;
  ax = (int16_t)((readByte(0x3b) << 8) | readByte(0x3c)) * aRes;
  ay = (int16_t)((readByte(0x3d) << 8) | readByte(0x3e)) * aRes;
  az = (int16_t)((readByte(0x3f) << 8) | readByte(0x40)) * aRes;
}

void I2C_MPU6886::getGyro(float gx, float gy, float gz) {
  float gRes = 2000.0 / 32768.0;
  gx = (int16_t)((readByte(0x43) << 8) | readByte(0x44)) * gRes;
  gy = (int16_t)((readByte(0x45) << 8) | readByte(0x46)) * gRes;
  gz = (int16_t)((readByte(0x47) << 8) | readByte(0x48)) * gRes;
}

void I2C_MPU6886::getTemp(float t) {
  t = 25.0 + ((readByte(0x41) << 8) | readByte(0x42)) / 326.8;
}

in arduino h file
B4X:
    void getAccel(float ax, float ay, float az);
    void getGyro(float gx, float gy, float gz);
    void getTemp(float t);

in wrapper cpp file:
B4X:
    void B4RI2C_MPU6886::getAccel(double ax, double ay, double az)
    {
       i2c_mpu6886.getAccel(ax, ay, az);
    }

    void B4RI2C_MPU6886::getGyro(double gx, double gy, double gz)
    {
       i2c_mpu6886.getGyro(gx, gy, gz);
    }

    void B4RI2C_MPU6886::getTemp(double t)
    {
       i2c_mpu6886.getTemp(t);
    }

in wrapper h file:
B4X:
            void getAccel(double ax, double ay, double az);
            void getGyro(double gx, double gy, double gz);
            void getTemp(double t);
and in B4X :
B4X:
    MPU.getAccel(gx,gy,gz)
 

Michael1968

Active Member
Licensed User
Longtime User
hi candide,
it would not work....
i don#t find the fault.
can you pls send me the files you edit ???

thx
Michael
 

Michael1968

Active Member
Licensed User
Longtime User
hi candide...
thank you for your help.
i think it work with i2c bus0 .
but i have a second i2cbus.

how to Initialize the second i2c bus?
in the lib with pointers this works :
B4X:
void B4RM5_MPU6886::Initialize() {
        Wire1.begin(21, 22);
        
         rs = new (be) M5_MPU6886(I2C_MPU6886_DEFAULT_ADDRESS, Wire1);
        
        rs->begin();
    }

sorry for the stupid question
 

candide

Active Member
Licensed User
can you share link on library you use? and wrapper for B4R used ?

and how do you manage a second I2C bus ? is it a second hard bus or a softWire?

can you look a library attached?
 

Attachments

  • rI2C_MPU6886.zip
    10.8 KB · Views: 282
Last edited:

Michael1968

Active Member
Licensed User
Longtime User
Thank you very much for your help,

i learned much about pointers ,variable an wrap library the last days.

can you share link on library you use? and wrapper for B4R used ?

and how do you manage a second I2C bus ? is it a second hard bus or a softWire?

can you look a library attached?

I have rewritten the library code and now the second i2c hard bus work.

I have test both examples

the lib use variable -> in funktion the result is ok (2.38)-> in B4R Log =0
the lib use pointers -> in funktion the result is ok(2.38) -> in B4R Log =0

so the problem must be in passing the value from function to b4r


thanks for any help

Michael
 

Michael1968

Active Member
Licensed User
Longtime User
ok...
i did not find an answer for this moment.
i use inline C and it works for me.

Michael
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…