Subversion Repositories svn.mios

Rev

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

Rev Author Line No. Line
628 adamjking 1
/*
2
 * @(#)LCDMessage.java  beta8   2006/04/23
3
 *
4
 * Copyright (C) 2008    Adam King (adamjking@optusnet.com.au)
5
 *
6
 * This application is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 2 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This application is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this application; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 */
20
 
21
package org.midibox.mios;
22
 
23
import javax.sound.midi.InvalidMidiDataException;
24
import javax.sound.midi.Receiver;
25
import javax.sound.midi.SysexMessage;
26
 
27
public class LCDMessage extends MIOSSysexSendReceive {
28
 
29
    public final static Object LCD_WIDTH = new Object();
30
 
31
    public final static Object LCD_HEIGHT = new Object();
32
 
33
    public final static Object MESSAGE_TEXT = new Object();
34
 
35
    public final static Object MODE = new Object();
36
 
37
    public final static Object POS_X = new Object();
38
 
39
    public final static Object POS_Y = new Object();
40
 
41
    public final static Object LOOP = new Object();
42
 
43
    public final static int CLEAR_SCREEN_MODE = 0;
44
 
45
    public final static int COORDS_MODE = 1;
46
 
47
    public final static int CURRENT_POS_MODE = 2;
48
 
49
    public final static int STOP_MODE = 3;
50
 
51
    private int lcdWidth;
52
 
53
    private int lcdHeight;
54
 
55
    private String messageText;
56
 
57
    private int mode;
58
 
59
    private int posX;
60
 
61
    private int posY;
62
 
63
    private boolean loop;
64
 
65
    public LCDMessage(Receiver receiver) {
66
 
67
        super(receiver);
68
 
69
        this.lcdWidth = 16;
70
 
71
        this.lcdHeight = 2;
72
 
73
        this.messageText = "";
74
 
75
        this.mode = CLEAR_SCREEN_MODE;
76
 
77
        this.posX = 0;
78
 
79
        this.posY = 0;
80
 
81
        this.loop = false;
82
    }
83
 
84
    public int getLcdHeight() {
85
        return lcdHeight;
86
    }
87
 
88
    public void setLcdHeight(int lcdHeight) {
89
        this.lcdHeight = lcdHeight;
727 adamjking 90
 
628 adamjking 91
        setChanged();
92
        notifyObservers(LCD_HEIGHT);
93
        clearChanged();
94
    }
95
 
96
    public int getLcdWidth() {
97
        return lcdWidth;
98
    }
99
 
100
    public void setLcdWidth(int lcdWidth) {
101
        this.lcdWidth = lcdWidth;
102
 
103
        setChanged();
104
        notifyObservers(LCD_WIDTH);
105
        clearChanged();
106
    }
107
 
108
    public String getMessageText() {
109
        return messageText;
110
    }
111
 
112
    public void setMessageText(String messageText) {
113
        this.messageText = messageText;
114
 
115
        setChanged();
116
        notifyObservers(MESSAGE_TEXT);
117
        clearChanged();
118
    }
119
 
120
    public boolean isLoop() {
121
        return loop;
122
    }
123
 
124
    public void setLoop(boolean loop) {
125
        this.loop = loop;
126
 
127
        setChanged();
128
        notifyObservers(LOOP);
129
        clearChanged();
130
    }
131
 
132
    public int getMode() {
133
        return mode;
134
    }
135
 
136
    public void setMode(int mode) {
137
        this.mode = mode;
138
 
139
        setChanged();
140
        notifyObservers(MODE);
141
        clearChanged();
142
    }
143
 
144
    public int getPosX() {
145
        return posX;
146
    }
147
 
148
    public void setPosX(int posX) {
149
        this.posX = posX;
150
 
151
        setChanged();
152
        notifyObservers(POS_X);
153
        clearChanged();
154
    }
155
 
156
    public int getPosY() {
157
        return posY;
158
    }
159
 
160
    public void setPosY(int posY) {
161
        this.posY = posY;
162
 
163
        setChanged();
164
        notifyObservers(POS_Y);
165
        clearChanged();
166
    }
167
 
168
    public void createMessage() {
169
        sendMessage(messageText, mode);
170
    }
171
 
172
    public void cancelMessage() {
173
        sendMessage("a", STOP_MODE);
174
        cancelled = true;
175
    }
176
 
177
    private void sendMessage(String message, int mode) {
178
 
179
        cancelled = false;
180
 
181
        done = false;
182
 
183
        setChanged();
184
        notifyObservers(WORKER);
185
        clearChanged();
186
 
187
        int cursor = mode;
188
 
189
        String[] messageLines = message.split("\n");
190
        byte[][] messageLinesBytes = new byte[messageLines.length][];
191
 
192
        for (int l = 0; l < messageLines.length; l++) {
193
            messageLinesBytes[l] = messageLines[l].getBytes();
194
 
195
            if (cursor == COORDS_MODE || l > 0) {
196
                messageLinesBytes[l] = addPos(messageLinesBytes[l],
197
                        (cursor == COORDS_MODE) ? posX : 0,
198
                        ((cursor == COORDS_MODE) ? posY : 0) + l);
199
            }
200
        }
201
 
202
        byte[][] lcdMessageCommand = new byte[messageLines.length][];
203
 
204
        for (int l = 0; l < messageLines.length; l++) {
205
 
206
            lcdMessageCommand[l] = new byte[8 + messageLinesBytes[l].length + 1];
207
 
208
            lcdMessageCommand[l][0] = (byte) 0xF0;
209
            lcdMessageCommand[l][1] = (byte) 0x00;
210
            lcdMessageCommand[l][2] = (byte) 0x00;
211
            lcdMessageCommand[l][3] = (byte) 0x7E;
212
            lcdMessageCommand[l][4] = (byte) 0x40;
213
            lcdMessageCommand[l][5] = (byte) (deviceID & 0x7F); // device-id
214
            lcdMessageCommand[l][6] = (byte) 0x08; // lcd message command
215
 
216
            lcdMessageCommand[l][7] = (byte) ((l > 0) ? COORDS_MODE : cursor); // lcd
217
            // command
218
 
219
            System.arraycopy(messageLinesBytes[l], 0, lcdMessageCommand[l], 8,
220
                    messageLinesBytes[l].length);
221
 
222
            lcdMessageCommand[l][lcdMessageCommand[l].length - 1] = (byte) 0xF7;
223
        }
224
 
225
        do {
226
            for (int l = 0; l < messageLines.length; l++) {
227
                try {
228
 
229
                    SysexMessage sysExMessage = new SysexMessage();
230
                    sysExMessage.setMessage(lcdMessageCommand[l],
231
                            lcdMessageCommand[l].length);
232
                    receiver.send(sysExMessage, -1);
233
                } catch (InvalidMidiDataException ex) {
234
                    return;
235
                }
236
            }
237
            if (loop && cursor != STOP_MODE) {
238
                try {
239
                    Thread.sleep(1500);
240
                } catch (Exception e) {
241
 
242
                }
243
            }
244
        } while (loop && cursor != STOP_MODE && !cancelled);
245
 
246
        done = true;
247
        addMessage("Done");
248
    }
249
 
250
    private byte[] addPos(byte[] message, int x, int y) {
251
 
252
        byte[] newMessage = new byte[message.length + 2];
253
        newMessage[0] = (byte) x;
254
        newMessage[1] = (byte) y;
255
        System.arraycopy(message, 0, newMessage, 2, message.length);
256
        message = newMessage;
257
        newMessage = null;
258
        return message;
259
    }
260
}