Maken van een h en cpp file

algemene C code
Berichten: 10
Geregistreerd: 03 Sep 2016, 17:15

Maken van een h en cpp file

Berichtdoor vlbrgt » 14 Jul 2017, 11:32

Goedendag.

Om mijn probleem beter te kunnen uitleggen heb ik een verkorte versie van mijn programma gemaakt.
Het betreft een MIDI router/merger. Code wordt ingevoerd met Visual Micro en gebruikt voor een Arduino DUE.

Volgende ino bestand gemaakt :

cpp code
//TeensyRouterMerger.ino
#include <MIDI.h>
#include <TimerObject.h>
#include "MyDefs.h"

MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI_A)

void setup()
{
MIDI_A.setHandleNoteOff(handleNoteOff_A);
MIDI_A.setHandleNoteOn(handleNoteOn_A);

MIDI_A.begin(MIDI_CHANNEL_OMNI);
}

void loop()
{
MIDI_A.read();
}
//******************************************************************
void handleNoteOff_A(byte channel, byte pitch, byte velocity)
{
MIDI_A.sendNoteOff(pitch, velocity, SEND_CHANNEL_EXTRAPART_1);
}

void handleNoteOn_A(byte channel, byte pitch, byte velocity)
{
MIDI_A.sendNoteOff(pitch, velocity, SEND_CHANNEL_EXTRAPART_1);
}
//******************************************************************

Het bestand MyDefs.h bevat volgende code :
cpp code
/* Prototype functions */
/* MIDI_A_CallBack functions */

void handleNoteOff_A(byte channel, byte pitch, byte velocity);
void handleNoteOn_A(byte channel, byte pitch, byte velocity);

/* Declaraties */

const byte SEND_CHANNEL_EXTRAPART_1 = 5;
const byte SEND_CHANNEL_EXTRAPART_2 = 6;
const byte SEND_CHANNEL_EXTRAPART_3 = 7;
const byte SEND_CHANNEL_EXTRAPART_4 = 8;

Dit project wordt correct gecompileerd in Visual Micro.

---------------------------------------------------------------------------------------------
Nu wil ik echter het programma opdelen in verschillende parts .
Het gedeelte met de functies
void handleNoteOff_A(byte channel, byte pitch, byte velocity)
en void handleNoteOn_A(byte channel, byte pitch, byte velocity)
wil ik in een h en cpp bestand plaatsen.

De ino code is nu :
cpp code
#include "MA_Actions.h"
#include <MIDI.h>
#include <TimerObject.h>
#include "MyDefs.h"

MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI_A)

void setup()
{
MIDI_A.setHandleNoteOff(handleNoteOff_A);
MIDI_A.setHandleNoteOn(handleNoteOn_A);

MIDI_A.begin(MIDI_CHANNEL_OMNI);
}

void loop()
{
MIDI_A.read();
}

De MyDefs.h code is nu :
cpp code
/* Prototype functions */
/* MIDI_A_CallBack functions */

/* Declaraties */

const byte SEND_CHANNEL_EXTRAPART_1 = 5;
const byte SEND_CHANNEL_EXTRAPART_2 = 6;
const byte SEND_CHANNEL_EXTRAPART_3 = 7;
const byte SEND_CHANNEL_EXTRAPART_4 = 8;

Bijgekomen zijn MA_Actions.h met code :
cpp code
// MA_Actions.h

#ifndef _MA_ACTIONS_h
#define _MA_ACTIONS_h

#if defined(ARDUINO) && ARDUINO >= 100
#include "arduino.h"
#else
#include "WProgram.h"
#endif


#endif

void handleNoteOff_A(byte channel, byte pitch, byte velocity);
void handleNoteOn_A(byte channel, byte pitch, byte velocity);

en MA_Actions.cpp met code :
cpp code
#include "MA_Actions.h"

//******************************************************************
void handleNoteOff_A(byte channel, byte pitch, byte velocity)
{
MIDI_A.sendNoteOff(pitch, velocity, SEND_CHANNEL_EXTRAPART_1);
}

void handleNoteOn_A(byte channel, byte pitch, byte velocity)
{
MIDI_A.sendNoteOff(pitch, velocity, SEND_CHANNEL_EXTRAPART_1);
}
//******************************************************************

Deze codes worden niet meer correct gecompileerd in Visual Micro.
Geeft volgende error codes :

Compiling debug version of 'TeensyRouterMerger' for 'Arduino Due (Programming Port)'
MA_Actions.cpp:In function 'void handleNoteOff_A(byte, byte, byte)
MA_Actions.cpp:10:2: error: 'MIDI_A' was not declared in this scope
:MIDI_A.sendNoteOff(pitch, velocity, SEND_CHANNEL_EXTRAPART_1)
MA_Actions.cpp:10:38: error: 'SEND_CHANNEL_EXTRAPART_1' was not declared in this scope
:MIDI_A.sendNoteOff(pitch, velocity, SEND_CHANNEL_EXTRAPART_1)
MA_Actions.cpp:In function 'void handleNoteOn_A(byte, byte, byte)
MA_Actions.cpp:15:2: error: 'MIDI_A' was not declared in this scope
:MIDI_A.sendNoteOff(pitch, velocity, SEND_CHANNEL_EXTRAPART_1)
MA_Actions.cpp:15:38: error: 'SEND_CHANNEL_EXTRAPART_1' was not declared in this scope
:MIDI_A.sendNoteOff(pitch, velocity, SEND_CHANNEL_EXTRAPART_1)

maw MIDI_A not declared in this scope .

Wat doe ik fout ?
Wat zie ik over het hoofd ?
Toevoegen van #include <MIDI.h> in MA_Actions.h geeft geen verbetering.

Bij voorbaat dank
Mvg
Etienne

Advertisement

Gebruikers-avatar
Berichten: 5043
Geregistreerd: 13 Mei 2013, 20:57
Woonplaats: Heemskerk

Re: Maken van een h en cpp file

Berichtdoor nicoverduin » 14 Jul 2017, 12:19

Je maakt midi_A aan in de sketch. In de cpp moet je dan aangeven dat deze al bestaat als extern. Ik weet niet hoe die als los wordt gedefinieerd. Dat zal wel in midi. H staan. Dan krijg je boven in iets van
cpp code
extern MIDI MIDI_A;
Docent HBO Technische Informatica, Embedded ontwikkelaar & elektronicus
http://www.verelec.nl

Terug naar C code

Wie is er online?

Gebruikers in dit forum: Geen geregistreerde gebruikers en 9 gasten