Pages: [1] 2 3   Go Down
Author Topic: Why not (finally) expose main.cpp to the user ?  (Read 4557 times)
0 Members and 1 Guest are viewing this topic.
"The old Europe"
Offline Offline
Edison Member
*
Karma: 1
Posts: 2005
Bootloaders suck!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#define ARDUINO_MAIN // [required]
#include <Arduino.h> // [required] - make sure everything that is required by the core libraries is known to the compiler

int main(void) // [required]
{
        init(); // [required] - make sure everything that is required by the core libraries to work is set up correctly

        setup(); // run user-defined initialization code - see below
   
        for (;;) { // [required] - never exit from main()
                loop(); // run user-defined code - repeated forever - see below
        }

        return 0;
}

void setup(void)
{
  // user-defined initialization code goes here
}

void loop(void)
{
 // user-defined program code goes here
}

Function prototypes could still be automatic.

Why am I asking for this? The newly added SerialEvent stuff takes more than 200 bytes away and essentially is just a function call added in main() after loop(). I hate that, as I will never use it.

The structure of main() is not difficult to understand at all... add enough comments and even my mother could use it.
Logged

• Upload doesn't work? Do a loop-back test.
• There's absolutely NO excuse for not having an ISP!
• Your AVR needs a brain surgery? Use the online FUSE calculator.
My projects: RGB LED matrix, RGB LED ring, various ATtiny gadgets...
• Microsoft is not the answer. It is the question, and the answer is NO!

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 227
Posts: 14048
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

50% workaround smiley
Code:
void setup()
{
  mymain();
}
void loop(){}

mymain()
{
  // enter your code here ....
}
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

"The old Europe"
Offline Offline
Edison Member
*
Karma: 1
Posts: 2005
Bootloaders suck!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

OK, that probably works too.

All I'm saying is that with the above shown 'template' ("bare minimum"), every user will sooner or later find out what goes where. There's really no need to hide that. Or add code that usually doesn't do anything and steals 200 bytes of flash.
« Last Edit: October 10, 2011, 03:22:35 pm by madworm » Logged

• Upload doesn't work? Do a loop-back test.
• There's absolutely NO excuse for not having an ISP!
• Your AVR needs a brain surgery? Use the online FUSE calculator.
My projects: RGB LED matrix, RGB LED ring, various ATtiny gadgets...
• Microsoft is not the answer. It is the question, and the answer is NO!

'round the world...
Offline Offline
Faraday Member
**
Karma: 42
Posts: 3330
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Why not stop using the IDE?
Logged

Eu não sou o teu criado. Se respondo no fórum é para ajudar todos mediante a minha disponibilidade e disposição. Responder por mensagem pessoal iria contra o propósito do fórum e por isso evito-o.
Se realmente pretendes que eu te ajude por mensagem pessoal, então podemos chegar a um acordo e contrato onde me pagas pela ajuda que eu fornecer e poderás então definir os termos de confidencialidade do meu serviço. De forma contrária toda e qualquer ajuda que eu der tem de ser visível a todos os participantes do fórum (será boa ideia, veres o significado da palavra fórum).
Nota também que eu não me responsabilizo por parvoíces escritas neste espaço pelo que se vais seguir algo dito por mim, entende que o farás por tua conta e risco.

Dito isto, mensagens pessoais só se forem pessoais, ou seja, se já interagimos de alguma forma no passado ou se me pretendes convidar para uma churrascada com cerveja (paga por ti, obviamente).

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 511
Posts: 19358
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You can do that now.

Old way:

Code:
void setup ()
{
  Serial.begin (115200);
  Serial.println ("hello, world");
}

void loop () {}

Compiles as:

Code:
Binary sketch size: 1970 bytes (of a 32256 byte maximum)

Let's just use main:

Code:
int main ()
{
  Serial.begin (115200);
  Serial.println ("hello, world");
  return 0;
}

Compiles as:

Code:
Binary sketch size: 1696 bytes (of a 32256 byte maximum)

