Using MIDI.h in a class

Status
Not open for further replies.

Sandro

Well-known member
Hi all,
I need to use MIDI.h functions inside a class; until now I've been able to use MIDI only in a single function of a class:

Main code:
Code:
#include <Test.h>

Test test;
void setup()
{
   
}

void loop()
{
  test.start_midi();
}

Test.h
Code:
#ifndef Test_h_
#define Test_h_

#include "Arduino.h"
#include <MIDI.h> // https://www.pjrc.com/teensy/td_libs_MIDI.html

class Test
{

public:
    void start_midi(void)
    {
        MIDI_CREATE_DEFAULT_INSTANCE();
        MIDI.begin(MIDI_CHANNEL_OMNI);
        do
        {
            if (MIDI.read())
            {
                switch(MIDI.getType())
                {
                case midi::NoteOn:       // If it is a Program Change,
                {
                    Serial.println();
                    Serial.print("NoteOn received");
                }
                break;

                case midi::NoteOff:
                {
                    Serial.println();
                    Serial.print("NoteOff received");
                }
                break;

                default:
                    break;
                }
            }
        }
        while(1);
    }
};
#endif


Starting from this point, how to use MIDI in more than one function? For example, I would separate MIDI.begin() from MIDI.read()... Something like this:

Main code
Code:
#include <Test.h>

Test test;
void setup()
{
  test.start_midi(); 
}

void loop()
{ 
  test.start_reading_midi();
}

Test.h
Code:
#ifndef Test_h_
#define Test_h_

#include "Arduino.h"
#include <MIDI.h> // https://www.pjrc.com/teensy/td_libs_MIDI.html

class Test
{

public:
    void start_midi(void)
    {
        MIDI_CREATE_DEFAULT_INSTANCE();
        MIDI.begin(MIDI_CHANNEL_OMNI);
    }

     void start_reading_midi(void)
    {
        do
        {
            if (MIDI.read())
            {
                switch(MIDI.getType())
                {
                case midi::NoteOn:       // If it is a Program Change,
                {
                    Serial.println();
                    Serial.print("NoteOn received");
                }
                break;

                case midi::NoteOff:
                {
                    Serial.println();
                    Serial.print("NoteOff received");
                }
                break;

                default:
                    break;
                }
            }
        }
        while(1);
    }
};
#endif

But this code cannot be compiled, because (I belive) MIDI doesn't exist in start_reading_midi(void)... the error message is:
In file included from C:\Users\dell\AppData\Local\Temp\arduino_modified_sketch_773841\Test_midi_in_class.ino:1:0:

C:\Users\mrX\ARDUINO SKETCHES\libraries\midi_in_class/Test.h: In member function 'void Test::start_reading_midi()':

C:\Users\mrX\ARDUINO SKETCHES\libraries\midi_in_class/Test.h:21:17: error: 'MIDI' was not declared in this scope

if (MIDI.read())

Can anybody suggest me how to make MIDI visible in all Test.h methods?
Thanks
 
Status
Not open for further replies.
Back
Top