Skip to content

lsm6ds3tr: avoid unnecessary heap allocations #766

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

Open
wants to merge 1 commit into
base: release
Choose a base branch
from

Conversation

ysoldak
Copy link
Contributor

@ysoldak ysoldak commented Jun 19, 2025

This re-uses internal buffer to avoid unnecessary heap allocations in lsm6ds3tr driver; switches away from the legacy I2C interface.

Copy link
Contributor

@soypat soypat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit that I think would do wonders for readability. I'd love to feature this in a youtube video once it is done if y'all don't mind!

Comment on lines +109 to +111
d.buf[0] = CTRL2_G
d.buf[1] = uint8(d.gyroRange) | uint8(d.gyroSampleRate)
err = d.bus.Tx(d.Address, d.buf[0:2], nil)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we abstract these calls to something like d.writeReg8/readReg8 ?

func (d *Device) writeReg8(addr, value byte) error {
   d.buf[0] = addr
   d.buf[1] = value
   return err = d.bus.Tx(d.Address, d.buf[0:2], nil)
}

func (d *Device) readReg8(addr byte) (byte,error) {
   d.buf[0] = addr
   err := d.bus.Tx(d.Address, d.buf[0:1], d.buf[1:2])
   return d.buf[1], err
}

Copy link
Contributor Author

@ysoldak ysoldak Jun 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just one of many drivers that scream for such improvement, a trial balloon if you wish.
Each of these drivers will have to repeat the pattern.

Any ideas how could we implement these helper functions once and re-use them, instead of implementing in every driver anew?

Copy link
Member

@aykevl aykevl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but I agree with @soypat some helper methods would make the code a bit easier to read.

@ysoldak
Copy link
Contributor Author

ysoldak commented Jun 19, 2025

Nit that I think would do wonders for readability. I'd love to feature this in a youtube video once it is done if y'all don't mind!

Nothing against from my side. All kudos to @aykevl for the idea, btw.

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