Removed mostly useless "TestApp" demo, as it was mainly useful only for checking...
[pub/USBasp.git] / Bootloaders / Incomplete / MIDI / JavaHost / BIN2BOOT.java
1 import javax.sound.midi.*;
2 import javax.sound.midi.MidiMessage.*;
3 import java.io.RandomAccessFile;
4
5 class BIN2BOOT
6 {
7 private static final int MIDI_CONTROL_CHANNEL = 9;
8 private static final int MIDI_DATA_CHANNEL = 0;
9
10 private static final int CONTROL_DEVICE_READY = 0xD1;
11 private static final int CONTROL_ENTER_PROG_MODE = 0xDC;
12 private static final int CONTROL_LEAVE_PROG_MODE = 0xDF;
13 private static final int CONTROL_GET_PAGE_SIZE = 0x01;
14
15 public static void main(String[] args)
16 {
17 if (args.length != 1)
18 {
19 System.out.println("BIN2BOOT - USB-MIDI bootloader");
20 System.out.println(" Usage: java BIN2BOOT {input}.bin");
21 }
22
23 RandomAccessFile inFile = null;
24
25 try
26 {
27 inFile = new RandomAccessFile(args[0], "r");
28 }
29 catch (Exception e)
30 {
31 System.out.println("Could not open input file!");
32 return;
33 }
34
35 MidiDevice currDevice = null;
36 Receiver midiOut = null;
37 Transmitter midiIn = null;
38 MIDIMessageReceiver midiInMessages = new MIDIMessageReceiver();
39
40 try
41 {
42 MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
43
44 for (MidiDevice.Info info : infos)
45 {
46 currDevice = MidiSystem.getMidiDevice(info);
47
48 if (!(currDevice instanceof Sequencer) && !(currDevice instanceof Synthesizer))
49 {
50 if (info.getName().indexOf("LUFA") == -1)
51 continue;
52
53 System.out.println(" MIDI Device: " + info.getName());
54
55 currDevice.open();
56
57 if (currDevice.getMaxReceivers() != 0)
58 {
59 midiOut = currDevice.getReceiver();
60 break;
61 }
62
63 if (currDevice.getMaxTransmitters() != 0)
64 {
65 midiIn = currDevice.getTransmitter();
66 midiIn.setReceiver(midiInMessages);
67 }
68 }
69 }
70
71 if ((midiOut == null) || (midiIn == null))
72 {
73 System.out.println("Could not find suitable MIDI device!");
74 return;
75 }
76 }
77 catch (Exception e)
78 {
79 System.out.println("Could not enumerate MIDI devices!");
80 return;
81 }
82
83 System.out.println("PROGRAMMING FILE...");
84
85 ProgramFirmware(inFile, midiOut, midiInMessages);
86
87 System.out.println("DONE.");
88
89 try
90 {
91 midiOut.close();
92 midiIn.close();
93 currDevice.close();
94 inFile.close();
95 }
96 catch (Exception e)
97 {
98 System.out.println("ERROR: Could not close open resources.");
99 }
100 }
101
102 private static void ProgramFirmware(RandomAccessFile inFile, Receiver midiOut, MIDIMessageReceiver midiInMessages)
103 {
104 try
105 {
106 System.out.println("Entering Programming Mode...");
107 sendByteViaMIDI(midiOut, MIDI_CONTROL_CHANNEL, CONTROL_ENTER_PROG_MODE);
108
109 int[] messageData;
110 do
111 {
112 messageData = receiveByteViaMIDI(midiInMessages);
113 }
114 while ((messageData[0] != MIDI_CONTROL_CHANNEL) && (messageData[1] != CONTROL_DEVICE_READY));
115
116 System.out.println("Getting Page Size...");
117 sendByteViaMIDI(midiOut, MIDI_CONTROL_CHANNEL, CONTROL_GET_PAGE_SIZE);
118
119 int nextByte = inFile.read();
120 while (nextByte != -1)
121 {
122 sendByteViaMIDI(midiOut, 9, nextByte);
123
124 if ((inFile.getFilePointer() % (inFile.length() / 100)) == 0)
125 System.out.println(" LOADING: " + (int)(inFile.getFilePointer() / (inFile.length() / 100.0)) + "%...");
126
127 nextByte = inFile.read();
128 }
129
130 sendByteViaMIDI(midiOut, MIDI_CONTROL_CHANNEL, CONTROL_LEAVE_PROG_MODE);
131 }
132 catch (Exception e)
133 {
134 System.out.println("ERROR: Could not send data to device.");
135 }
136 }
137
138 private static void sendByteViaMIDI(Receiver midiOut, int channel, int data)
139 {
140 ShortMessage sendMessage = new ShortMessage();
141
142 try
143 {
144 sendMessage.setMessage(ShortMessage.NOTE_ON, channel, (data & 0x7F), (((data & 0x80) == 0x80) ? 64 : 32));
145 midiOut.send(sendMessage, -1);
146
147 // try {Thread.sleep(1);} catch (Exception e) {}
148
149 sendMessage.setMessage(ShortMessage.NOTE_OFF, channel, (data & 0x7F), (((data & 0x80) == 0x80) ? 64 : 32));
150 midiOut.send(sendMessage, -1);
151 }
152 catch (Exception e)
153 {
154 System.out.println("ERROR: Could not send MIDI note press.");
155 }
156 }
157
158 private static int[] receiveByteViaMIDI(MIDIMessageReceiver midiInReceiver)
159 {
160 byte[] messageData;
161
162 do
163 {
164 while (!(midiInReceiver.hasReceived()));
165 messageData = midiInReceiver.receive().getMessage();
166 }
167 while ((messageData[0] & 0xF0) != ShortMessage.NOTE_ON);
168
169 int channel = (messageData[0] & 0x0F);
170 int data = (messageData[1] | ((messageData[2] == 64) ? 0x80 : 0x00));
171
172 int[] formattedMessageData = new int[2];
173 formattedMessageData[0] = channel;
174 formattedMessageData[1] = data;
175
176 return formattedMessageData;
177 }
178 }