My Fork of F401RE-USBHost. Add USBHostMIDI functions (originaled by Kaoru Shoji http://mbed.org/users/kshoji/code/USBHostMIDI/)

Dependencies:   FATFileSystem

Dependents:   F401RE-USBHostMIDI_RecieveExample

Fork of F401RE-USBHost by Norimasa Okamoto

USBHostMIDI/USBHostMIDI.h

Committer:
hsgw
Date:
2014-09-17
Revision:
23:9c1d09c6a2b6
Parent:
22:81d8c59d1070
Child:
24:5281ea9f6e68

File content as of revision 23:9c1d09c6a2b6:

/* USBHostMidi library
 * Originaled by k.shoji
 * porting by Takuya Urakawa
 */

/* mbed USBHost Library
 * Copyright (c) 2006-2013 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef USBHOSTMIDI_H
#define USBHOSTMIDI_H

#include "USBHostConf.h"

#if USBHOST_MIDI

#include "USBHost.h"

// STM nucleo boards do not support mbed-rtos
//#include "MtxCircBuffer.h"
#include "CircBuffer.h"

/** 
 * A class to communicate a USB MIDI device
 */
class USBHostMIDI : public IUSBEnumerator {
public:
    /**
    * Constructor
    */
    USBHostMIDI();

    /**
    * Check if a USB MIDI device is connected
    *
    * @returns true if a midi device is connected
    */
    bool connected();
    
    /**
     * Try to connect a midi device
     *
     * @return true if connection was successful
     */
    bool connect();
    
    /**
     * Attach a callback called when note on is received
     *
     * @param ptr function pointer
     */
    inline void attachNoteOn(void (*fn)(unsigned char, unsigned char, unsigned char)) {
        if (fn != NULL) {
            noteOn = fn;
        }
    }

    /**
     * Attach a callback called when note off is received
     *
     * @param ptr function pointer
     */
    inline void attachNoteOff(void (*fn)(unsigned char, unsigned char, unsigned char)) {
        if (fn != NULL) {
            noteOff = fn;
        }
    }

    /**
     * Attach a callback called when control change is received
     *
     * @param ptr function pointer
     */
    inline void attachControlChange(void (*fn)(unsigned char, unsigned char, unsigned char)) {
        if (fn != NULL) {
            controlChange = fn;
        }
    }

    /**
     * Attach a callback called when program change is received
     *
     * @param ptr function pointer
     */
    inline void attachProgramChange(void (*fn)(unsigned char, unsigned char)) {
        if (fn != NULL) {
            programChange = fn;
        }
    }

    /**
     * Attach a callback called when pitch bend is received
     *
     * @param ptr function pointer
     */
    inline void attachPitchBend(void (*fn)(unsigned char, unsigned int)) {
        if (fn != NULL) {
            pitchBend = fn;
        }
    }
    
    int sendNoteOn(unsigned char channel, unsigned char note, unsigned char velocity);
    int sendNoteOff(unsigned char channel, unsigned char note, unsigned char velocity);
    int sendControlChange(unsigned char channel, unsigned char key, unsigned char value);
    int sendProgramChange(unsigned char channel, unsigned char program);
    int sendPitchBend(unsigned char channel, unsigned int value);

protected:
    //From IUSBEnumerator
    virtual void setVidPid(uint16_t vid, uint16_t pid);
    virtual bool parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol); //Must return true if the interface should be parsed
    virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used

private:
    USBHost * host;
    USBDeviceConnected * dev;
    USBEndpoint * bulk_in;
    USBEndpoint * bulk_out;
    uint32_t size_bulk_in;
    uint32_t size_bulk_out;

    bool dev_connected;

    void init();

    CircBuffer<uint8_t, 128> circ_buf;

    uint8_t buf[128];

    void rxHandler();

    void (*noteOn)(unsigned char, unsigned char, unsigned char);
    void (*noteOff)(unsigned char, unsigned char, unsigned char);
    void (*controlChange)(unsigned char, unsigned char, unsigned char);
    void (*programChange)(unsigned char, unsigned char);
    void (*pitchBend)(unsigned char, unsigned int);

    int midi_intf;
    bool midi_device_found;

};

#endif

#endif