MCUdude / MegaCore

Arduino hardware package for ATmega64, ATmega128, ATmega165, ATmega169, ATmega325, ATmega329, ATmega640, ATmega645, ATmega649, ATmega1280, ATmega1281, ATmega2560, ATmega2561, ATmega3250, ATmega3290, ATmega6450, ATmega6490, AT90CAN32, AT90CAN64 and AT90CAN128
Other
384 stars 118 forks source link

Customizable LED_BUILTIN #204

Closed marshfolx closed 1 year ago

marshfolx commented 1 year ago

I build a mega128 board and want to put LED_BUILTIN on D16 / PG3 rather than D13 / PB5 which is a PWM channel of Timer1. I think it is better to put a #ifndef around the #define LED_BUILTIN in 64-pin-avr/pins_arduino.h, so that I can use build flag to define my assignment.

// Builtin LED
#ifndef LED_BUILTIN
#define LED_BUILTIN   (13)
#endif
static const uint8_t LED = LED_BUILTIN;
MCUdude commented 1 year ago

LED_BUILTIN is just a constant, and isn't "tied" to a specific pin at all. You don't have to use the constant if you don't want to. However, you should be able to deal with it in your code like so:

#ifdef LED_BUILTIN
#undef LED_BUILTIN
#define LED_BUILTIN PIN_PG3
#endif
marshfolx commented 1 year ago

@MCUdude Some libraries and example code use LED_BUILTIN to show simple notifications, I have to directly change the source code if i can't use build flag. Or I must place #undef LED_BUILTIN before including the library.

MCUdude commented 1 year ago

You can't override pre-defined macros in the sketch, and expect the compiler to pick it up and re-compile the libraries.

This means that the only viable way to override the LED_BUILTIN macro would either to add -DLED_BUILTIN=13 as a compiler build flag (difficult with Arduino IDE, possible with PlatformIO), or modify the source code in the MCUdude_corefiles folder. I'm happy to add your suggestion to the code base (only defined LED_BUILTIN if not already defined), but it won't solve your issue. The compiler would still not pick this up as long as the macro is overridden in the sketch.

marshfolx commented 1 year ago

Thanks for that, and of course I'm using PlatformIO. I realized that #undef can't affect library code just after I sent the comment. No problem about it.