@@ -1839,129 +1839,177 @@ class Stream extends Thread {
18391839 }
18401840
18411841 private void sendAccelerometerData () {
1842+
18421843 if (this . protocol. equals(" OSC" )) {
1844+
18431845 for (int i = 0 ; i < NUM_ACCEL_DIMS ; i++ ) {
1844- msg. clearArguments();
1845- if (i == 0 ) {
1846- msg. setAddrPattern(baseOscAddress + " /accelerometer/x" );
1847- } else if (i == 1 ) {
1848- msg. setAddrPattern(baseOscAddress + " /accelerometer/y" );
1849- } else if (i == 2 ) {
1850- msg. setAddrPattern(baseOscAddress + " /accelerometer/z" );
1851- }
1852- msg. add(w_accelerometer. getLastAccelVal(i));
1853- try {
1854- this . osc. send(msg, this . oscNetAddress);
1855- } catch (Exception e) {
1856- println (e. getMessage());
1846+ for (int j = 0 ; j < w_networking. accelDataBufferToSend[i]. length; j++ ) {
1847+ msg. clearArguments();
1848+ if (i == 0 ) {
1849+ msg. setAddrPattern(baseOscAddress + " /accelerometer/x" );
1850+ } else if (i == 1 ) {
1851+ msg. setAddrPattern(baseOscAddress + " /accelerometer/y" );
1852+ } else if (i == 2 ) {
1853+ msg. setAddrPattern(baseOscAddress + " /accelerometer/z" );
1854+ }
1855+ msg. add(w_networking. accelDataBufferToSend[i][j]);
1856+ try {
1857+ this . osc. send(msg, this . oscNetAddress);
1858+ } catch (Exception e) {
1859+ println (e. getMessage());
1860+ }
18571861 }
18581862 }
1863+
18591864 } else if (this . protocol. equals(" UDP" )) {
1860- String outputter = " {\" type\" :\" accelerometer\" ,\" data\" :[" ;
1865+
1866+ StringBuilder output = new StringBuilder ();
1867+ output. append(" {\" type\" :\" accelerometer\" ,\" data\" :[" );
1868+
18611869 for (int i = 0 ; i < NUM_ACCEL_DIMS ; i++ ) {
1862- float accelData = w_accelerometer. getLastAccelVal(i);
1863- // Formatting in this way is resilient to internationalization
1864- String accelData_3dec = threeDecimalPlaces. format(accelData);
1865- outputter += accelData_3dec;
1866- if (i != NUM_ACCEL_DIMS - 1 ) {
1867- outputter += " ," ;
1868- } else {
1869- outputter += " ]}\r\n " ;
1870+ output. append(" [" );
1871+ for (int j = 0 ; j < w_networking. accelDataBufferToSend[i]. length; j++ ) {
1872+ float accelData = w_networking. accelDataBufferToSend[i][j];
1873+ // Formatting in this way is resilient to internationalization
1874+ String accelData_3dec = threeDecimalPlaces. format(accelData);
1875+ output. append(accelData_3dec);
1876+ if (j != w_networking. accelDataBufferToSend[i]. length - 1 ) {
1877+ output. append(" ," );
1878+ }
18701879 }
1880+ String channelArrayEnding = i != NUM_ACCEL_DIMS - 1 ? " ]," : " ]" ;
1881+ output. append(channelArrayEnding);
18711882 }
1883+
1884+ output. append(" ]}\r\n " );
1885+
18721886 try {
1873- this . udp. send(outputter , this . ip, this . port);
1887+ this . udp. send(output . toString() , this . ip, this . port);
18741888 } catch (Exception e) {
18751889 println (e. getMessage());
18761890 }
1891+
18771892 } else if (this . protocol. equals(" LSL" )) {
1878- for (int i = 0 ; i < NUM_ACCEL_DIMS ; i++ ) {
1879- dataToSend[i] = w_accelerometer. getLastAccelVal(i);
1893+
1894+ int numChannels = NUM_ACCEL_DIMS ;
1895+ int numSamples = w_networking. accelDataBufferToSend[0 ]. length;
1896+ float [] _dataToSend = new float [numChannels * numSamples];
1897+ for (int sample = 0 ; sample < numSamples; sample++ ) {
1898+ for (int channel = 0 ; channel < numChannels; channel++ ) {
1899+ _dataToSend[channel + sample * numChannels] = w_networking. accelDataBufferToSend[channel][sample];
1900+ }
18801901 }
1881- outlet_data. push_sample(dataToSend);
1902+ outlet_data. push_sample(_dataToSend);
1903+
18821904 } else if (this . protocol. equals(" Serial" )) {
1883- // Data Format: +0.900,-0.042,+0.254\n
1884- // 7 chars per axis, including \n char for Z
1885- serialMessage = " " ;
1905+
1906+ StringBuilder serialMessage = new StringBuilder ();
18861907 for (int i = 0 ; i < NUM_ACCEL_DIMS ; i++ ) {
1887- float accelData = w_accelerometer. getLastAccelVal(i);
1888- String accelData_3dec = threeDecimalPlaces. format(accelData);
1889- if (accelData >= 0 )
1890- serialMessage += " +" ;
1891- serialMessage += accelData_3dec;
1892- if (i != NUM_ACCEL_DIMS - 1 ) {
1893- serialMessage += " ," ;
1894- } else {
1895- serialMessage += " \n " ;
1908+ serialMessage. append(" [" );
1909+ for (int j = 0 ; j < w_networking. accelDataBufferToSend[i]. length; j++ ) {
1910+ float accelData = w_networking. accelDataBufferToSend[i][j];
1911+ // Formatting in this way is resilient to internationalization
1912+ String accelData_3dec = threeDecimalPlaces. format(accelData);
1913+ if (accelData >= 0 ) {
1914+ serialMessage. append(" +" );
1915+ }
1916+ serialMessage. append(accelData_3dec);
1917+ if (j != w_networking. accelDataBufferToSend[i]. length - 1 ) {
1918+ serialMessage. append(" ," );
1919+ }
18961920 }
1921+ serialMessage. append(" ]" );
18971922 }
18981923 try {
18991924 // println(serialMessage);
1900- this . serial_networking. write(serialMessage);
1925+ this . serial_networking. write(serialMessage. toString() );
19011926 } catch (Exception e) {
19021927 println (e. getMessage());
19031928 }
19041929 }
19051930 }
19061931
19071932 private void sendAnalogReadData () {
1908- // this function is only called if the board is analog capable
1909- int [] analogChannels = ((AnalogCapableBoard ) currentBoard). getAnalogChannels();
1910- List<double[]> lastData = ((AnalogCapableBoard ) currentBoard). getDataWithAnalog(1 );
1911- double [] lastSample = lastData. get(0 );
19121933
1913- final int NUM_ANALOG_READS = analogChannels . length;
1934+ final int NUM_ANALOG_READS = (( AnalogCapableBoard )currentBoard) . getAnalogChannels() . length;
19141935
19151936 if (this . protocol. equals(" OSC" )) {
1937+
19161938 for (int i = 0 ; i < NUM_ANALOG_READS ; i++ ) {
19171939 msg. clearArguments();
19181940 msg. setAddrPattern(baseOscAddress + " /analog/" + i);
1919- msg. add((int ) lastSample[analogChannels[i]]);
1941+ for (int j = 0 ; j < w_networking. analogDataBufferToSend[i]. length; j++ ) {
1942+ msg. add(w_networking. analogDataBufferToSend[i][j]);
1943+ }
19201944 try {
19211945 this . osc. send(msg, this . oscNetAddress);
19221946 } catch (Exception e) {
19231947 println (e. getMessage());
19241948 }
19251949 }
1950+
19261951 } else if (this . protocol. equals(" UDP" )) {
1927- String outputter = " {\" type\" :\" analog\" ,\" data\" :[" ;
1952+
1953+ StringBuilder output = new StringBuilder ();
1954+ output. append(" {\" type\" :\" analog\" ,\" data\" :[" );
1955+
19281956 for (int i = 0 ; i < NUM_ANALOG_READS ; i++ ) {
1929- int auxData = (int ) lastSample[analogChannels[i]];
1930- String auxData_formatted = fourLeadingPlaces. format(auxData);
1931- outputter += auxData_formatted;
1932- if (i != NUM_ANALOG_READS - 1 ) {
1933- outputter += " ," ;
1934- } else {
1935- outputter += " ]}\r\n " ;
1957+ output. append(" [" );
1958+ for (int j = 0 ; j < w_networking. analogDataBufferToSend[i]. length; j++ ) {
1959+ float analogData = w_networking. analogDataBufferToSend[i][j];
1960+ // Formatting in this way is resilient to internationalization
1961+ String analogData_3dec = threeDecimalPlaces. format(analogData);
1962+ output. append(analogData_3dec);
1963+ if (j != w_networking. analogDataBufferToSend[i]. length - 1 ) {
1964+ output. append(" ," );
1965+ }
19361966 }
1967+ String channelArrayEnding = i != NUM_ANALOG_READS - 1 ? " ]," : " ]" ;
1968+ output. append(channelArrayEnding);
19371969 }
1970+
1971+ output. append(" ]}\r\n " );
1972+
19381973 try {
1939- this . udp. send(outputter , this . ip, this . port);
1974+ this . udp. send(output . toString() , this . ip, this . port);
19401975 } catch (Exception e) {
19411976 println (e. getMessage());
19421977 }
1978+
19431979 } else if (this . protocol. equals(" LSL" )) {
1944- for (int i = 0 ; i < NUM_ANALOG_READS ; i++ ) {
1945- dataToSend[i] = (int ) lastSample[analogChannels[i]];
1980+
1981+ int numChannels = NUM_ANALOG_READS ;
1982+ int numSamples = w_networking. analogDataBufferToSend[0 ]. length;
1983+ float [] _dataToSend = new float [numChannels * numSamples];
1984+ for (int sample = 0 ; sample < numSamples; sample++ ) {
1985+ for (int channel = 0 ; channel < numChannels; channel++ ) {
1986+ _dataToSend[channel + sample * numChannels] = w_networking. analogDataBufferToSend[channel][sample];
1987+ }
19461988 }
1947- outlet_data. push_sample(dataToSend);
1989+ outlet_data. push_sample(_dataToSend);
1990+
19481991 } else if (this . protocol. equals(" Serial" )) {
1949- // Data Format: 0001,0002,0003\n or 0001,0002\n depending if Wi-Fi Shield is used
1950- // 5 chars per pin, including \n char for Z
1951- serialMessage = " " ;
1992+
1993+ StringBuilder serialMessage = new StringBuilder ();
1994+
19521995 for (int i = 0 ; i < NUM_ANALOG_READS ; i++ ) {
1953- int auxData = (int ) lastSample[analogChannels[i]];
1954- String auxData_formatted = fourLeadingPlaces. format(auxData);
1955- serialMessage += auxData_formatted;
1956- if (i != NUM_ANALOG_READS - 1 ) {
1957- serialMessage += " ," ;
1958- } else {
1959- serialMessage += " \n " ;
1996+ serialMessage. append(" [" );
1997+ for (int j = 0 ; j < w_networking. analogDataBufferToSend[i]. length; j++ ) {
1998+ float analogData = w_networking. analogDataBufferToSend[i][j];
1999+ // Formatting in this way is resilient to internationalization
2000+ String analogData_3dec = threeDecimalPlaces. format(analogData);
2001+ serialMessage. append(analogData_3dec);
2002+ if (j != w_networking. analogDataBufferToSend[i]. length - 1 ) {
2003+ serialMessage. append(" ," );
2004+ }
19602005 }
2006+ String channelArrayEnding = i != NUM_ANALOG_READS - 1 ? " ]," : " ]" ;
2007+ serialMessage. append(channelArrayEnding);
19612008 }
2009+ serialMessage. append(" \n " );
19622010 try {
19632011 // println(serialMessage);
1964- this . serial_networking. write(serialMessage);
2012+ this . serial_networking. write(serialMessage. toString() );
19652013 } catch (Exception e) {
19662014 println (e. getMessage());
19672015 }
0 commit comments