3 import javax.microedition.io.StreamConnection;
\r
4 import lejos.nxt.comm.*;
\r
8 * Datalogger class; stores float values then then transmits via bluetooth or usb<br>
\r
9 * works with DataViewer in pctools.
\r
10 * A maximum of 2000 data values can be stored.
\r
12 public class Datalogger
\r
14 // overcome limitation of array size; created as needed
\r
15 private float [] log0;
\r
16 private float [] log1;
\r
17 private float [] log2;
\r
18 private float [] log3;
\r
19 private int _indx = 0; //where the data will be witten
\r
21 private final int BLOCK = 510; // block size
\r
22 private int _blocks = 1;
\r
28 log0 = new float[BLOCK];
\r
32 * write a float value to the log
\r
35 public void writeLog(float v)
\r
37 if(_indx>=_blocks * BLOCK)
\r
39 if(_blocks == 1 && log1 == null)log1 = new float[BLOCK];
\r
40 else if(_blocks == 2 && log2 == null)log2 = new float[BLOCK];
\r
41 else if(_blocks == 3 && log3 == null)log3 = new float [BLOCK];
\r
42 else if (_blocks == 4) return;
\r
45 if(_blocks == 1) log0[_indx]= v;
\r
46 else if(_blocks == 2)log1[_indx %BLOCK]= v;
\r
47 else if(_blocks == 3)log2[_indx %BLOCK]= v;
\r
48 else if(_blocks == 4)log3[_indx %BLOCK]= v;
\r
54 * Clears the log; next write is at the beginning;
\r
64 * transmit the stored values to the PC via USB or bluetooth;<br>
\r
65 * Displays "waiting" , so then start the DataViewer.
\r
66 * When finished, displays the number values sent, and asks "Resend?".
\r
67 * Press ENTER for yes, ESC to exit the program.
\r
68 * @param useUSB if false, uses Bluetooth
\r
71 public void transmit(boolean useUSB)
\r
73 StreamConnection connection= null;
\r
74 DataOutputStream dataOut = null;
\r
75 InputStream is = null;
\r
76 boolean more = true;
\r
80 LCD.drawInt(_indx, 0, 0);
\r
81 LCD.drawString("waiting",8,0);
\r
85 connection = new USBConnection();
\r
89 connection = Bluetooth.waitForConnection();
\r
93 dataOut= connection.openDataOutputStream();
\r
94 is = connection.openInputStream();
\r
95 LCD.drawString("connected", 0, 1);
\r
99 LCD.drawInt(b, 8, 1);
\r
102 catch(IOException ie){LCD.drawString("no connection",0,0); LCD.refresh();}
\r
104 LCD.drawString("sending",0,0);
\r
105 LCD.drawInt(_indx, 12, 0);
\r
109 dataOut.writeFloat(_indx);
\r
111 for (int i = 0; i<_indx ; i++)
\r
113 if(i<BLOCK)dataOut.writeFloat(log0[i]);
\r
114 else if(i<2*BLOCK)dataOut.writeFloat(log1[i%BLOCK]);
\r
115 else if(i<3*BLOCK)dataOut.writeFloat(log2[i%BLOCK]);
\r
116 else if(i<3*BLOCK)dataOut.writeFloat(log3[i%BLOCK]);
\r
117 try{Thread.sleep(4);} catch (InterruptedException e ){}
\r
122 catch(IOException e) {LCD.drawString("write error",0,0); LCD.refresh();}
\r
123 Sound.beepSequence();
\r
125 LCD.drawString("sent",0,0);
\r
126 LCD.drawInt(_indx, 8, 0);
\r
127 LCD.drawString("Resend?", 0, 1);
\r
129 more = Button.waitForPress()==1;
\r
131 try{dataOut.close();}catch(IOException e){}
\r
138 * to test the DL object
\r
140 public static void main(String[] args)
\r
143 Datalogger dl = new Datalogger();
\r
144 boolean more = true;
\r
147 for(int i = 0 ; i<size; i++)
\r
152 dl.transmit(true);
\r
155 LCD.drawString("more?",0,2);
\r
157 more = 1 == Button.waitForPress();
\r