Creating library help!

Dear reader,

To easy my programming I want to make a library that reads and ecoder en eventually send MIDI data over midi USB.

Since this is my first lib I want to make it step by step so errors are more easy to filter out and so I can understand how this proces works but even the first steps give me so much errors and I cannot find any solution more.

So to start this is the program i want to convert into a lib

#define pin_A 2 // interrupt 0
#define pin_B   4 // this can be any pin

volatile uint8_t encoder_counter;
volatile uint8_t encoder_pulse;


void setup() {
  Serial.begin(9600);
  pinMode(pin_A , INPUT);
  pinMode(pin_B , INPUT);
  attachInterrupt(digitalPinToInterrupt(pin_A), read_encoder, FALLING);
}

void loop() {
  if (encoder_pulse) {
    encoder_pulse = 0;
    Serial.print("encoder_counter: ");
    Serial.print(encoder_counter);
  }
}

void read_encoder() { 
    if (digitalRead(pin_B) == HIGH) // switch to LOW to reverse direction of Encoder counting
    {
      encoder_counter++;
    }
    else {
      encoder_counter--;
    }
    encoder_pulse++;
  }
}

so i made a .h file which looks like this:

/*
  MIDI_encoder.h - Library for interfacing rotary encoders with software which communicate through MIDI.
  Created by Jazzy Gérard, February, 2017.
  Released into the public domain.
*/

#ifndef MIDI_encoder_h
#define MIDI_encoder_h

#include "Arduino.h"

class MIDI_encoder
{
  public:
    MIDI_encoder(int pin_A, int pin_B);
    void read_encoder();
    volatile uint8_t encoder_counter = 127;
    volatile uint8_t encoder_pulse;
  private:
    int _pin_A;
    int _pin_B;
};

#endif

and a .cpp file which looks like this:

/*
  MIDI_encoder.cpp - Library for interfacing rotary encoders with software which communicate through MIDI.
  Created by Jazzy Gérard, February, 2017.
  Released into the public domain.
*/

#include "Arduino.h"
#include "MIDI_encoder.h"

MIDI_encoder::MIDI_encoder(int pin_A, int pin_B)
{
  pinMode(pin_A, INPUT);
  _pin_A = pin_A;
  pinMode(pin_B, INPUT);
  _pin_B = pin_B;
  attachInterrupt(digitalPinToInterrupt(_pin_A), read_encoder, FALLING);
}

void MIDI_encoder::read_encoder()
{
  if (digitalRead(_pin_B) == HIGH) // switch to LOW to reverse direction of Encoder counting
  {
      encoder_counter++;
  }
  else
  {
      encoder_counter--;
  }
  
    encoder_pulse++;
}

So want I want to do in my first step is to make an instance which declares all inputs and the interrupt pin and work with the function "void read_encoder()"

I got already al lot of errors but the last one was the following:

Arduino: 1.8.0 (Mac OS X), TD: 1.34, Board:"Teensy 3.2 / 3.1, Serial, 96 MHz (overclock), Fast, US English"

/Users/Jazzy/Documents/Arduino/libraries/MIDI_encoder/MIDI_encoder.cpp: In constructor 'MIDI_encoder::MIDI_encoder(int, int)':
/Users/Jazzy/Documents/Arduino/libraries/MIDI_encoder/MIDI_encoder.cpp:16:71: error: cannot convert 'MIDI_encoder::read_encoder' from type 'void (MIDI_encoder::)()' to type 'void (*)()'
attachInterrupt(digitalPinToInterrupt(_pin_A), read_encoder, FALLING);
^
Fout bij het compileren van board Teensy 3.2 / 3.1

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Can anyone please guide me through this proces!

thanks in advance!

Read_encoder is expected as a function. In a class, it's a method, so attachinterrupt can't find it. I believe you need to use a static method and object.

It's a bit tricky. Something like this

a.h

 static MIDI_encoder *thisClassObj; // get instance to call pointer
 static void get_encoder() {
     thisClassObj->read_endcoder();
}

a.cpp

MIDI_encoder *MIDI_encoder::thisClassObj // define this outside any method like on top of the page.
...

attachInterrupt(digitalPinToInterrupt(_pin_A), get_encoder, FALLING);
     thisClassObj

Thank you for the quick reply! what do you mean with thisClassObj?

Jazzy

It's a reference of class's instance in memory when you instantiate it.
It's not the most elegant thing in the world. You can see the limitation of C++ there.

Please use code tags also for warning or error methods.

mistergreen:
Read_encoder is expected as a function. In a class, it's a method

There is no such thing as a method in C++, in a class it's called a member function.