Skip to content

Commit

Permalink
cleanup casting of conversions/adapters
Browse files Browse the repository at this point in the history
* rounding errors because (float)casting/conversion inbetween
  • Loading branch information
fr3ts0n committed Jun 20, 2015
1 parent e6fabb6 commit 018b9c6
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 36 deletions.
9 changes: 4 additions & 5 deletions src/com/fr3ts0n/ecu/BitmapConversion.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,16 @@ public void initFromStrings(String[] initData)
hashData.put(key, value);
}
}

}

public Number memToPhys(long value)
{
return (float) value;
return value;
}

public Number physToMem(Number value)
{
return (value);
return value;
}

@Override
Expand All @@ -111,8 +110,8 @@ public String physToPhysFmtString(Number physVal, String format)
result += "\n";
// now add the result
result += String.format("%s %s",
((val & item.getKey()) != 0) ? "(*)" : "( )",
item.getValue() );
((val & item.getKey()) != 0) ? "(*)" : "( )",
item.getValue() );
}
// if we haven't found a string representation, return numeric value
if (result == null) result = super.physToPhysFmtString(physVal,format);
Expand Down
14 changes: 7 additions & 7 deletions src/com/fr3ts0n/ecu/EcuDataItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ public EcuDataItem( int newPid,
int numBytes,
Conversion[] conversions,
String format,
Float minValue,
Float maxValue,
Number minValue,
Number maxValue,
String labelText)
{
pid = newPid;
Expand All @@ -98,8 +98,8 @@ public EcuDataItem( int newPid,
fmt = format;
label = labelText;
pv = new EcuDataPv();
Float minVal = minValue;
Float maxVal = maxValue;
Number minVal = minValue;
Number maxVal = maxValue;

// initialize new PID with current data
pv.put(EcuDataPv.FID_PID, Integer.valueOf(pid));
Expand All @@ -110,8 +110,8 @@ public EcuDataItem( int newPid,
pv.put(EcuDataPv.FID_CNVID, cnv);
if(cnv != null)
{
if(minVal == null) minVal = (Float) cnv[cnvSystem].memToPhys(0);
if(maxVal == null) maxVal = (Float) cnv[cnvSystem].memToPhys(byteValues[bytes]);
if(minVal == null) minVal = cnv[cnvSystem].memToPhys(0);
if(maxVal == null) maxVal = cnv[cnvSystem].memToPhys(byteValues[bytes]);
}
pv.put(EcuDataPv.FID_MIN, minVal);
pv.put(EcuDataPv.FID_MAX, maxVal);
Expand Down Expand Up @@ -145,7 +145,7 @@ Object physFromBuffer(char[] buffer)
} catch(Exception ex)
{
result = String.format("%s:%s", ProtUtils.hexDumpBuffer(buffer), ex.getMessage());
log.error(String.format("%s: %s", toString(), result));
log.warn(String.format("%s: %s", toString(), result));
}
return (result);
}
Expand Down
4 changes: 2 additions & 2 deletions src/com/fr3ts0n/ecu/HashConversion.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ public void initFromStrings(String[] initData)

public Number memToPhys(long value)
{
return (float) value;
return value;
}

public Number physToMem(Number value)
{
return (value);
return value;
}

@Override
Expand Down
5 changes: 2 additions & 3 deletions src/com/fr3ts0n/ecu/IntConversion.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,13 @@ public class IntConversion
@Override
public Number memToPhys(long value)
{
return (float) value;

return value;
}

@Override
public Number physToMem(Number value)
{
return value.longValue();
return value;
}

@Override
Expand Down
12 changes: 6 additions & 6 deletions src/com/fr3ts0n/ecu/gui/androbd/ObdGaugeAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void add(EcuDataPv currPv)
{
category = new CategorySeries(String.valueOf(currPv.get(EcuDataPv.FID_DESCRIPT)));
category.add(String.valueOf(currPv.get(EcuDataPv.FID_UNITS)),
Double.valueOf(String.valueOf(currPv.get(EcuDataPv.FID_VALUE))));
((Number)currPv.get(EcuDataPv.FID_VALUE)).doubleValue());
currPv.put(FID_GAUGE_SERIES, category);
}
// make this adapter to listen for PV data updates
Expand Down Expand Up @@ -129,8 +129,8 @@ public View getView(int position, View convertView, ViewGroup parent)
holder.tvDescr = (TextView) convertView.findViewById(R.id.label);
holder.gauge = (FrameLayout) convertView.findViewById(R.id.gauge);

