I’m sure my question will seem nonsensical to some but i have been struggling for a couple of days and will really appreciate the support. Long time C programmer but never moved in C++.
I am doing some CEC stuff using the Arduino CEClient library. All completely fine and working well in a simple test file with all functions in a main.ino file. For ease this is the example code provided with the library:
#include "CEClient.h"
#define CEC_PHYSICAL_ADDRESS 0x1000
#define CEC_INPUT_PIN 2
#define CEC_OUTPUT_PIN 3
// create a CEC client
CEClient ceclient(CEC_PHYSICAL_ADDRESS, CEC_INPUT_PIN, CEC_OUTPUT_PIN);
void setup() {
Serial.begin(115200);
// initialize the client with the default device type (PLAYBACK)
ceclient.begin();
// enable promiscuous mode (print all the incoming messages)
ceclient.setPromiscuous(true);
// enable monitor mode (do not transmit)
ceclient.setMonitorMode(true);
}
void loop() {
// run the client
ceclient.run();
}
//CEClient.h
class CEClient : public CEC_Device {
typedef void (*OnReceiveCallbackFunction)(int, int, unsigned char*, int);
typedef void (*OnTransmitCompleteCallbackFunction)(bool);
public:
CEClient(int physicalAddress, int inputPin, int outputPin=-1);
void begin(CEC_DEVICE_TYPE type = CEC_LogicalDevice::CDT_PLAYBACK_DEVICE);
bool isReady();
bool write(int targetAddress, unsigned char* buffer, int count);
int getLogicalAddress();
void setPromiscuous(bool promiscuous);
void setMonitorMode(bool monitorMode);
void onTransmitCompleteCallback(OnTransmitCompleteCallbackFunction);
void onReceiveCallback(OnReceiveCallbackFunction);
void run();
private:
void OnTransmitComplete(bool);
void OnReceive(int source, int dest, unsigned char* buffer, int count);
void OnReady();
OnTransmitCompleteCallbackFunction _onTransmitCompleteCallback;
OnReceiveCallbackFunction _onReceiveCallback;
bool _ready;
};
I am now looking to integrate this code into a much larger project, normally no more than a bit of copy paste but this project is structured into a number of separate files to keep this organised.
My problem quite simply is that I can’t access the ceclient functions from my other files without getting “not available in this scope” type errors. Other functions are much more useful stuff like ceclient.write etc.
If this was C I would solve the issue in moments (global variable in the ino, external in the header of the other file) but my total lack of OO programming has caught me out here. I can’t even find the right terms to search to try and help me solve what I am sure is a trivially simple issue.
@ceenhad
It is not clear from your question, where did this file CEClient.h come from and where is the implementation of all these functions, the headers of which we see in this file
To use the object in other files, include the class header and qualify the object as extern.
# include "CEClient.h"
// Declare the instance (no initializer!)
extern CEClient ceclient;
void someFunction() {
ceclient.sendMessage(...); // Use it as needed
}
From a software design perspective, you should not need to use this library everywhere.
It is best to keep things together that belong together....
One function/module should do one thing...