Show Posts
Pages: [1]
1  Using Arduino / Programming Questions / Re: Setting up interrupts in a library ... problems with attachInterrupt() on: March 26, 2013, 09:43:53 am
I'm trying to implement a version of the above code but I'm missing something.  This is somewhat new to me, so I'm having a little trouble comprehending everything.  First, I'm not sure I understand these glue lines completely:

Code:
// for use by ISR glue routines
Foo * Foo::instance0_;
Foo * Foo::instance1_;

I'll come back to that in a second, let me try to state what I believe I understand.  You define instance0_ & instance1_ as static pointers to a Foo object in the class definition:

Code:
static Foo * instance0_;
static Foo * instance1_;

And then you set these to the Foo object pointer (this) in the .begin() method:

Code:
void Foo::begin ()
  {
  switch (whichISR_)
    {
    case 0:
      attachInterrupt (0, isr0, FALLING);
      instance0_ = this;
      break;
   
    case 1:
      attachInterrupt (1, isr1, FALLING);
      instance1_ = this;
    break;
    } 
  }  // end of Foo::begin

I think I'm alright with all of that.  I believe you are using the static instancex_ to provide a link back to the specific FOO class so it's members can be accessed in the ISR.  Am I correct to this point?

You then use these instancex_ references to access the handleInterrupt() method in the ISR methods:

Code:
// ISR glue routines
void Foo::isr0 ()
  {
    instance0_->handleInterrupt ();
  }  // end of Foo::isr0

void Foo::isr1 ()
  {
    instance1_->handleInterrupt ();
  }  // end of Foo::isr1

Now we come to the glue lines I'm confused about:

Code:
// for use by ISR glue routines
Foo * Foo::instance0_;
Foo * Foo::instance1_;

What are these doing?  I'm guessing these are taking care of something associated with the fact that the class definition of instance0_ & instance1_ are static but it's just not making sense.

Appreciate any help.





2  Using Arduino / Programming Questions / Re: Setting up interrupts in a library ... problems with attachInterrupt() on: March 25, 2013, 10:50:55 pm
I basically understand what you've done but I'm a little lost on the glue routines.  Can you give me a little more info on what they are doing?
3  Using Arduino / Programming Questions / Re: Setting up interrupts in a library ... problems with attachInterrupt() on: March 25, 2013, 07:53:16 pm
OK, so ISR routines cannot be methods of a class.  I guess that makes some sense since the ISR is called based on resources (i.e. pin state) that are not within the control of the class.  But, the Easy_Motor class is defining what needs to be done for the interrupt and it's instantiation really defines the point that the interrupt needs to be made available.  This needs to run with as small a memory footprint as possible, so I don't want to create a global version of the Easy_Motor class (not to mention the possible issues if the code uses this improperly).  Unless that is the only way to allow any access to Easy_Motor attributes.  I wish there was a better way to model this.

I can move the ISR routines to Easy_Motor.h but currently I need to reference some Easy_Motor private class variables (pin definitions, names beginning with _) & the MOTOR object (_MyMtr) from within the ISR.  If the ISR becomes a general function within Easy_Motor.h, how can I reference the private class objects & variables?
4  Using Arduino / Programming Questions / Setting up interrupts in a library ... problems with attachInterrupt() on: March 25, 2013, 05:50:24 pm
I'm creating a library object (called Easy_Motor) and need to setup some interrupt handlers within the object.  I've attempted to use attachInterrupt() in the .cpp but I'm getting compiler errors regarding the specified handler method name.  Here's an example of the returned error message:

C:\Documents and Settings\Preferred Customer\My Documents\Arduino\libraries\Easy_Motor\Easy_Motor.cpp:103: error: invalid use of void expression
C:\Documents and Settings\Preferred Customer\My Documents\Arduino\libraries\Easy_Motor\Easy_Motor.cpp:107: error: argument of type 'void (Easy_Motor::)()' does not match 'void (*)()'


Here's the code that is causing the error(s) (tried both ways shown, same error):

Code:
attachInterrupt(0, _switchHOME, FALLING);
attachInterrupt(0, Easy_Motor::_switchHOME, FALLING);

Same error is given regardless of the interrupt number used (I'm using a MEGA, so #'s 0-5 are valid).

The interrupt handler method(s) are private methods of the Easy_Motor class, named _switchHOME() & _switchLIMIT().  Here's their code:

Code:
void Easy_Motor::_switchHOME ()
{
  // Interrupt Handler for HOME switch.

  // STOP MOTOR (with BRAKE)!
  digitalWrite(_byMtrA_1pin, 255); 
  digitalWrite(_byMtrA_2pin, 255);

  // Have already physically STOPPED motor to help eliminate damage.
  // Update the MOTOR object accordingly.
  _MyMtr.speed(255);
  _MyMtr.stop(BRAKE);
}

void Easy_Motor::_switchLIMIT ()
{
  // Interrupt Handler for LIMIT switch.

  // STOP MOTOR (with BRAKE)!
  digitalWrite(_byMtrA_1pin, 255); 
  digitalWrite(_byMtrA_2pin, 255);

  // Have already physically STOPPED motor to help eliminate damage.
  // Update the MOTOR object accordingly.
  _MyMtr.speed(255);
  _MyMtr.stop(BRAKE);
}

It it possible to use attachInterrupt() within a library .cpp file?  If not, how do I setup the interrupts without using the arduino-aware attachInterrupt() macro?

Appreciate any help.
5  Using Arduino / Installation & Troubleshooting / Re: Problem uploading specific sketch on MEGA2560 ... on: March 10, 2013, 03:16:58 pm
Can you please post a link to the updated bootloader for the MEGA2560 ...
6  Using Arduino / Installation & Troubleshooting / Re: Problem uploading specific sketch on MEGA2560 ... on: March 10, 2013, 08:53:07 am
That is really strange!  I never would have been able to unravel that issue on my own.  From now on, I'll definitely keep my enthusiasm in-check [!!]  Thank you very much for the help.
7  Using Arduino / Installation & Troubleshooting / Re: Problem uploading specific sketch on MEGA2560 ... on: March 10, 2013, 08:45:09 am
Wow!  I did have a "!!!" in a Serial.print in my code.  Took that out and it uploaded OK.  Is this an issue specific to 1.0.1 or does it happen in 1.0.3 too?

Does this occur only with 3 !'s or any number of !'s?  Why does that character (string) cause an upload hang?  Is it only an issue with a Serial.print or could similar problems occur if there is just a reference to a "!!!" string in the program?  Why does this happen?  Are there any other characters to watch out for?  Is there a list of "oddities" like this that is maintained somewhere?  Thanks for the help.
8  Using Arduino / Installation & Troubleshooting / Problem uploading specific sketch on MEGA2560 ... on: March 10, 2013, 08:22:20 am
I have a sketch that I'm working on that has been uploading fine for a week and now I can't get uploading to complete (using V.1.0.1).  I'm using a Seeeduino Mega board (and have been for many months without any problems).  I'm not getting any visible errors reported in the IDE for this sketch (and normal compile completes without errors), it just won't finish an upload cycle on this specific sketch.  I can upload other sketches without problems.  Here's what it looks like when it hangs:



When it begins to upload, the TX & RX LEDs blink for a bit (as expected) and then it appears the pin13 LED is lighted and it hangs (I'm not using the pin13 OUTPUT in this sketch).  If I shutdown the IDE and remove/reset power to the board, it doesn't seem like its running any program and the pin13 LED appears to blink in a sequence (2 quick pulses, OFF, repeat).  I'm a little perplexed, appreciate any help.
Pages: [1]