Why not (finally) expose main.cpp to the user ?

You can do that now.

Old way:

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

void loop () {}

Compiles as:

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

Let's just use main:

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

Compiles as:

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

That saved 274 bytes. Both work.

Or if you need init (you may not):

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

That compiled as:

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

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.

@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.

madworm:
@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.

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!

@Nick:

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

Edit:

how about something like this:

main.cpp

#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

#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.

@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?

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

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

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.

madworm:
@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:

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:

#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.

madworm:
But why hide something that is SIMPLE ?

To you it is simple... for someone that just started in programming and embedded devices (which is probably the majority of Arduino users) main() makes a lot less sense than setup() and loop().

I can understand you. I only use the board and the bootloader from Arduino, but I also see why Arduino is made the way it is now. But the good thing is that it allows people to evolve and try different ways of using the hardware and software according to their own personal knowledge.

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

Adding entries to boards.txt similar to below works for me (I have others that are just different clock freqs). Your programmer may vary :wink:

uno16.name=Arduino Uno ICSP @ 16MHz
uno16.upload.using=arduino:usbtinyisp
uno16.upload.protocol=stk500
uno16.upload.maximum_size=32256
uno16.upload.speed=115200
uno16.bootloader.low_fuses=0xff
uno16.bootloader.high_fuses=0xde
uno16.bootloader.extended_fuses=0x05
uno16.bootloader.path=optiboot
uno16.bootloader.file=optiboot_atmega328.hex
uno16.bootloader.unlock_bits=0x3F
uno16.bootloader.lock_bits=0x0F
uno16.build.mcu=atmega328p
uno16.build.f_cpu=16000000L
uno16.build.core=arduino

So you replace

uno16.bootloader.file=optiboot_atmega328.hex

this file with the sketch file to use instead?

@Nick:

That doesn't seem to work on 1.0 for me. I have to add the 'weak' attribute there.

I don't have version 1.0. I thought I had version 0022, but I suppose that is alpha, right?

I can't find version version 1.0 on the download page.

I wonder why version 0022 works? They don't have the weak attribute.

@CrossRoads: no.

##############################################################
#
# to activate this board, select it and use:
#
# 'Burn Bootloader' - 'w/ USBtinyISP' just once
# this makes sure the correct FUSE settings are used
#
8x8RGBLedMatrix1.name=8x8 RGB LED Matrix - ATmega168 / 16MHz Quartz / USBtiny
8x8RGBLedMatrix1.upload.maximum_size=16384
8x8RGBLedMatrix1.upload.speed=115200
8x8RGBLedMatrix1.upload.using=arduino:usbtinyisp
8x8RGBLedMatrix1.bootloader.low_fuses=0xFF
8x8RGBLedMatrix1.bootloader.high_fuses=0xDD
8x8RGBLedMatrix1.bootloader.extended_fuses=0x05

## just so the IDE doesn't throw an error
## it will be overwritten, but we need the correct FUSE settings
8x8RGBLedMatrix1.bootloader.path=optiboot
8x8RGBLedMatrix1.bootloader.file=optiboot_diecimila.hex
##

8x8RGBLedMatrix1.bootloader.unlock_bits=0x3F
8x8RGBLedMatrix1.bootloader.lock_bits=0x3F
8x8RGBLedMatrix1.build.mcu=atmega168
8x8RGBLedMatrix1.build.f_cpu=16000000L
8x8RGBLedMatrix1.build.core=arduino
#
#
#
##############################################################

If I want to do without a bootloader, I change the fuse settings, lockbits and the available flash size to respect that fact. Uploading works as usual.

Uploading is currently slower with an usbtiny than with optiboot. There is a trivial 'patch' for that, but it was not accepted... it involves adding the bitclock parameter '-B 1' to the programmers.txt and a tiny modification in the java code that deals with uploading. This would increase upload speed at least 10x.

programmers.txt

usbtinyisp.name=USBtinyISP
usbtinyisp.protocol=usbtiny
usbtinyisp.bitclock=1

app/src/processing/app/debug/AvrdudeUploader.java

@@ -104,6 +104,10 @@ public class AvrdudeUploader extends Uploader  {
Map<String, String> programmerPreferences = target.getProgrammers().get(programmer);
List params = new ArrayList();
params.add("-c" + programmerPreferences.get("protocol"));
+
+ if (programmerPreferences.get("bitclock") != null) {
+ params.add("-B" + Integer.parseInt(programmerPreferences.get("bitclock")));
+ }

if ("usb".equals(programmerPreferences.get("communication"))) {
params.add("-Pusb");

I have given up getting anything added/patched in the IDE, it has proven to be pointless for me. I just complain here.

This will be ignored too I guess, but it is less painful for me overall.

O.O I had no idea you could do that and this is 100% awesome. I just shaved 132 bytes off my sketch and now have enough space to implement a URL decode function (94 bytes) to fix that bug that's been annoying me for months.

See! It is useful.

CapnBry:
O.O I had no idea you could do that and this is 100% awesome. I just shaved 132 bytes off my sketch and now have enough space to implement a URL decode function (94 bytes) to fix that bug that's been annoying me for months.

Glad that helped. Better not upgrade to version 1.0 though, unless they fix that .