I have a a simple project with three files. myreader.cpp and myreader.h that define a simple ooPinChangeInt (Google Code Archive - Long-term storage for Google Code Project Hosting.) CallBackInterface-derived class. Then I have my main file, called readpwm1.ino. I have attached a zip file with them (absolute minimum to demonstrate the problem). Basically in the ino file if I comment out the ooPinChangeInt.h include, then arduino cannot compile myreader.cpp; the compiler does not recognize CallBackInterface, which is defined in a header file that is in included by myreader.cpp. I am mystified as to why g++ cannot see it.
If I add the ooPinChangeInt.h to the main ino file, then the myreader.cpp file compiles just fine. However now readpwm1.ino will not compile and it claims that ooPinChangeInt.h is redefining a bunch of things.
Here are the files inline:
/* uncomment next line and myreader.cpp will compile,
but arduino complains about redefinitions. without
it, myreader.cpp says it can't find
CallBackInterface or PCintPort.*/
//#include <ooPinChangeInt.h>
#include "myreader.h"
MyReader p(2);
void setup()
{
Serial.begin(9600);
}
void loop()
{
delay(500);
}
myreader.h
#ifndef __MYREADER_H__
#define __MYREADER_H__
#include <Arduino.h>
#include <ooPinChangeInt.h>
#include <cbiface.h>
class MyReader : public CallBackInterface
{
public:
unsigned char count;
unsigned long time;
unsigned long ticksperminute;
uint8_t pin;
unsigned long total_ticks;
MyReader(uint8_t _pin);
void cbmethod();
private:
void init ();
};
#endif
And myreader.cpp:
#include <limits.h>
#include "myreader.h"
#include <ooPinChangeInt.h>
//#include <cbiface.h>
//#include <Arduino.h>
inline unsigned long elapsed_time(unsigned long now, unsigned long then) {
if (now < then) {
/* number wrapped around; do some math */
return ULONG_MAX - then + now + 1;
} else {
/* straight-forward difference */
return now - then;
}
}
#define MAX_TICKS_PER_CALC 255
#define TICKS_PER_CALC 10
static const double coeff = 0.10;
MyReader::MyReader (uint8_t _pin): pin(_pin)
{
init();
};
void MyReader::cbmethod() {
unsigned long now;
unsigned long delta;
unsigned long tpm;
count++;
total_ticks++;
if(count >= TICKS_PER_CALC) {
now = micros();
if (now == time)
return;
tpm = (unsigned long)( TICKS_PER_CALC / (double) (elapsed_time(now, time)) * 1000000 * 60 );
ticksperminute = tpm * coeff + ticksperminute * (1 - coeff);
count=0;
time = micros();
}
}
void MyReader::init () {
ticksperminute=0;
pinMode(pin, INPUT);
digitalWrite(pin, LOW);
PCintPort::attachInterrupt(pin, this, RISING);
time = micros();
total_ticks = 0;
}
I've read about how the ino preprocessor is broken when it comes to certain # statements. Is this a case of Arduino's broken preprocessor biting me?
Is there any way to get arduino to compile my little project as it is layed out? I cannot add the class to the main arduino file because that seems to break compiling of other classes I have added in.
As an aside, I am finding Arduino's build system to be rather cumbersome and very fragile. Are there any good recipes for building using other tools like cmake?
readpwm1.zip (3.18 KB)