system
October 2, 2012, 8:16pm
1
Hi.
I'm programing a PLC under Arduino. We use several libraries to manipulate the inputs, outputs, PWM, memory, etc.
Now, I have a problem with passing constants to a library at compiling time. For example:
const int _pepe_ = 10;
#include <Salidas.h>
Salidas Sal;
void setup() {}
void loop() {}
Salidas library is:
#ifndef Salidas_H
#define Salidas_H
#include "Arduino.h"
class Salidas
{
public:
Salidas();
private:
int a[_pepe_];
};
#endif
I have to pass pepe to the library because it have a list whit "pepe " items.
This form is wrong. ¿why? ¿Do I can pass a constant to library to compile it?
#define _pepe_ 3
Doesn't work too.
The constant has to be in your include file (Salidas.h), otherwise Salidas.cpp won't see it.
Here's one possible solution. Write a pepe.h file conaining this:
#ifndef _PEPE_H
#define PEPE 10
#endif
In main sketch
#include "pepe.h"
In salidas.h:
#include "pepe.h"
This way you'll have just one definition of PEPE.
system
October 2, 2012, 9:03pm
4
Or, you could pass the value to the class constructor, and allocate the array dynamically, freeing it in the destructor.
system
October 2, 2012, 9:08pm
5
So bad. ¿Isn't any way? Because, I use a multiple sketch that compile the same libraries. If I do that, I have to change the "#define " instruction in every library every time that I change the sketch to compile.
"I do that now..."
tuxduino: I'll try this option.
AWOL:
[...] and allocate the array dynamically [...]
I don't have idea that it is posible. I can't find a documentation about that. Do you can give me a little help?
system
October 2, 2012, 9:18pm
6
tuxduino:
Here's one possible solution. Write a pepe.h file conaining this:
#ifndef _PEPE_H
#define PEPE 10
#endif
In main sketch
#include "pepe.h"
In salidas.h:
#include "pepe.h"
This way you'll have just one definition of PEPE.
The salidas.h don't see pepe.h ...
Where did you put pepe.h ?
system
October 2, 2012, 9:39pm
8
I don't have idea that it is posible. I can't find a documentation about that. Do you can give me a little help?
Surely you are aware of malloc() and free() or the C++ counterparts new and delete.
system
October 3, 2012, 12:35am
9
tuxduino:
Where did you put pepe.h ?
In the library .h and .cpp and in the sketch. The file pepe.h is in a second TAB in the arduino IDE.
system
October 3, 2012, 5:27am
10
I solved it using malloc:
In the .h of the library:
int *a;
And in constructor:
a = (int *) malloc(_pepe_ * sizeof(int));
Thanks to AWOL, PaulS and others posters that help me in this.
leonelpepe:
tuxduino:
Where did you put pepe.h ?
In the library .h and .cpp and in the sketch. The file pepe.h is in a second TAB in the arduino IDE.
Ok, next question: where is salidas.h ? If it's in the same folder as pepe.h and the main sketch, it must see pepe.h. Of course, if it's somewhere in the libraries subfolder of your sketchbook, you need to move pepe.h.
leonelpepe:
I solved it using malloc:
In the .h of the library:
int *a;
And in constructor:
a = (int *) malloc(_pepe_ * sizeof(int));
Thanks to AWOL, PaulS and others posters that help me in this.
You put "a" in the .h file ?
Is it declared inside a class ? Or is it supposed to be a "module-global" variable ?
system
October 3, 2012, 6:37am
13
You put "a" in the .h file ?
Why not? It is a private member of the class (see original post)
system
October 3, 2012, 10:43am
15
I solved it using malloc:
With a call to free(a); in the destructor, right?
system
October 3, 2012, 9:58pm
16
PaulS:
I solved it using malloc:
With a call to free(a); in the destructor, right?
Yes and no...
This library is declared during load. And is not destructed at any time. But, I should add the free(a), just in case.
Thanks.