Hi everyone, I would really appriciate your help.
I am a student working on an Ball on Beam shield project with arduino, but I am stucked right now.
I have a sampling library that is fully functional in meaning of code (interrupts and so on..), it also can recognize architecture and therefore change the timmer used.
But I need to adjust it, - WHENEVER THE LIBRARY FINDS OUT THAT Servo LIBRARY IS INCLUDED with AVR arch., it recoginzes it and sets itself to timer 2, because Servo is using timer 1.
For further understanding of topic, here are some infos on how does compilation works (let me know if I am wrong):
-The headers (.h) are compiled all together and exchange informations if included in one another, than they are compiled into an object - so the header knows that another header is included.
-All .cpp files in library folder are compiled separately, and all are compiled into functional objects!!!
See pic.1 - I added into 3 of .cpps in Servo library warning sentences (warnings are in slovak language) - all of them are compiled:
IDE has developed preprocessor macros (ARDUINO_ARCH_XXX). Those are different from tokens and are possible to recognize even from .cpp files.
That is how Servo library is able to activate only one .cpp file, out of many included in the folder.
thanks to ifdef condition (see pic.2)
I tried to use the same logic, working with tokens only.
Here is my library folders : (pics 4 5 6)
and the code of main header:
#include "Arduino.h"
#ifndef SAMPLING3_H_
#define SAMPLING3_H_
#if defined(ARDUINO_ARCH_AVR) && defined(Servo_h)
#include "avr_servo3/SamplingTimers3.h"
#elif defined(ARDUINO_ARCH_AVR) && !defined(Servo_h)
#include "avr3/SamplingTimers3.h"
#else
#error "This library only supports boards with an AVR, SAM or SAMD processor."
#endif
typedef void (*p_to_void_func)(); /*define a term p_to_void_func for pointer to function, which
has a return type void and has no input parameters*/
class SamplingClass{
public:
SamplingClass();
void period(unsigned long microseconds);
void interrupt(p_to_void_func interruptCallback);
p_to_void_func getInterruptCallback ();
float getSamplingPeriod();
private:
static void defaultInterrupt();
p_to_void_func interruptCallback;
#if defined(ARDUINO_ARCH_AVR) && !defined(Servo_h)
// Default: Timer1
const unsigned long timerResolution = 65536; // AVR Timer 1 is 16bit
const unsigned char cpuFrequency = 16; // CPU frequency in micro Hertz
#warning "sampling is running on timer 1"
#elif defined(ARDUINO_ARCH_AVR) && defined(Servo_h)
// Default: Timer2
const unsigned long timerResolution = 256; // AVR Timer 2 is 8bit
const unsigned char cpuFrequency = 16; // CPU frequency in micro Hertz
#warning "sampling is running on timer 2"
#endif
float samplingPeriod; // Sampling period in seconds
bool setSamplingPeriod(unsigned long microseconds);
};
extern SamplingClass Sampling;
#endif
Architectures sam and samd not in there yet, do not pay attention to that.
The problem is with recognizing library token(Servo_h) from servo lib., within Sampling library .
Here is what happens:
Step one:
.Ino file compiles (Servo.h included) and calls Sampling.h, witch recognizes insider tokens of Servo and sets to timmer 2 (just simplifing).
Step two:
.cpp file (All of them in library folder) compiles and each one runs on different timer.... see on pic.3
Therefore tokens are quiet useless in what I want to achieve.
Please any ideas about how to make it work???