Hello Girls and Guys,
i am quite new to programming real C++ and only did Arduino before.
I read the “tutorial” about splitting C++ code in different files and sucsessfully implemented it so far.
However i cant get the HardwareTimers to work.
Code:
In Main.cpp i do:
#Include <Arduino.h>
HardwareTimer *Tim1 = new HardwareTimer(TIM1);
Tim1->setMode(3, TIMER_OUTPUT_COMPARE_PWM1, PWM);
and so on, no problem everything works.
If i now want to have a function in a different file say:
In function.cpp i do:
#Include <Arduino.h>
Tim1->setMode(3, TIMER_OUTPUT_COMPARE_PWM1, PWM);
Doesnt work, makes sense, compiler doesnt know what Tim1 is because its not declared there.
I tried:
In main.h i do:
class Tim1;
Together with including main.h
in function.cpp
.
Didnt work.
What i want to do is set a PWM value from mutiple different functions, so i would need to have access to Tim1 from multiple functions.
I tried to extern Tim1
but that also didnt work.
I am struggeling to understand what Tim1 actually is. A Pointer to a object?
I do something wrong which is very obvius for people fluent in C++.
Thanks for Helping!
In your other source files, which need to access Tim1
, simply add one line:
extern HardwareTimer *Tim1;
You might need to add a line above that, if the header for the HardwareTimer library hasn’t been included:
class HardwareTimer;
extern HardwareTimer *Tim1;
Try those.
HTH
Cheers,
Norm.
1 Like
Thanks, that worked flawlessly.
Thats forward declaration right?
I am still having a different but very similar issue.
I made a Class that i use to generate eeprom storage objects. Its quite simple:
EEPROM_RW.h
class EEPROM_RW{
int Adress;
bool type;
int Value;
int ValueOld;
public:
EEPROM_RW(bool ObjType);
int Read();
bool NeedsSaving();
void Set(int NewValue);
void Save();
};
EEPROM_RW.cpp
EEPROM_RW::EEPROM_RW(bool ObjType){
Adress = Used_Adresses;
type = ObjType;
}
int EEPROM_RW::Read(){
return Value;
}
bool EEPROM_RW::NeedsSaving(){
//return stuff
}
void EEPROM_RW::Save(){
//save stuff
}
How would i go about using a class member in a different file?
I cant figure out how i need to do the ?forward declaration?.
Maybe i am just missing a keyword to google.
In the main file, create your main member of the class, call it eerw
.
In the different file, declare an extern EEPROM_RW eerw
to match the one in the main file. Alternatively, a pointer to a class, if that’s how it’s defined in the top level.
You will most likely also need to include the header file for the class.
Then either:
eerw.function(parameters);
// Or, for a pointer if eerw is one.
eerw->function (parameters);
HTH
Cheers,
Norm.