B4R Question inline c for ads1115 [solved]

Hans- Joachim Krahe

Active Member
Licensed User
Longtime User
Hi, I had some succes with inline c reading and writing the gain.

Code:
void ads_gain_get (B4R::Object* o) {
  b4r_main::_ads_gain = (int)ads.getGain();
}

void ads_gain_set (B4R::Object* o) {
ads.setGain(GAIN_TWO);
}

... but don't know, if or how I can put the content 'GAIN_TWO' into a string. Do I need to write an inline for every gain, or can I call ads_gain_set with different strings? Should be something like "(o->toString())" ?



thanks
 
Last edited:

tigrot

Well-Known Member
Licensed User
Longtime User
you could use :
B4X:
void ads_gain_set (B4R::Object* o) {
ads.setGain((int)b4r_main::_ads_gainin);
}
You only have to assing a value to the integer ads_gainin in main module.
Or you could write
B4X:
void ads_gain_set (B4R::Object* o) {
ads.setGain((int)o);
}
to call this code you must write
B4X:
 RunNative("ads_gain_set", gain)
where gain is a integer.
 
Upvote 0

Hans- Joachim Krahe

Active Member
Licensed User
Longtime User
hm, does not match. ads.setGain is waiting for name of an constant, that is defined in ccp

'{
' GAIN_TWOTHIRDS = ADS1015_REG_CONFIG_PGA_6_144V,
' GAIN_ONE = ADS1015_REG_CONFIG_PGA_4_096V,
' GAIN_TWO = ADS1015_REG_CONFIG_PGA_2_048V,
' GAIN_FOUR = ADS1015_REG_CONFIG_PGA_1_024V,
' GAIN_EIGHT = ADS1015_REG_CONFIG_PGA_0_512V,
' GAIN_SIXTEEN = ADS1015_REG_CONFIG_PGA_0_256V
'} adsGain_t;

so ads.setGain(GAIN_TWOTHIRDS); works, but not ads.setGain((int)o);

makes error:
initializing argument 1 of 'void Adafruit_ADS1015::setGain(adsGain_t)' [-fpermissive]
void setGain(adsGain_t gain);
^
exit status 1

.. or did I understand it wrong?
 
Upvote 0

tigrot

Well-Known Member
Licensed User
Longtime User
B4X:
#if C
#include "Adafruit_ADS1015.h"
void abs_gain_set (B4R::Object* o) {
  Adafruit_ADS1015 abs;
 switch((int)o)
 {
  case 0:
    abs.setGain(GAIN_TWOTHIRDS);
    break;
  case 1:   
    abs.setGain(GAIN_ONE);
    break;
  case 2:
    abs.setGain(GAIN_TWO);
    break;
  case 3:
    break;
   // .......
   //.....
 }
}
#End If

I have used the library above. Pass an integer from 0 to n, I have defined only 3 cases.

Ciao
Mauro
 
Upvote 0

tigrot

Well-Known Member
Licensed User
Longtime User
probably this is the correct code:
B4X:
#if C
#include "Adafruit_ADS1015.h"
void abs_gain_set (B4R::Object* o) {
  Adafruit_ADS1015 abs;
 switch((int)o->toLong())
 {
  case 0:
    abs.setGain(GAIN_TWOTHIRDS);
    break;
  case 1:   
    abs.setGain(GAIN_ONE);
    break;
  case 2:
    abs.setGain(GAIN_TWO);
    break;
  case 3:
    break;
   // .......
   //.....
 }
}
#End If

see switch() line...
 
Upvote 0
Top