That saved 274 bytes. Both work.

Or if you need init  (you may not):

Code:
int main ()
{
  init ();
 
  Serial.begin (115200);
  Serial.println ("hello, world");
  return 0;
}

That compiled as:

Code:
Binary sketch size: 1962 bytes (of a 32256 byte maximum)
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 27427
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Seems like a somewhat silly discussion - from what I read here, most programs seem to run out of SRAM before they run out of Flash memory.

Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

"The old Europe"
Offline Offline
Edison Member
*
Karma: 1
Posts: 2005
Bootloaders suck!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

@CrossRoads:

That attitude is what has brought us things like .NET installations that gobble up half of your disk space and take 2 weeks to install. Just look at AVRStudio5 vs. 4!

I happen to play with ATtiny chips more often, and at just 2k of flash, wasting more than 200 bytes is an absolute no no.

I just don't understand why something that simple has to be hidden. Why? If people can use for-loops inside "loop()", they can do the same in "main()".

@bubulindo:

I do that already when I can. The arduino IDE leaves a lot to be desired compared to say Code::Blocks, Eclipse...

I'm just giving feedback. I'm not against simplifications - when actually helpful. Hiding main() is just evil.
Logged

• Upload doesn't work? Do a loop-back test.
• There's absolutely NO excuse for not having an ISP!
• Your AVR needs a brain surgery? Use the online FUSE calculator.
My projects: RGB LED matrix, RGB LED ring, various ATtiny gadgets...
• Microsoft is not the answer. It is the question, and the answer is NO!

'round the world...
Offline Offline
Faraday Member
**
Karma: 42
Posts: 3330
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

@bubulindo:

I do that already when I can. The arduino IDE leaves a lot to be desired compared to say Code::Blocks, Eclipse...

I'm just giving feedback. I'm not against simplifications - when actually helpful. Hiding main() is just evil.

I guess the objective of the IDE is in fact to hide these things from the average user. If you have enough knowledge to skip the IDE and use whatever you please, go ahead.
Logged

Eu não sou o teu criado. Se respondo no fórum é para ajudar todos mediante a minha disponibilidade e disposição. Responder por mensagem pessoal iria contra o propósito do fórum e por isso evito-o.
Se realmente pretendes que eu te ajude por mensagem pessoal, então podemos chegar a um acordo e contrato onde me pagas pela ajuda que eu fornecer e poderás então definir os termos de confidencialidade do meu serviço. De forma contrária toda e qualquer ajuda que eu der tem de ser visível a todos os participantes do fórum (será boa ideia, veres o significado da palavra fórum).
Nota também que eu não me responsabilizo por parvoíces escritas neste espaço pelo que se vais seguir algo dito por mim, entende que o farás por tua conta e risco.

Dito isto, mensagens pessoais só se forem pessoais, ou seja, se já interagimos de alguma forma no passado ou se me pretendes convidar para uma churrascada com cerveja (paga por ti, obviamente).

"The old Europe"
Offline Offline
Edison Member
*
Karma: 1
Posts: 2005
Bootloaders suck!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

But why hide something that is SIMPLE ?

I can understand that it is not encouraged to write your own interrupt handlers on day one. That's why we have wrappers like attachInterrupt() - I don't use them either, but I'm perfectly fine with them. But hiding main() ... come one! Thats less than 10 lines of perfectly understandable code. The most intellectual part of it is the for-loop. No pointers, no object oriented code, no if statement, no boolean operators... how simple can it get?

If you really think that hiding main() is _necessary_, then I don't want to know how simple-minded the user-base is thought to be. Every beginner should instantly feel offended by that. That's outrageous!
Logged

• Upload doesn't work? Do a loop-back test.
• There's absolutely NO excuse for not having an ISP!
• Your AVR needs a brain surgery? Use the online FUSE calculator.
My projects: RGB LED matrix, RGB LED ring, various ATtiny gadgets...
• Microsoft is not the answer. It is the question, and the answer is NO!

