-
Notifications
You must be signed in to change notification settings - Fork 808
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Consider ordering list of sensors alphabetically #2597 #2599
base: development
Are you sure you want to change the base?
Changes from all commits
1c27f92
4ff8ce3
584f6c9
9321d17
d1a92ae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
@@ -40,16 +41,11 @@ | |
import io.pslab.sensors.SensorTSL2561; | ||
import io.pslab.sensors.SensorVL53L0X; | ||
|
||
/** | ||
* Created by asitava on 18/6/17. | ||
*/ | ||
|
||
public class SensorActivity extends GuideActivity { | ||
|
||
private I2C i2c; | ||
private ScienceLab scienceLab; | ||
private final Map<Integer, String> sensorAddr = new LinkedHashMap<>(); | ||
private final List<String> dataAddress = new ArrayList<>(); | ||
private final Map<Integer, List<String>> sensorAddr = new LinkedHashMap<>(); | ||
private final List<String> dataName = new ArrayList<>(); | ||
private ArrayAdapter<String> adapter; | ||
private ListView lvSensor; | ||
|
@@ -74,17 +70,19 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { | |
} | ||
|
||
i2c = scienceLab.i2c; | ||
sensorAddr.put(0x48, "ADS1115"); | ||
sensorAddr.put(0x77, "BMP180"); | ||
sensorAddr.put(0x5A, "MLX90614"); | ||
sensorAddr.put(0x1E, "HMC5883L"); | ||
sensorAddr.put(0x68, "MPU6050"); | ||
sensorAddr.put(0x40, "SHT21"); | ||
sensorAddr.put(0x39, "TSL2561"); | ||
sensorAddr.put(0x69, "MPU925x"); | ||
sensorAddr.put(0x29, "VL53L0X"); | ||
sensorAddr.put(0x5A, "CCS811"); | ||
sensorAddr.put(0x39, "APDS9960"); | ||
|
||
// Initialize the sensor map | ||
addSensorToMap(0x48, "ADS1115"); | ||
addSensorToMap(0x77, "BMP180"); | ||
addSensorToMap(0x5A, "MLX90614"); | ||
addSensorToMap(0x5A, "CCS811"); // Duplicate address | ||
addSensorToMap(0x1E, "HMC5883L"); | ||
addSensorToMap(0x68, "MPU6050"); | ||
addSensorToMap(0x40, "SHT21"); | ||
addSensorToMap(0x39, "TSL2561"); | ||
addSensorToMap(0x39, "APDS9960"); // Duplicate address | ||
addSensorToMap(0x69, "MPU925x"); | ||
addSensorToMap(0x29, "VL53L0X"); | ||
|
||
adapter = new ArrayAdapter<>(getApplication(), R.layout.sensor_list_item, R.id.tv_sensor_list_item, dataName); | ||
|
||
|
@@ -99,6 +97,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { | |
tvSensorScan.setText(getResources().getString(R.string.scanning)); | ||
new PopulateSensors().execute(); | ||
}); | ||
|
||
lvSensor.setOnItemClickListener((parent, view, position, id) -> { | ||
String itemValue = (String) lvSensor.getItemAtPosition(position); | ||
Intent intent; | ||
|
@@ -154,17 +153,22 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { | |
}); | ||
} | ||
|
||
private void addSensorToMap(int address, String sensorName) { | ||
// Add the sensor to the map, creating a new list if necessary | ||
sensorAddr.computeIfAbsent(address, k -> new ArrayList<>()).add(sensorName); | ||
} | ||
|
||
private class PopulateSensors extends AsyncTask<Void, Void, Void> { | ||
private List<Integer> data; | ||
private List<Integer> detectedAddresses; | ||
|
||
@Override | ||
protected Void doInBackground(Void... voids) { | ||
data = new ArrayList<>(); | ||
detectedAddresses = new ArrayList<>(); | ||
dataName.clear(); | ||
dataAddress.clear(); | ||
|
||
if (scienceLab.isConnected()) { | ||
try { | ||
data = i2c.scan(null); | ||
detectedAddresses = i2c.scan(null); | ||
} catch (IOException | NullPointerException e) { | ||
e.printStackTrace(); | ||
} | ||
|
@@ -176,31 +180,30 @@ protected Void doInBackground(Void... voids) { | |
protected void onPostExecute(Void aVoid) { | ||
super.onPostExecute(aVoid); | ||
|
||
StringBuilder tvData = new StringBuilder(); | ||
if (data != null) { | ||
for (Integer myInt : data) { | ||
if (myInt != null && sensorAddr.get(myInt) != null) { | ||
dataAddress.add(String.valueOf(myInt)); | ||
if (scienceLab.isConnected() && detectedAddresses != null) { | ||
for (Integer address : detectedAddresses) { | ||
if (sensorAddr.containsKey(address)) { | ||
dataName.addAll(sensorAddr.get(address)); | ||
} | ||
} | ||
|
||
for (final String s : dataAddress) { | ||
tvData.append(s).append(":").append(sensorAddr.get(Integer.parseInt(s))).append("\n"); | ||
} | ||
|
||
} else { | ||
tvData.append(getResources().getString(R.string.sensor_not_connected)); | ||
} | ||
|
||
for (int key : sensorAddr.keySet()) { | ||
dataName.add(sensorAddr.get(key)); | ||
// Add all sensors, even if not detected | ||
for (List<String> sensors : sensorAddr.values()) { | ||
for (String sensor : sensors) { | ||
if (!dataName.contains(sensor)) { | ||
dataName.add(sensor); | ||
} | ||
} | ||
} | ||
Collections.sort(dataName); | ||
Comment on lines
+191
to
+199
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: The UI doesn't distinguish between detected and available sensors, which could be misleading Consider visually indicating which sensors are actually detected vs which ones are just potentially available, perhaps using different styles or grouping in the ListView. Suggested implementation: // Create map of sensor names to their detection status
Map<String, Boolean> sensorStatus = new HashMap<>();
// First add all available sensors as undetected
for (List<String> sensors : sensorAddr.values()) {
for (String sensor : sensors) {
sensorStatus.put(sensor, false);
}
}
// Mark detected sensors
if (detectedAddresses != null) {
for (int address : detectedAddresses) {
if (sensorAddr.containsKey(address)) {
for (String sensor : sensorAddr.get(address)) {
sensorStatus.put(sensor, true);
}
}
}
}
// Create sorted list of sensor names
List<String> sortedSensors = new ArrayList<>(sensorStatus.keySet());
Collections.sort(sortedSensors);
// Create and set custom adapter
SensorListAdapter adapter = new SensorListAdapter(this, sortedSensors, sensorStatus);
listView.setAdapter(adapter);
if (scienceLab.isConnected()) { You'll need to create a new public class SensorListAdapter extends ArrayAdapter<String> {
private final Map<String, Boolean> sensorStatus;
public SensorListAdapter(Context context, List<String> sensors, Map<String, Boolean> status) {
super(context, android.R.layout.simple_list_item_1, sensors);
this.sensorStatus = status;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView text = view.findViewById(android.R.id.text1);
String sensor = getItem(position);
// Style based on detection status
if (sensorStatus.get(sensor)) {
text.setTextColor(ContextCompat.getColor(getContext(), android.R.color.black));
text.setTypeface(null, Typeface.NORMAL);
} else {
text.setTextColor(ContextCompat.getColor(getContext(), android.R.color.darker_gray));
text.setTypeface(null, Typeface.ITALIC);
}
return view;
}
} Also ensure you have a ListView with id |
||
|
||
if (scienceLab.isConnected()) { | ||
tvSensorScan.setText(tvData); | ||
tvSensorScan.setText(getString(R.string.not_connected)); | ||
} else { | ||
tvSensorScan.setText(getString(R.string.not_connected)); | ||
} | ||
|
||
adapter.notifyDataSetChanged(); | ||
buttonSensorAutoScan.setClickable(true); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (bug_risk): The connected state message is incorrect - it always shows 'not_connected' even when scienceLab.isConnected() is true
This could confuse users about the actual connection state of their device. Consider showing the list of detected sensors and their addresses when connected.