STMicroelectronics / STMems_Standard_C_drivers

Platform-independent drivers for STMicroelectronics MEMS motion and environmental sensors, based on standard C programming language.
BSD 3-Clause "New" or "Revised" License
720 stars 518 forks source link

added c++ usage example for this driver #155

Open BeSeeTek opened 2 years ago

BeSeeTek commented 2 years ago

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

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);