[SOLVED] Advice on writing a library/class for arduino, what to do/not do in the constructor?

Status
Not open for further replies.

David_

Advanced Member level 2
Joined
Dec 6, 2013
Messages
573
Helped
8
Reputation
16
Reaction score
8
Trophy points
1,308
Location
Sweden
Visit site
Activity points
12,244
Hello.

I do not know much about C/C++ but I am trying to make classes for my data converters that I use with my arduino Due, and why not then put each class in a library.

I want to ask about the following:
Code:
MAX5216::MAX5216(int CSpin)
{
  _CSpin = CSpin;
}

void MAX5216::begin()
{
  // MAX5216 DAC Setup
  // MSB first, shifts data on SCKs falling edge, clock idle Low, SCK freq « 50MHz. 
  SPI.begin(_CSpin);
  SPI.setBitOrder(_CSpin, MSBFIRST);
  SPI.setDataMode(_CSpin, SPI_MODE1);
  SPI.setClockDivider(_CSpin, 2);
}

Is it any meaning in doing almost nothing in the constructor and then initialize everything in a begin function?
Is there a reason to not put everything that is in MAX5216::begin() into MAX5216::MAX5216(int CSpin)?

Regards

- - - Updated - - -

I am aware of the things I need to do in order to use the SPI library inside my own library.
 

I can't find any difference what so ever in putting a lot of functions or peripheral setup routines in the constructor and I guess the idea is to do it that way. And to have a destructor as well but I have never seen a destructor being used in a arduino related subject(but I have not even read 1% of the C++ codes that lays behind the arduino scenes.
 

apart from the constructor and destructor options ,
what would be the final hex code memory size in the c++ implementation ?
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…