Skip to content

option to pass I2C object #4

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

Merged
merged 3 commits into from
Jul 26, 2018
Merged

option to pass I2C object #4

merged 3 commits into from
Jul 26, 2018

Conversation

pilotak
Copy link
Contributor

@pilotak pilotak commented Jun 18, 2018

Ability to pass I2C object

@@ -21,11 +21,26 @@
I2CEEBlockDevice::I2CEEBlockDevice(
PinName sda, PinName scl, uint8_t addr,
bd_size_t size, bd_size_t block, int freq)
: _i2c(sda, scl), _i2c_addr(addr), _size(size), _block(block)
: _i2c_p(new I2C(sda, scl)), _i2c(*_i2c_p), _i2c_addr(addr),
Copy link
Contributor

@geky geky Jun 18, 2018

Choose a reason for hiding this comment

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

Could you instead use placement new? That way the class still uses the memory needed for the I2C object and we don't have to rely on dynamic memory:

uint32_t _i2c_buffer[sizeof(I2C) / sizeof(uint32_t);
I2C *_i2c;
_i2c = new (_i2c_buffer) I2C(sda, scl);
_i2c->~I2C(); // note needs manual delete

@pilotak
Copy link
Contributor Author

pilotak commented Jun 18, 2018

I don't understand the _i2c_buffer bit, but it works without it fine

@pilotak
Copy link
Contributor Author

pilotak commented Jul 25, 2018

can this be merged?

@geky
Copy link
Contributor

geky commented Jul 25, 2018

@pilotak Oh sorry! This needs to be using placement new so static memory errors get caught at compile time. Mind if I modify your pr?

* @param freq The frequency of the I2C bus, defaults to 400K.
*/
I2CEEBlockDevice(
I2C &i2c_obj, uint8_t address,
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Using a pointer here would make it clear the I2C object is being borrowed.

@geky
Copy link
Contributor

geky commented Jul 25, 2018

Oh you're fast. I added the placement new change.

@pilotak does this look good to you?

@pilotak
Copy link
Contributor Author

pilotak commented Jul 26, 2018

that's perfect, this way you can actually see how much space it takes during compile

@geky geky merged commit 3839563 into ARMmbed:master Jul 26, 2018
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