"The old Europe"
Offline Offline
Edison Member
*
Karma: 1
Posts: 2005
Bootloaders suck!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

@Nick:

have you tried adding pinMode()? or tested it with 1.0 beta ?

Edit:

how about something like this:

main.cpp
Code:
#define ARDUINO_MAIN
#include <Arduino.h>
#include <main.h> // <-- new line

int main(void)
{
        init();

#if defined(USBCON)
        USB.attach();
#endif

        setup();

        for (;;) {
                loop();
                if (serialEventRun) serialEventRun();
        }

        return 0;
}

main.h
Code:
#ifndef _main_h_
#define _main_h_

int main(void) __attribute__((weak)); // if the user defines his/her own main() it will be used instead

#endif

Now the 'hidden' main() can be overridden by a user-defined main. On Arduino 1.0 this saves a couple of bytes of flash.
« Last Edit: October 11, 2011, 08:27:47 am by madworm » Logged

• Upload doesn't work? Do a loop-back test.
• There's absolutely NO excuse for not having an ISP!
• Your AVR needs a brain surgery? Use the online FUSE calculator.
My projects: RGB LED matrix, RGB LED ring, various ATtiny gadgets...
• Microsoft is not the answer. It is the question, and the answer is NO!

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 27427
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

@madworm,
I was thinking more along the '328 lines. That's a good point about the smaller parts - I imagine you are not using any bootloader on those then? Just download sketches via the ICSP interface?
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

"The old Europe"
Offline Offline
Edison Member
*
Karma: 1
Posts: 2005
Bootloaders suck!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Correct. The ATtiny24 and 2313 don't even have a bootloader section (I believe, could be wrong though).
Logged

• Upload doesn't work? Do a loop-back test.
• There's absolutely NO excuse for not having an ISP!
• Your AVR needs a brain surgery? Use the online FUSE calculator.
My projects: RGB LED matrix, RGB LED ring, various ATtiny gadgets...
• Microsoft is not the answer. It is the question, and the answer is NO!

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 27427
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This came up in another thread as well - do you know how to download a sketch via the ICSP pins using the IDE?
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

"The old Europe"
Offline Offline
Edison Member
*
Karma: 1
Posts: 2005
Bootloaders suck!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

At least for the bigger chips I use custom 'boards.txt' entries, but I suppose it'll be the same thing for the tiny ones. And I don't use the IDE for the tiny ones...
From what I've seen of 1.0, switching between bootloader and a real programmer will be much easier (menu based, which is great). The fuse settings don't change though, one doesn't regain any flash space that way.


off-topic:

The next big step would be to use one of the bigger ATmegaXX-U chips on the arduino boards with enough flash space to hold both the serial port emulation AND and ISP emulation at the same time. This should once an for all kill this damn "can't upload" issue. Pololu has a programming adapter that does just that, but it doesn't seem to be 'open' and requires winblows tools to change settings.

But I guess the inclusion of avrdude 5.11 (which itself can resets the target board now using '-c arduino') will be a big increase for upload reliability.
Logged

• Upload doesn't work? Do a loop-back test.
• There's absolutely NO excuse for not having an ISP!
• Your AVR needs a brain surgery? Use the online FUSE calculator.
My projects: RGB LED matrix, RGB LED ring, various ATtiny gadgets...
• Microsoft is not the answer. It is the question, and the answer is NO!

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 511
Posts: 19358
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

@Nick:

have you tried adding pinMode()? or tested it with 1.0 beta ?

I'm not sure I understand the question. I didn't before, but this compiles:

Code:
int main ()
{
//  init ();
 
  Serial.begin (115200);
  Serial.println ("hello, world");
  pinMode (2, OUTPUT);
  return 0;
}

(With or without init () commented out).

This is the contents of main.cpp:

Code:
#include <WProgram.h>

int main(void)
{
init();

setup();
   
for (;;)
loop();
       
return 0;
}


If you supply your own main, the linker doesn't attempt to use the inbuilt one. I don't see any problem here.
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Pages: [1] 2 3   Go Up
Jump to: