Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make wire accessing functions virtual #66

Closed
Chris-42 opened this issue Oct 4, 2024 · 4 comments · Fixed by #68
Closed

make wire accessing functions virtual #66

Chris-42 opened this issue Oct 4, 2024 · 4 comments · Fixed by #68
Assignees
Labels
enhancement New feature or request

Comments

@Chris-42
Copy link

Chris-42 commented Oct 4, 2024

will be great to been able to overwrite the methods accessing the wire virtual.
So it will be possible in a derived class to easy access the sensor behind a mux.
virtual bool isConnected();

  virtual uint8_t  readReg(uint8_t reg);
  virtual uint16_t readReg2(uint8_t reg);
  virtual uint8_t  writeReg(uint8_t reg, uint8_t value);
  virtual uint8_t  writeReg2(uint8_t reg, uint16_t value);


I use it like this in my derived ENCODER class:
uint8_t ENCODER::readReg(uint8_t reg) {
  if(!_mux->setMuxChannel(_channel)) {
    _error = AS5600_ERROR_MUX;
    return 0;
  }
  xSemaphoreTake(i2cMutex, portMAX_DELAY);
  uint8_t ret = AS5600::readReg(reg);
  xSemaphoreGive(i2cMutex);
  _mux->freeMux();
  return ret;
}

(updated for syntac highlighting)

@RobTillaart RobTillaart self-assigned this Oct 4, 2024
@RobTillaart RobTillaart added the enhancement New feature or request label Oct 4, 2024
@RobTillaart
Copy link
Owner

Q: how does one knows the _channel?

Does the derived class provides a channel parameter in constructor or setter/getter function for the channel?


Not tested, however I think the setting of the I2C channel should also be under the mutex control.

Otherwise one can change channel in another thread before the actual read is done.

I use it like this in my derived ENCODER class:
uint8_t ENCODER::readReg(uint8_t reg) {
  xSemaphoreTake(i2cMutex, portMAX_DELAY);
  if(!_mux->setMuxChannel(_channel)) {
    xSemaphoreGive(i2cMutex);
    _error = AS5600_ERROR_MUX;
    return 0;
  }

  uint8_t ret = AS5600::readReg(reg);
  xSemaphoreGive(i2cMutex);
  _mux->freeMux();
  return ret;
}

@Chris-42
Copy link
Author

Chris-42 commented Oct 4, 2024

#ifndef AS5600_ENCODER_H
#define AS5600_ENCODER_H

#include "AS5600.h"
#include "i2c_mux.h"

class ENCODER : public AS5600 {
  public:
    const int     AS5600_ERROR_MUX = -300;
    ENCODER(I2CMUX* mux, uint8_t channel);
    //bool init();
    bool isConnected() override;
    float getAngularSpeed(uint8_t mode = AS5600_MODE_DEGREES);
    int32_t getCumulativePosition();
    void update();
  protected:
    uint8_t readReg(uint8_t reg) override;
    uint16_t readReg2(uint8_t reg) override;
    uint8_t writeReg(uint8_t reg, uint8_t value) override;
    uint8_t writeReg2(uint8_t reg, uint16_t value) override;
  private:
    I2CMUX* _mux;
    uint8_t _channel;
    float _angular_speed;
};


#endif

@Chris-42
Copy link
Author

Chris-42 commented Oct 4, 2024

it is only about to be allowed to overide...

RobTillaart added a commit that referenced this issue Oct 5, 2024
@RobTillaart
Copy link
Owner

@Chris-42

I added the virtual keywords in the PR #68
build is running.

@RobTillaart RobTillaart linked a pull request Oct 5, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants