Go Down

Topic: How to only require setup() (Read 1 time) previous topic - next topic

AlphaBeta

Apr 29, 2009, 02:07 am Last Edit: Apr 30, 2009, 12:46 am by AlphaBeta Reason: 1
I just discovered something I think is wonderful. :)

[edit]See: Bens post with correct code.

I've posted wrong code below.  :-[[/edit]

Change %arduino%\hardware\cores\arduino\main.cxx to:
Code: [Select]
int main(void)
{
 init();
   
 setup();

 for (;;){
   #ifdef loop
     loop();
   #endif
 }
 return 0;
}


Now I can write:
Quote

void setup(){
 Serial.begin(9600);
 Serial.println("Hello World!");
}


This is probably an old hack. But still useful :)

Why not implement it in the official release? Are there any usecases that this may cause errors?

I could only think of this:
Quote

boolean setup = false;
void setup(){
 Serial.begin(9600);
 Serial.println("Hello World!");
}

But it causes this: (Which actually is descriptive :) )
Quote
error: 'boolean setup' redeclared as different kind of symbol

Coding Badly

This may raise some interesting possibilities.  

For example...

Code: [Select]

int main(void)
{
 init();
   
 setup();

 for (;;){
   #ifdef loop
     loop();
   #else
     // Put sleep code here
   #endif
 }
 return 0;
}


The Arduino would run the code in setup and go to sleep.  Perfect for an application that needs to run once and can be triggered by a reset.

The same technique works with setup.

- Brian

Anachrocomputer

So, in that first piece of code, where does one #define loop?  I'm not sure I understand how this is supposed to work.

mem

#3
Apr 29, 2009, 01:31 pm Last Edit: Apr 29, 2009, 01:33 pm by mem Reason: 1
Brian, why not simply put code for an application that needs to run once in setup. If execution needs to block then something like:
  while(true)
     ;

AlphaBeta

#4
Apr 29, 2009, 04:58 pm Last Edit: Apr 29, 2009, 05:00 pm by AlphaBeta Reason: 1
Quote
So, in that first piece of code, where does one #define loop?  I'm not sure I understand how this is supposed to work.


One does not define the void loop(), that's the beauty of it :)

The piece of code enables one to write sketches that does not need the loop.

Such as my imaginary Hello World. (Which compiles)
Quote

void setup(){
 Serial.begin(9600);
 Serial.println("Hello World!");
}




Without the #ifdef one will get an error message if the sketch has no loop.

Anachrocomputer

Sorry, still don't understand!  How does defining or not defining a 'loop()' function affect the '#ifdef' which is looking for a '#define loop'?

AlphaBeta

When we write:
Code: [Select]

void setup(){
}

void loop(){
}


Both setup and loop will be defined for the preprocessor, as in: 'It knows of setup and loop'

My code ask the preprocessor:
Quote
Do you know about loop?
If you do, execute it. But if you do no know about loop, do not try to execute it.


This way, we can omit the void loop() altogether in the Arduino IDE, and the compiler won't complain.

(Without the #ifdef the compiler would expect a function called loop to be defined and available.)

Anachrocomputer

Quote
Both setup and loop will be defined for the preprocessor,


Is that something that the Arduino IDE does, behind the scenes?

AlphaBeta

#8
Apr 29, 2009, 07:09 pm Last Edit: Apr 29, 2009, 08:04 pm by AlphaBeta Reason: 1
Quote
Quote
Both setup and loop will be defined for the preprocessor,


Is that something that the Arduino IDE does, behind the scenes?


Kind of.

The arduino IDE makes function prototypes, and declares the functions that are to be used/called.
By doing this it actually makes a variable that points to a function code block. Which we define in out sketch.

Quote
And, becuase the #ifdef / #ifndef preprocessor directives works on variable names as well as the standard #define, it works on function names.
I might have to take this back.  :-[
[edit]Hmmm. Wait a minute.

Can't get it to work anymore  >:([/edit]

mem

I am wondering why it matters.  What do you want to do that you can't do in setup as it is ?


AlphaBeta

Why it matters?

Well. Simplicity matters.

A lot of concepts could be thought without the loop, so I think it should.

Sets the focus in the subject at hand, without the 'distraction' of an empty loop.

Ray Andrews

I'm still confused, why not just

void loop(){;}

AlphaBeta

Quote
I'm still confused, why not just

void loop(){;}



Because then someone could ask: "Why do you create a function that does nothing?"
Or: "What does the void loop(){;} do?"


This is a detail, and writing an empty function definition woks. I just think it's not conform with the arduino simplicity.

retrolefty

Sorry, this sure looks a lot like a solution looking for a problem.  ;)

Seems I've been shown that if you want setup to do nothing you can just leave it in but empty and if you want loop to not loop you just put a blocking while statement at the end of it. Is there something your method does in addition other then just confuse us hardware guys  ;)

Lefty

AlphaBeta

Well. Right now this confuses me too.  :(


I just think that the opportunity to omit loop() would've been a useful one.

Go Up