PageRenderTime 67ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 1ms

/src/PicnomeCommunication.java

https://github.com/tkrworks/pic2osc
Java | 2064 lines | 1738 code | 235 blank | 91 comment | 573 complexity | c8d5ca460d3366756f8e16997f34a075 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. * Copylight (C) 2009, Shunichi Yamamoto, tkrworks.net
  3. *
  4. * This file is part of pic2osc.
  5. *
  6. * pic2osc 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 3 of the License, or
  9. * (at your option ) any later version.
  10. *
  11. * pic2osc is distributed in the hope that it will be useful,
  12. * but WITHIOUT 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 PicnomeSerial. if not, see <http:/www.gnu.org/licenses/>.
  18. *
  19. * PicnomeCommunication.java,v.1.6.03(149) 2012/02/15
  20. */
  21. // RXTX
  22. import gnu.io.*;
  23. // JavaOSC
  24. import com.illposed.osc.*;
  25. import com.illposed.osc.utility.*;
  26. import java.nio.channels.*;
  27. // JavaMIDI
  28. import javax.sound.midi.*;
  29. import javax.swing.*;
  30. import java.util.*;
  31. import java.io.*;
  32. import java.net.*;
  33. public class PicnomeCommunication implements PicnomeSystems {
  34. private static final String APP_VERSION = "1.6.03";
  35. private static final int MAX_CONNECTABLE_NUM = 2;
  36. private static final int MAX_ADCON_NUM = 11;
  37. private OSCReader oscr;
  38. private String fwver = "";
  39. private boolean fwver_flag = false;
  40. private Vector<String> device_list = new Vector<String>();
  41. private Vector<String> midiinput_list = new Vector<String>();
  42. private Vector<String> midioutput_list = new Vector<String>();
  43. private List<MidiDevice.Info> midiinputdevices = new ArrayList<MidiDevice.Info>();
  44. private List<MidiDevice.Info> midioutputdevices = new ArrayList<MidiDevice.Info>();
  45. public JButton mididetail_b;
  46. public JButton led_clear_b;
  47. public JButton led_test_b;
  48. public JButton hex_b;
  49. public JButton update_b;
  50. public JComboBox protocol_cb;
  51. public JComboBox device_cb;
  52. public JComboBox cable_cb;
  53. public JComboBox midiinput_cb;
  54. public JComboBox midioutput_cb;
  55. public JComboBox midiparameter_cb;
  56. public JTextField hostaddress_tf;
  57. public JTextField prefix_tf;
  58. public JTextField hostport_tf;
  59. public JTextField listenport_tf;
  60. public JTextField hex_tf;
  61. public JSpinner intensity_s;
  62. public JSpinner startcolumn_s;
  63. public JSpinner startrow_s;
  64. public JCheckBox[] adc_ck = new JCheckBox[MAX_ADCON_NUM];
  65. public JComboBox[] adc_cmb0 = new JComboBox[MAX_ADCON_NUM];
  66. public JComboBox[] adc_cmb1 = new JComboBox[MAX_ADCON_NUM];
  67. public JProgressBar update_pb;
  68. private int current_picnome_num;
  69. private int msg_index = 0;
  70. private CommPortIdentifier[] portId = new CommPortIdentifier[MAX_CONNECTABLE_NUM];
  71. private SerialPort[] port = new SerialPort[MAX_CONNECTABLE_NUM];
  72. private InputStream[] in = new InputStream[MAX_CONNECTABLE_NUM];
  73. private OutputStream[] out = new OutputStream[MAX_CONNECTABLE_NUM];
  74. private OSCPortIn oscpin;
  75. private OSCPortOut oscpout;
  76. private MidiDevice[] midiin = new MidiDevice[MAX_CONNECTABLE_NUM];
  77. private MidiDevice[] midiout = new MidiDevice[MAX_CONNECTABLE_NUM];
  78. private Receiver[] midi_r = new Receiver[MAX_CONNECTABLE_NUM];
  79. private Transmitter[] midi_t = new Transmitter[MAX_CONNECTABLE_NUM];
  80. private int midi_pgm_number;
  81. private String[] device = new String[MAX_CONNECTABLE_NUM];
  82. private String[] device2 = new String[MAX_CONNECTABLE_NUM];
  83. private String[] protocol_type = new String[MAX_CONNECTABLE_NUM];
  84. private String[] host_address = new String[MAX_CONNECTABLE_NUM];
  85. private String[] host_port = new String[MAX_CONNECTABLE_NUM];
  86. private String[] listen_port = new String[MAX_CONNECTABLE_NUM];
  87. private String[] connect_state = new String[MAX_CONNECTABLE_NUM];
  88. private String[] ledtest_state = new String[MAX_CONNECTABLE_NUM];
  89. private String[] cable_orientation = new String[MAX_CONNECTABLE_NUM];
  90. private String[] address_pattern_prefix = new String[MAX_CONNECTABLE_NUM];
  91. private int[] midi_in = new int[MAX_CONNECTABLE_NUM];
  92. private int[] midi_out = new int[MAX_CONNECTABLE_NUM];
  93. private int[] intensity = new int[MAX_CONNECTABLE_NUM];
  94. private int[] starting_column = new int[MAX_CONNECTABLE_NUM];
  95. private int[] starting_row = new int[MAX_CONNECTABLE_NUM];
  96. private int[] co_max_num = new int[MAX_CONNECTABLE_NUM];
  97. private boolean[][] adc_enable = new boolean[MAX_CONNECTABLE_NUM][MAX_ADCON_NUM];
  98. private int[][] adc_type = new int[MAX_CONNECTABLE_NUM][MAX_ADCON_NUM];
  99. private int[][] adc_curve = new int[MAX_CONNECTABLE_NUM][MAX_ADCON_NUM];
  100. private int[][][] midi_parameter = new int[16][8][3];
  101. private int count_ma = 0;
  102. private int[] atb = new int[MAX_ADCON_NUM];
  103. private double[][] atb_box = new double[MAX_ADCON_NUM][32];
  104. private StringBuilder msgled_buf = new StringBuilder("l" + 0 + (char)0 + (char)0 + (char)0x0D);
  105. private boolean isPrB = false;
  106. public PicnomeCommunication() {
  107. current_picnome_num = 0;
  108. initDeviceList();
  109. for(int i = 0; i < MAX_CONNECTABLE_NUM; i++) {
  110. in[i] = null;
  111. out[i] = null;
  112. protocol_type[i] = "Open Sound Control";
  113. midi_in[i] = 0;
  114. midi_out[i] = 0;
  115. host_address[i] = "127.0.0.1";
  116. host_port[i] = "8000";
  117. listen_port[i] = "8080";
  118. connect_state[i] = "Open";
  119. ledtest_state[i] = "LED Test On";
  120. cable_orientation[i] = "left";
  121. address_pattern_prefix[i] = "/test";
  122. intensity[i] = 15;
  123. starting_column[i] = 0;
  124. starting_row[i] = 0;
  125. co_max_num[i] = 7;
  126. for(int j = 0; j < MAX_ADCON_NUM; j++) {
  127. adc_enable[i][j] = false;
  128. }
  129. }
  130. initMIDIPort();
  131. }
  132. public boolean getIsPrB() {
  133. return isPrB;
  134. }
  135. public void enableAllAdcPorts() {
  136. for(int i = 0; i < 11; i++) {
  137. String str =new String("ae " + i + " " + 1 + (char)0x0D);
  138. adc_ck[i].setSelected(true);
  139. adc_cmb0[i].setEnabled(true);
  140. if(adc_cmb0[i].getSelectedIndex() < 2)
  141. adc_cmb1[i].setEnabled(true);
  142. if(((String)device_cb.getSelectedItem()).equals(getCurrentDevice(0))) {
  143. setAdcEnable(0, i, true);
  144. sendDataToSerial(0, str);
  145. }
  146. else if(((String)device_cb.getSelectedItem()).equals(getCurrentDevice(1))) {
  147. setAdcEnable(1, i, true);
  148. sendDataToSerial(1, str);
  149. }
  150. if(i == 1 || i == 2)
  151. adc_cmb0[i].setSelectedIndex(0);
  152. else
  153. adc_cmb0[i].setSelectedIndex(1);
  154. if(i > 2)
  155. adc_cmb1[i].setSelectedIndex(4);
  156. else
  157. adc_cmb1[i].setSelectedIndex(2);
  158. }
  159. }
  160. private List<String> getUsbInfo(String name) {
  161. String id = "none";
  162. String iousbdevices = new String();
  163. List<String> sfx = new ArrayList<String>();
  164. if(System.getProperty("os.name").startsWith("Mac OS X")) {
  165. try {
  166. ProcessBuilder pb = new ProcessBuilder("ioreg", "-w", "0", "-S", "-p", "IOUSB", "-n", name, "-r");
  167. Process p = pb.start();
  168. InputStream is = p.getInputStream();
  169. int c;
  170. while((c = is.read()) != -1) {
  171. iousbdevices += (new Character((char)c)).toString();
  172. }
  173. is.close();
  174. }catch(IOException e) {}
  175. List<String> vid = new ArrayList<String>();
  176. List<String> pid = new ArrayList<String>();
  177. while(iousbdevices.indexOf(name) != -1) {
  178. int pos_start = iousbdevices.indexOf(name);
  179. int pos_end = iousbdevices.indexOf(" }");
  180. String iousbdevice = iousbdevices.substring(pos_start, pos_end);
  181. if(iousbdevice.indexOf(name + "@") != -1) {
  182. pos_start = iousbdevice.indexOf(name + "@") + name.length() + 1;
  183. pos_end = iousbdevice.indexOf("00");
  184. if(pos_start != -1)
  185. id = iousbdevice.substring(pos_start, pos_end);
  186. sfx.add(id);
  187. }
  188. if((pos_start = iousbdevice.indexOf("idVendor")) != -1) {
  189. pos_end = iousbdevice.length();
  190. iousbdevice = iousbdevice.substring(pos_start, pos_end);
  191. pos_end = iousbdevice.indexOf("\n");
  192. id = iousbdevice.substring(iousbdevice.indexOf(" = ") + 3, pos_end);
  193. vid.add(id);
  194. }
  195. iousbdevice = iousbdevices.substring(iousbdevices.indexOf(name), iousbdevices.indexOf(" }"));
  196. if((pos_start = iousbdevice.indexOf("idProduct")) != -1) {
  197. pos_end = iousbdevice.length();
  198. //test System.out.println(pid.get(pid.size() - 1));
  199. iousbdevice = iousbdevice.substring(pos_start, pos_end);
  200. pos_end = iousbdevice.indexOf("\n");
  201. id = iousbdevice.substring(iousbdevice.indexOf(" = ") + 3, pos_end);
  202. pid.add(id);
  203. }
  204. iousbdevices = iousbdevices.substring(iousbdevices.indexOf(" }") + 2, iousbdevices.length());
  205. if(!(vid.get(vid.size() - 1).equals("1240") && (pid.get(pid.size() - 1).equals("65477") || pid.get(pid.size() - 1).equals("64768") || pid.get(pid.size() - 1).equals("63622")))) {
  206. sfx.remove(sfx.size() - 1);
  207. vid.remove(vid.size() - 1);
  208. pid.remove(pid.size() - 1);
  209. }
  210. }
  211. }
  212. else if(System.getProperty("os.name").startsWith("Windows")) {
  213. try {
  214. ProcessBuilder pb = new ProcessBuilder("powercfg", "/devicequery", "all_devices");
  215. Process p = pb.start();
  216. InputStream is = p.getInputStream();
  217. int c;
  218. while((c = is.read()) != -1)
  219. iousbdevices += (new Character((char)c)).toString();
  220. is.close();
  221. }catch(IOException e) {}
  222. while(iousbdevices.indexOf(name) != -1) {
  223. int pos_start = iousbdevices.indexOf(name);
  224. iousbdevices = iousbdevices.substring(pos_start, iousbdevices.length());
  225. int pos_end = iousbdevices.indexOf(")");
  226. String iousbdevice = iousbdevices.substring(0, pos_end + 1);
  227. pos_start = iousbdevice.indexOf("(");
  228. pos_end = iousbdevice.indexOf(")");
  229. id = iousbdevice.substring(pos_start + 1, pos_end);
  230. if((iousbdevice.indexOf("PICnome128") == -1 && name.indexOf("PICnome128") == -1) ||
  231. (iousbdevice.indexOf("PICnome128") != -1 && name.indexOf("PICnome128") != -1))
  232. sfx.add(id);
  233. iousbdevices = iousbdevices.substring(iousbdevices.indexOf(")") + 2, iousbdevices.length());
  234. }
  235. }
  236. return sfx;
  237. }
  238. private void initDeviceList() {
  239. if(System.getProperty("os.name").startsWith("Mac OS X")) {
  240. String device_name;
  241. List<String> suffix0 = getUsbInfo("IOUSBDevice");
  242. List<String> suffix1 = getUsbInfo("PICnome");
  243. List<String> suffix2 = getUsbInfo("PICnome128");
  244. List<String> suffix3 = getUsbInfo("PICratchBOX_OSC");
  245. Enumeration e = CommPortIdentifier.getPortIdentifiers();
  246. for(int i = 0; i < MAX_CONNECTABLE_NUM; i++) {
  247. device[i] = "";
  248. device2[i] = "";
  249. }
  250. while(e.hasMoreElements()) {
  251. device_name = ((CommPortIdentifier)e.nextElement()).getName();
  252. if(device_name.indexOf("/dev/cu.usbmodem") != -1) {
  253. for(String s0str: suffix0) {
  254. if(device_name.indexOf(s0str) != -1) {
  255. if(current_picnome_num >= MAX_CONNECTABLE_NUM)
  256. break;
  257. device[current_picnome_num] = "tkrworks-PICnome64-" + s0str;
  258. device2[current_picnome_num] = device_name;
  259. current_picnome_num++;
  260. device_list.add("tkrworks-PICnome64-" + s0str);
  261. }
  262. }
  263. for(String s3str: suffix3) {//for picratchbox
  264. if(device_name.indexOf(s3str) != -1) {
  265. if(current_picnome_num >= MAX_CONNECTABLE_NUM)
  266. break;
  267. device[current_picnome_num] = "tkrworks-PICratchBOX-" + s3str;
  268. device2[current_picnome_num] = device_name;
  269. current_picnome_num++;
  270. device_list.add("tkrworks-PICratchBOX-" + s3str);
  271. isPrB = true;
  272. }
  273. }
  274. for(String s2str: suffix2) {//for one twenty eight
  275. if(device_name.indexOf(s2str) != -1) {
  276. if(current_picnome_num >= MAX_CONNECTABLE_NUM)
  277. break;
  278. device[current_picnome_num] = "tkrworks-PICnome128-" + s2str;
  279. device2[current_picnome_num] = device_name;
  280. current_picnome_num++;
  281. device_list.add("tkrworks-PICnome128-" + s2str);
  282. }
  283. }
  284. for(String s1str: suffix1) {//for sixty four
  285. if(device_name.indexOf(s1str) != -1) {
  286. if(current_picnome_num >= MAX_CONNECTABLE_NUM)
  287. break;
  288. device[current_picnome_num] = "tkrworks-PICnome64-" + s1str;
  289. device2[current_picnome_num] = device_name;
  290. current_picnome_num++;
  291. device_list.add("tkrworks-PICnome64-" + s1str);
  292. }
  293. }
  294. }
  295. }
  296. }
  297. else if(System.getProperty("os.name").startsWith("Windows")) {
  298. int dev_num = 0;
  299. String device_name;
  300. List<String> comport0 = this.getUsbInfo("tkrworks PICnome");
  301. List<String> comport1 = this.getUsbInfo("tkrworks PICnome128");
  302. List<String> comport2 = this.getUsbInfo("tkrworks PICratchBOX_OSC");
  303. Enumeration e = CommPortIdentifier.getPortIdentifiers();
  304. for(int i = 0; i < MAX_CONNECTABLE_NUM; i++) {
  305. device[i] = "";
  306. device2[i] = "";
  307. }
  308. while(e.hasMoreElements()) {
  309. device_name = ((CommPortIdentifier)e.nextElement()).getName();
  310. for(String c0str: comport0) {
  311. if(device_name.indexOf(c0str) != -1) {
  312. if(current_picnome_num >= MAX_CONNECTABLE_NUM)
  313. break;
  314. device[current_picnome_num] = "tkrworks-PICnome64-" + c0str;
  315. device2[current_picnome_num] = device_name;
  316. current_picnome_num++;
  317. device_list.add("tkrworks-PICnome64-" + c0str);
  318. }
  319. }
  320. for(String c1str: comport1) {
  321. if(device_name.indexOf(c1str) != -1) {
  322. if(current_picnome_num >= MAX_CONNECTABLE_NUM)
  323. break;
  324. device[current_picnome_num] = "tkrworks-PICnome128-" + c1str;
  325. device2[current_picnome_num] = device_name;
  326. current_picnome_num++;
  327. device_list.add("tkrworks-PICnome128-" + c1str);
  328. }
  329. }
  330. for(String c2str: comport2) {
  331. if(device_name.indexOf(c2str) != -1) {
  332. if(current_picnome_num >= MAX_CONNECTABLE_NUM)
  333. break;
  334. device[current_picnome_num] = "tkrworks-PICratchBOX-" + c2str;
  335. device2[current_picnome_num] = device_name;
  336. current_picnome_num++;
  337. device_list.add("tkrworks-PICratchBOX-" + c2str);
  338. }
  339. }
  340. }
  341. }
  342. }
  343. public void changeDeviceSettings(int index) {
  344. protocol_type[1 - index] = (String)protocol_cb.getSelectedItem();
  345. midi_in[1 - index] = midiinput_cb.getSelectedIndex();
  346. midi_out[1 - index] = midioutput_cb.getSelectedIndex();
  347. //debug System.out.println(index + " " + host_port[index] + " " + host_port[1 - index] + " " + hostport_tf.getText());
  348. host_address[1 - index] = hostaddress_tf.getText();
  349. host_port[1 - index] = hostport_tf.getText();
  350. listen_port[1 - index] = listenport_tf.getText();
  351. ledtest_state[1 - index] = led_test_b.getText();
  352. for(int i = 0; i < 6; i++) {
  353. adc_enable[1 - index][i] = adc_ck[i].isSelected();
  354. adc_type[1 - index][i] = adc_cmb0[i].getSelectedIndex();
  355. adc_curve[1 - index][i] = adc_cmb1[i].getSelectedIndex();
  356. }
  357. protocol_cb.setSelectedItem(protocol_type[index]);
  358. midiinput_cb.setSelectedIndex(midi_in[index]);
  359. midioutput_cb.setSelectedIndex(midi_out[index]);
  360. hostaddress_tf.setText(host_address[index]);
  361. hostport_tf.setText(host_port[index]);
  362. listenport_tf.setText(listen_port[index]);
  363. led_test_b.setText(ledtest_state[index]);
  364. cable_cb.setSelectedItem(cable_orientation[index]);
  365. prefix_tf.setText(address_pattern_prefix[index]);
  366. intensity_s.setValue(intensity[index]);
  367. startcolumn_s.setValue(starting_column[index]);
  368. startrow_s.setValue(starting_row[index]);
  369. for(int i = 0; i < 6; i++) {
  370. adc_ck[i].setSelected(adc_enable[index][i]);
  371. if(adc_ck[i].isSelected())
  372. adc_cmb0[i].setEnabled(true);
  373. else
  374. adc_cmb0[i].setEnabled(false);
  375. adc_cmb0[i].setSelectedIndex(adc_type[index][i]);
  376. if(adc_cmb0[i].getSelectedIndex() < 2)
  377. adc_cmb1[i].setEnabled(true);
  378. else
  379. adc_cmb1[i].setEnabled(false);
  380. adc_cmb1[i].setSelectedIndex(adc_curve[index][i]);
  381. }
  382. for(int i = 0; i < MAX_ADCON_NUM; i++)
  383. adc_ck[i].setSelected(adc_enable[index][i]);
  384. }
  385. public boolean openSerialPort(int index) {
  386. try {
  387. if(System.getProperty("os.name").startsWith("Mac OS X")) {
  388. if(device[index].indexOf("PICnome128") != -1)
  389. co_max_num[index] = 15;
  390. else
  391. co_max_num[index] = 7;
  392. portId[index] = CommPortIdentifier.getPortIdentifier(device2[index]);
  393. }
  394. else if(System.getProperty("os.name").startsWith("Windows")) {
  395. if(device[index].indexOf("PICnome128") != -1) {
  396. co_max_num[index] = 15;
  397. portId[index] = CommPortIdentifier.getPortIdentifier(device2[index]);
  398. }
  399. else {
  400. co_max_num[index] = 7;
  401. portId[index] = CommPortIdentifier.getPortIdentifier(device2[index]);
  402. }
  403. }
  404. port[index] = (SerialPort)portId[index].open("pic2osc", 2000);
  405. in[index] = port[index].getInputStream();
  406. out[index] = port[index].getOutputStream();
  407. (new Thread(new SerialReader(index, in[index]))).start();
  408. initOSCPort(index);
  409. initOSCListener();
  410. openMIDIPort(index);
  411. }
  412. catch (NoSuchPortException e) {
  413. e.printStackTrace();
  414. return false;
  415. }
  416. catch (PortInUseException e) {
  417. e.printStackTrace();
  418. return false;
  419. }
  420. catch(IOException e) {
  421. e.printStackTrace();
  422. return false;
  423. }
  424. catch (NullPointerException e) {
  425. e.printStackTrace();
  426. return false;
  427. }
  428. return true;
  429. }
  430. public boolean setSerialPort(int index) {
  431. try {
  432. if(System.getProperty("os.name").startsWith("Mac OS X"))
  433. //sy port[index].setSerialPortParams(230400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
  434. port[index].setSerialPortParams(460800, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
  435. else if(System.getProperty("os.name").startsWith("Windows"))
  436. //sy port[index].setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
  437. port[index].setSerialPortParams(256000, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
  438. port[index].setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
  439. port[index].setDTR(true);
  440. port[index].setRTS(false);
  441. //check firmware version
  442. sendDataToSerial(index, new String("f" + (char)0x0D));
  443. }
  444. catch (UnsupportedCommOperationException e) {
  445. e.printStackTrace();
  446. return false;
  447. }
  448. catch (NullPointerException e) {
  449. e.printStackTrace();
  450. return false;
  451. }
  452. return true;
  453. }
  454. public boolean closeSerialPort(int index) {
  455. try {
  456. in[index].close();
  457. out[index].flush();
  458. out[index].close();
  459. port[index].close();
  460. }
  461. catch(Exception e) {
  462. e.printStackTrace();
  463. return false;
  464. }
  465. return true;
  466. }
  467. public void initOSCPort(int index) {
  468. byte[] hostaddress = new byte[4];
  469. String ha_str = host_address[index];
  470. int idx = 0, idx2;
  471. for(int i = 0; i < 3; i++) {
  472. idx2 = ha_str.indexOf(".", idx);
  473. hostaddress[i] = Byte.parseByte(ha_str.substring(idx, idx2));
  474. idx = idx2 + 1;
  475. }
  476. hostaddress[3] = Byte.parseByte(ha_str.substring(idx, ha_str.length()));
  477. try {
  478. oscr = new OSCReader(Integer.valueOf(listen_port[index]));
  479. //sy (new Thread(new OSCReader(Integer.valueOf(listen_port[index])))).start();
  480. (new Thread(oscr)).start();
  481. oscpout = new OSCPortOut(InetAddress.getByAddress(hostaddress), Integer.valueOf(host_port[index]));
  482. }
  483. catch(IOException ioe){}
  484. }
  485. public void setOSCHostInfo(int index, String newHostPort, String newListenPort) {
  486. byte[] hostaddress = new byte[4];
  487. String ha_str = hostaddress_tf.getText();
  488. int idx = 0, idx2;
  489. for(int i = 0; i < 3; i++) {
  490. idx2 = ha_str.indexOf(".", idx);
  491. hostaddress[i] = Byte.parseByte(ha_str.substring(idx, idx2));
  492. idx = idx2 + 1;
  493. }
  494. hostaddress[3] = Byte.parseByte(ha_str.substring(idx, ha_str.length()));
  495. try {
  496. host_port[index] = newHostPort;
  497. listen_port[index] = newListenPort;
  498. //debug System.out.println(index + " " + host_port[index] + " " + listen_port[index]);
  499. hostport_tf.setText(host_port[index]);
  500. listenport_tf.setText(listen_port[index]);
  501. //debug System.out.println("oscr.setPort");
  502. oscr.setPort(Integer.valueOf(listen_port[index]));
  503. oscpout = new OSCPortOut(InetAddress.getByAddress(hostaddress), Integer.valueOf(host_port[index]));
  504. }
  505. catch(UnknownHostException uhe) {}
  506. catch(SocketException se) {}
  507. }
  508. //sy MIDI Setup
  509. private void initMIDIPort() {
  510. MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
  511. MidiDevice device;
  512. for(MidiDevice.Info info : infos) {
  513. try {
  514. device = MidiSystem.getMidiDevice(info);
  515. }
  516. catch(MidiUnavailableException e) {
  517. e.printStackTrace();
  518. continue;
  519. }
  520. if(device.getMaxTransmitters() == 0)
  521. continue;
  522. midiinput_list.add(info.getName());
  523. midiinputdevices.add(info);
  524. }
  525. for(MidiDevice.Info info : infos) {
  526. try {
  527. device = MidiSystem.getMidiDevice(info);
  528. }
  529. catch(MidiUnavailableException e) {
  530. e.printStackTrace();
  531. continue;
  532. }
  533. if(device.getMaxReceivers() == 0)
  534. continue;
  535. midioutput_list.add(info.getName());
  536. midioutputdevices.add(info);
  537. }
  538. }
  539. public void openMIDIPort(int index) {
  540. try {
  541. midiin[index] = MidiSystem.getMidiDevice(midiinputdevices.get(midi_in[index]));
  542. if(midiin[index].isOpen())
  543. midiin[index].close();
  544. midiin[index].open();
  545. midi_t[index] = midiin[index].getTransmitter();
  546. enableMidiLed(index);
  547. midiout[index] = MidiSystem.getMidiDevice(midioutputdevices.get(midi_out[index]));
  548. if(midiout[index].isOpen())
  549. midiout[index].close();
  550. midiout[index].open();
  551. midi_r[index] = midiout[index].getReceiver();
  552. }
  553. catch(MidiUnavailableException mue){}
  554. }
  555. public Vector<String> getDeviceList() {
  556. return device_list;
  557. }
  558. public int getCurrentNum() {
  559. return current_picnome_num;
  560. }
  561. public String getCurrentDevice(int index) {
  562. return device[index];
  563. }
  564. public void setCurrentProtocol(int index, String name) {
  565. protocol_type[index] = name;
  566. }
  567. public void setCurrentMidiIn(int index, int id) {
  568. midi_in[index] = id;
  569. }
  570. public void setCurrentMidiOut(int index, int id) {
  571. midi_out[index] = id;
  572. }
  573. public Vector<String> getMidiInputList() {
  574. return midiinput_list;
  575. }
  576. public Vector<String> getMidiOutputList() {
  577. return midioutput_list;
  578. }
  579. public int getCurrentMidiIn(int index) {
  580. return midi_in[index];
  581. }
  582. public int getCurrentMidiOut(int index) {
  583. return midi_out[index];
  584. }
  585. public int getMidiNoteChannel(int nx, int ny) {
  586. return midi_parameter[nx][ny][0];
  587. }
  588. public int getMidiNoteOnVelocity(int nx, int ny) {
  589. return midi_parameter[nx][ny][1];
  590. }
  591. public int getMidiNoteOffVelocity(int nx, int ny) {
  592. return midi_parameter[nx][ny][2];
  593. }
  594. public void setMidiNoteChannel(int nx, int ny, int ch) {
  595. midi_parameter[nx][ny][0] = ch;
  596. }
  597. public void setMidiNoteOnVelocity(int nx, int ny, int vel) {
  598. midi_parameter[nx][ny][1] = vel;
  599. }
  600. public void setMidiNoteOffVelocity(int nx, int ny, int vel) {
  601. midi_parameter[nx][ny][2] = vel;
  602. }
  603. public void setCurrentCable(int index, String orientation) {
  604. cable_orientation[index] = orientation;
  605. }
  606. public void setCurrentStartingColumn(int index, int column) {
  607. starting_column[index] = column;
  608. }
  609. public void setCurrentStartingRow(int index, int row) {
  610. starting_row[index] = row;
  611. }
  612. public int getCurrentMaxColumn(int index) {
  613. return co_max_num[index];
  614. }
  615. public int getCurrentIntensity(int index) {
  616. return intensity[index];
  617. }
  618. public void setCurrentIntensity(int index, int value) {
  619. intensity[index] = value;
  620. }
  621. public void setAdcEnable(int index0, int index1, boolean b) {
  622. adc_enable[index0][index1] = b;
  623. }
  624. public String getAppVersion() {
  625. return APP_VERSION;
  626. }
  627. public int getMaxAnalogNum() {
  628. return MAX_ADCON_NUM;
  629. }
  630. public boolean isFirmwareVersion() {
  631. return fwver_flag;
  632. }
  633. public String getFirmwareVersion() {
  634. return fwver;
  635. }
  636. public boolean checkPortState(int index) {
  637. if(portId[index] != null && portId[index].isCurrentlyOwned())
  638. return true;
  639. else
  640. return false;
  641. }
  642. public void sendDataToSerial(int index, int data) {
  643. try {
  644. out[index].write(data);
  645. }
  646. catch(IOException e) {}
  647. }
  648. public void sendDataToSerial(int index, String str) {
  649. try {
  650. if(str == null )
  651. return;
  652. out[index].write(str.getBytes());
  653. }
  654. catch(IOException e) {}
  655. }
  656. public void sendDataToSerial(int index, String[] str) {
  657. try {
  658. if(str == null )
  659. return;
  660. for(String str0 : str) {
  661. out[index].write(str0.getBytes());
  662. int j = 0;
  663. for(int i = 0; i < 100000; i++)
  664. j++;
  665. }
  666. }
  667. catch(IOException e) {}
  668. }
  669. private int getHexStringToInt(String str) {
  670. int value = 0;
  671. int c = str.charAt(0);
  672. if(c >= 65)
  673. value = (c - 65) + 10;
  674. else
  675. value = c - 48;
  676. return value;
  677. }
  678. private void sendOSCMessageFromHw(int index, int[] chs) {
  679. Object[] args;
  680. OSCMessage msg;
  681. if((char)chs[0] == 'p' || (char)chs[0] == 'r') {
  682. if(protocol_type[index].equals("Open Sound Control") || protocol_type[index].equals("OSC/MIDI(ext.)")) {
  683. args = new Object[3];
  684. int x0 = chs[1] & 0x0F;
  685. int y0 = (chs[1] >> 4) & 0x0F;
  686. int sc = starting_column[index];
  687. int sr = starting_row[index];
  688. if(cable_orientation[index].equals("left")) {
  689. args[0] = x0 + sc; // X
  690. args[1] = y0 + sr; // Y
  691. }
  692. else if(cable_orientation[index].equals("right")) {
  693. args[0] = co_max_num[index] - x0 + sc; // X
  694. args[1] = 7 - y0 + sr; // Y
  695. }
  696. else if(cable_orientation[index].equals("up")) {
  697. args[1] = co_max_num[index] - x0 + sr; // Y
  698. args[0] = y0 + sc; // X
  699. }
  700. else if(cable_orientation[index].equals("down")) {
  701. args[1] = x0 + sr; // Y
  702. args[0] = 7 - y0 + sc; // X
  703. }
  704. if((char)chs[0] == 'p')
  705. args[2] = 1; // State
  706. else if((char)chs[0] == 'r')
  707. args[2] = 0; // State
  708. msg = new OSCMessage(address_pattern_prefix[index] + "/press", args);
  709. try {
  710. oscpout.send(msg);
  711. }
  712. catch(IOException e) {
  713. e.printStackTrace();
  714. return;
  715. }
  716. catch(NullPointerException e) {
  717. e.printStackTrace();
  718. return;
  719. }
  720. }
  721. else if(protocol_type[index].equals("DORAnome")) {// for DORAnome
  722. int x0 = chs[1] & 0x0F;
  723. int y0 = (chs[1] >> 4) & 0x0F;
  724. // send MIDI note
  725. if(x0 == 11) {
  726. int note_number = y0 + 1;
  727. try {
  728. ShortMessage sm = new ShortMessage();
  729. if((char)chs[0] == 'p')
  730. sm.setMessage(ShortMessage.NOTE_ON, 1,// MIDI ch.2
  731. (byte)note_number, 127);
  732. else if((char)chs[0] == 'r')
  733. sm.setMessage(ShortMessage.NOTE_OFF, 1,// MIDI ch.2
  734. (byte)note_number, 0);
  735. midi_r[index].send(sm, 1);
  736. } catch(InvalidMidiDataException imde) {}
  737. }
  738. else if(x0 == 10) {
  739. int note_number = y0 + 1;
  740. try {
  741. ShortMessage sm = new ShortMessage();
  742. if((char)chs[0] == 'p')
  743. sm.setMessage(ShortMessage.NOTE_ON, 2,// MIDI ch.3
  744. (byte)note_number, 127);
  745. else if((char)chs[0] == 'r')
  746. sm.setMessage(ShortMessage.NOTE_OFF, 2,// MIDI ch.3
  747. (byte)note_number, 0);
  748. midi_r[index].send(sm, 1);
  749. } catch(InvalidMidiDataException imde) {}
  750. }
  751. else if((x0 == 12 && y0 == 2) || (x0 == 13 && y0 == 2) || (x0 == 12 && y0 == 3) || (x0 == 13 && y0 == 3)) {
  752. int note_number = (y0 + 7) + 2 * (x0 - 12);
  753. try {
  754. ShortMessage sm = new ShortMessage();
  755. if((char)chs[0] == 'p')
  756. sm.setMessage(ShortMessage.NOTE_ON, 1,// MIDI ch.2
  757. (byte)note_number, 127);
  758. else if((char)chs[0] == 'r')
  759. sm.setMessage(ShortMessage.NOTE_OFF, 1,// MIDI ch.2
  760. (byte)note_number, 0);
  761. midi_r[index].send(sm, 1);
  762. } catch(InvalidMidiDataException imde) {}
  763. }
  764. args = new Object[3];
  765. int sc = starting_column[index];
  766. int sr = starting_row[index];
  767. if(cable_orientation[index].equals("left")) {
  768. args[0] = x0 + sc; // X
  769. args[1] = y0 + sr; // Y
  770. }
  771. else if(cable_orientation[index].equals("right")) {
  772. args[0] = co_max_num[index] - x0 + sc; // X
  773. args[1] = 7 - y0 + sr; // Y
  774. }
  775. else if(cable_orientation[index].equals("up")) {
  776. args[1] = co_max_num[index] - x0 + sr; // Y
  777. args[0] = y0 + sc; // X
  778. }
  779. else if(cable_orientation[index].equals("down")) {
  780. args[1] = x0 + sr; // Y
  781. args[0] = 7 - y0 + sc; // X
  782. }
  783. if((char)chs[0] == 'p')
  784. args[2] = 1; // State
  785. else if((char)chs[0] == 'r')
  786. args[2] = 0; // State
  787. msg = new OSCMessage(address_pattern_prefix[index] + "/press", args);
  788. try {
  789. oscpout.send(msg);
  790. }
  791. catch(IOException e) {
  792. e.printStackTrace();
  793. return;
  794. }
  795. catch(NullPointerException e) {
  796. e.printStackTrace();
  797. return;
  798. }
  799. }
  800. else {// for MIDI
  801. int notex = chs[1] & 0x0F;
  802. int notey = (chs[1] >> 4) & 0x0F;
  803. int note_number = notex + (notey * (co_max_num[index] + 1));
  804. try {
  805. ShortMessage sm = new ShortMessage();
  806. if((char)chs[0] == 'p')
  807. sm.setMessage(ShortMessage.NOTE_ON, getMidiNoteChannel(notex, notey),
  808. (byte)note_number, getMidiNoteOnVelocity(notex, notey));
  809. else if((char)chs[0] == 'r')
  810. sm.setMessage(ShortMessage.NOTE_OFF, getMidiNoteChannel(notex, notey),
  811. (byte)note_number, getMidiNoteOffVelocity(notex, notey));
  812. midi_r[index].send(sm, 1);
  813. } catch(InvalidMidiDataException imde) {}
  814. }
  815. }
  816. else if((char)chs[0] == 'a') {
  817. float f = 0.0f;
  818. double f1 = 0.0;
  819. int adc_id = chs[1] >> 4;
  820. int ac0 = adc_cmb0[adc_id].getSelectedIndex();
  821. double ac1 = adc_cmb1[adc_id].getSelectedIndex();
  822. if(!adc_ck[adc_id].isSelected())
  823. return;
  824. atb[adc_id] = (((chs[1] & 0x03) << 8) + chs[2]);
  825. if(atb[adc_id] > 1000) atb[adc_id] = 1023;
  826. else if(atb[adc_id] < 9) atb[adc_id] = 0;
  827. //debug System.out.printf("%d = %d%n", adc_id, atb[adc_id]);
  828. if(ac0 == 0) {//IF
  829. atb[adc_id] = atb[adc_id] >> 3;
  830. f = (float)(Math.pow(atb[adc_id] / 127.0, Math.pow(2.0, (2.0 * ac1) - 4.0)));
  831. }
  832. else if(ac0 == 1) {//CF
  833. atb[adc_id] = atb[adc_id] >> 3;
  834. f1 = atb[adc_id] * 1.008;
  835. if(f1 < 2.0)
  836. f1 = 0.0;
  837. else if(f1 > 127.0)
  838. f1 = 127.0;
  839. if(atb[adc_id] < 64)
  840. f = (float)(0.5 * Math.pow(f1 / 64.0, Math.pow(2.0, (2.0 * ac1) - 8.0)));
  841. else
  842. f = (float)(1.0 - (0.5 * Math.pow((127.0 - f1) / 64.0, Math.pow(2.0, (2.0 * ac1) - 8.0))));
  843. }
  844. else {
  845. atb_box[adc_id][count_ma] = atb[adc_id];
  846. double sum = 0;
  847. for(int i = 0; i < 32; i++)
  848. sum += atb_box[adc_id][i];
  849. f = (float)((sum / 32.0) / 1023.0);
  850. count_ma++;
  851. if(count_ma > 31)
  852. count_ma = 0;
  853. }
  854. if(protocol_type[index].equals("Open Sound Control")) {
  855. args = new Object[2];
  856. args[0] = adc_id;
  857. args[1] = f;//sy (float)(((int)(f * 250.0)) / 250.0);
  858. if(getIsPrB()) {
  859. if(adc_id < 3) {
  860. if(adc_id == 1)
  861. args[0] = 2;
  862. else if(adc_id == 2)
  863. args[0] = 1;
  864. msg = new OSCMessage(address_pattern_prefix[index] + "/fader", args);
  865. }
  866. else {
  867. Object[] argsv = new Object[3];
  868. if(adc_id < 7) {
  869. argsv[0] = 1;
  870. argsv[1] = 3 - (adc_id - 3);
  871. }
  872. else {
  873. argsv[0] = 0;
  874. argsv[1] = 3 - (adc_id - 7);
  875. }
  876. argsv[2] = 1.0f - f;
  877. msg = new OSCMessage(address_pattern_prefix[index] + "/vol", argsv);
  878. }
  879. }
  880. else
  881. msg = new OSCMessage(address_pattern_prefix[index] + "/adc", args);
  882. try {
  883. oscpout.send(msg);
  884. }
  885. catch(IOException e) {
  886. e.printStackTrace();
  887. return;
  888. }
  889. catch(NullPointerException e) {
  890. e.printStackTrace();
  891. return;
  892. }
  893. }
  894. else if(protocol_type[index].equals("DORAnome")) {// for DORAnome
  895. if(adc_id == 2) {
  896. args = new Object[2];
  897. args[0] = adc_id;
  898. args[1] = f;//sy (float)(((int)(f * 250.0)) / 250.0);
  899. msg = new OSCMessage(address_pattern_prefix[index] + "/adc", args);
  900. try {
  901. oscpout.send(msg);
  902. }
  903. catch(IOException e) {
  904. e.printStackTrace();
  905. return;
  906. }
  907. catch(NullPointerException e) {
  908. e.printStackTrace();
  909. return;
  910. }
  911. }
  912. //test else {
  913. try {
  914. ShortMessage sm = new ShortMessage();
  915. if(protocol_type[index].equals("DORAnome"))
  916. sm.setMessage(ShortMessage.CONTROL_CHANGE, 3, adc_id, (int)(f * 127));
  917. else
  918. sm.setMessage(ShortMessage.CONTROL_CHANGE, 0, adc_id, (int)(f * 127));
  919. midi_r[index].send(sm, 1);
  920. } catch(InvalidMidiDataException imde){}
  921. //test }
  922. }
  923. else {// for MIDI
  924. try {
  925. ShortMessage sm = new ShortMessage();
  926. if(protocol_type[index].equals("DORAnome"))
  927. sm.setMessage(ShortMessage.CONTROL_CHANGE, 3, adc_id, (int)(f * 127));
  928. else
  929. sm.setMessage(ShortMessage.CONTROL_CHANGE, 0, adc_id, (int)(f * 127));
  930. midi_r[index].send(sm, 1);
  931. } catch(InvalidMidiDataException imde){}
  932. }
  933. }
  934. else if((char)chs[0] == 'c' && (char)chs[1] == 'g') {
  935. if(protocol_type[index].equals("Open Sound Control")) {
  936. args = new Object[2];
  937. args[0] = chs[2];
  938. args[1] = chs[3];
  939. msg = new OSCMessage(address_pattern_prefix[index] + "/adc/curve", args);
  940. /*
  941. if(!getIsPrB())
  942. return;
  943. */
  944. try {
  945. oscpout.send(msg);
  946. }
  947. catch(IOException e) {
  948. e.printStackTrace();
  949. return;
  950. }
  951. catch(NullPointerException e) {
  952. e.printStackTrace();
  953. return;
  954. }
  955. }
  956. }
  957. }
  958. private void sendOSCMessageFromHw(int index, String str) {
  959. StringTokenizer st = new StringTokenizer(str);
  960. Object[] args;
  961. OSCMessage msg;
  962. String token = st.nextToken();
  963. if(str.substring(0, 1).equals("p") || str.substring(0, 1).equals("r")) {
  964. if(((String)protocol_cb.getSelectedItem()).equals("Open Sound Control")) {
  965. args = new Object[3];
  966. int x0 = getHexStringToInt(str.substring(1, 2));
  967. int y0 = getHexStringToInt(str.substring(2, 3));
  968. int state0 = -1;
  969. if(str.substring(0, 1).equals("p"))
  970. state0 = 1;
  971. else if(str.substring(0, 1).equals("r"))
  972. state0 = 0;
  973. int sc = starting_column[index];
  974. int sr = starting_row[index];
  975. if(cable_orientation[index].equals("left")) {
  976. args[0] = x0 + sc; // X
  977. args[1] = y0 + sr; // Y
  978. }
  979. else if(cable_orientation[index].equals("right")) {
  980. args[0] = co_max_num[index] - x0 + sc; // X
  981. args[1] = 7 - y0 + sr; // Y
  982. }
  983. else if(cable_orientation[index].equals("up")) {
  984. args[1] = co_max_num[index] - x0 + sr; // Y
  985. args[0] = y0 + sc; // X
  986. }
  987. else if(cable_orientation[index].equals("down")) {
  988. args[1] = x0 + sr; // Y
  989. args[0] = 7 - y0 + sc; // X
  990. }
  991. args[2] = state0; // State
  992. msg = new OSCMessage(address_pattern_prefix[index] + "/press", args);
  993. try {
  994. oscpout.send(msg);
  995. }
  996. catch(IOException e){}
  997. }
  998. else {// for MIDI
  999. int notex = getHexStringToInt(str.substring(1, 2));
  1000. int notey = getHexStringToInt(str.substring(2, 3));
  1001. int state = -1;
  1002. if(str.substring(0, 1).equals("p"))
  1003. state = 1;
  1004. else if(str.substring(0, 1).equals("r"))
  1005. state = 0;
  1006. int note_number = notex + (notey * (co_max_num[index] + 1));
  1007. try {
  1008. ShortMessage sm = new ShortMessage();
  1009. if(state == 1)
  1010. sm.setMessage(ShortMessage.NOTE_ON, getMidiNoteChannel(notex, notey),
  1011. (byte)note_number, getMidiNoteOnVelocity(notex, notey));
  1012. else
  1013. sm.setMessage(ShortMessage.NOTE_OFF, getMidiNoteChannel(notex, notey),
  1014. (byte)note_number, getMidiNoteOffVelocity(notex, notey));
  1015. midi_r[index].send(sm, 1);
  1016. }
  1017. catch(InvalidMidiDataException imde) {}
  1018. }
  1019. }
  1020. else if(token.equals("adc")) {
  1021. if(((String)protocol_cb.getSelectedItem()).equals("Open Sound Control")) {
  1022. args = new Object[2];
  1023. args[0] = Integer.parseInt(st.nextToken()); // Pin
  1024. args[1] = Float.parseFloat(st.nextToken()); // Value
  1025. msg = new OSCMessage(address_pattern_prefix[index] + "/adc", args);
  1026. try {
  1027. oscpout.send(msg);
  1028. }
  1029. catch(IOException e){}
  1030. }
  1031. /*
  1032. else//for MIDI
  1033. {
  1034. int ctrl_number = Integer.parseInt(st.nextToken()); // Pin
  1035. int ctrl_value = (int)(127.0 * Float.parseFloat(st.nextToken())); // Value
  1036. Controller cc = new Controller(ctrl_number, ctrl_value);
  1037. //sy midiout.sendController(new Controller(ctrl_number, ctrl_value));
  1038. midiout[0].sendController(new Controller(ctrl_number, ctrl_value));
  1039. }
  1040. */
  1041. }
  1042. else if(token.equals("report")) {
  1043. int v1,v2;
  1044. v1 = Integer.parseInt(st.nextToken());
  1045. v2 = Integer.parseInt(st.nextToken());
  1046. if(v2 == 1) {
  1047. hex_tf.setEnabled(false);
  1048. hex_b.setEnabled(false);
  1049. update_b.setEnabled(false);
  1050. }
  1051. }
  1052. }
  1053. private String controlMsgLed(int index, OSCMessage message) {
  1054. String address = message.getAddress();
  1055. Object[] args = message.getArguments();
  1056. //sy0 if(args.length < 3)
  1057. if(message.getArgumentsLength() < 3)
  1058. return null;
  1059. int args0 = (int)Float.parseFloat(args[0].toString());
  1060. int args1 = (int)Float.parseFloat(args[1].toString());
  1061. int args2 = (int)Float.parseFloat(args[2].toString());
  1062. int sc = starting_column[index];
  1063. int sr = starting_row[index];
  1064. //debug System.out.printf("%d %d %d %d", args0, args1, args2, message.getArgumentsLength());
  1065. //debug System.out.println(args0 + " " + args1 + " " + args2);
  1066. if(cable_orientation[index].equals("left")) {
  1067. sc = args0 - sc;
  1068. sr = args1 - sr;
  1069. }
  1070. else if(cable_orientation[index].equals("right")) {
  1071. sc = co_max_num[index] - args0 + sc;
  1072. sr = 7 - args1 + sr;
  1073. }
  1074. else if(cable_orientation[index].equals("up")) {
  1075. int sc1 = co_max_num[index] - args1 + sr;
  1076. int sr1 = args0 - sc;
  1077. sc = sc1;
  1078. sr = sr1;
  1079. }
  1080. else if(cable_orientation[index].equals("down")) {
  1081. int sc1 = args1 - sr;
  1082. int sr1 = 7 - args0 + sc;
  1083. sc = sc1;
  1084. sr = sr1;
  1085. }
  1086. if(sc < 0 || sr < 0)
  1087. return null;
  1088. sc = (sc >= 10)?('A' + (sc - 10)):(sc + '0');
  1089. sr = (sr >= 10)?('A' + (sr - 10)):(sr + '0');
  1090. msgled_buf.setCharAt(1, (char)(args2 + '0'));
  1091. msgled_buf.setCharAt(2, (char)sc);
  1092. msgled_buf.setCharAt(3, (char)sr);
  1093. msgled_buf.setCharAt(4, (char)0x0D);
  1094. return msgled_buf.toString();
  1095. //sy return "l" + args2 + (char)sc + (char)sr + (char)0x0D;
  1096. }
  1097. private void enableMidiLed(int index) {
  1098. final int index2 = index;
  1099. Receiver rcv = new Receiver() {
  1100. int index = index2;
  1101. public void close() {}
  1102. public void send(MidiMessage message, long timeStamp) {
  1103. if(!protocol_type[index].equals("MIDI"))
  1104. return ;
  1105. byte[] data = message.getMessage();
  1106. if((256 + data[0]) == 144 || (256 + data[0]) == 128) {// NOTE_ON -> 144, NOTE_OFF -> 128
  1107. int sc = (data[1] % (co_max_num[index] + 1));
  1108. int sr = (data[1] / (co_max_num[index] + 1));
  1109. if(sc < 0) sc = 0;
  1110. if(sr < 0) sr = 0;
  1111. String ssc, ssr;
  1112. if(sc >= 10)
  1113. ssc = String.valueOf((char)('A' + (sc - 10)));
  1114. else
  1115. ssc = String.valueOf(sc);
  1116. if(sr >= 10)
  1117. ssr = String.valueOf((char)('A' + (sr - 10)));
  1118. else
  1119. ssr = String.valueOf(sr);
  1120. String str = "";
  1121. if((data[0] + 256) == 144 && data[2] > 0)
  1122. str =new String("l1" + ssc + ssr + (char)0x0D);
  1123. else if((data[0] + 256) == 128 || data[2] == 0)
  1124. str =new String("l0" + ssc + ssr + (char)0x0D);
  1125. if(checkPortState(index))
  1126. sendDataToSerial(index, str);
  1127. }
  1128. }
  1129. };
  1130. midi_t[index].setReceiver(rcv);
  1131. }
  1132. private String controlMsgLedCol(int index, OSCMessage message) {
  1133. Object[] args = message.getArguments();
  1134. int args0 = (int)Float.parseFloat(args[0].toString());
  1135. int args1 = (int)Float.parseFloat(args[1].toString());
  1136. //debug System.out.println("led_col " + args0 + " " + args1);
  1137. //sy0 if(args.length < 2)
  1138. if(message.getArgumentsLength() < 2)
  1139. return null;
  1140. int sc = 0, sr = 0;
  1141. if(cable_orientation[index].equals("left")) {
  1142. sc = args0 - starting_column[index];
  1143. }
  1144. else if(cable_orientation[index].equals("right")) {
  1145. sc = co_max_num[index] - args0 + starting_column[index];
  1146. }
  1147. else if(cable_orientation[index].equals("up")) {
  1148. sc = args0 - starting_column[index];
  1149. }
  1150. else if(cable_orientation[index].equals("down")) {
  1151. sc= 7 - args0 + starting_column[index];
  1152. }
  1153. if(sc < 0) return null;
  1154. int shift = starting_row[index] % (co_max_num[index] + 1);
  1155. if(cable_orientation[index].equals("left")) {
  1156. sr = (char)(args1 >> shift);
  1157. }
  1158. else if(cable_orientation[index].equals("right")) {
  1159. char sr0 = (char)args1;
  1160. char sr1 = 0;
  1161. for(int i = 0; i < 8; i++)
  1162. if((sr0 & (0x01 << i)) == (0x01 << i))
  1163. sr1 |= (0x01 << (7 - i));
  1164. sr = (char)(sr1 << shift);
  1165. }
  1166. else if(cable_orientation[index].equals("up")) {
  1167. char sr0 = (char)args1;
  1168. char sr1 = 0;
  1169. for(int i = 0; i < co_max_num[index] + 1; i++)
  1170. if((sr0 & (0x01 << i)) == (0x01 << i))
  1171. sr1 |= (0x01 << (co_max_num[index] - i));
  1172. sr = (char)(sr1 << shift);
  1173. }
  1174. else if(cable_orientation[index].equals("down")) {
  1175. sr = (char)(args1 >> shift);
  1176. }
  1177. String str;
  1178. if(cable_orientation[index].equals("left") || cable_orientation[index].equals("right"))
  1179. str =new String("lc " + sc + " " + sr + (char)0x0D); // (l)ed_(c)ol
  1180. else
  1181. str =new String("lr " + sc + " " + sr + (char)0x0D); // (l)ed_(r)ow
  1182. return str;
  1183. }
  1184. private String controlMsgLedRow(int index, OSCMessage message) {
  1185. Object[] args = message.getArguments();
  1186. int args0 = (int)Float.parseFloat(args[0].toString());
  1187. int args1 = (int)Float.parseFloat(args[1].toString());
  1188. //debug System.out.println("led_row " + args0 + " " + args1);
  1189. //sy0 if(args.length < 2)
  1190. if(message.getArgumentsLength() < 2)
  1191. return null;
  1192. int sc = 0, sr = 0;
  1193. if(cable_orientation[index].equals("left")) {
  1194. sr = args0 - starting_row[index];
  1195. if(sr > 7)
  1196. return null;
  1197. }
  1198. else if(cable_orientation[index].equals("right")) {
  1199. sr = 7 - args0 + starting_row[index];
  1200. if(sr > 7)
  1201. return null;
  1202. }
  1203. else if(cable_orientation[index].equals("up"))
  1204. sr = co_max_num[index] - args0 + starting_row[index];
  1205. else if(cable_orientation[index].equals("down"))
  1206. sr = args0 - starting_row[index];
  1207. if(sr < 0) return null;
  1208. //sy int shift = starting_column[index] % (co_max_num[index] + 1);
  1209. int shift = starting_column[index];
  1210. if(cable_orientation[index].equals("left")) {
  1211. sc = (char)(args1 >> shift);
  1212. }
  1213. else if(cable_orientation[index].equals("right")) {
  1214. char sc0 = (char)args1;
  1215. char sc1 = 0;
  1216. for(int i = 0; i < co_max_num[index] + 1; i++)
  1217. if((sc0 & (0x01 << i)) == (0x01 << i))
  1218. sc1 |= (0x01 << (co_max_num[index] - i));
  1219. sc = (char)(sc1 << shift);
  1220. }
  1221. else if(cable_orientation[index].equals("up")) {
  1222. sc = (char)(args1 >> shift);
  1223. }
  1224. else if(cable_orientation[index].equals("down")) {
  1225. char sc0 = (char)args1;
  1226. char sc1 = 0;
  1227. for(int i = 0; i < 8; i++)
  1228. if((sc0 & (0x01 << i)) == (0x01 << i))
  1229. sc1 |= (0x01 << (7 - i));
  1230. sc = (char)(sc1 << shift);
  1231. }
  1232. String str;
  1233. if(cable_orientation[index].equals("left") || cable_orientation[index].equals("right"))
  1234. str =new String("lr " + sr + " " + sc + (char)0x0D); // (l)ed_(r)ow
  1235. else
  1236. str =new String("lc " + sr + " " + sc + (char)0x0D); // (l)ed_(c)o0
  1237. return str;
  1238. }
  1239. private String[] controlMsgFrame(int index, OSCMessage message) {
  1240. Object[] args0 = message.getArguments();
  1241. StringBuilder[] str;
  1242. String[] str1;
  1243. int sc = 0, sr = 0;
  1244. int[] args = new int[16];
  1245. int shift;
  1246. if(cable_orientation[index].equals("left") || cable_orientation[index].equals("right")) {
  1247. str = new StringBuilder[8];
  1248. str1 = new String[8];
  1249. for(int i = 0; i < 8; i++)
  1250. str[i] = new StringBuilder("lr " + i + " 0 " + (char)0x0D);
  1251. }
  1252. else {
  1253. str = new StringBuilder[co_max_num[index]];
  1254. str1 = new String[co_max_num[index]];
  1255. for(int i = 0; i < co_max_num[index]; i++)
  1256. str[i] = new StringBuilder("lc " + i + " 0 " + (char)0x0D);
  1257. }
  1258. if(cable_orientation[index].equals("left") || cable_orientation[index].equals("right"))
  1259. shift = starting_column[index] % (co_max_num[index] + 1);
  1260. else
  1261. shift = starting_column[index] % (7 + 1);
  1262. //sy0 for(int i = 0; i < args0.length; i++) {
  1263. for(int i = 0; i < message.getArgumentsLength(); i++) {
  1264. args[i] = (int)Float.parseFloat(args0[i].toString());
  1265. }
  1266. for(int i = 0; i < (co_max_num[index] + 1); i++) {
  1267. if(cable_orientation[index].equals("left")) {
  1268. if(i > 7)
  1269. break;
  1270. sr = i - starting_row[index];
  1271. }
  1272. else if(cable_orientation[index].equals("right")) {
  1273. if(i > 7)
  1274. break;
  1275. sr = 7 - i + starting_row[index];
  1276. }
  1277. else if(cable_orientation[index].equals("up"))
  1278. sr = co_max_num[index] - i + starting_column[index];
  1279. else if(cable_orientation[index].equals("down"))
  1280. sr = i - starting_column[index];
  1281. if(co_max_num[index] == 7) {
  1282. if(cable_orientation[index].equals("left") || cable_orientation[index].equals("right")) {
  1283. if(i < starting_row[index] || ((i - starting_row[index]) > 7))
  1284. continue;
  1285. }
  1286. else {
  1287. if(i < starting_column[index] || ((i - starting_column[index]) > 7))
  1288. continue;
  1289. }
  1290. }
  1291. else if(co_max_num[index] == 15) {
  1292. if(cable_orientation[index].equals("left") || cable_orientation[index].equals("right")) {
  1293. if(i < starting_row[index] || ((i - starting_row[index]) > 7))
  1294. continue;
  1295. }
  1296. else {
  1297. if(i < starting_column[index] || ((i - starting_column[index]) > 15))
  1298. continue;
  1299. }
  1300. }
  1301. if(cable_orientation[index].equals("left"))
  1302. sc = (char)(args[i] >> shift);
  1303. else if(cable_orientation[index].equals("right")) {
  1304. char sc0 = (char)args[i];
  1305. char sc1 = 0;
  1306. for(int j = 0; j < co_max_num[index] + 1; j++)
  1307. if((sc0 & (0x01 << j)) == (0x01 << j))
  1308. sc1 |= (0x01 << (co_max_num[index] - j));
  1309. sc = (char)(sc1 << shift);
  1310. }
  1311. else if(cable_orientation[index].equals("up"))
  1312. sc = (char)(args[i] >> shift);
  1313. else if(cable_orientation[index].equals("down")) {
  1314. char sc0 = (char)args[i];
  1315. char sc1 = 0;
  1316. for(int j = 0; j < (7 + 1); j++)
  1317. if((sc0 & (0x01 << j)) == (0x01 << j))
  1318. sc1 |= (0x01 << (7 - j));
  1319. sc = (char)(sc1 << shift);
  1320. }
  1321. if(cable_orientation[index].equals("left") || cable_orientation[index].equals("right")) {
  1322. str[i].delete(0, str[i].length());
  1323. str[i].append("lr " + sr + " " + sc + (char)0x0D);
  1324. //sy str[i] = new String("lr " + sr + " " + sc + (…

Large files files are truncated, but you can click here to view the full file