auto-sync
This commit is contained in:
parent
b57e11871d
commit
ee48146c4b
|
@ -89,9 +89,9 @@ public class IoUtil {
|
|||
long time = System.currentTimeMillis();
|
||||
|
||||
final CountDownLatch rpmLatch = new CountDownLatch(1);
|
||||
SensorCentral.AdcListener listener = new SensorCentral.AdcListener() {
|
||||
SensorCentral.SensorListener listener = new SensorCentral.SensorListener() {
|
||||
@Override
|
||||
public void onAdcUpdate(SensorCentral model, double value) {
|
||||
public void onSensorUpdate(double value) {
|
||||
double actualRpm = SensorCentral.getInstance().getValue(Sensor.RPM);
|
||||
if (isCloseEnough(rpm, actualRpm))
|
||||
rpmLatch.countDown();
|
||||
|
@ -115,9 +115,9 @@ public class IoUtil {
|
|||
static void waitForFirstResponse() throws InterruptedException {
|
||||
FileLog.MAIN.logLine("Let's give it some time to start...");
|
||||
final CountDownLatch startup = new CountDownLatch(1);
|
||||
SensorCentral.AdcListener listener = new SensorCentral.AdcListener() {
|
||||
SensorCentral.SensorListener listener = new SensorCentral.SensorListener() {
|
||||
@Override
|
||||
public void onAdcUpdate(SensorCentral model, double value) {
|
||||
public void onSensorUpdate(double value) {
|
||||
startup.countDown();
|
||||
}
|
||||
};
|
||||
|
|
|
@ -22,7 +22,7 @@ public class FileUtils {
|
|||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
String packed = EngineState.packString(line);
|
||||
engineState.append(packed + "\r\n");
|
||||
engineState.processNewData(packed + "\r\n");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException(e);
|
||||
|
|
|
@ -39,7 +39,7 @@ class PortHolder {
|
|||
open(port, new DataListener() {
|
||||
public void onStringArrived(String string) {
|
||||
// jTextAreaIn.append(string);
|
||||
es.append(string);
|
||||
es.processNewData(string);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -83,7 +83,7 @@ public class TcpConnector implements LinkConnector {
|
|||
while (true) {
|
||||
try {
|
||||
String line = reader.readLine();
|
||||
LinkManager.engineState.append(line + "\r\n");
|
||||
LinkManager.engineState.processNewData(line + "\r\n");
|
||||
} catch (IOException e) {
|
||||
System.err.println("End of connection");
|
||||
return;
|
||||
|
|
|
@ -14,7 +14,6 @@ import java.util.concurrent.CopyOnWriteArrayList;
|
|||
* @see #registerStringValueAction
|
||||
*/
|
||||
public class EngineState {
|
||||
public static final String RPM_KEY = "rpm";
|
||||
public static final String SEPARATOR = ",";
|
||||
public static final int SNIFFED_ADC_COUNT = 16;
|
||||
public static final ValueCallback<String> NOTHING = new ValueCallback<String>() {
|
||||
|
@ -29,7 +28,7 @@ public class EngineState {
|
|||
private static final CharSequence TS_PROTOCOL_TAG = "ts_p_al";
|
||||
private final Object lock = new Object();
|
||||
|
||||
static class StringActionPair extends Pair<String, ValueCallback<String>> {
|
||||
private static class StringActionPair extends Pair<String, ValueCallback<String>> {
|
||||
public final String prefix;
|
||||
|
||||
StringActionPair(String key, ValueCallback<String> second) {
|
||||
|
@ -43,7 +42,7 @@ public class EngineState {
|
|||
}
|
||||
}
|
||||
|
||||
public List<EngineTimeListener> timeListeners = new CopyOnWriteArrayList<EngineTimeListener>();
|
||||
public final List<EngineTimeListener> timeListeners = new CopyOnWriteArrayList<EngineTimeListener>();
|
||||
|
||||
private final ResponseBuffer buffer;
|
||||
private final List<StringActionPair> actions = new ArrayList<StringActionPair>();
|
||||
|
@ -68,62 +67,17 @@ public class EngineState {
|
|||
}
|
||||
);
|
||||
|
||||
SensorCentral.getInstance().initialize(this);
|
||||
|
||||
// SensorStats.start(Sensor.CLT, Sensor.COOLANT_WIDTH);
|
||||
// SensorStats.start(Sensor.IAT, Sensor.INTAKE_AIR_WIDTH);
|
||||
SensorStats.start(Sensor.VREF, Sensor.VREF_WIDTH);
|
||||
|
||||
addDoubleSensor(RPM_KEY, Sensor.RPM);
|
||||
addDoubleSensor("mat", Sensor.IAT);
|
||||
addDoubleSensor("map", Sensor.MAP);
|
||||
addDoubleSensor("map_r", Sensor.MAP_RAW);
|
||||
addDoubleSensor("baro", Sensor.BARO);
|
||||
addDoubleSensor("clt", Sensor.CLT);
|
||||
addDoubleSensor("tp", Sensor.TPS);
|
||||
addDoubleSensor(Sensor.DWELL0);
|
||||
addDoubleSensor(Sensor.DWELL1);
|
||||
addDoubleSensor(Sensor.TOTAL_DWELL0);
|
||||
addDoubleSensor(Sensor.TOTAL_DWELL1);
|
||||
addDoubleSensor("tch", Sensor.T_CHARGE);
|
||||
addDoubleSensor("afr", Sensor.AFR);
|
||||
addDoubleSensor("d_fuel", Sensor.DEFAULT_FUEL);
|
||||
addDoubleSensor("fuel", Sensor.FUEL);
|
||||
addDoubleSensor("fuel_base", Sensor.FUEL_BASE);
|
||||
addDoubleSensor("fuel_lag", Sensor.FUEL_LAG);
|
||||
addDoubleSensor("fuel_clt", Sensor.FUEL_CLT);
|
||||
addDoubleSensor("fuel_iat", Sensor.FUEL_IAT);
|
||||
addDoubleSensor("table_spark", Sensor.TABLE_SPARK);
|
||||
addDoubleSensor("advance0", Sensor.ADVANCE0);
|
||||
addDoubleSensor("advance1", Sensor.ADVANCE1);
|
||||
addDoubleSensor("vref", Sensor.VREF);
|
||||
addDoubleSensor("vbatt", Sensor.VBATT);
|
||||
addDoubleSensor("maf", Sensor.MAF);
|
||||
addDoubleSensor("period0", Sensor.PERIOD0);
|
||||
addDoubleSensor("period1", Sensor.PERIOD0);
|
||||
addDoubleSensor("duty0", Sensor.DUTY0);
|
||||
addDoubleSensor("duty1", Sensor.DUTY1);
|
||||
addDoubleSensor("timing", Sensor.TIMING);
|
||||
addDoubleSensor(Sensor.TRG_0_DUTY);
|
||||
addDoubleSensor(Sensor.TRG_1_DUTY);
|
||||
|
||||
addDoubleSensor("idl", Sensor.IDLE_SWITCH);
|
||||
|
||||
addDoubleSensor("chart", Sensor.CHART_STATUS, true);
|
||||
addDoubleSensor("chartsize", Sensor.CHARTSIZE, true);
|
||||
addDoubleSensor("adcDebug", Sensor.ADC_STATUS, true);
|
||||
addDoubleSensor("adcfast", Sensor.ADC_FAST);
|
||||
addDoubleSensor("adcfastavg", Sensor.ADC_FAST_AVG);
|
||||
registerStringValueAction("adcfast_co", NOTHING);
|
||||
registerStringValueAction("adcfast_max", NOTHING);
|
||||
registerStringValueAction("adcfast_min", NOTHING);
|
||||
registerStringValueAction("key", NOTHING);
|
||||
registerStringValueAction("value", NOTHING);
|
||||
// addDoubleSensor("adcfast_co", Sensor.ADC_FAST_AVG);
|
||||
|
||||
addDoubleSensor("injector0", Sensor.INJECTOR_0_STATUS, true);
|
||||
addDoubleSensor("injector1", Sensor.INJECTOR_1_STATUS, true);
|
||||
addDoubleSensor("injector2", Sensor.INJECTOR_2_STATUS, true);
|
||||
addDoubleSensor("injector3", Sensor.INJECTOR_3_STATUS, true);
|
||||
|
||||
registerStringValueAction("msg", new ValueCallback<String>() {
|
||||
@Override
|
||||
|
@ -157,7 +111,6 @@ public class EngineState {
|
|||
registerStringValueAction("i_p", NOTHING);
|
||||
registerStringValueAction("a_time", NOTHING);
|
||||
|
||||
|
||||
registerStringValueAction("time", new ValueCallback<String>() {
|
||||
public void onUpdate(String value) {
|
||||
double time;
|
||||
|
@ -173,35 +126,6 @@ public class EngineState {
|
|||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Sensor enum name matches the protocol key
|
||||
*/
|
||||
private void addDoubleSensor(Sensor sensor) {
|
||||
addDoubleSensor(sensor.name(), sensor);
|
||||
}
|
||||
|
||||
private void addDoubleSensor(String key, final Sensor sensor) {
|
||||
addDoubleSensor(key, sensor, false);
|
||||
}
|
||||
|
||||
private void addDoubleSensor(final String key, final Sensor sensor, final boolean verbose) {
|
||||
registerStringValueAction(key, new ValueCallback<String>() {
|
||||
@Override
|
||||
public void onUpdate(String stringValue) {
|
||||
double value;
|
||||
try {
|
||||
value = Double.parseDouble(stringValue);
|
||||
} catch (NumberFormatException e) {
|
||||
// serial protocol is not safe
|
||||
return;
|
||||
}
|
||||
SensorCentral.getInstance().setValue(value, sensor);
|
||||
if (verbose)
|
||||
MessagesCentral.getInstance().postMessage(EngineState.class, key + "=" + value);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @see #unpackString(String)
|
||||
*/
|
||||
|
@ -210,8 +134,11 @@ public class EngineState {
|
|||
}
|
||||
|
||||
/**
|
||||
* This method extract the content of a 'line with known length' packet
|
||||
* <p/>
|
||||
* serial protocol is not error-prone, so our simple approach is to validate the length of incoming strings
|
||||
*
|
||||
* @return null in case of error, line message if valid packed ine
|
||||
* @see #packString(String)
|
||||
*/
|
||||
public static String unpackString(String message) {
|
||||
|
@ -340,7 +267,7 @@ public class EngineState {
|
|||
}
|
||||
}
|
||||
|
||||
public void append(String append) {
|
||||
public void processNewData(String append) {
|
||||
buffer.append(append);
|
||||
}
|
||||
|
||||
|
|
|
@ -6,18 +6,19 @@ import java.util.Map;
|
|||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
/**
|
||||
* This class keeps track of {@link Sensor} current values and {@link SensorCentral.SensorListener}
|
||||
* <p/>
|
||||
* <p/>
|
||||
* Date: 1/6/13
|
||||
* (c) Andrey Belomutskiy
|
||||
*/
|
||||
public class SensorCentral {
|
||||
public static final String RPM_KEY = "rpm";
|
||||
private static final SensorCentral INSTANCE = new SensorCentral();
|
||||
|
||||
private final Map<Sensor, Double> values = new EnumMap<Sensor, Double>(Sensor.class);
|
||||
|
||||
private final Map<Sensor, List<AdcListener>> allListeners = new EnumMap<Sensor, List<AdcListener>>(Sensor.class);
|
||||
|
||||
|
||||
// private final List<AdcListener> listeners = new CopyOnWriteArrayList<AdcListener>();
|
||||
private final Map<Sensor, List<SensorListener>> allListeners = new EnumMap<Sensor, List<SensorListener>>(Sensor.class);
|
||||
|
||||
public static SensorCentral getInstance() {
|
||||
return INSTANCE;
|
||||
|
@ -26,18 +27,6 @@ public class SensorCentral {
|
|||
private SensorCentral() {
|
||||
}
|
||||
|
||||
// public static String getTitle(int i) {
|
||||
// Sensor sensor = Sensor.findByMazdaIndex(i);
|
||||
// return "adc " + i + sensor.getName();
|
||||
// }
|
||||
|
||||
// public String getSniffedAdcRepresentation(Sensor channel) {
|
||||
// double value = getValue(channel);
|
||||
//
|
||||
// double volts = 5 * value / 255;
|
||||
// return "" + value + " (" + volts + ")";
|
||||
// }
|
||||
|
||||
public double getValue(Sensor sensor) {
|
||||
Double value = values.get(sensor);
|
||||
if (value == null)
|
||||
|
@ -47,14 +36,14 @@ public class SensorCentral {
|
|||
|
||||
public void setValue(double value, Sensor sensor) {
|
||||
values.put(sensor, value);
|
||||
List<AdcListener> listeners;
|
||||
List<SensorListener> listeners;
|
||||
synchronized (allListeners) {
|
||||
listeners = allListeners.get(sensor);
|
||||
}
|
||||
if (listeners == null)
|
||||
return;
|
||||
for (AdcListener listener : listeners)
|
||||
listener.onAdcUpdate(this, value);
|
||||
for (SensorListener listener : listeners)
|
||||
listener.onSensorUpdate(value);
|
||||
}
|
||||
|
||||
public static String getInternalAdcRepresentation(double value) {
|
||||
|
@ -62,19 +51,19 @@ public class SensorCentral {
|
|||
return String.format("%.2f (%.2fv)", value, volts);
|
||||
}
|
||||
|
||||
public void addListener(Sensor sensor, AdcListener listener) {
|
||||
List<AdcListener> listeners;
|
||||
public void addListener(Sensor sensor, SensorListener listener) {
|
||||
List<SensorListener> listeners;
|
||||
synchronized (allListeners) {
|
||||
listeners = allListeners.get(sensor);
|
||||
if (listeners == null)
|
||||
listeners = new CopyOnWriteArrayList<AdcListener>();
|
||||
listeners = new CopyOnWriteArrayList<SensorListener>();
|
||||
allListeners.put(sensor, listeners);
|
||||
}
|
||||
listeners.add(listener);
|
||||
}
|
||||
|
||||
public void removeListener(Sensor sensor, AdcListener listener) {
|
||||
List<AdcListener> listeners;
|
||||
public void removeListener(Sensor sensor, SensorListener listener) {
|
||||
List<SensorListener> listeners;
|
||||
synchronized (allListeners) {
|
||||
listeners = allListeners.get(sensor);
|
||||
}
|
||||
|
@ -82,7 +71,84 @@ public class SensorCentral {
|
|||
listeners.remove(listener);
|
||||
}
|
||||
|
||||
public interface AdcListener {
|
||||
void onAdcUpdate(SensorCentral model, double value);
|
||||
public void initialize(EngineState es) {
|
||||
addDoubleSensor("injector0", Sensor.INJECTOR_0_STATUS, true, es);
|
||||
addDoubleSensor("injector1", Sensor.INJECTOR_1_STATUS, true, es);
|
||||
addDoubleSensor("injector2", Sensor.INJECTOR_2_STATUS, true, es);
|
||||
addDoubleSensor("injector3", Sensor.INJECTOR_3_STATUS, true, es);
|
||||
|
||||
addDoubleSensor(RPM_KEY, Sensor.RPM, es);
|
||||
addDoubleSensor("mat", Sensor.IAT, es);
|
||||
addDoubleSensor("map", Sensor.MAP, es);
|
||||
addDoubleSensor("map_r", Sensor.MAP_RAW, es);
|
||||
addDoubleSensor("baro", Sensor.BARO, es);
|
||||
addDoubleSensor("clt", Sensor.CLT, es);
|
||||
addDoubleSensor("tp", Sensor.TPS, es);
|
||||
addDoubleSensor(Sensor.DWELL0, es);
|
||||
addDoubleSensor(Sensor.DWELL1, es);
|
||||
addDoubleSensor(Sensor.TOTAL_DWELL0, es);
|
||||
addDoubleSensor(Sensor.TOTAL_DWELL1, es);
|
||||
addDoubleSensor("tch", Sensor.T_CHARGE, es);
|
||||
addDoubleSensor(Sensor.AFR, es);
|
||||
addDoubleSensor("d_fuel", Sensor.DEFAULT_FUEL, es);
|
||||
addDoubleSensor(Sensor.FUEL, es);
|
||||
addDoubleSensor(Sensor.FUEL_BASE, es);
|
||||
addDoubleSensor(Sensor.FUEL_LAG, es);
|
||||
addDoubleSensor(Sensor.FUEL_CLT, es);
|
||||
addDoubleSensor(Sensor.FUEL_IAT, es);
|
||||
addDoubleSensor(Sensor.TABLE_SPARK, es);
|
||||
addDoubleSensor(Sensor.ADVANCE0, es);
|
||||
addDoubleSensor(Sensor.ADVANCE1, es);
|
||||
addDoubleSensor(Sensor.VREF, es);
|
||||
addDoubleSensor(Sensor.VBATT, es);
|
||||
addDoubleSensor(Sensor.MAF, es);
|
||||
addDoubleSensor("period0", Sensor.PERIOD0, es);
|
||||
addDoubleSensor("period1", Sensor.PERIOD0, es);
|
||||
addDoubleSensor("duty0", Sensor.DUTY0, es);
|
||||
addDoubleSensor("duty1", Sensor.DUTY1, es);
|
||||
addDoubleSensor("timing", Sensor.TIMING, es);
|
||||
addDoubleSensor(Sensor.TRG_0_DUTY, es);
|
||||
addDoubleSensor(Sensor.TRG_1_DUTY, es);
|
||||
|
||||
addDoubleSensor("idl", Sensor.IDLE_SWITCH, es);
|
||||
|
||||
addDoubleSensor("chart", Sensor.CHART_STATUS, true, es);
|
||||
addDoubleSensor("chartsize", Sensor.CHARTSIZE, true, es);
|
||||
addDoubleSensor("adcDebug", Sensor.ADC_STATUS, true, es);
|
||||
addDoubleSensor("adcfast", Sensor.ADC_FAST, es);
|
||||
addDoubleSensor("adcfastavg", Sensor.ADC_FAST_AVG, es);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sensor enum name matches the protocol key
|
||||
*/
|
||||
private void addDoubleSensor(Sensor sensor, EngineState es) {
|
||||
addDoubleSensor(sensor.name(), sensor, es);
|
||||
}
|
||||
|
||||
private void addDoubleSensor(String key, final Sensor sensor, EngineState es) {
|
||||
addDoubleSensor(key, sensor, false, es);
|
||||
}
|
||||
|
||||
private void addDoubleSensor(final String key, final Sensor sensor, final boolean verbose, EngineState es) {
|
||||
es.registerStringValueAction(key, new EngineState.ValueCallback<String>() {
|
||||
@Override
|
||||
public void onUpdate(String stringValue) {
|
||||
double value;
|
||||
try {
|
||||
value = Double.parseDouble(stringValue);
|
||||
} catch (NumberFormatException e) {
|
||||
// serial protocol is not safe
|
||||
return;
|
||||
}
|
||||
setValue(value, sensor);
|
||||
if (verbose)
|
||||
MessagesCentral.getInstance().postMessage(EngineState.class, key + "=" + value);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public interface SensorListener {
|
||||
void onSensorUpdate(double value);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,14 +7,14 @@ package com.irnems.core;
|
|||
public class SensorStats {
|
||||
public static void start(final Sensor source, final Sensor destination) {
|
||||
|
||||
SensorCentral.getInstance().addListener(source, new SensorCentral.AdcListener() {
|
||||
SensorCentral.getInstance().addListener(source, new SensorCentral.SensorListener() {
|
||||
|
||||
int counter;
|
||||
double min = Double.MAX_VALUE;
|
||||
double max = Double.MIN_VALUE;
|
||||
|
||||
@Override
|
||||
public void onAdcUpdate(SensorCentral model, double value) {
|
||||
public void onSensorUpdate(double value) {
|
||||
counter++;
|
||||
|
||||
min = Math.min(value, min);
|
||||
|
|
|
@ -38,10 +38,10 @@ public class EngineStateTest {
|
|||
rpmResult.set(Integer.parseInt(value));
|
||||
}
|
||||
});
|
||||
es.append("line:7:");
|
||||
es.append(EngineState.RPM_KEY + SEPARATOR);
|
||||
es.processNewData("line:7:");
|
||||
es.processNewData(SensorCentral.RPM_KEY + SEPARATOR);
|
||||
assertEquals(0, rpmResult.get());
|
||||
es.append("600\r");
|
||||
es.processNewData("600\r");
|
||||
assertEquals(600, rpmResult.get());
|
||||
}
|
||||
|
||||
|
|
|
@ -19,7 +19,7 @@ import javax.swing.*;
|
|||
* @see WavePanel
|
||||
*/
|
||||
public class Launcher extends FrameHelper {
|
||||
public static final int CONSOLE_VERSION = 20140829;
|
||||
public static final int CONSOLE_VERSION = 20140831;
|
||||
public static final boolean SHOW_STIMULATOR = true;
|
||||
|
||||
public Launcher(String port) {
|
||||
|
|
|
@ -65,7 +65,7 @@ public class AdcPanel {
|
|||
|
||||
// private Pair<JPanel, JLabel> createAdcLabel(final int channel) {
|
||||
// final Pair<JPanel, JLabel> result = createLabelWithCaption(SensorCentral.getTitle(channel));
|
||||
// model.addListener(new SensorCentral.AdcListener() {
|
||||
// model.addListener(new SensorCentral.SensorListener() {
|
||||
// public void onAdcUpdate(SensorCentral model, Sensor sensor, double value) {
|
||||
// if (sensor.getMazdaIndex() != channel)
|
||||
// return;
|
||||
|
|
|
@ -26,9 +26,9 @@ public class RpmModel {
|
|||
}
|
||||
|
||||
private RpmModel() {
|
||||
SensorCentral.getInstance().addListener(Sensor.RPM, new SensorCentral.AdcListener() {
|
||||
SensorCentral.getInstance().addListener(Sensor.RPM, new SensorCentral.SensorListener() {
|
||||
@Override
|
||||
public void onAdcUpdate(SensorCentral model, double value) {
|
||||
public void onSensorUpdate(double value) {
|
||||
setValue((int) value);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -40,9 +40,9 @@ public class BooleanFlagControlPanel {
|
|||
}
|
||||
|
||||
protected void installStatusReader(final Sensor statusSensor) {
|
||||
SensorCentral.getInstance().addListener(statusSensor, new SensorCentral.AdcListener() {
|
||||
SensorCentral.getInstance().addListener(statusSensor, new SensorCentral.SensorListener() {
|
||||
@Override
|
||||
public void onAdcUpdate(SensorCentral model, double value) {
|
||||
public void onSensorUpdate(double value) {
|
||||
checkBox.setSelected(value > 0);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -11,9 +11,9 @@ import javax.swing.*;
|
|||
*/
|
||||
public class IdleLabel extends JLabel {
|
||||
public IdleLabel() {
|
||||
SensorCentral.getInstance().addListener(Sensor.IDLE_SWITCH, new SensorCentral.AdcListener() {
|
||||
SensorCentral.getInstance().addListener(Sensor.IDLE_SWITCH, new SensorCentral.SensorListener() {
|
||||
@Override
|
||||
public void onAdcUpdate(SensorCentral model, double value) {
|
||||
public void onSensorUpdate(double value) {
|
||||
IdleLabel.this.setText("Idle: " + (value == 0));
|
||||
}
|
||||
});
|
||||
|
|
|
@ -47,8 +47,8 @@ public class SensorGauge {
|
|||
|
||||
gauge.setBackgroundColor(sensor.getColor());
|
||||
|
||||
SensorCentral.getInstance().addListener(sensor, new SensorCentral.AdcListener() {
|
||||
public void onAdcUpdate(SensorCentral model, double value) {
|
||||
SensorCentral.getInstance().addListener(sensor, new SensorCentral.SensorListener() {
|
||||
public void onSensorUpdate(double value) {
|
||||
gauge.setValue(value);
|
||||
}
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue