Declared "MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI);" and MIDI is still Out Of Scope! #269
-
Hi, I'm doing a project that is split in multiple header files in a single namespace. In a crude way my project looks like this: The file MidiInclude.h is like this: #ifndef MIDIINCLUDE_H_INCLUDED
#define MIDIINCLUDE_H_INCLUDED
#include <Arduino.h>
#include <MIDI.h>
namespace MidiProject {
MidiClass {
public:
static void initiateMidi();
static void sendMidi();
};
}
#endif // MIDIINCLUDE_H_INCLUDED The file MidiInclude.cpp is like this: #include "MidiInclude.h"
namespace MidiProject {
MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI);
MidiClass::initiateMidi() {
MIDI.begin(MIDI_CHANNEL_OMNI);
MIDI.setThruFilterMode(midi::Thru::Off);
}
MidiClass::sendMidi() {
MIDI.sendNoteOff(60, 100, 1); // **Compiles and works fine**
delay(1000);
MIDI.sendNoteOff(60, 0, 1); // **Compiles and works fine**
}
} Now the MainProject.h file: #ifndef MAINPROJECT_H_INCLUDED
#define MAINPROJECT_H_INCLUDED
#include <Arduino.h>
#include "MidiInclude.h"
namespace MidiProject {
ProjectClass {
public:
static void initiateMidi();
static void sendMidi();
};
}
#endif // MAINPROJECT_H_INCLUDED The MainProject.cpp file: #include "MainProject.h"
namespace MidiProject {
ProjectClass::initiateMidi() {
MidiClass::initiateMidi(); // **Compiles and works fine**
}
ProjectClass::sendMidi() {
MIDI.sendNoteOff(60, 100, 1); // **ERROR: 'MIDI' was not declared in this scope**
delay(1000);
MIDI.sendNoteOff(60, 0, 1); // **ERROR: 'MIDI' was not declared in this scope**
}
} I don't understand why the Finally the main.cpp file is like this: #include "MainProject.h"
void setup() {
MidiProject::ProjectClass::initiateMidi();
MidiProject::ProjectClass::sendMidi();
}
void loop() {
// Empty
} Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
That would be because the To use it somewhere else, you'd have to declare it first as #ifndef MIDIINCLUDE_H_INCLUDED
#define MIDIINCLUDE_H_INCLUDED
#include <Arduino.h>
#include <MIDI.h>
namespace MidiProject {
extern midi::MidiInterface<midi::SerialMIDI<HardwareSerial>> MIDI;
// ↓ I assume there was a missing class or struct keyword here?
class MidiClass {
public:
static void initiateMidi();
static void sendMidi();
};
}
#endif // MIDIINCLUDE_H_INCLUDED |
Beta Was this translation helpful? Give feedback.
-
OK. I'm not using OOP in what I'm doing. I just have straight C code broken in to files. One of them is a callbacks.cpp and a callbacks.h where I define the callbacks I'm using. How do I access MIDI (the object) in my files - as in MIDI.sendPitchBend (newValue, channel);? And, in my main Setup() and Loop()? callbacks.cpp
callbacks.h
|
Beta Was this translation helpful? Give feedback.
That would be because the
MIDI_CREATE_INSTANCE
macro is called in a .cpp file, which limits the scope of the createdMIDI
object to that compilation unit.To use it somewhere else, you'd have to declare it first as
extern
inMidiInclude.h
, as such: