Subversion Repositories svn.mios32

Rev

Rev 1726 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
877 tk 1
/* -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- */
2
// $Id: MiosTerminal.cpp 1734 2013-04-05 21:19:53Z tk $
3
/*
4
 * MIOS Terminal Component
5
 *
6
 * ==========================================================================
7
 *
8
 *  Copyright (C) 2010 Thorsten Klose (tk@midibox.org)
9
 *  Licensed for personal non-commercial use only.
10
 *  All other rights reserved.
11
 *
12
 * ==========================================================================
13
 */
14
 
15
#include "MiosTerminal.h"
878 tk 16
#include "MiosStudio.h"
877 tk 17
 
18
 
19
//==============================================================================
878 tk 20
MiosTerminal::MiosTerminal(MiosStudio *_miosStudio)
21
    : miosStudio(_miosStudio)
22
    , gotFirstMessage(0)
877 tk 23
{
894 tk 24
    addAndMakeVisible(terminalLogBox = new LogBox(T("MIOS Terminal")));
920 tk 25
    terminalLogBox->addEntry(Colours::grey, T("MIOS Terminal ready."));
877 tk 26
 
1734 tk 27
    addAndMakeVisible(inputLine = new CommandLineEditor());
909 tk 28
    inputLine->setTextToShowWhenEmpty(T("(send a command to MIOS32 application)"), Colours::grey);
29
    inputLine->addListener(this);
30
 
877 tk 31
    setSize(400, 200);
32
}
33
 
34
MiosTerminal::~MiosTerminal()
35
{
36
}
37
 
38
//==============================================================================
39
void MiosTerminal::paint (Graphics& g)
40
{
41
    g.fillAll(Colours::white);
42
}
43
 
44
void MiosTerminal::resized()
45
{
909 tk 46
    terminalLogBox->setBounds(4, 4, getWidth()-8, getHeight()-8-24-4);
47
    inputLine->setBounds(4, getHeight()-4-24, getWidth()-8, 24);
877 tk 48
}
878 tk 49
 
50
//==============================================================================
909 tk 51
void MiosTerminal::textEditorTextChanged(TextEditor &editor)
52
{
53
}
54
 
55
void MiosTerminal::textEditorReturnKeyPressed(TextEditor &editor)
56
{
57
    if( &editor == inputLine ) {
58
        String command = inputLine->getText();
59
 
1300 tk 60
        Array<uint8> dataArray = SysexHelper::createMios32DebugMessage(miosStudio->uploadHandler->getDeviceId());
909 tk 61
        dataArray.add(0x00); // input string
62
        for(int i=0; i<command.length(); ++i)
63
            dataArray.add(command[i] & 0x7f);
64
        dataArray.add('\n');
65
        dataArray.add(0xf7);
66
        MidiMessage message = SysexHelper::createMidiMessage(dataArray);
67
        miosStudio->sendMidiMessage(message);
68
 
69
        inputLine->setText(String::empty);
910 tk 70
 
913 tk 71
        if( !gotFirstMessage )
72
            terminalLogBox->clear();
73
        gotFirstMessage = 1;
74
 
910 tk 75
#if 0
76
        String timeStampStr = T("input");
77
#else
78
        double timeStamp = Time::getMillisecondCounter() / 1000.0; // Note: it's intented that this is the system up time
79
        String timeStampStr = (timeStamp > 0)
80
            ? String::formatted(T("%8.3f"), timeStamp)
81
            : T("now");
82
#endif
920 tk 83
        terminalLogBox->addEntry(Colours::grey, "[" + timeStampStr + "] " + command);
909 tk 84
    }
85
}
86
 
87
void MiosTerminal::textEditorEscapeKeyPressed(TextEditor &editor)
88
{
1734 tk 89
    editor.setText(String::empty);
909 tk 90
}
91
 
92
void MiosTerminal::textEditorFocusLost(TextEditor &editor)
93
{
94
}
95
 
96
 
97
//==============================================================================
878 tk 98
void MiosTerminal::handleIncomingMidiMessage(const MidiMessage& message, uint8 runningStatus)
99
{
1724 tk 100
    uint8 *data = (uint8 *)message.getRawData();
886 tk 101
    uint32 size = message.getRawDataSize();
878 tk 102
    int messageOffset = 0;
103
 
1542 tk 104
    bool messageReceived = false;
105
    if( runningStatus == 0xf0 &&
106
        SysexHelper::isValidMios32DebugMessage(data, size, -1) &&
1726 tk 107
        (data[7] == 0x40 || data[7] == 0x00) ) { // allow 0x40 (received) and 0x00 (sent) terminal message
108
                                                 // 0x00 is allowed for the "feedback test" which is described in the MIDI troubleshooting guide
886 tk 109
            messageOffset = 8;
1542 tk 110
            messageReceived = true;
111
    }
878 tk 112
 
1542 tk 113
    if( messageReceived ) {
878 tk 114
        String str = "";
115
 
116
        for(int i=messageOffset; i<size; ++i) {
1542 tk 117
            if( data[i] < 0x80 ) {
878 tk 118
                if( data[i] != '\n' || size < (i+1) )
119
                    str += String::formatted(T("%c"), data[i] & 0x7f);
120
            }
121
        }
122
 
123
        if( !gotFirstMessage )
894 tk 124
            terminalLogBox->clear();
913 tk 125
        gotFirstMessage = 1;
878 tk 126
 
912 tk 127
        double timeStamp = message.getTimeStamp() ? message.getTimeStamp() : ((double)Time::getMillisecondCounter() / 1000.0);
878 tk 128
        String timeStampStr = (timeStamp > 0)
129
            ? String::formatted(T("%8.3f"), timeStamp)
130
            : T("now");
131
 
920 tk 132
        terminalLogBox->addEntry(Colours::black, "[" + timeStampStr + "] " + str);
878 tk 133
    }
134
}