B4R Question how return bool from inline-c ?

peacemaker

Expert
Licensed User
Longtime User
Hi, All

Is it possible ?



Now for ESP32 it's compiled OK, but if to switch to ESP32-C3 it's errors like:

B4X:
B4R Version: 4.00 BETA #1
Parsing code.    (0.03s)
Building folders structure.    (0.01s)
Compiling code.    (0.10s)
Building project    (0.03s)
Compiling (XIAO_ESP32C3)    Error
P:\GoogleDrive\1Sources\Sensors_ORION\Bluetooth\B4R\orion_bt_sensor\Objects\src\b4r_esp_bh1750.cpp: In function 'B4R::Object* prepare_bh1750(B4R::Object*)':
P:\GoogleDrive\1Sources\Sensors_ORION\Bluetooth\B4R\orion_bt_sensor\Objects\src\b4r_esp_bh1750.cpp:108:85: error: call of overloaded 'wrapNumber(bool)' is ambiguous
     return returnvalue.wrapNumber(lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE));
                                                                                     ^
In file included from P:\GoogleDrive\1Sources\Sensors_ORION\Bluetooth\B4R\orion_bt_sensor\Objects\src\B4RDefines.h:26,
                 from P:\GoogleDrive\1Sources\Sensors_ORION\Bluetooth\B4R\orion_bt_sensor\Objects\src\b4r_esp_bh1750.cpp:1:
P:\GoogleDrive\1Sources\Sensors_ORION\Bluetooth\B4R\orion_bt_sensor\Objects\src\rCore.h:88:11: note: candidate: 'B4R::Object* B4R::Object::wrapNumber(Byte)'
   Object* wrapNumber(Byte i);
           ^~~~~~~~~~
P:\GoogleDrive\1Sources\Sensors_ORION\Bluetooth\B4R\orion_bt_sensor\Objects\src\rCore.h:89:11: note: candidate: 'B4R::Object* B4R::Object::wrapNumber(ULong)'
   Object* wrapNumber(ULong i);
           ^~~~~~~~~~
P:\GoogleDrive\1Sources\Sensors_ORION\Bluetooth\B4R\orion_bt_sensor\Objects\src\rCore.h:90:11: note: candidate: 'B4R::Object* B4R::Object::wrapNumber(Long)'
   Object* wrapNumber(Long i);
           ^~~~~~~~~~
P:\GoogleDrive\1Sources\Sensors_ORION\Bluetooth\B4R\orion_bt_sensor\Objects\src\rCore.h:94:11: note: candidate: 'B4R::Object* B4R::Object::wrapNumber(Int)'
   Object* wrapNumber(Int i);
           ^~~~~~~~~~
P:\GoogleDrive\1Sources\Sensors_ORION\Bluetooth\B4R\orion_bt_sensor\Objects\src\rCore.h:95:11: note: candidate: 'B4R::Object* B4R::Object::wrapNumber(float)'
   Object* wrapNumber(float d);
           ^~~~~~~~~~
P:\GoogleDrive\1Sources\Sensors_ORION\Bluetooth\B4R\orion_bt_sensor\Objects\src\rCore.h:96:11: note: candidate: 'B4R::Object* B4R::Object::wrapNumber(double)'
   Object* wrapNumber(double d);
           ^~~~~~~~~~
Error during build: exit status 1

where code is like:
B4X:
Dim res As Boolean = RunNative("prepare_bh1750", Null)

...
#if C
B4R::Object returnvalue;

B4R::Object* prepare_bh1750(B4R::Object* o) {
...
    return returnvalue.wrapNumber(lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE));
}
 

hatzisn

Well-Known Member
Licensed User
Longtime User
Just cast the " lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE) " to a boolean.
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
And also set the returned value to a B4R::Object*. You say to the compiler I will return A pointer and you return a B4R::Object.
(although I believe you set the object and then return its pointer?... Since the object is local variable does it remain in memory?)

Edit: Sorry my bad. It is global variable.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
I'm a blind kitty with Inline-C :),maybe any example ?
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
I'm a blind kitty with Inline-C :),maybe any example ?

I am an one eyed sleeping kitty which feels sleepy with closing eyes. 😴. I have saved in my YouTube library a 12 hour C++ lesson. Let me get it and I will post it in a while...
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
Here it is. Not 12 hours though but almost 32. 4 full time workdays.

 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
Try this:

C++:
returnvalue = B4R::Object.wrapNumber(lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE));
return returnvalue* ;
 
Last edited:
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
And how bool here casted to B4R.boolean ?


returnvalue = B4R::Object.wrapNumber(lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)); return returnvalue* ;
Error
P:\GoogleDrive\1Sources\Sensors_ORION\Bluetooth\B4R\orion_bt_sensor\Objects\src\b4r_esp_bh1750.cpp: In function 'B4R::Object* prepare_bh1750(B4R::Object*)':
P:\GoogleDrive\1Sources\Sensors_ORION\Bluetooth\B4R\orion_bt_sensor\Objects\src\b4r_esp_bh1750.cpp:109:26: error: expected primary-expression before '.' token
returnvalue = B4R::Object.wrapNumber(lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE));
^
P:\GoogleDrive\1Sources\Sensors_ORION\Bluetooth\B4R\orion_bt_sensor\Objects\src\b4r_esp_bh1750.cpp:110:21: error: expected primary-expression before ';' token
return returnvalue* ;
^
Error during build: exit status 1
 
Last edited:
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Solved as:

B4X:
Dim res As Byte = RunNative("prepare_bh1750", Null)
    If res > 0 Then
....

#if C
BH1750 lightMeter(0x23);
B4R::Object returnvalue;

B4R::Object* prepare_bh1750(B4R::Object* o) {
    bool begun = lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE);
    Byte a = begun;
    return returnvalue.wrapNumber(a);
}

How shortly to cast bool into Byte in C++ ?


p.s.
Googled:
bool input = true;
byte value = *((byte*)(&input)); // 1

//KILL ME ! it's simple ... .As(a bullet in the head)... God bless Erel.As(B4X)...
 
Last edited:
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
Solved as:

B4X:
Dim res As Byte = RunNative("prepare_bh1750", Null)
    If res > 0 Then
....

#if C
BH1750 lightMeter(0x23);
B4R::Object returnvalue;

B4R::Object* prepare_bh1750(B4R::Object* o) {
    bool begun = lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE);
    Byte a = begun;
    return returnvalue.wrapNumber(a);
}

[/CODE]

Is casting a bool to Byte that easy? I did not know that... I always thought (C# and Java oriented thought) it would be something like your second code:

p.s.
Googled:
bool input = true;
byte value = *((byte*)(&input)); // 1
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Seems, just there is no bool type in B4R classes to cast from C++, so i used Byte.
But latest C++ variant from Google - it's ... i could not imagine such "simple" casting...
 
Upvote 0
Top