00001
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 package net.tinyos.sim.plugins;
00036
00037 import java.io.*;
00038 import java.lang.*;
00039 import java.util.*;
00040 import java.awt.*;
00041 import java.awt.event.*;
00042 import javax.swing.*;
00043
00044 import net.tinyos.message.*;
00045 import net.tinyos.sim.*;
00046 import net.tinyos.sim.event.*;
00047
00048 public class SimADCPlugin extends GuiPlugin implements SimConst {
00049 public Process sim = null;
00050 public final String exe = "sim_adc";
00051 public JTextField inputFilename;
00052
00053 JTable table;
00054 final boolean debug = false;
00055
00056 final int NUM_PORTS = 7;
00057 final int MAX_MOTES = 1000;
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069 final int NUM_COLS = NUM_PORTS + 1;
00070 final String columnNames[] = { "mote id", "photo", "temp", "mic",
00071 "accel_x", "accel_y", "mag_x", "mag_y" };
00072
00073 Integer adc_ports[][] = new Integer[MAX_MOTES][NUM_PORTS];
00074
00075
00076 private void update_table(int moteid) {
00077 table.setValueAt(String.valueOf(moteid), moteid, 0);
00078 for (int i = 0; i < NUM_PORTS; i++) {
00079 if (adc_ports[moteid][i] != null)
00080 table.setValueAt(String.format("0x%04x", adc_ports[moteid][i]),
00081 moteid, i + 1);
00082 }
00083 }
00084
00085
00086 public void handleEvent(SimEvent event) {
00087 if (event instanceof ADCDataReadyEvent) {
00088 ADCDataReadyEvent dev = (ADCDataReadyEvent)event;
00089 int moteid = dev.getMoteID();
00090 MoteSimObject mote = state.getMoteSimObject(moteid);
00091 if (mote == null)
00092 return;
00093 if (moteid >= MAX_MOTES)
00094 return;
00095
00096 int port = dev.get_port();
00097 if (port == 8)
00098 port--;
00099
00100 adc_ports[moteid][port - 1] = dev.get_data();
00101 update_table(moteid);
00102
00103 motePanel.refresh();
00104 }
00105 }
00106
00107
00108 public void register() {
00109 JPanel filePane = new JPanel();
00110 filePane.setLayout(new GridLayout(2,2));
00111
00112 JButton startButton = new JButton("Start ADC simulation");
00113 startButton.addActionListener(new SimADCStarter(this));
00114 startButton.setFont(tv.defaultFont);
00115
00116 JButton stopButton = new JButton("Stop ADC simulation");
00117 stopButton.addActionListener(new SimADCStopper(this));
00118 stopButton.setFont(tv.defaultFont);
00119
00120 inputFilename = new JTextField(20);
00121 JButton setButton = new JButton("Choose simulation data file");
00122 setButton.addActionListener(new FileSelectListener(this, pluginPanel));
00123 setButton.setFont(tv.defaultFont);
00124 filePane.add(inputFilename);
00125 filePane.add(setButton);
00126
00127 String[][] tableData = new String[MAX_MOTES][NUM_COLS];
00128 table = new JTable(tableData, columnNames);
00129 JScrollPane scrollpane = new JScrollPane(table);
00130
00131 pluginPanel.add(startButton);
00132 pluginPanel.add(stopButton);
00133 pluginPanel.add(filePane);
00134 pluginPanel.add(scrollpane);
00135 pluginPanel.revalidate();
00136 }
00137
00138 public void deregister() {}
00139
00140 public void draw(Graphics graphics) {}
00141
00142 public String toString() {
00143 return "Simulated ADC Readings";
00144 }
00145
00146 public static String getExtension(File f) {
00147 String ext = null;
00148 String s = f.getName();
00149 int i = s.lastIndexOf('.');
00150
00151 if (i > 0 && i < s.length() - 1)
00152 ext = s.substring(i+1).toLowerCase();
00153
00154 return ext;
00155 }
00156
00157
00158 class SimADCStarter implements ActionListener {
00159 SimADCPlugin plug;
00160
00161 public SimADCStarter(SimADCPlugin parent) {
00162 plug = parent;
00163 }
00164
00165 public void actionPerformed(ActionEvent evt) {
00166 String filename = plug.inputFilename.getText();
00167 if (filename == "")
00168 return;
00169
00170 if (plug.debug)
00171 System.out.println("\tStart ADC simulation");
00172 try {
00173 plug.sim = Runtime.getRuntime().exec(plug.exe +" "+ filename);
00174 } catch (IOException e) {
00175 e.printStackTrace();
00176 }
00177 }
00178 }
00179
00180
00181 class SimADCStopper implements ActionListener {
00182 SimADCPlugin plug;
00183
00184 public SimADCStopper(SimADCPlugin parent) {
00185 plug = parent;
00186 }
00187
00188 public void actionPerformed(ActionEvent evt) {
00189 if (plug.debug)
00190 System.out.println("\tStop ADC simulation");
00191 try {
00192 if (plug.sim != null) {
00193 plug.sim.destroy();
00194 plug.sim = null;
00195 }
00196 Runtime.getRuntime().exec("killall " + plug.exe);
00197 } catch (IOException e) {
00198 e.printStackTrace();
00199 }
00200 }
00201 }
00202
00203
00204 class ADCFileFilter extends javax.swing.filechooser.FileFilter {
00205 public boolean accept(File f) {
00206 if (f.isDirectory())
00207 return true;
00208
00209 String extension = SimADCPlugin.getExtension(f);
00210 if (extension != null && extension.equals("adc"))
00211 return true;
00212
00213 return false;
00214 }
00215
00216 public String getDescription() {
00217 return "ADC simulation files";
00218 }
00219 }
00220
00221
00222 class FileSelectListener implements ActionListener {
00223 JPanel parent;
00224 SimADCPlugin plug;
00225
00226 public FileSelectListener(SimADCPlugin sim, JPanel panel) {
00227 plug = sim;
00228 parent = panel;
00229 }
00230
00231 public void actionPerformed(ActionEvent e) {
00232 String filename;
00233 JFileChooser fileChooser = new JFileChooser();
00234 fileChooser.setDialogTitle("ADC Simulation Data File");
00235 fileChooser.addChoosableFileFilter(new ADCFileFilter());
00236 fileChooser.setAcceptAllFileFilterUsed(false);
00237 fileChooser.setCurrentDirectory(new File("sensix_directory"));
00238
00239 if (fileChooser.showOpenDialog(parent) ==
00240 JFileChooser.APPROVE_OPTION) {
00241 filename = fileChooser.getSelectedFile().getAbsolutePath();
00242 plug.inputFilename.setText(filename);
00243 }
00244 }
00245 }
00246 }