Pages: [1]   Go Down
Author Topic: Creating libraries for different Adruino's  (Read 832 times)
0 Members and 1 Guest are viewing this topic.
Texas
Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8933
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
#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?
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Texas
Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 43
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: