-
Notifications
You must be signed in to change notification settings - Fork 5
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
Conversation
I2CEEBlockDevice.cpp
Outdated
@@ -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), |
There was a problem hiding this comment.
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
I don't understand the |
can this be merged? |
@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? |
I2CEEBlockDevice.h
Outdated
* @param freq The frequency of the I2C bus, defaults to 400K. | ||
*/ | ||
I2CEEBlockDevice( | ||
I2C &i2c_obj, uint8_t address, |
There was a problem hiding this comment.
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.
Oh you're fast. I added the placement new change. @pilotak does this look good to you? |
that's perfect, this way you can actually see how much space it takes during compile |
Ability to pass I2C object