Float minValue = (Float) currPv.get(EcuDataPv.FID_MIN);
Float maxValue = (Float) currPv.get(EcuDataPv.FID_MAX);
Number minValue = (Number) currPv.get(EcuDataPv.FID_MIN);
Number maxValue = (Number) currPv.get(EcuDataPv.FID_MAX);
if (minValue == null) minValue = 0f;
if (maxValue == null) maxValue = 255f;

Expand All @@ -147,8 +147,8 @@ public View getView(int position, View convertView, ViewGroup parent)

renderer.setVisualTypes(new DialRenderer.Type[]{DialRenderer.Type.NEEDLE});

renderer.setMinValue(minValue);
renderer.setMaxValue(maxValue);
renderer.setMinValue(minValue.doubleValue());
renderer.setMaxValue(maxValue.doubleValue());

// renderer.setChartTitleTextSize(24);
renderer.setChartTitle(currPv.getUnits());
Expand Down Expand Up @@ -188,6 +188,6 @@ public void pvChanged(PvChangeEvent event)
CategorySeries series = (CategorySeries) currPv.get(FID_GAUGE_SERIES);
series.set(0,
String.valueOf(currPv.get(EcuDataPv.FID_UNITS)),
(Float) event.getValue());
((Number)event.getValue()).doubleValue());
}
}
20 changes: 7 additions & 13 deletions src/com/fr3ts0n/ecu/gui/androbd/ObdItemAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@

import java.util.Arrays;
import java.util.Comparator;
import java.util.Iterator;

/**
* Adapter for OBD data items (PVs)
Expand Down Expand Up @@ -123,8 +122,8 @@ public View getView(int position, View convertView, ViewGroup parent)
String fmtText;
Object colVal = currPv.get(EcuDataPv.FID_VALUE);
Object cnvObj = currPv.get(EcuDataPv.FID_CNVID);
Float min = (Float) currPv.get(EcuDataPv.FID_MIN);
Float max = (Float) currPv.get(EcuDataPv.FID_MAX);
Number min = (Number) currPv.get(EcuDataPv.FID_MIN);
Number max = (Number) currPv.get(EcuDataPv.FID_MAX);
int pid = currPv.getAsInt(EcuDataPv.FID_PID);

try
Expand All @@ -134,16 +133,16 @@ public View getView(int position, View convertView, ViewGroup parent)
Conversion[] cnv;
cnv = (Conversion[]) cnvObj;
// set formatted text
fmtText = cnv[EcuDataItem.cnvSystem].physToPhysFmtString( (Float)colVal,
String.valueOf(currPv.get(EcuDataPv.FID_FORMAT)));
fmtText = cnv[EcuDataItem.cnvSystem].physToPhysFmtString((Number)colVal,
(String)currPv.get(EcuDataPv.FID_FORMAT));
// set progress bar only on LinearConversion
if( min != null
&& max != null
&& cnv[EcuDataItem.cnvSystem] instanceof LinearConversion)
{
pb.setVisibility(ProgressBar.VISIBLE);
pb.getProgressDrawable().setColorFilter(ChartActivity.getColor(pid), PorterDuff.Mode.SRC_IN);
pb.setProgress((int)(100 * (((Float)colVal - min) / (max - min))));
pb.setProgress((int)(100 * ((((Number)colVal).doubleValue() - min.doubleValue()) / (max.doubleValue() - min.doubleValue()))));
}
else
{
Expand All @@ -169,15 +168,10 @@ public View getView(int position, View convertView, ViewGroup parent)
*/
protected synchronized void addAllDataSeries()
{
IndexedProcessVar pv;
@SuppressWarnings("unchecked")
Iterator<IndexedProcessVar> it = pvs.values().iterator();
while (it.hasNext())
for (IndexedProcessVar pv : (Iterable<IndexedProcessVar>) pvs.values())
{
pv = it.next();
XYSeries series = (XYSeries) pv.get(FID_DATA_SERIES);
if (series == null)
{
if (series == null) {
series = new XYSeries(String.valueOf(pv.get(EcuDataPv.FID_DESCRIPT)));
pv.put(FID_DATA_SERIES, series);
pv.addPvChangeListener(this, PvChangeEvent.PV_MODIFIED);
Expand Down

0 comments on commit 018b9c6

Please sign in to comment.