If you passed a pointer instead of a instance would you test to see if the pointer = NULL in the class member Begin()? before trying to use it.
Yes, and anywhere else you want to dereference the pointer:
if(printer)
printer->print("whatever");
If you passed a pointer instead of a instance would you test to see if the pointer = NULL in the class member Begin()? before trying to use it.
Yes, and anywhere else you want to dereference the pointer:
if(printer)
printer->print("whatever");
PaulS:
It does, and it would, IF you knew that the HardwareSerial instance had been created. Do you KNOW that? Or, are you only hoping that your constructor gets called after the HardwareSerial constructor?MyLibrary myLibrary(Serial);Here you are passing a possibly unvalued variable to the constructor of your class. Does that really seem like a good idea?
It would be so much easier to pass a pointer to your class than to pass an instance to your class. Why is that not an acceptable possibility?
I hear what your saying, and I believe in a C++ program I should be able to deterministically tell the order of creation, but I have no idea what the Arduino IDE is doing under the hood. so I made some assumptions as bad as that might seem.
If I pass a pointer to my class of type HardwareSerial I still need to ensure the Serial class is created first.
I'll need to think on this for a while. Thanks for your input today.
Cheers Kb
but I have no idea what the Arduino IDE is doing under the hood. so I made some assumptions as bad as that might seem.
It's not bad, as long as you understand that they are assumptions, and that you are will to make changes when the assumptions prove incorrect.
If I pass a pointer to my class of type HardwareSerial I still need to ensure the Serial class is created first.
That depends. If you (try to) pass the pointer to the constructor, then, yes, it is a problem. If, instead, you pass the pointer to the begin() method, and you call the begin() method in setup(), then you can be confident that the Serial instance will exist, and you can pass a pointer to it to your method.
Well I seem to be stuck trying to implement what you are referring to. I understand that if I don't declare a constructor C++ will create a default one for me so in this simple example I should be able to get away with not declaring a constructor. so I re-wrote the example to call the constructor with no arguments, I created a begin function which should accept a HardwareSerial pointer. But When I compile this I get compiler errors.
.ino
#include "MyLibrary.h"
MyLibrary myLibrary();
void setup() {
HardwareSerial* hwPrint;
hwPrint = &Serial;
myLibrary.begin(hwPrint);
myLibrary.test();
}
void loop() {
myLibrary.test();
delay(1000);
}
.cpp
#include "MyLibrary.h"
#include "HardwareSerial.h"
void MyLibrary::begin(HardwareSerial* hwPrint) {
printer = hwPrint; //operate on the address of print
if(printer) {
printer->begin(115200);
}
}
void MyLibrary::test() {
if(printer) {
printer->println("Hello library with serial connectivity!");
}
}
.h
#ifndef _MYLIBRARY_H
#define _MYLIBRARY_H
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
class MyLibrary {
public:
//pass a reference to a Print object
void begin(HardwareSerial* hwPrint);
void test();
private:
HardwareSerial* printer;
};
#endif
Errors:
HardwareSerialClassExampleSplit_V2.cpp: In function 'void setup()':
HardwareSerialClassExampleSplit_V2:8: error: request for member 'begin' in 'myLibrary', which is of non-class type 'MyLibrary ()()'
HardwareSerialClassExampleSplit_V2:9: error: request for member 'test' in 'myLibrary', which is of non-class type 'MyLibrary ()()'
HardwareSerialClassExampleSplit_V2.cpp: In function 'void loop()':
HardwareSerialClassExampleSplit_V2:12: error: request for member 'test' in 'myLibrary', which is of non-class type 'MyLibrary ()()'
This is probably something trivial, but I can't seem to find a good reference to what the error is cause by.
Cheers Kb
MyLibrary myLibrary();
The parentheses are needed to pass arguments to the constructor. If there are no arguments, there are no parentheses.
Remove them, and see what that does to the error messages.
Thanks, That solves it. No errors and the code functions, i.e. I get prints to the serial port.
Thanks again so much. Now on to designing my library properly. (that could take a while) But I really appreciate learning how to use these objects correctly.
Cheers Kb
No errors and the code functions, i.e. I get prints to the serial port.
Great. It's easy after 30 years.
i found this code useful in my own projects. thank you.
for altercations and advancements pertaining to your base
code i will post a polished version to further the gratitude.