B4R Question Pass var from B4R to inline C object declaration

Mostez

Well-Known Member
Licensed User
Longtime User
This is a simple code to turn LED on and off by using PCF8574, the code works as expected, but I want to pass the 'Address' which is 0x27 to object declaration part to let user change it at any time cause address may change according to the chip number(PCF8574AT, PCF8574T).

B4X:
PCF8574 pcf(&I2Ctwo, b4r_stn::_i2caddress); // did not work, how can I pass address to this line?

it did not work, I have to hard code the address like this:
B4X:
PCF8574 pcf(&I2Ctwo, 0x27); //works OK, address is hard codded

Main:
Sub Process_Globals
    Public Serial1 As Serial

End Sub

Private Sub AppStart
    STN.Begin(21,22,0x27) 'pass SDA, SCL and address to STN module
End Sub

STN module:
Sub Process_Globals
    Private SDApin As Byte
    Private SCLpin As Byte
    Private I2Caddress As Byte
End Sub


public Sub Begin(SDAp As Byte, SCLp As Byte, Address As Byte)
    SDApin = SDAp
    SCLpin = SCLp
    I2Caddress = Address
    RunNative("pcfsetup",Null)
    Delay(200)
    RunNative("play",Null)
End Sub

#IF C
#include <PCF8574.h>
    // Instantiate Wire for generic use at 400kHz
    TwoWire I2Cone = TwoWire(0);
    // Instantiate Wire for generic use at 100kHz
    TwoWire I2Ctwo = TwoWire(1);   
    
    //PCF8574 pcf(&I2Ctwo, b4r_stn::_i2caddress); // did not work, how can I pass address to this line?
    PCF8574 pcf(&I2Ctwo, 0x27); //works OK, address is hard codded
    
    
void pcfsetup(B4R::Object* o) {   
    I2Ctwo.begin(b4r_stn::_sdapin, b4r_stn::_sclpin,100000U); // SDA pin, SCL pin, 100kHz frequency
    pcf.pinMode(P0, OUTPUT);
    bool b = pcf.begin();   
}

void play(B4R::Object* o)
{
    pcf.digitalWrite(P0, HIGH);
    delay(2000);
    pcf.digitalWrite(P0, LOW);
}

#End If
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Something like:
B4X:
PCF8574* pcf;
void pcfsetup(B4R::Object* o) {   
    pcf = new PCF8574(&I2Ctwo, b4r_stn::_i2caddress)
    I2Ctwo.begin(b4r_stn::_sdapin, b4r_stn::_sclpin,100000U); // SDA pin, SCL pin, 100kHz frequency
    pcf->pinMode(P0, OUTPUT); //note that usage of -> instead of .
    bool b = pcf->begin();  
}
 
Upvote 0

Mostez

Well-Known Member
Licensed User
Longtime User
That's perfect Erel, thanks a million.
I added ; to this line
B4X:
 pcf = new PCF8574(&I2Ctwo, b4r_stn::_i2caddress);

and play sub changed to:
B4X:
void play(B4R::Object* o)
{
    pcf->digitalWrite(P0, HIGH);
    delay(2000);
    pcf->digitalWrite(P0, LOW);
}

may I ask what does it mean to use PCF8574* pcf; instead of PCF8574 pcf(&I2Ctwo, 0x27);
and what does it mean -> instead of .
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Previously pcf was a variable that held the object directly. Similar to the way that "bool b" holds the boolean value.
Now pcf is a variable that holds a pointer to an object that is allocated at runtime on the heap.

and what does it mean -> instead of .
The arrow operator is a shortcut for the dereference operator + the dot operator:
pcf->digitalWrite = (*pcf).digitalWrite
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…