can I include a library within a library

I'd like to write a library which uses the MIDI.h library within it. The idea is to take a reading from a potentiometer and pass it through a noise filter and then finally send it via MIDI control Change. I got this working when i was just sending Serial.print but as soon as I changed the send to MIDI CC it started throwing up errors. Any help would be appreciated. thanks
Here is what I've tried so far: -

sketch: -

#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();

#include "stablePot.h"

stablePot stablePot0(A0);

void setup() {                  
  MIDI.begin(1);
}
void loop(){
  stablePot0.midiSendCC();
  delay(25);
}

header file: -

#ifndef stablePot_h
#define stablePot_h

class stablePot
{
  public:
    stablePot(int pin);
    void midiSendCC();
  private:
    int _pin;
    int _val;
    int _lastVal;
};
#endif

cpp file: -

#include "Arduino.h"
#include "stablePot.h"
#include "MIDI.h"
//MIDI_CREATE_DEFAULT_INSTANCE();

stablePot::stablePot(int pin)
{
  _pin = pin;
  _val = 0;
  _lastVal = 0;
}

void stablePot::midiSendCC()
  {
  _val = analogRead(_pin);

  if(abs(_val - _lastVal) > 4){
    MIDI.sendControlChange(0, 0, 1);
    _lastVal = _val;
    }
  }

The MIDI_CREATE_DEFAULT_INSTANCE(); line is required in the main sketch to make the MIDI library work so I tried adding it in the cpp file as well but it didn't help. therefore i've commented it out.

ALso... the MIDI.h and MIDI.cpp files are in a folder called 'MIDI_Library-5.0.2' which is located inside \Documents\Arduino\libraries
And the stablePot.h and stablePot.cpp are inside a folder named 'stablePot' which is in the same location.

liamobsolete:
but as soon as I changed the send to MIDI CC it started throwing up errors.

Perhaps it would be a good idea to post those error messages?

You should add another parameter to your stablePot constructor, that is a reference to the MidiInterface instance, then use this reference to call its methods. At least that's how I would do it, even if there is only one instance of MidiInterface in the program.

Example (untested)

#ifndef stablePot_h
#define stablePot_h
#include "MIDI.h"

class stablePot
{
  public:
    stablePot(MidiInterface & midi, int pin);
    void midiSendCC();
  private:
    int _pin;
    int _val;
    int _lastVal;
    MidiInterface * _midiInterface;
};
#endif
#include "Arduino.h"
#include "stablePot.h"

stablePot::stablePot(MidiInterface & midi, int pin)
{
  _pin = pin;
  _val = 0;
  _lastVal = 0;
  _midiInterface = &midi;
}

void stablePot::midiSendCC()
  {
  _val = analogRead(_pin);

  if(abs(_val - _lastVal) > 4){
    _midiInterface->sendControlChange(0, 0, 1);
    _lastVal = _val;
    }
  }
#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();

#include "stablePot.h"

stablePot stablePot0(MIDI, A0);

void setup() {                 
  MIDI.begin(1);
}
void loop(){
  stablePot0.midiSendCC();
  delay(25);
}

GUIX: -
sorry I should probably have mentioned...
I am using Hiduino so there isn't a MIDI interface. USB straight to the computer. But the MIDI.sendControlChange function usually sends it fine. (from a normal sketch)

GFVALVO: -
here is the error message: -

Arduino: 1.8.13 (Windows 10), Board: "Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"
C:\Users\Liam\Documents\Arduino\libraries\stablePot\stablePot.cpp: In member function 'void stablePot::midiSendCC()':
C:\Users\Liam\Documents\Arduino\libraries\stablePot\stablePot.cpp:18:5: error: 'MIDI' was not declared in this scope
     MIDI.sendControlChange(0, 0, 1);
     ^~~~
C:\Users\Liam\Documents\Arduino\libraries\stablePot\stablePot.cpp:18:5: note: suggested alternative: 'MISO'
     MIDI.sendControlChange(0, 0, 1);
     ^~~~
     MISO
exit status 1
Error compiling for board Arduino Mega or Mega 2560.


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

Thanks to both of you for your time

In your sketch, MIDI is an instance of the MidiInterface class from MIDI.h. It's created when you call MIDI_CREATE_DEFAULT_INSTANCE

You might be reinventing the wheel: Control Surface: Control-Change-Potentiometer.ino

Pieter

guix:
In your sketch, MIDI is an instance of the MidiInterface class from MIDI.h. It's created when you call MIDI_CREATE_DEFAULT_INSTANCE

It's actually an instance of a templated class. So just using "MidiInterface" probably won't work.

Try adding:

MIDI_CREATE_DEFAULT_INSTANCE();

under "private:" in the header file.

Also, at the top of the header file:

#include "Arduino.h"
#include <MIDI.h>

