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

Code dedup #33

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Code dedup #33

wants to merge 2 commits into from

Conversation

eringerli
Copy link
Contributor

@eringerli eringerli commented Apr 28, 2022

added methods for converting the raw values to the ones in SI units

This change moves the code for the conversion of raw values to their own virtual methods. This enables the next commit:

LSM6DSO32: removed a lot of duplicated code

With dedicated converter methods, the LSM6DSO32 can do its conversion for
the bigger ranges there, so duplicated code (the whole _read() method) can be removed.
Also, getAccelRange() and setAccelRange() are exactly the same as in
the base class, the difference is only the enum type. Replaced these
methods with a call to the base class with the casted range.

With dedicated converter methods, the LSM6DSO32 can do its conversion for
the bigger ranges there, so duplicated code in _read() can be removed.
Also, getAccelRange() and setAccelRange() are exactly the same as in
the base class, the difference is only the enum type. Replaced these
methods with a call to the baseclass with the casted range.
@eringerli eringerli mentioned this pull request Apr 28, 2022
eringerli added a commit to eringerli/Adafruit_LSM6DS that referenced this pull request May 1, 2022
@ladyada
Copy link
Member

ladyada commented May 2, 2022

these functions should take a value and return a value, or take in a pointer to 3 floats and do the conversion?

@eringerli
Copy link
Contributor Author

eringerli commented May 2, 2022

I thought, as these two methods are protected, they can access the data directly. As virtual methods can't be inlined, I took the most efficient way by doing the conversion in place, without using 6 (+6 in the most used way of accessing/converting temp&acc&gyro) references in code that gets called potentionally thousands of times a second.

I can do it, if you really want to, but I hope I wouldn't have to.

@caternuson
Copy link
Contributor

so duplicated code (the whole _read() method) can be removed.

Don't the other derived classes still need _read() from the LSM6DS base class?

@eringerli
Copy link
Contributor Author

eringerli commented May 4, 2022

The only class which reimpmement it is LSM6DSO32, because it has extended ranges. As the conversion is done in two virtual methods (first commit), I reimplemented the one which is actually different anew. _read() is exactly the same for all the chips in this lib, this could even be made non-virtual.

@caternuson
Copy link
Contributor

Oh, sry, was reading the code diff wrong. The _read was removed from the derived class Adafruit_LSM6DSO32. Not the base class.

OK, this looks like some nice code clean up.

Did you test these changes?

@eringerli
Copy link
Contributor Author

TBH, I don't own an LSM6DSO32. But after I worked on PR #34, the dedup was obvious.

@eringerli
Copy link
Contributor Author

@caternuson It is almost three weeks later. Are there any changes I have to implement?

@caternuson
Copy link
Contributor

Sorry. Wanted to find a time to test this on hardware. Went ahead and set something up for testing.

Looks like something isn't quite correct somewhere.

Running the adafruit_lsm6dso32_test.ino example on an Itsy M4 (PID 3800) with a LSM6DSO32 breakout (PID 4692) with current library produces expected output (accelo held flat on table):
Screenshot from 2022-05-26 14-04-10

With PR code, readings are off:
Screenshot from 2022-05-26 14-03-38

Looks like maybe a factor of 2 somewhere?

@eringerli
Copy link
Contributor Author

I have found the error: getAccelRange() and setAccelRange() are not used in _read() in this branch. I suggest testing PR #34, there the ranges should be correct. I can close this and add the description to #34 if you like.

@ladyada
Copy link
Member

ladyada commented Jun 12, 2022

@eringerli can you amend this PR so it is complete and working? alternatively, can reduce this PR - each PR should be independant so it can be tested/reverted as needed.

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.

3 participants