Go Down

Topic: Unions+Functions+serialWrite = Error (Read 1 time) previous topic - next topic

adnalok

Mar 21, 2013, 10:27 pm Last Edit: Mar 21, 2013, 10:29 pm by adnalok Reason: 1
Hello Everybody,

I'm working on a project, wich is kind of a midi controller, and i've ran into an error wich i can't solve, somebody please help me out :(

This is part of the code, i'm currently starting using pointers for giving variables to functions, but this gives me an error:

Code: [Select]

#define _C4 0x30
#define _Cs4 0x31
#define _D4 0x32
#define _Ds4 0x33
#define _E4 0x34
#define _F4 0x35
#define _Fs4 0x36
#define _G4 0x37
#define _Gs4 0x38
#define _A4 0x39
#define _As4 0x3A
#define _B4 0x3B
#define _C5 0x3C
#define NoteOn 0x7B
#define MaxVel 0xFF

typedef union {
 struct {
   uint8_t Command;
   uint8_t Channel;
   uint8_t Data1;
   uint8_t Data2;
 } Msg;
 uint8_t Raw[4];
} MidiMsg;

void setup() {
Serial.begin(31250);
}

void SendMsgSerial (MidiMsg* Message){
Serial.write(*Message.Raw, sizeof(*Message));
}

void SendNoteOn (uint8_t Note, uint8_t Velocity, uint8_t Channel){
MidiMsg Message;
Message.Msg.Command=NoteOn;
Message.Msg.Channel=Channel;
Message.Msg.Data1=Note;
Message.Msg.Data2=Velocity;
SendMsgSerial(&Message);
}

void SendScale (int ValveState){
uint8_t Vel=MaxVel;
uint8_t Ch=0x00;
switch (ValveState) {
case 0: SendNoteOn(_C4,Vel,Ch); break;
case 1: SendNoteOn(_D4,Vel,Ch); break;
case 2: SendNoteOn(_F4,Vel,Ch); break;
case 3: SendNoteOn(_E4,Vel,Ch); break;
case 4: SendNoteOn(_A4,Vel,Ch); break;
case 5: SendNoteOn(_B4,Vel,Ch); break;
case 6: SendNoteOn(_G4,Vel,Ch); break;
case 7: SendNoteOn(_C5,Vel,Ch); break;
}
}
void loop(){
SendScale(0);
}


the error is : "sketch_mar21a:36: error: request for member 'Raw' in 'Message', which is of non-class type 'MidiMsg*'"


SendMsgSerial is a must have function, and I would like to give the data to it with a pointer to a "MidiMsg" union, wich consists of 4 bytes (later it will be only 3) , because 3 serialWrite commands / midi message is a bit too much in my case, and i would like to send the 3 byte command in one "serialWrite" command...


Anybody could help me out?
Thanks a lot

PaulS

What happens if you define the struct outside of the union?

billroy

Also, what happens if you change:
Code: [Select]

Serial.write(*Message.Raw, sizeof(*Message));

to:
Code: [Select]

Serial.write(Message->Raw, sizeof(*Message));


-br

adnalok


Also, what happens if you change:
Code: [Select]

Serial.write(*Message.Raw, sizeof(*Message));

to:
Code: [Select]

Serial.write(Message->Raw, sizeof(*Message));


-br



I've done both recomenndations

Code: [Select]
#define _C4 0x30
#define _Cs4 0x31
#define _D4 0x32
#define _Ds4 0x33
#define _E4 0x34
#define _F4 0x35
#define _Fs4 0x36
#define _G4 0x37
#define _Gs4 0x38
#define _A4 0x39
#define _As4 0x3A
#define _B4 0x3B
#define _C5 0x3C
#define NoteOn 0x7B
#define MaxVel 0xFF

struct Msg{
    uint8_t Command;
    uint8_t Channel;
    uint8_t Data1;
    uint8_t Data2;
  };
 
typedef union {
  Msg Msg1;
  uint8_t Raw[4];
} MidiMsg;

void setup() {
Serial.begin(31250);
}

void SendMidiMsg (MidiMsg* Message){
Serial.write(Message->Raw, sizeof(*Message));
}

void SendNoteOn (uint8_t Note, uint8_t Velocity, uint8_t Channel){
MidiMsg Message;
Message.Msg1.Command=NoteOn;
Message.Msg1.Channel=Channel;
Message.Msg1.Data1=Note;
Message.Msg1.Data2=Velocity;
SendMidiMsg(&Message);
}

void SendScale (int ValveState){
uint8_t Vel=MaxVel;
uint8_t Ch=0x00;
switch (ValveState) {
case 0: SendNoteOn(_C4,Vel,Ch); break;
case 1: SendNoteOn(_D4,Vel,Ch); break;
case 2: SendNoteOn(_F4,Vel,Ch); break;
case 3: SendNoteOn(_E4,Vel,Ch); break;
case 4: SendNoteOn(_A4,Vel,Ch); break;
case 5: SendNoteOn(_B4,Vel,Ch); break;
case 6: SendNoteOn(_G4,Vel,Ch); break;
case 7: SendNoteOn(_C5,Vel,Ch); break;
}
}
void loop(){
SendScale(0);
}


Now i only have 3 errors

sketch_mar21a:19: error: variable or field 'SendMidiMsg' declared void
sketch_mar21a:19: error: 'MidiMsg' was not declared in this scope
sketch_mar21a:19: error: 'Message' was not declared in this scope

I think the original problem is solved, but im missing out something

lloyddean

Code: [Select]

#define ENTRIES(ARRAY) (sizeof(ARRAY) / sizeof(ARRAY[0]))

#define _C4             0x30
#define _Cs4            0x31
#define _D4             0x32
#define _Ds4            0x33
#define _E4             0x34
#define _F4             0x35
#define _Fs4            0x36
#define _G4             0x37
#define _Gs4            0x38
#define _A4             0x39
#define _As4            0x3A
#define _B4             0x3B
#define _C5             0x3C
#define NoteOn          0x7B
#define MaxVel          0xFF

struct MidiMsg
{
    union
    {
        struct { uint8_t    Command, Channel, Data1, Data2; };
                 uint8_t    Raw[4];
    };
} ;

void setup()
{
    Serial.begin(31250);
}

void SendMsgSerial(struct MidiMsg* Message)
{
    Serial.write(Message->Raw,  ENTRIES(Message->Raw));
}

void SendNoteOn(uint8_t Note, uint8_t Velocity, uint8_t Channel)
{
    MidiMsg     Message;
    Message.Command = NoteOn;
    Message.Channel = Channel;
    Message.Data1   = Note;
    Message.Data2   = Velocity;
    SendMsgSerial(&Message);
}

void SendScale(int ValveState)
{
    uint8_t Vel = MaxVel;
    uint8_t Ch  = 0x00;
    switch ( ValveState )
    {
        case 0: SendNoteOn(_C4, Vel, Ch);   break;
        case 1: SendNoteOn(_D4, Vel, Ch);   break;
        case 2: SendNoteOn(_F4, Vel, Ch);   break;
        case 3: SendNoteOn(_E4, Vel, Ch);   break;
        case 4: SendNoteOn(_A4, Vel, Ch);   break;
        case 5: SendNoteOn(_B4, Vel, Ch);   break;
        case 6: SendNoteOn(_G4, Vel, Ch);   break;
        case 7: SendNoteOn(_C5, Vel, Ch);   break;
    }
}

void loop()
{
    SendScale(0);
}


Go Up