Skip to content

Commit

Permalink
Merge pull request #26 from ivanseidel/fix-servo-compatible
Browse files Browse the repository at this point in the history
fixes #10
  • Loading branch information
Ivan Seidel committed Mar 25, 2014
2 parents 01222db + 7f71489 commit fd1e90a
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 12 deletions.
37 changes: 31 additions & 6 deletions DueTimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,39 @@ 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};

/*
Initializing all timers, so you can use them like this: Timer0.start();
*/
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);
Expand Down Expand Up @@ -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]();
Expand All @@ -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]();
Expand Down
29 changes: 23 additions & 6 deletions DueTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@

#include <inttypes.h>

/*
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:
Expand Down Expand Up @@ -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;
Expand All @@ -76,4 +93,4 @@ extern DueTimer Timer8;

#else
#error Oops! Trying to include DueTimer on another device?
#endif
#endif
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit fd1e90a

Please sign in to comment.