Good afternoon dear Forum friends,
Simple question: Inside the loop() method, I want to use a variable updated inside a Class, by means of reading its updated contents from inside other Classes. All these Classes reside inside a Library. How do I do that?
Need clear code example on best way to do this:)
//library definition
//class initialization
//setup (Serial comms)
loop(){
data.listen();
process_A.doTaskA1();
process_A.doTaskA2();
process_B.doTaskB1();
process_B.doTaskB2();
}
My expanded Example:
I want to check Serial line in case a msg has arrived.
- I create a Class called DATA, a Class called PROCESS_A, and a Class called Process_B
- I create the .cpp and the .h structure to call them from a library added to a .ino file
- At the beginning of the loop() the program executes data.listen();
- Once data.listen() has been executed, the other 2 classes are invoked
- The PROCESS_A/PROCESS_B Classes will read the variable "serialData" residing inside Class DATA using an accesor function (getMsgStatus()) and will update variables or execute actions inside them accordingly.
I have not succeeded in Step 5, always get an error 'getMsgStatus()' was not declared in this scope', meaning inside the PROCESS_A/PROCESS_B Classes, even if I declared it public in the DATA Class. I guess I need to declare the usage of an external variable inside the PROCESS_A / PROCESS_B Class but the syntax/structure escapes me. C++ Noob OOP hardship I guess.
Let me know your comments. Structure is below:
.h
#ifndef TARGET_LIB_H
#define TARGET_LIB_H
#include <Arduino.h>
class DATA{
private: String serialData;
public:
void listen();
String getMsgStatus();
};
class PROCESS_A{
private: String rBuffer = "";
public:
void doTask_A1();
void doTask_A2();
};
class PROCESS_B{
private: String rBuffer = "";
public:
void doTask_B1();
void doTask_B2();
};
#endif
.cpp
#include "Target_Lib.h"
void DATA::listen(){
if(Serial.available() > 0)
{
Serial.print("Listening... ");
serialData = Serial.readStringUntil('\n');
Serial.println(serialData);
}
else
{
;
}
}
String DATA::getMsgStatus(){ //getter for Data Class
return(serialData);
}
void PROCESS_A::doTask_A1(){
rBuffer = data.getMsgStatus(); //read var in Class 'DATA' by means of getter method
Serial.println("Task A1 executed. Value captured: " + rBuffer);
}
void PROCESS_A::doTask_A2(){
rBuffer = data.getMsgStatus();
Serial.println("Task A2 executed. Value captured: " + rBuffer);
}
void PROCESS_B::doTask_B1(){
rBuffer = data.getMsgStatus();
Serial.println("Task B1 executed. Value captured: " + rBuffer);
}
void PROCESS_B::doTask_B2(){
rBuffer = data.getMsgStatus();
Serial.println("Task B2 executed. Value captured: " + rBuffer);
}
.ino
#include "Target_Lib.h"
DATA data;
PROCESS_A process_A;
PROCESS_B process_B;
void setup() {
Serial.begin(115200);
}
void loop() {
data.listen();
process_A.doTask_A1();
process_A.doTask_A2();
process_B.doTask_B1();
process_B.doTask_B2();
delay(1000);
}
thanks in advance for your tips and support.
regards
-EZ