Skip to content

Conversation

@BeSeeTek
Copy link

I'm using this drivers with C++ Code.

I needed some time to figure out how to use the hande to pass the instance pointer to my nonstatic member functions.
An example would have helped me a lot.
There for i sugest to add an c++ example to the readme like this one below and in the the pull request

  • Using the handle to integrate this driver in c++ classes

non static member functions of classes need an pointer to their own instance as first argument this is implicit done by the compiler.
This libraries pass the ctx.handle as first argument to the read and write Functions so we can store the pointer to the class instance there.
When defining out platform member functions we must skip the first argument since this will be the this pointer
See example:

class MemsSensor{
public:
  ...
private:
  int32_t platform_write(uint8_t reg, const uint8_t *bufp,uint16_t len);
  int32_t platform_read(uint8_t reg, uint8_t *bufp,uint16_t len);
  stmdev_ctx_t _dev_ctx={(stmdev_write_ptr)  &MemsSensor::platform_write,
		  	  	    (stmdev_read_ptr)    &MemsSensor::platform_read,
						(stmdev_mdelay_ptr)  NULL, // no delay function set
						                     this};
}

With in the classes code the driver functions can than easily be used with &_dev_ctx as first argument like

lsm6dsrx_device_id_get(&_dev_ctx, &_whoamI);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants