75 views
in System Integration by

I have two float properties ("Transmitted_Power" and "Reflected_Power") which I pass by reference to a method  "I2C_Get_Transmitted_Reflected" using the followin syntax

var bool result = I2C_Get_Transmitted_Reflected(^RF_Driver::Device.Transmitted_Power, ^RF_Driver::Device.Reflected_Power);

The method "I2C_Get_Transmitted_Reflected" has the follwing conains the following input parameters (two references to float properties):
 

method bool I2C_Get_Transmitted_Reflected

(

arg float^ transmitted_mw

arg float^ reflected_mw

)

and contains the following code:

$if $prototyper

  var float random_number1 = math_rand( 0.0, 15000.0 );
  var float random_number2 = math_rand( 0.0, 15000.0 );

  transmitted_mw^ = random_number1;
  reflected_mw^ = random_number2;
  return true;

$endif

$if !$prototyper
 
  var bool result = false;

  native ( result, transmitted_mw, reflected_mw)
  {
     // get transmitted counts
    XInt16 DeviceDriver_Get_TransmittedCounts( void );
    XInt16 transmitted_counts = DeviceDriver_Get_TransmittedCounts();

    if(transmitted_counts != -99)
    {
      // get reflected counts 
      XInt16 DeviceDriver_Get_ReflectedCounts( void );
  	  XInt16 reflected_counts = DeviceDriver_Get_ReflectedCounts(); 

      if(reflected_counts != -99)
      {   
        XBool ConvertBidir(XRef transmitted_mw, XRef reflected_mw, XInt16 value_CH0, XInt16 value_CH1);
        result = ConvertBidir(transmitted_mw, reflected_mw, transmitted_counts, reflected_counts);
      }
    }
  }

  return result;
$endif

The method works perfectly fine in prototyper mode. However, when I pass the property references ("transmitted_mw" and "reflected_mw") to an external C function ""ConvertBidir" I run into trouble.

I pass the property references in the from of an XRef to the external function. The XRef struct contains an "Object" field which is an void pointer to the memory address of the floating point number and an "OnGet" and "OnSet" function pointer.

I can change the variable in the external C function by for example running 

*(transmitted_mw.object) = 123.0;

But this does not run the internal "onset"  EmWi function that is normally called when the variable has been changed. How can I implement this ? How do I call the "onset" function from the "OnSet" function pointer that is present in the XRef struct ? 

Many thanks in advance,

Jim

 

 

 

 

 

 

1 Answer

0 votes
by
 
Best answer

Hello Jim,

the following code in its actual form will not work:

*(transmitted_mw.object) = 123.0;

The mentioned pointer does not refer the property but the entire object containing the property. To read/write the property the getter/setter function has to be invoked explicitly. For example, the write access to a property via its reference is generated as following C code:

EwOnSetFloat( transmitted_mw, 123.0f );

The best approach is, you implement the operation in Chora, generate code and analyse the resulting code. Then you can take over the relevant code fragments.

From the provide code fragments it is unclear for me, what the function  ConvertBidir() does. If the function stores the passed property references for later use (e.g. in a global variable), your application will probably crash when the stored reference is used and the corresponding Chora object has been freed in the meantime. See also the section: Don't retain nor modify objects or strings.

Does it help you?

Best regards

Paul Banach

by

Dear Paul,

Thanks for your answer. The trick of implementing in Chora and then analyzing the automatically generated code is a very good idea. I will certainly use this trick more often in the future !

I have now implemented the function "ConvertBidir" in the following way. The output properties (transmitted_mw and reflected_mw) are linked so they must be computed simultaneously.

XBool ConvertBidir(XRef transmitted_mw, XRef reflected_mw, XInt16 value_CH0, XInt16 value_CH1)
{

	 float transval;
	 float reflval;

	 XBool status = Bidir_Convert_Counts_To_Powers( &transval, &reflval, value_CH0, value_CH1);

	 EwOnSetFloat(transmitted_mw, (XFloat) transval);
	 EwOnSetFloat(reflected_mw, (XFloat) reflval);

	 return status;
}

Best,

Jim

by
Hello Jim,

thank you for the further details. So far I see, your implementation is correct.

Best regards

Paul Banach

Ask Embedded Wizard

Welcome to the question and answer site for Embedded Wizard users and UI developers.

Ask your question and receive answers from the Embedded Wizard support team or from other members of the community!

Embedded Wizard Website | Privacy Policy | Imprint

...