Okay I've tried adding MIDI_CREATE_DEFAULT_INSTANCE();

under "private:" in the header file.

so it looks like this: -

#ifndef stablePot_h
#define stablePot_h

class stablePot
{
  public:
    stablePot(int pin);
    void midiSendCC();
  private:
    MIDI_CREATE_DEFAULT_INSTANCE();
    int _pin;
    int _val;
    int _lastVal;
};
#endif

the other two files remain the same. and now im getting the following error...

Arduino: 1.8.13 (Windows 10), Board: "Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"

In file included from C:\Users\Liam\Documents\Arduino\libraries\MIDI_Library-5.0.2\src/MIDI.h:35:0,

                 from C:\Users\Liam\Documents\Arduino\libraryTest\_midiTest\_midiTest.ino:1:

C:\Users\Liam\Documents\Arduino\libraries\MIDI_Library-5.0.2\src/serialMIDI.h:113:46: error: 'Serial' is not a type

         MIDI_CREATE_INSTANCE(HardwareSerial, Serial,  MIDI);

 C:\Users\Liam\Documents\Arduino\libraries\MIDI_Library-5.0.2\src/serialMIDI.h:100:51: note: in definition of macro 'MIDI_CREATE_INSTANCE'

     MIDI_NAMESPACE::SerialMIDI<Type> serial##Name(SerialPort);\

                                                   ^~~~~~~~~~

C:\Users\Liam\Documents\Arduino\libraries\stablePot/stablePot.h:10:5: note: in expansion of macro 'MIDI_CREATE_DEFAULT_INSTANCE'

     MIDI_CREATE_DEFAULT_INSTANCE();

     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~

C:\Users\Liam\Documents\Arduino\libraries\MIDI_Library-5.0.2\src/serialMIDI.h:101:74: error: expected identifier before '(' token

     MIDI_NAMESPACE::MidiInterface<MIDI_NAMESPACE::SerialMIDI<Type>> Name((MIDI_NAMESPACE::SerialMIDI<Type>&)serial##Name);

                                                                          ^

C:\Users\Liam\Documents\Arduino\libraries\MIDI_Library-5.0.2\src/serialMIDI.h:113:9: note: in expansion of macro 'MIDI_CREATE_INSTANCE'

         MIDI_CREATE_INSTANCE(HardwareSerial, Serial,  MIDI);

         ^~~~~~~~~~~~~~~~~~~~

C:\Users\Liam\Documents\Arduino\libraries\stablePot/stablePot.h:10:5: note: in expansion of macro 'MIDI_CREATE_DEFAULT_INSTANCE'

     MIDI_CREATE_DEFAULT_INSTANCE();

     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~

C:\Users\Liam\Documents\Arduino\libraries\MIDI_Library-5.0.2\src/serialMIDI.h:101:109: error: expected ',' or '...' before 'serialMIDI'

     MIDI_NAMESPACE::MidiInterface<MIDI_NAMESPACE::SerialMIDI<Type>> Name((MIDI_NAMESPACE::SerialMIDI<Type>&)serial##Name);

                                                                                                             ^

C:\Users\Liam\Documents\Arduino\libraries\MIDI_Library-5.0.2\src/serialMIDI.h:113:9: note: in expansion of macro 'MIDI_CREATE_INSTANCE'

         MIDI_CREATE_INSTANCE(HardwareSerial, Serial,  MIDI);

         ^~~~~~~~~~~~~~~~~~~~

C:\Users\Liam\Documents\Arduino\libraries\stablePot/stablePot.h:10:5: note: in expansion of macro 'MIDI_CREATE_DEFAULT_INSTANCE'

     MIDI_CREATE_DEFAULT_INSTANCE();

     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~

exit status 1

Error compiling for board Arduino Mega or Mega 2560.



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

thanks again

gfvalvo:
Also, at the top of the header file:

#include "Arduino.h"

#include <MIDI.h>

I just added those but im still getting errors relating to this: -

C:\Users\Liam\Documents\Arduino\libraries\stablePot/stablePot.h:12:5: note: in expansion of macro 'MIDI_CREATE_DEFAULT_INSTANCE'
MIDI_CREATE_DEFAULT_INSTANCE();

hmmm.... :slightly_frowning_face:

OK,@guix's idea might work with a couple modifications:

stablePot(midi::MidiInterface<HardwareSerial>& midi, int pin);
midi::MidiInterface<HardwareSerial> * _midiInterface;

Another way:

#include "Arduino.h"
#include <MIDI.h>

class TestClass {
  public:
    TestClass() : MIDI(Serial) {
    }

    void aFunction() {
      MIDI.sendControlChange(0, 0, 1);
    }

  private:
    midi::MidiInterface<HardwareSerial> MIDI;
};

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.