midi device enerates tones according freedom rotation

Dependencies:   MMA8451Q USBDevice mbed TSI

main.cpp

Committer:
stefanimrich
Date:
2013-08-19
Revision:
3:e0684e01fca1
Parent:
2:c79be10b5ff3
Child:
4:9c81faf1b372

File content as of revision 3:e0684e01fca1:

#include "mbed.h"
#include "MMA8451Q.h"
#include "USBMIDI.h"

#define MMA8451_I2C_ADDRESS (0x1d<<1)

#define C1  0 
#define D1  0.125
#define E1  0.250
#define F1  0.375
#define G1  0.5
#define A1  0.625
#define H1  0.750
#define C2  0.875
#define D2  1
#define E2  1.125
#define F2  1.250
#define G2  1.375
#define A2  1.5
#define H2  1.625
#define C3  1.750

#define NC1 48
#define ND1 50
#define NE1 52
#define NF1 53
#define NG1 55
#define NA1 57
#define NH1 59
#define NC2 60
#define ND2 62
#define NE2 64
#define NF2 65
#define NG2 67
#define NA2 69
#define NH2 71
#define NC3 72

Serial pc(USBTX,USBRX);
USBMIDI midi;

 
void show_message(MIDIMessage msg) {
    switch (msg.type()) {
        case MIDIMessage::NoteOnType:
            printf("NoteOn key:%d, velocity: %d, channel: %d\n", msg.key(), msg.velocity(), msg.channel());
            break;
        case MIDIMessage::NoteOffType:
            printf("NoteOff key:%d, velocity: %d, channel: %d\n", msg.key(), msg.velocity(), msg.channel());
            break;
        case MIDIMessage::ControlChangeType:    
            printf("ControlChange controller: %d, data: %d\n", msg.controller(), msg.value());
            break;
        case MIDIMessage::PitchWheelType:
            printf("PitchWheel channel: %d, pitch: %d\n", msg.channel(), msg.pitch());
            break;
        default:
            printf("Another message\n");
    }    
}
 

 
int main() { 

    int note=48;
    float notechng;
    
    MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
             
    midi.attach(show_message);         // call back for messages received 
       
    while (1) { 
    
        notechng = 1 - acc.getAccX();
        pc.printf("ZZ: %f, XX: %f, YY: %f, Note: %d \n",(1 - acc.getAccZ()), notechng,  (1 - acc.getAccY()), note); 
        
        midi.write(MIDIMessage::NoteOff(note));
        
        if(notechng > C1) note= NC1;
        if(notechng > D1) note= ND1;
        if(notechng > E1) note= NE1;
        if(notechng > F1) note= NF1;
        if(notechng > G1) note= NG1;
        if(notechng > A1) note= NA1;
        if(notechng > H1) note= NH1;
        if(notechng > C2) note= NC2;
        if(notechng > D2) note= ND2;
        if(notechng > E2) note= NE2;
        if(notechng > F2) note= NF2;
        if(notechng > G2) note= NG2;
        if(notechng > A2) note= NA2;
        if(notechng > H2) note= NH2;  
        if(notechng > C3) note= NC3;          

        midi.write(MIDIMessage::NoteOn(note));
        wait(0.1);
      
    }
}