Problem writing a library using PinChangeInt

80 views
Skip to first unread message

Henrik Larsen

unread,
Sep 7, 2014, 3:51:57 PM9/7/14
to arduino-pincha...@googlegroups.com
Hi,

I'm a newbie in the Arduino world, but got many years as programmer in my backpack. Now you know where we are...

Im writing my first library so its problably something silly, but i'm lost here...

In the samples I have removed unimportant stuff to awoid overloading.

My Header file looks like this:

/*
PWMSampler - Library for samling a pin for a PWM signal.
*/
 
#ifndef PWMSampler_h
#define PWMSampler_h
 
#ifndef PinChangeInt_h
#define LIBCALL_PINCHANGEINT
#include <PinChangeInt.h>
#endif
 
#include "Arduino.h";
 
class PWMSampler
{
 
public:
 
	PWMSampler(int pin, unsigned long timeout);
private:
.....
	void doSampleLow(); // interrupt handler for Low going signal
	void doSampleHigh(); // interrupt handler for High going signal
};
 
#endif


And my Code file looks ike this

#include "Arduino.h";
#include "PWMSampler.h";
 
#ifndef PinChangeInt_h
#define LIBCALL_PINCHANGEINT
#include <PinChangeInt.h>
#endif
 
/*
CONSTRUCTOR
*/
 
PWMSampler::PWMSampler(int pin, unsigned long timeout)
{
	unsigned long now = micros();
 
	pinMode(pin, INPUT);
	.....
	PinChangeInt_h::attachInterrupt(pin, &PWMSampler::doSampleLow, FALLING);
	PinChangeInt_h::attachInterrupt(pin, &PWMSampler::doSampleHigh, RISING);
}
 
/*
PUBLIC
*/
 
.....
/*
PRIVATE
*/
 
void PWMSampler::doSampleLow()
{ 
	.....
}
 
void PWMSampler::doSampleHigh()
{
	.....
}

And I'm using the Visual Micro IDE with Visual Studio 2012.

My library is added directly to my solution, and the Header file is added to the Headers subdir in my solution and the cpp file is added to the sources subdir of my solution.

PinChangeInt library is added to the Arduino environment Library folder.

When I compile I get this:

Compiling 'SpindleControl' for 'Arduino Uno'

PWMSampler.cpp:In constructor 'PWMSampler::PWMSampler(int, long unsigned int)'

PWMSampler.cpp:28: error: cannot convert 'void (PWMSampler::*)()' to 'void (*)()' for argument '2' to 'void attachInterrupt(uint8_t, void (*)(), int)'

PWMSampler.cpp:29: error: cannot convert 'void (PWMSampler::*)()' to 'void (*)()' for argument '2' to 'void attachInterrupt(uint8_t, void (*)(), int)'

Error compiling


I guess this is very simple, as I have no experience with this language other than basic stuf.

Can anyone explain this error.


Henrik Larsen

unread,
Sep 8, 2014, 3:50:39 AM9/8/14
to arduino-pincha...@googlegroups.com
Got some more info on this.
 
As I understand it from some forums I read last night, the C class method has an implicit first argument, "this" - the class itself. So the class methods do not have void argument list thus the construction for PinCangeInt_h::attachinterrupt(...) is rejected by the compiler.
 
Hmmm, so in order to solve this, I will on my next try, make a constructor for my library class, requiring a pointer to a callback method, given by the main program, that my library can bind to the attachInterrupt. The mainprogram must then, from the callback method, call my PWMSampler library interrupt service method. Its a reroute, but it should work.
 
I know, it's really a poor construction, in fact the whole idea of making a Library is nearly lost.
 
Did this make any sense.
 
(I'm at work now, but just has a bleeding in my brain and got this idea, had to write it down while fresh in my mind.)
 

Michael Schwager

unread,
Sep 8, 2014, 7:58:41 AM9/8/14
to arduino-pincha...@googlegroups.com
Yes, I was going to say that I had the same problem. It's that you're trying to use a C-style library with C++ methods.

Check out my ooPinChangeInt library (http://code.google.com/p/oopinchangeint/) You may find that does the trick for you!

Let me know how it goes.


--
You received this message because you are subscribed to the Google Groups "arduino-pinchangeint-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to arduino-pinchangeint...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
-Mike Schwager

Henrik Larsen

unread,
Sep 9, 2014, 12:43:34 PM9/9/14
to arduino-pincha...@googlegroups.com
Hi Again,

I looked at the oo library but ran into all kinds of weird compiler problems, probably due to me not ensuring correct syntax and semantics.

Then I went back to my own idea, and I got that to work.

My Skecth offers simple methods that are offered to the constructor of my own liberary using the PinChangeInt library. That instance hookes the PinChangeInt interrupt to the skecth method, and since the sketch has a pointer to my instantiated library "object", it can call a public method there to do the interrupt work.

This actually has advantages. The skects can easily hook into the library so it can be used for easy debugging/testing.

The high end super duper programming practice is broken completely, but who cares - i don't.

I got it to work with my own libraries, thats the important stuff. Now I can pack complex functionality away and let the sketch do continuous decisions in its mainloop.

/Thanks

Reply all
Reply to author
Forward
0 new messages