0

I use the AccelStepper library in my Arduino project, the library has a constructor, with functions as parameters:

AccelStepper(void (*forward)(), void (*backward)());

In the main sketch, this is the code used:

void forwardstep() {
  AFstepper->onestep(FORWARD, stepType);  //some code to move the motor
}
void backwardstep() {
  AFstepper->onestep(BACKWARD, stepType);  //some code to move the motor
}
AccelStepper stepper(forwardstep, backwardstep);

as long as this code is in the main sketch, everything works well.

I have created a class that has an AccelStepper object and the forwardstep() and backwardstep() functions as members, but I cannot pass the functions to the constructor of AccelStepper: .h file:

#define IICADDRESS 0x60

class FilterWheel : public Device
{
public:
    FilterWheel();

    void forwardstep();
    void backwardstep();
    void (*fwdstp)();      //function pointer 
    void (*bckwdstp)();    //function pointer
private:
    //Adafruit Motor Shield object
    Adafruit_MotorShield AFMS; 
    //Adafruit Stepper Motor object
    Adafruit_StepperMotor *AFstepper; 
    //AccelStepper wrapper
    AccelStepper stepper; 
};

.cpp file:

#include "FilterWheel.h"

//constructor
FilterWheel::FilterWheel()
{
    fwdstp = &FilterWheel::forwardstep;
    bckwdstp = &FilterWheel::backwardstep;

    Adafruit_MotorShield AFMS (IICADDRESS);
    Adafruit_StepperMotor *AFstepper = AFMS.getStepper(200, 1); //M1 M2
    //AccelStepper stepper(forwardstep, backwardstep);  //doesn't work
    AccelStepper stepper(fwdstp, bckwdstp);             //works only if fwdstp = &FilterWheel::forwardstep; and bckwdstp = &FilterWheel::backwardstep; are commented out
}

//go 1 step forward
void FilterWheel::forwardstep() {
    AFstepper->onestep(FORWARD, stepType);
}
//go 1 step backward
void FilterWheel::backwardstep() {
    AFstepper->onestep(BACKWARD, stepType);
}

when I try to pass the functions directly,

AccelStepper stepper(forwardstep, backwardstep);

the compiler shows the following error:

FilterWheel.cpp:34: error: no matching function for call to 'AccelStepper::AccelStepper(<unresolved overloaded function type>, <unresolved overloaded function type>)'
AccelStepper.h:AccelStepper(void (*)(), void (*)())
AccelStepper.h:AccelStepper(uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, bool)
AccelStepper.h:AccelStepper(const AccelStepper&)
Error compiling

when I attach the functions to the function pointers,

    fwdstp = &FilterWheel::forwardstep;
    bckwdstp = &FilterWheel::backwardstep;
    AccelStepper stepper(fwdstp, bckwdstp);

the compiler shows these errors:

FilterWheel.cpp:In constructor 'FilterWheel::FilterWheel()'
FilterWheel.cpp:22: error: cannot convert 'void (FilterWheel::*)()' to 'void (*)()' in assignment
FilterWheel.cpp:23: error: cannot convert 'void (FilterWheel::*)()' to 'void (*)()' in assignment
Error compiling

how can I solve this issue?

wergor
  • 3
  • 1
  • A variable like `void (*forward)()` can accept a pointer to a standalone non-member function, or a static member function. It won't work with a non-static member function - it won't know which object to call it on. – Igor Tandetnik Sep 02 '14 at 23:53

1 Answers1

0

forwardstep() and backwardstep() are non-static member functions, so this fwdstp = &FilterWheel::forwardstep; is a pointer to member function, it can't be converted to pointer to function because it needs an object to call it on.

You have to make your functions static or standalone.

Anton Savin
  • 40,838
  • 8
  • 54
  • 90
  • i have thought about this as well. i would rather not use static or standalone functions because i think it would make the code much more complex. during my research i found this post [link]http://stackoverflow.com/a/402385 (and further, [link]http://stackoverflow.com/questions/282372/demote-boostfunction-to-a-plain-function-pointer ) what seems like a solution to my problem. unfortunately i cannot use bind (not declared in this scope) altough the boost for arduino library is installed. – wergor Sep 03 '14 at 11:44
  • @wergor the problem is that you have to pass real pointers to functions, `boost::bind` won't help you. And the second problem is that these functions are parameterless, that is they can take the state only from some global object or a static member of some class. – Anton Savin Sep 03 '14 at 11:52