Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
9 years, 8 months ago.
Creating a class to use mbed SPI
Hello,
I am trying to create a class(see below) that uses the mbed SPI. I know that I can construct an SPI object like SPI device(PE_6, PE_5, PE_2); and pass a pointer of &device into MyClass. I know that I could create a constructor on MyClass with an initializer list to init SPI object. I know that I could pass in the pins and create an SPI object inside the class.
I don't want to do any of these. I want to create a MyClass object. Then call the MyClass.begin(device) or MyClass.begin(SPI(PE_6, PE_5, PE_2)) function to pass in the SPI object. Is this possible to do? I get an error, that the class does not have a function call to mbed::SPI::SPI(). I understand why this is happening. I am just wondering is there a way to construct a class like this or something similar. Is it possible that a default SPI constructor SPI() could be added to the mbed SPI library to accommodate this type of style?
Thanks
- include "mbed.h"
class MyClass { public: MyClass() {} void begin(SPI spi); private: SPI _spi; };
void MyClass::begin(SPI spi){ _spi=spi; }
SPI device(PE_6, PE_5, PE_2);
int main() { Initialize class w/o initializing SPI.
MyClass x;
Use MyClass begin function;
x.begin(device) alternatively tried y.begin(SPI(PE_6, PE_5, PE_2))
In constructor 'MyClass::MyClass()': error: no matching function for call to 'mbed::SPI::SPI()' }
2 Answers
9 years, 8 months ago.
Would something like this be acceptable? Pass a pointer to a new instance and have your class take ownership of cleaning up the object when it's finished.
class MyClass { public: MyClass() {_spi = NULL;}; ~MyClass() { if (_spi) delete _spi; }; void begin(SPI *spi) { if (_spi) delete _spi; _spi=spi; } private: SPI *_spi; } MyClass myClassInstance; main () { myClassInstance.begin(new SPI(pins...)); }
Andy,
This looks interesting. Why "void begin(SPI *spi) { ->if (_spi) delete _spi;<- _spi=spi; }"? Is that a typo, between the ><-
Thanks
posted by 26 Feb 2015No typo. If you only call begin once then it has no effect. But without that line if you called begin twice the first SPI isn't cleaned up and you leak memory. It's one of those things that isn't needed if you are very carful how you use the class but for such little effort you eliminate the risk. In this situation it may be excessive but it's a good habit to have and makes for far safer code.
posted by 26 Feb 20159 years, 8 months ago.
Quote:
I know that I could pass in the pins and create an SPI object inside the class.
Why not that option? That seems like the best solution for your requirements.
Alternative is accepting pointer to SPI definition in your connect function, however then you need to make sure that your SPI definition stays valid: That will go fine if you have global SPI defined, but not necesarily if you have it defined in the begin function itself.