diff --git a/DueTimer.cpp b/DueTimer.cpp index 6d6de35..e0626ae 100644 --- a/DueTimer.cpp +++ b/DueTimer.cpp @@ -22,7 +22,23 @@ const DueTimer::Timer DueTimer::Timers[9] = { {TC2,2,TC8_IRQn}, }; -void (*DueTimer::callbacks[9])() = {}; +// Fix for compatibility with Servo library +#ifdef USING_SERVO_LIB + // Set callbacks as used, allowing DueTimer::getAvailable() to work + void (*DueTimer::callbacks[9])() = { + (void (*)()) 1, // Timer 0 - Occupied + (void (*)()) 0, // Timer 1 + (void (*)()) 1, // Timer 2 - Occupied + (void (*)()) 1, // Timer 3 - Occupied + (void (*)()) 1, // Timer 4 - Occupied + (void (*)()) 1, // Timer 5 - Occupied + (void (*)()) 0, // Timer 6 + (void (*)()) 0, // Timer 7 + (void (*)()) 0 // Timer 8 + }; +#else + void (*DueTimer::callbacks[9])() = {}; +#endif double DueTimer::_frequency[9] = {-1,-1,-1,-1,-1,-1,-1,-1,-1}; /* @@ -30,12 +46,15 @@ double DueTimer::_frequency[9] = {-1,-1,-1,-1,-1,-1,-1,-1,-1}; */ DueTimer Timer(0); -DueTimer Timer0(0); DueTimer Timer1(1); -DueTimer Timer2(2); -DueTimer Timer3(3); -DueTimer Timer4(4); -DueTimer Timer5(5); +// Fix for compatibility with Servo library +#ifndef USING_SERVO_LIB + DueTimer Timer0(0); + DueTimer Timer2(2); + DueTimer Timer3(3); + DueTimer Timer4(4); + DueTimer Timer5(5); +#endif DueTimer Timer6(6); DueTimer Timer7(7); DueTimer Timer8(8); @@ -228,14 +247,19 @@ long DueTimer::getPeriod(){ Implementation of the timer callbacks defined in arduino-1.5.2/hardware/arduino/sam/system/CMSIS/Device/ATMEL/sam3xa/include/sam3x8e.h */ +// Fix for compatibility with Servo library +#ifndef USING_SERVO_LIB void TC0_Handler(){ TC_GetStatus(TC0, 0); DueTimer::callbacks[0](); } +#endif void TC1_Handler(){ TC_GetStatus(TC0, 1); DueTimer::callbacks[1](); } +// Fix for compatibility with Servo library +#ifndef USING_SERVO_LIB void TC2_Handler(){ TC_GetStatus(TC0, 2); DueTimer::callbacks[2](); @@ -252,6 +276,7 @@ void TC5_Handler(){ TC_GetStatus(TC1, 2); DueTimer::callbacks[5](); } +#endif void TC6_Handler(){ TC_GetStatus(TC2, 0); DueTimer::callbacks[6](); diff --git a/DueTimer.h b/DueTimer.h index c999909..dbbf11a 100644 --- a/DueTimer.h +++ b/DueTimer.h @@ -16,6 +16,20 @@ #include +/* + This fixes compatibility for Arduono Servo Library. + Uncomment to make it compatible. + + Note that: + + Timers: 0,2,3,4,5 WILL NOT WORK, and will + neither be accessible by Timer0,... +*/ +// #define USING_SERVO_LIB true + +#ifdef USING_SERVO_LIB + #warning "HEY! You have set flag USING_SERVO_LIB. Timer0, 2,3,4 and 5 are not available" +#endif + class DueTimer { protected: @@ -62,12 +76,15 @@ class DueTimer // Just to call Timer.getAvailable instead of Timer::getAvailable() : extern DueTimer Timer; -extern DueTimer Timer0; extern DueTimer Timer1; -extern DueTimer Timer2; -extern DueTimer Timer3; -extern DueTimer Timer4; -extern DueTimer Timer5; +// Fix for compatibility with Servo library +#ifndef USING_SERVO_LIB + extern DueTimer Timer0; + extern DueTimer Timer2; + extern DueTimer Timer3; + extern DueTimer Timer4; + extern DueTimer Timer5; +#endif extern DueTimer Timer6; extern DueTimer Timer7; extern DueTimer Timer8; @@ -76,4 +93,4 @@ extern DueTimer Timer8; #else #error Oops! Trying to include DueTimer on another device? -#endif +#endif \ No newline at end of file diff --git a/README.md b/README.md index b4bbe41..7d969c6 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ Timer Library to work with Arduino DUE 1. [Download](https://github.com/ivanseidel/DueTimer/releases) the Latest release from gitHub. 2. Unzip and modify the Folder name to "DueTimer" (Remove the '-version') 3. Paste the modified folder on your Library folder (On your `Libraries` folder inside Sketchbooks or Arduino software). +4. Re-open Arduino Software ## Getting Started @@ -69,6 +70,16 @@ DueTimer::getAvailable().attachInterrupt(callback2).start(10); // And so on... ``` +### Compatibility with Servo.h + +Because Servo Library uses the same callbacks of DueTimer, we provides a custom solution for working with both of them. However, Timers 0,2,3,4 and 5 will not Work anymore. + +You will need uncommend the line in `DueTimer.h` in `DueTimer` folder inside the `Libraries` folder. Uncomment the following line in `DueTimer.h`: + +``` +#define USING_SERVO_LIB true +``` + ## Library Reference ### You should know: