#defines and contants in libraries

Hello, I have written a function to send MIDI messages, that automatically uses the right settings for the right board (Uno or Leonardo). It looks something like this:

#define BLINK   // Comment this line out to stop the LED from blinking whenever a MIDI message is sent.
#define LED_PIN 13
//#define DEBUG // Uncomment this line to use debug mode. MIDI messages will be sent as HEX over serial @9600 baud.

#define NOTE_OFF       0x80
#define NOTE_ON        0x90
#define CC             0xB0

void setupMidi(){
#if defined (__AVR_ATmega328P__)  //only include these lines when compiling for Arduino Uno
  Serial.begin(115200); // Start communication with ATmega16U2 @115200 baud (for MIDI firmware)
#endif

#if defined DEBUG  //only include these lines when in debug mode
  Serial.begin(115200); // Start communication with ATmega16U2 @9600 baud (for Arduino Serial firmware)
#endif

#ifdef BLINK 
  pinMode(LED_PIN,OUTPUT);  // The pin with the LED connected is set to output.
#endif
}

void sendMidi(byte m, byte c, byte d1, byte d2) // Custom function to send MIDI messages: message, channel, data1, data2
{
#ifdef BLINK 
  digitalWrite(LED_PIN,1);
#endif

#ifdef DEBUG                //When in debug mode, send every part of the message seperately, in HEX
    Serial.print(m, HEX); 
    Serial.print('\t');
    // Etc. etc. to print out all variables in the serial monitor
#else

#if defined (__AVR_ATmega32U4__)  //only include these lines when compiling for Arduino Leonardo
    if(m == NOTE_ON){
      usbMIDI.sendNoteOn(d1, d2, c);
    }
    // Etc. etc. use the Teensy usbMIDI library to send the right MIDI message.
#endif

#if defined (__AVR_ATmega328P__)  //only include these lines when compiling for Arduino Uno
    Serial.write( // Write the message over serial, to the ATmega16U2, running custom MIDI <-> USB firmware
#endif

#endif

#ifdef BLINK 
  digitalWrite(LED_PIN,0);
#endif

}

I put this in a separate file, named it midiSend.cpp, added:

#include "Arduino.h"
#include "midiSend.h"

and I created the midiSend.h file with just the prototypes:

#include "Arduino.h"

void sendMidi(byte m, byte c, byte d1, byte d2);
void setupMidi();

Then I added the line #include "midiSend.h" to a library I wrote myself, that needs MIDI output.
It compiles for both the Uno and the Leonardo, the debug mode works too, but ...

I was wondering if there is a way to move the #define DEBUG line in the sendMIDI.cpp file to my main .ino file, so I can choose there whether or not I want to compile in debug mode. Now I have to change my .cpp file, every time I want to debug.
The problem is that I have to do this with preprocessor directives, since the compiler refuses to continue if it sees a MIDI-mode specific function when the 'board' is set to 'Leonardo'.

Next, how can I use predefined constants, like "INPUT_PULLUP" for example, in my own libraries and functions? (The ones that show up in blue.)
If I have a function 'run(int speed)' for example, and I want to be able to use both 'run(FAST_AS_POSSIBLE)' and 'run(5)'.

Thanks in advance!

tttapa

I was wondering if there is a way to move the #define DEBUG line in the sendMIDI.cpp file to my main .ino file, so I can choose there whether or not I want to compile in debug mode. Now I have to change my .cpp file, every time I want to debug.

No. #define statements are pre-processed before the compiler sees them, for each compilation unit. If you define DEBUG in the sketch, that value will not be seen in the library file.

You could out the #define statements in a header file, seen by both the sketch and the library.

Next, how can I use predefined constants, like "INPUT_PULLUP" for example, in my own libraries and functions? (The ones that show up in blue.)

#include <Arduino.h> in your library.

Thanks for the reply, I'm sorry, it wasn't clear: I mean that I want to make my own constant, like AS_FAST_AS_POSSIBLE, and define it somewhere in my own library.
For example a library man.h & man.cpp with a class "man" and a function "run(int speed)" that takes the speed as a parameter. Somewhere in that library, I would like to define the maximum speed of a man, for example 10. Then I could create a member, say 'Bob', and use the syntax Bob.run(AS_FAST_AS_POSSIBLE); and it would do the exact same thing as Bob.run(10); but without having to remember that the maximum speed of a man is 10, and without having to define AS_FAST_AS_POSSIBLE in my main .ino file. Just like using pinMode(x, INPUT); does the same thing as pinMode(x, 0);.

Just like using pinMode(x, INPUT); does the same thing as pinMode(x, 0);.

Then take a look at where INPUT is #defined. Here's a hint. It's in a header file that is included (indirectly) in your sketch.

I hope that your explanation is only hypothetical. AS_FAST_AS_POSSIBLE should not be a constant for the library. A default, maybe, but someone might want to use your class and have a different (user-defined) value for AS_FAST_AS_POSSIBLE.

Found it, thanks.

(const static = ; and 'LITERAL1' in the 'keywords.txt' file, if anyone's wondering)