Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

T4 - Can now handle sketches with main() instead of setup()/loop() #397

Open
wants to merge 1 commit into
base: master
from

Conversation

@KurtE
Copy link
Contributor

commented Oct 10, 2019

As per the Teensyduino 1.48 release thread.

T3.x sketches could compile that had int mai() {} and not void() and setup():

So simply copied over setup.cpp from T3 folder of cores, and then changed setup.c to call main, which simply calls setup and loops calling loop/yield.

Added another endless loop after the call to main() to hang there if the users main returns.

Tested with simple sketch

#include <arduino.h>
int main (void) {
  pinMode(13, OUTPUT);
  for(;;) {
    digitalWriteFast(13, HIGH);
    delay(250);
    digitalWriteFast(13, LOW);
    delay(250);
  }
}

tried it on T3.6 verified before compile failed and now works. Also compiled and run a normal app to just make sure it was not totally busted

As per the Teensyduino 1.48 release thread.

T3.x sketches could compile that had int mai() {} and not void() and setup():

So simply copied over setup.cpp from T3 folder of cores, and then changed setup.c to call main, which simply calls setup and loops calling loop/yield.

Added another endless loop after the call to main() to hang there if the users main returns.

Tested with simple sketch
```
#include <arduino.h>
int main (void) {
  pinMode(13, OUTPUT);
  for(;;) {
    digitalWriteFast(13, HIGH);
    delay(250);
    digitalWriteFast(13, LOW);
    delay(250);
  }
}
```
tried it on T3.6 verified before compile failed and now works.  Also compiled and run a normal app to just make sure it was not totally busted
@luni64

This comment has been minimized.

Copy link
Contributor

commented Oct 10, 2019

Interesting, tested it, works fine. You can even reduce the int main() in core/main.cpp to

extern "C" int main(void)
{
   setup();
   while (1)
   {
     loop();
     yield();	
   }
}

Looks like as soon as you define a int main() somewhere in user code it "overrides" the one from the core. Almost like it was defined as weak which it isn't. I would expect a linker error when there are two mains in the code but the linker does not choke at all and it works perfectly.
Strange but useful indeed. Any idea why this works?

Here my test code in some *.ino. As soon as you comment out int main() it uses the core one and calls setup and loop....

int main(void)
{
   pinMode(LED_BUILTIN, OUTPUT);

   while (1)
   {
      digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN));
      delay(50);
   }
}

void setup()
{
   pinMode(LED_BUILTIN, OUTPUT);
}

void loop()
{
   digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN));
   delay(1000);
}
@KurtE

This comment has been minimized.

Copy link
Contributor Author

commented Oct 10, 2019

@luni64

This comment has been minimized.

Copy link
Contributor

commented Oct 10, 2019

I believe it works here as well as for Teensy 3.x as, The first thing the is loaded is the object file for the sketch. It then tries to resolve missing symbols that are remaining by trying to load in the library files that were previously generated. And since the object file that corresponds to main.cpp, does not have any of the remaining missing symbols, and as such the linker does not bring it it.

Good point. Tested it by directly linking the core object files (not the lib) which generates the expected linker error (multiple definition of main). That does of course mean that it is a bit fragile (only works with the core files in a lib) but that is probably an academic issue.

Thanks for saving me from getting mad.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.