Hello, I am doing an app with different scenes and I am using ofxSceneManager addon. I would like to be able to use the same serial port between the different scenes, but I can’t because when I change of Scene, the another scene is using this port.
Where should I setup and connect uart port? Maybe in ofApp.cpp?
yes create an ofSerial in ofApp and pass a reference to it to the scenes. usually the easiest is to use a shared_ptr to share the ownership of the object among different objects, like:
ofApp:
//.h
shared_ptr<ofSerial> serial(new ofSerial);
//setup
serial->setup(...)
//create scenes:
for(auto & scene: scenes){
scene.setup(serial);
}
in every scene:
//.h
shared_ptr<ofSerial> serial;
.cpp
void MyScene::setup(shared_ptr<ofSerial> serial){
this->serial = serial;
}
Thank you! It doesn’t work… I think I am not doing it correctly. I have this problem:
Exception thrown: read access violation.
this was nullptr.
If there is a handler for this exception, the program may be safely continued.
be sure that you are setting the serial object to every scene before you start using it, otherwise it’ll be null and the application will crash
also be sure that in ofApp.h when you declare the serial object you also initialize it like:
shared_ptr<ofSerial> serial(new ofSerial);
if you are not sure when the object is going to be initialized you can add in your secenes whenever you are going to use the serial pointer:
if(serial){
serial->read(...)
}else{
ofLogError() << "trying to use null serial from scene blah";
}
I had problems with the word “new”:
So, I read in a book, I should write “shared_ptr serial(new ofSerial);” in ofApp.cpp setup. Whatsmore, that’s the example I read.
void ofApp::setup(){
shared_ptr a(new int);
*a = 5;
vector<shared_ptr > v;
v.push_back(a);
}
And well… If I put “shared_ptr serial(new ofSerial);” in ofApp.cpp, it doesn’t work… but I don’t have problems with the word “new”