Go Down

Topic: Creating libraries for different Adruino's (Read 873 times) previous topic - next topic

btmcmahan

I may not be posting this in the right place, but I think someone here can help me.  I'm working on updating a library, and I would like to know how to write a condition based on what arduino board I am using.  I want my library to be able to work with several arduino models, depending on what "Board" is selected in Arduino IDE.  How is this possible?  And, is it possible to have Arduino IDE automatically select the correct H file, depending on that "Board" selection?  Any advice is welcome.

johnwasser

Most libraries I have seen that support both the UNO and the Mega will look for the processor type:

Code: [Select]

#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
// Mega stuff here
#else
// UNO stuff here
#endif


Can you give an example of an Arduino board for which that would not be sufficient?
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

btmcmahan

That looks perfect for what I'm gonna be doing.  Thanks a lot!

What differences are you talking about? If it is to do with different pin mappings then see the macros and functions in pins_arduino.h. If it is for different microcontroller architectures you could test which MCU flags are defined (__AVR_ATmega2560__, __AVR_ATmega1284P__ etc). A better way might be to test for the existence of different #defines. For instance, HardwareSerial.cpp contains this code:
Code: [Select]
#if defined(UBRRH) || defined(UBRR0H)
  extern HardwareSerial Serial;
#elif defined(USBCON)
  #include "USBAPI.h"
//  extern HardwareSerial Serial_;
#endif
#if defined(UBRR1H)
  extern HardwareSerial Serial1;
#endif
#if defined(UBRR2H)
  extern HardwareSerial Serial2;
#endif
#if defined(UBRR3H)
  extern HardwareSerial Serial3;
#endif


It defines up to four serial devices, based on whether the appropriate hardware registers exist. If you can do it this way it is much better than for testing for  __AVR_ATmega2560__ etc since it is likely to work with Atmel microcontrollers that you weren't even aware of.

Go Up