Subversion Repositories svn.mios32

Rev

Rev 1724 | Rev 1734 | 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 1726 2013-03-28 21:20:34Z 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
 
909 tk 27
    addAndMakeVisible(inputLine = new TextEditor(String::empty));
28
    inputLine->setMultiLine(false);
29
    inputLine->setReturnKeyStartsNewLine(false);
30
    inputLine->setReadOnly(false);
31
    inputLine->setScrollbarsShown(false);
32
    inputLine->setCaretVisible(true);
33
    inputLine->setPopupMenuEnabled(true);
34
    inputLine->setTextToShowWhenEmpty(T("(send a command to MIOS32 application)"), Colours::grey);
35
    inputLine->addListener(this);
36
 
877 tk 37
    setSize(400, 200);
38
}
39
 
40
MiosTerminal::~MiosTerminal()
41
{
42
}
43
 
44
//==============================================================================
45
void MiosTerminal::paint (Graphics& g)
46
{
47
    g.fillAll(Colours::white);
48
}
49
 
50
void MiosTerminal::resized()
51
{
909 tk 52
    terminalLogBox->setBounds(4, 4, getWidth()-8, getHeight()-8-24-4);
53
    inputLine->setBounds(4, getHeight()-4-24, getWidth()-8, 24);
877 tk 54
}
878 tk 55
 
56
//==============================================================================
909 tk 57
void MiosTerminal::textEditorTextChanged(TextEditor &editor)
58
{
59
}
60
 
61
void MiosTerminal::textEditorReturnKeyPressed(TextEditor &editor)
62
{
63
    if( &editor == inputLine ) {
64
        String command = inputLine->getText();
65
 
1300 tk 66
        Array<uint8> dataArray = SysexHelper::createMios32DebugMessage(miosStudio->uploadHandler->getDeviceId());
909 tk 67
        dataArray.add(0x00); // input string
68
        for(int i=0; i<command.length(); ++i)
69
            dataArray.add(command[i] & 0x7f);
70
        dataArray.add('\n');
71
        dataArray.add(0xf7);
72
        MidiMessage message = SysexHelper::createMidiMessage(dataArray);
73
        miosStudio->sendMidiMessage(message);
74
 
75
        inputLine->setText(String::empty);
910 tk 76
 
913 tk 77
        if( !gotFirstMessage )
78
            terminalLogBox->clear();
79
        gotFirstMessage = 1;
80
 
910 tk 81
#if 0
82
        String timeStampStr = T("input");
83
#else
84
        double timeStamp = Time::getMillisecondCounter() / 1000.0; // Note: it's intented that this is the system up time
85
        String timeStampStr = (timeStamp > 0)
86
            ? String::formatted(T("%8.3f"), timeStamp)
87
            : T("now");
88
#endif
920 tk 89
        terminalLogBox->addEntry(Colours::grey, "[" + timeStampStr + "] " + command);
909 tk 90
    }
91
}
92
 
93
void MiosTerminal::textEditorEscapeKeyPressed(TextEditor &editor)
94
{
95
    if( &editor == inputLine ) {
96
        inputLine->setText(String::empty);
97
    }
98
}
99
 
100
void MiosTerminal::textEditorFocusLost(TextEditor &editor)
101
{
102
}
103
 
104
 
105
//==============================================================================
878 tk 106
void MiosTerminal::handleIncomingMidiMessage(const MidiMessage& message, uint8 runningStatus)
107
{
1724 tk 108
    uint8 *data = (uint8 *)message.getRawData();
886 tk 109
    uint32 size = message.getRawDataSize();
878 tk 110
    int messageOffset = 0;
111
 
1542 tk 112
    bool messageReceived = false;
113
    if( runningStatus == 0xf0 &&
114
        SysexHelper::isValidMios32DebugMessage(data, size, -1) &&
1726 tk 115
        (data[7] == 0x40 || data[7] == 0x00) ) { // allow 0x40 (received) and 0x00 (sent) terminal message
116
                                                 // 0x00 is allowed for the "feedback test" which is described in the MIDI troubleshooting guide
886 tk 117
            messageOffset = 8;
1542 tk 118
            messageReceived = true;
119
    }
878 tk 120
 
1542 tk 121
    if( messageReceived ) {
878 tk 122
        String str = "";
123
 
124
        for(int i=messageOffset; i<size; ++i) {
1542 tk 125
            if( data[i] < 0x80 ) {
878 tk 126
                if( data[i] != '\n' || size < (i+1) )
127
                    str += String::formatted(T("%c"), data[i] & 0x7f);
128
            }
129
        }
130
 
131
        if( !gotFirstMessage )
894 tk 132
            terminalLogBox->clear();
913 tk 133
        gotFirstMessage = 1;
878 tk 134
 
912 tk 135
        double timeStamp = message.getTimeStamp() ? message.getTimeStamp() : ((double)Time::getMillisecondCounter() / 1000.0);
878 tk 136
        String timeStampStr = (timeStamp > 0)
137
            ? String::formatted(T("%8.3f"), timeStamp)
138
            : T("now");
139
 
920 tk 140
        terminalLogBox->addEntry(Colours::black, "[" + timeStampStr + "] " + str);
878 tk 141
    }
142
}