Timer usage

Several libraries allow to configure the timer to use, e.g. the IRremote library. But the implemented timer selection requires to edit something like

#define IR_USE_TIMER1

in some library header. Unfortunately such a change will affect all projects, using that library, what’s a bad idea :frowning:

Now I came across a solution that declares a distinct class for each timer, so that the user is free to instantiate an object of the desired class. Instead of copy&paste the same class and edit it for using a specific timer, I would like to #define new class names, and #include the implementation for each timer. Like this

#define IR_USE_TIMER1 //create code for using timer 1
#define IRsend IRsend1 //sender class name
#define IRrecv IRrecv1 //receiver class name
#include <IRremote.cpp> //class implementation
#undef IR_USE_TIMER1
#define IR_USE_TIMER2
#define IR_send IRsend2
//and so on...

Now I have some headaches, e.g. what will happen if the included source files (*.cpp) are also compiled outside above module, or the implementation is split across multiple cpp files, and header guards are #defined, and whatever further bogus may happen. What should I be aware of?

Or is there another way to achieve the goal, to create classes bound to specific timers or other resources, with less trouble and modifications to the given library code?

Or how can a library be created, that can be configured by a project to use a specific resource? A #define in the *.ino project file won’t help, when the library source files are compiled independently :frowning:

Not an answer to your question, but in newer IDEs you can use a src directory in the sketch directory and place libraries in there; you can modify those as you like without interfering with the original.

|   test.ino
        |   .gitignore
        |   .travis.yml
        |   arduino-irremote.sublime-workspace
        |   boarddefs.h
        |   changelog.md
        |   Contributing.md
        |   Contributors.md
        |   irPronto.cpp
        |   irRecv.cpp
        |   IRremote.cpp
        |   IRremote.h
        |   IRremoteInt.h
        |   irSend.cpp
        |   ir_Aiwa.cpp
        |   ir_Denon.cpp
        |   ir_Dish.cpp
        |   ir_JVC.cpp
        |   ir_Lego_PF.cpp
        |   ir_Lego_PF_BitStreamEncoder.h
        |   ir_LG.cpp
        |   ir_Mitsubishi.cpp
        |   ir_NEC.cpp
        |   ir_Panasonic.cpp
        |   ir_RC5_RC6.cpp
        |   ir_Samsung.cpp
        |   ir_Sanyo.cpp
        |   ir_Sharp.cpp
        |   ir_Sony.cpp
        |   ir_Template.cpp
        |   ir_Whynter.cpp
        |   ISSUE_TEMPLATE.md
        |   keywords.txt
        |   library.json
        |   library.properties
        |   LICENSE.txt
        |   README.md
            |       AiwaRCT501SendDemo.ino
            |       IRrecord.ino
            |       IRrecvDemo.ino
            |       IRrecvDump.ino
            |       IRrecvDumpV2.ino
            |       IRrelay.ino
            |       IRremoteInfo.ino
            |       IRsendDemo.ino
            |       IRsendRawDemo.ino
            |       IRtest.ino
            |       IRtest2.ino
            |       JVCPanasonicSendDemo.ino
            |       LegoPowerFunctionsSendDemo.ino
            |       LegoPowerFunctionsTests.ino

Note: I did not clean it out to get rid of e.g. the examples.

The tetsketch is a literal copy of one of the examples

 * IRremote: IRrecvDemo - demonstrates receiving IR codes with IRrecv
 * An IR detector/demodulator must be connected to the input RECV_PIN.
 * Version 0.1 July, 2009
 * Copyright 2009 Ken Shirriff
 * http://arcfn.com

#include "src/IRremote/IRremote.h"

int RECV_PIN = 11;

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
  // In case the interrupt driver crashes on setup, give a clue
  // to the user what's going on.
  Serial.println("Enabling IRin");
  irrecv.enableIRIn(); // Start the receiver
  Serial.println("Enabled IRin");

void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    irrecv.resume(); // Receive the next value

It compiles (IDE 1.6.7), not tested if it works.

Note: initially tested with only the IRremote library in the src directory. Later added it to the libraries directory as well. In both case it compiles. Added a #pragma message to the local library to prove that that's the one that is used.

I do like your approach where you can specify the timer to use.