Contents page

Rules for Tools/sendserial.c


 /*      Serial.C

        Routines for reading and writing MIDI System Exclusive packets
        through MIDI in and out Tools.

        These use two new additions to the "bars.h" file - the
        tooltype TOOL_MIDI and a new field in the ToolMaster
        structure - "readsysex".

        © 1990  Blue Ribbon Bakery */

/*      To send a packet, place it in a StringEvent structure and
        send that to any output tool that has TOOL_MIDI set.  The
        Tool will deallocate the string. */

void sendsysex(buff,size)

char *buff; short size;

{
    struct StringEvent *event;
    struct Tool *tool = functions->midiouttool;
    short i;
    struct String *string;
    char buffer[30];
    if (tool && (tool->tooltype & TOOL_MIDI)) {
        event = (struct StringEvent *) (*functions->allocevent)();
        string = (struct String *) (*functions->myalloc)(size + 3,MEMF_CLEAR);
        if (event && string) {
            event->string = string;
            string->length = size + 2;
            memcpy(string->string,buff,size);
            event->type = EVENT_SYSX;
            event->status = MIDI_SYSX;
            event->time = functions->timenow;
            event->tool = tool;
            WaitTOF();  /* Problems with MTC1. */
            (*functions->qevent)(event);
        }
    } }

/*      To receive a system exclusive packet, provide an interrupt
        routine for the MIDI In Tool of your choice.  You place this
        routine in the readsysex field of the ToolMaster structure.
        Your routine will be called each time a new byte comes in,
        the byte passed as the one parameter.

        Remember to remove your routine from the ToolMaster structure
        when done.

        This example routine simply grabs the first available MIDI
        in Tool.  Obviously, you'll probably wish to be more
        selective. */

long signal = 0; long task = 0; char *buffer; long maxlength; long index;

void readbyte(data)

char data;

{
    if (index >= maxlength) return;
    buffer[index++] = data;
    if ((data == 0xF7) || (index == maxlength)) {
        Signal(task,signal);
    } }

receivesysex(packet,size)

char packet[]; long size;

{
    struct ToolMaster *master = functions->toolmasterlist;
    long routine;
    short signalnum;
    for (;master;master = master->next) {
        if ((master->tooltype & TOOL_MIDI) && (master->tooltype & TOOL_INPUT)) 
            break;
    }
    if (master) {
        maxlength = size;
        buffer = packet;
        index = 0;
        signalnum = AllocSignal(-1);
        signal = signalnum << 1;
        task = FindTask(0);
        routine = master->readsysex;
        master->readsysex = readbyte;
        Wait(signal);
        master->readsysex = routine;
        return(index);
    }
    return(0); }