title: Interrupts within a class
body: <p>I am trying to write a library to calculate the PWM duty period using interrupts. I understand a class member is not the right format of function for attachInterrupt. </p>
  
  <p>However, I have tried to follow this post <a href="http://www.gammon.com.au/forum/?id=12983" rel="nofollow noreferrer">Calling an ISR from a class</a> by <a href="https://arduino.stackexchange.com/users/10794/nick-gammon">Nick Gammon</a>, who has a work around, but frustratingly I'm still getting the error:</p>
  
  <blockquote>
    <p>cannot declare member function 'static void PWMin::risingInt()' to have static linkage </p>
  </blockquote>
  
  <p>Can someone please shine some light on what's wrong with my code or any other suggestions?</p>
  
  <p>This is the cpp file:</p>
  
  <pre><code>#include "PWMin.h"
  
  PWMin::PWMin(int intPin, int* outputTime, bool direction=true){
      instance = this;
  
      this-&gt;_intPin = intPin;
      this-&gt;_outputTime = outputTime;
      this-&gt;_direction = direction;
  
      pinMode(this-&gt;_intPin, INPUT);
      attachInt();
  }
  
  void PWMin::attachInt(){
      attachInterrupt(this-&gt;_intPin, this-&gt;_direction ? risingInt : fallingInt, this-&gt;_direction ? RISING : FALLING);
  }
  
  void PWMin::risingISR(){
      this-&gt;start = micros();
      this-&gt;_direction = false;
      this-&gt;attachInt();
  }
  
  void PWMin::fallingISR(){
      this-&gt;timeElapsed = micros() - this-&gt;start;
      *_outputTime = this-&gt;timeElapsed;
      this-&gt;_direction = true;
      this-&gt;attachInt();
  }
  
  unsigned long PWMin::lastElapsedTime(){
      return this-&gt;timeElapsed;
  }
  
  static void PWMin::risingInt(){
      if(PWMin::instance != NULL){
          PWMin::instance-&gt;risingISR();
      }
  }
  
  static void PWMin::fallingInt(){
      if(PWMin::instance != NULL){
          PWMin::instance-&gt;fallingISR();
      }
  }
  </code></pre>
  
  <p>This is the header file:</p>
  
  <pre><code>#ifndef PWMin_h
  #define PWMin_h
  
  class PWMin {
      public:
          PWMin(int intPin, int* outputTime, bool direction);
          unsigned long lastElapsedTime();
  
      private:
          static PWMin *instance;
  
          int _intPin;
          int* _outputTime;
          bool _direction;
          unsigned long start, timeElapsed;
  
          void attachInt();
          void risingISR();
          void fallingISR();
          static void risingInt();
          static void fallingInt();
  };
  
  #endif /* PWMin_h */
  </code></pre>
  
  <p>Thanks,
  Shaun</p>