Hi… I’ve been pulling my hair out for a couple of days with this one…
I’ve extensively searched online, and also the arduino forum, but no one has posted a complete example with both the cpp & h files, so I only get half the story of one way to do this…
So… I have plenty of experience in programming in Python, Propeller SPIN / ASM & PIC ASM/Basic, so I have some experience… and I’ve ‘hacked’ other peoples sketches to do what I want them to do, but C pointers & references, and declaring/storing them are confusing the hell out of me…
SO… My aim is, I want to pass references to two Stream objects to the constructor of my library, and save them as member vars, so I can use the serial ports from within any method in my library class (sorry if I’m getting terminology wrong)
I’ve tried lots of stuff (using the * notation for pointers instead, directly calling Serial.begin, changing whether its Stream& USB or Stream &USB) … Nothings working
I’ve copied and pasted examples of the forum, as a get me started… still wont compile
CH376_Demo.ino
#include <CH376.h>
#include <SoftwareSerial.h>
SoftwareSerial USB(10,11);
CH376 FS(Serial, USB);
void setup() {
// put your setup code here, to run once:
FS.testMethod();
}
void loop() {
// put your main code here, to run repeatedly:
}
CH376.h
#ifndef CH376_H
#define CH376_H
#include "Arduino.h"
class CH376
{
public:
/* ****** Constructor method ******
* pass a reference to stream objects so either hardware
* or software serial can be used for either port, for flexibility...
*/
CH376(Stream &usbSerPort, Stream &debugSerPort);
//Test method that uses the debug port
void testMethod();
private:
//Private references to stream (serial port) objects
Stream& USB;
Stream& DEGUG;
};
#endif
CH376.cpp
#include "Arduino.h"
#include "CH376.h"
Stream& USB;
Stream& DEGUG;
CH376::CH376(Stream &usbSerPort, Stream &debugSerPort) {
//Save references
USB = usbSerPort;
DEBUG = debugSerPort;
//Start CH376 port
USB.begin(9600);
//Start debug port
DEBUG.begin(9600);
}
void CH376::testMethod(){
bool alwaysTrue = true;
/* Do loads of stuff in this method ...
* then when done, I need to send a debug message
*
* Blah blah blah....
*
* {{ some code }}
* {{ more code }}
* ......
*/
//I'm done.... send a debug or error message or whatever
if (alwaysTrue){
DEBUG.print("Command sent OK");
} else {
DEBUG.print("It went horribly wrong!!");
}
}
CH376_Demo.ino (251 Bytes)
CH376.h (507 Bytes)
CH376.cpp (736 Bytes)