Creating a class: syntax

Hi!

I'm using classes in Processing without issue, but in Arduino the syntax seems different?

Can you tell me what's wrong here?

Pad pad0;

void setup() {
  pad0=new Pad(0,BTN_PAD0);
(...)
}
class Pad{
  int numero;
  int pin;
  
 
 Pad(int tempnumero, int temppin){
   numero=tempnumero;
   pin=temppin;
 }
  
};

error: 'Pad' does not name a type In function 'void setup()':

Thanks!

Have you used #include to include the file your class is defined in if it's in a separate file?

I opened a new tab (arrow on the right), so that it's being merged.
(and it is for all the other tabs!)

First off, keyword new is not defined in arduinoland.

Second, you need to make the constructor publically visible.

Try this: :slight_smile:

class Pad{
public**:**
inline Pad(int tempnumero, int temppin){
numero=tempnumero;
pin=temppin;
}
private**:**
int numero;
int pin;
};

Pad pad = Pad(2,3);

void setup(){}
void loop(){}

Thanks for your help!

But still:

error: 'Pad' does not name a type

:-/

Hm it seems it's because it's in a tab. How to fix that?

If the file the class is in is called foo.h then I think you should just be able to do:

#include "foo.h"

at the top of your code.

The file is classPad.pde (I guessed .h but the IDE created it like that) and #include "classPad.pde" gives me a "no such file" error...
Note that it was not needed to make the include with all the other tabs!

/*
 * pad.h
 */
#ifndef PAD_H
#define PAD_H
class Pad{
public:
  Pad(int tempnumero, int temppin);
private:
  int numero;
  int pin;
};
Pad::Pad(int tempnumero, int temppin){
  numero=tempnumero;
  pin=temppin;
}
#endif

Put that in a file called pad.h, and compile it as a static library.

# Customize to your system's specifications

CC=avr-gcc
AR=ar
CPUTYPE=atmega168
ARDUINO_INCLUDES=/home/tron/src/workspace/ArduinoCoreLibrary
AVR_INCLUDES=-I/usr/avr/include
AVR_INCLUDES={AVR_INCLUDES} -I/usr/lib/gcc/avr/4.3.3/include
AVR_INCLUDES={AVR_INCLUDES} -I/usr/lib/gcc/avr/4.3.3/include-fixed

# Below this point, nothing should be modified
SRC=pad.h
OUTPUT=pad.o
LIB=libArduinoPad.a
OPTIONS=-std=c99 -Os -mmcu=${CPUTYPE}

all: compile

compile:
      ${CC} -c ${SRC} -o ${OUTPUT} -I. -I${ARDUINO_INCLUDES} ${AVR_INCLUDES} ${OPTIONS}
      ${AR} rcs ${LIB} ${OUTPUT}

clean:
      -rm -f ${OUTPUT} ${LIB}

If you are using GNU Make, you can use something similar to what I have above as your Makefile.

In your program,

#include "pad.h"

and do not forget to link to the library.

DO NOT USE inline. Sure, code gets executed faster, but inline will tell the compiler to "insert the function code" wherever the function is called, making your resulting binary bigger than it should be.

The main reason I don't use the Arduino IDE is this problem, writing custom stuff gets painful (plus it has another set of bugs and interface problems I can't really bother to put up with) so I use Eclipse instead.

Frankly though, I question the usability of creating a class to manipulate an object. IIRC, when creating an instance of an object, an instance of the code is loaded in memory, correct me if I am wrong though. I find it better to create a struct and hold the data of the object in a struct and then pass the address of this object as an argument to a function, manipulating it via pointers.

Thanks, I finally put it really in a lib. That was what I planned, but not immediately!
BTW you don't need to complile it first if you use the IDE.

Ok, for the inline, someone wrote about it a bit higher, so I used it... and I need the smallest binary possible! What's the speed difference?

I will continue and to all my code in the lib!

when creating an instance of an object, an instance of the code is loaded in memory

That is not correct. Code is not loaded into memory, it is executed from flash. Instances of a class share the same code so there is no resource advantage to using structs over a class. The major benefit of using a class over a struct is that the code tends to be more readable because you don't need to pass pointers or references to the methods that access the private data.

Code is executed from flash, yes, that's in the manual (should be common sense to figure this out too, ATmega chips have much less RAM than flash, the Arduino bootloader alone is 2K, 2x as much as the RAM present in the ATmega168).

However, when you do something like

int i = 6;

two bytes in memory are allocated and filled
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 |
and thus the six is stored.
If you then do something like

int *a = &i;

another 16 bits of memory are allocated (the size of size_t) and written with the address the value of i is stored at. Some other sizes that may be of interest:

sizeof(int)=2
sizeof(size_t)=2
sizeof(uint8_t)=1
sizeof(uint8_t*)=2

When you do something like:

typedef struct _LCD {
      int datapin[8];
      int rs_pin;
      int rw_pin;
      int enable_pin;
      int bitmode;
} LCD;

every time you do something like:

LCD l;

you are allocating 816 + 16 + 16 + 16 + 16 = 1216 = 192 bits = 24 bytes of memory. In C, you don't get OOP as direct as you would with C++. You use pointer to functions and things like that, pointing to a function from within a struct will cost you 2 bytes (size_t), and probably a bit more for the arguments.

When you create an object in C++. That object is loaded into memory. If a function is a part of your object, where does that function go to? Probably the C++ compiler here works differently than it does on x86, I don't know. Or I just have a bad understanding on how C++ actually handles objects at low level. Enlighten me.

Ok, for the inline, someone wrote about it a bit higher, so I used it... and I need the smallest binary possible! What's the speed difference?

As for this, it depends on how many times you use that function and how big this function is. If this is a big function and you call it often, you can expect your binary to be quite large. A quick glance at the ATmega44/88/168 manual say that branching instructions involved in function handling (relative jump, direct jump, relative subroutine call, subroutine return...) take 2 - 4 clocks to complete. If your CPU is running at 16MHz, the slowest of this instructions will take 4/16000000 = 1/4000000 seconds to complete. Your call on this one, you're the designer.

When you create an object in C++. That object is loaded into memory. If a function is a part of your object, where does that function go to?

The code associated with a class is stored once, no matter how many instances are created. The benefit of C++ is that although it is creating structures under the covers to hold the instance data, all the ugly pointer work necessary to get the code to point to the right data is hidden from the user.