lathoub / Arduino-AppleMIDI-Library

Send and receive MIDI messages over Ethernet (rtpMIDI or AppleMIDI)
Other
306 stars 66 forks source link

Can not wrap instance of the AppleMidi library in an own class #76

Closed luc-debug closed 4 years ago

luc-debug commented 4 years ago

Hello guys, as described i would like to wrap the use of the library in a class.

class WirelessMidi { public: WirelessMidi(); ~WirelessMidi();

appleMidi::AppleMidiInterface<WiFiUDP> *appleMidiProperty;
static void OnAppleMidiConnected(uint32_t ssrc, char *name);  

};

include

include

WirelessMidi::WirelessMidi() { APPLEMIDI_CREATE_INSTANCE(WiFiUDP, AppleMIDI); this->appleMidiProperty=&AppleMIDI; }

WirelessMidi::~WirelessMidi() { }

void WirelessMidi:: OnAppleMidiConnected(uint32_t ssrc, char *name) { isConnected = true; Serial.print(F("Connected to session ")); Serial.println(name); }

WirelessMidi midi; midi.appleMidiProperty->begin("test");

This is the error I get: collect2.exe: error: ld returned 1 exit status *** [.pio\build\az-delivery-devkit-v4\firmware.elf] Error 1

Any help is highly appreciated. Thank you in advance.

lathoub commented 4 years ago

@luc-debug this is not so much of a AppleMIDI library question, but rather a C++ question. (I will not ask what you are trying to accomplish - out of scope of the issues section of the library)

However:

WirelessMidi::WirelessMidi()
{
APPLEMIDI_CREATE_INSTANCE(WiFiUDP, AppleMIDI);
this->appleMidiProperty=&AppleMIDI;
}

would create an instance of AppleMIDISession in the constructor - and bring it back down when the constructor is out of scope, with a stale reference in this->appleMidiProperty.

See what is deflated and instantiated in the APPLEMIDI_CREATE_INSTANCE macro (AppleMIDI.h, towards the end).

luc-debug commented 4 years ago

Awesome thank you so much for your fast response. Sorry for the dump question I thought it would be related to the library, I am pretty new to C++. Anyway thank you for your C++ hint.

lathoub commented 4 years ago

no worries @luc-debug there is actually no need to wrap the instance - APPLEMIDI_CREATE_INSTANCE creates the instance you can use right away

luc-debug commented 4 years ago

I want to clean up my main file and give my code a object-oriented design. That is why I want to wrap all functionalities (like the instance of the library, event handlers, IsConnected bool variable) of your (very awsome!) library in my own class. Thanks to your advice I know now that I have to improve my C++ skills to solve it.

luc-debug commented 4 years ago

@lathoub I am right in assuming that I can only declare the AppleMidi instance globally?

lathoub commented 4 years ago

the AppleMIDI instance has the same lifetime/scope of any other c++ instance (incl: int) (it's just a bit more complex using templates - do not be blinded by it. Start with simple things, then work your way up). Good luck.

mortocks commented 1 year ago

@lathoub sorry, I know this isn't really the forum to ask but my research has come up empty.

What type should this-> appleMidiProperty by in the class definition?

#ifndef MIDI_MANAGER_H
#define MIDI_MANAGER_H
#include <Arduino.h>
#include <AppleMIDI.h>

class MidiManager {
    public:
        MidiManager();
        void begin();
        void update();
        SOMETHING?? appleMidiProperty ;
};

#endif