Skip to content

Commit 3a0e685

Browse files
Fix:Alphabetical_order_sensor
1 parent d1a92ae commit 3a0e685

File tree

1 file changed

+40
-34
lines changed

1 file changed

+40
-34
lines changed

app/src/main/java/io/pslab/activity/SensorActivity.java

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.io.IOException;
2020
import java.util.ArrayList;
21-
import java.util.Collections;
2221
import java.util.LinkedHashMap;
2322
import java.util.List;
2423
import java.util.Map;
@@ -41,11 +40,16 @@
4140
import io.pslab.sensors.SensorTSL2561;
4241
import io.pslab.sensors.SensorVL53L0X;
4342

43+
/**
44+
* Created by asitava on 18/6/17.
45+
*/
46+
4447
public class SensorActivity extends GuideActivity {
4548

4649
private I2C i2c;
4750
private ScienceLab scienceLab;
4851
private final Map<Integer, List<String>> sensorAddr = new LinkedHashMap<>();
52+
private final List<String> dataAddress = new ArrayList<>();
4953
private final List<String> dataName = new ArrayList<>();
5054
private ArrayAdapter<String> adapter;
5155
private ListView lvSensor;
@@ -71,18 +75,16 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
7175

7276
i2c = scienceLab.i2c;
7377

74-
// Initialize the sensor map
75-
addSensorToMap(0x48, "ADS1115");
76-
addSensorToMap(0x77, "BMP180");
77-
addSensorToMap(0x5A, "MLX90614");
78-
addSensorToMap(0x5A, "CCS811"); // Duplicate address
79-
addSensorToMap(0x1E, "HMC5883L");
80-
addSensorToMap(0x68, "MPU6050");
81-
addSensorToMap(0x40, "SHT21");
82-
addSensorToMap(0x39, "TSL2561");
83-
addSensorToMap(0x39, "APDS9960"); // Duplicate address
84-
addSensorToMap(0x69, "MPU925x");
85-
addSensorToMap(0x29, "VL53L0X");
78+
// Populate sensor addresses with multiple sensors mapped to the same address
79+
sensorAddr.put(0x48, List.of("ADS1115"));
80+
sensorAddr.put(0x77, List.of("BMP180"));
81+
sensorAddr.put(0x5A, List.of("MLX90614", "CCS811"));
82+
sensorAddr.put(0x1E, List.of("HMC5883L"));
83+
sensorAddr.put(0x68, List.of("MPU6050"));
84+
sensorAddr.put(0x40, List.of("SHT21"));
85+
sensorAddr.put(0x39, List.of("TSL2561", "APDS9960"));
86+
sensorAddr.put(0x69, List.of("MPU925x"));
87+
sensorAddr.put(0x29, List.of("VL53L0X"));
8688

8789
adapter = new ArrayAdapter<>(getApplication(), R.layout.sensor_list_item, R.id.tv_sensor_list_item, dataName);
8890

@@ -153,57 +155,61 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
153155
});
154156
}
155157

156-
private void addSensorToMap(int address, String sensorName) {
157-
// Add the sensor to the map, creating a new list if necessary
158-
sensorAddr.computeIfAbsent(address, k -> new ArrayList<>()).add(sensorName);
159-
}
160-
161158
private class PopulateSensors extends AsyncTask<Void, Void, Void> {
162-
private List<Integer> detectedAddresses;
159+
private List<Integer> detectedSensors;
163160

164161
@Override
165162
protected Void doInBackground(Void... voids) {
166-
detectedAddresses = new ArrayList<>();
163+
detectedSensors = new ArrayList<>();
164+
dataAddress.clear();
167165
dataName.clear();
168166

169167
if (scienceLab.isConnected()) {
170168
try {
171-
detectedAddresses = i2c.scan(null);
169+
// Perform I2C scan to detect connected sensors
170+
detectedSensors = i2c.scan(null);
172171
} catch (IOException | NullPointerException e) {
173172
e.printStackTrace();
174173
}
175174
}
175+
176176
return null;
177177
}
178178

179179
@Override
180180
protected void onPostExecute(Void aVoid) {
181181
super.onPostExecute(aVoid);
182182

183-
if (scienceLab.isConnected() && detectedAddresses != null) {
184-
for (Integer address : detectedAddresses) {
185-
if (sensorAddr.containsKey(address)) {
183+
StringBuilder scanResults = new StringBuilder();
184+
185+
// Populate the list with detected sensors
186+
if (detectedSensors != null && scienceLab.isConnected()) {
187+
for (Integer address : detectedSensors) {
188+
if (address != null && sensorAddr.containsKey(address)) {
189+
dataAddress.add(String.valueOf(address));
186190
dataName.addAll(sensorAddr.get(address));
187191
}
188192
}
193+
194+
// Build scan results for detected sensors
195+
for (final String address : dataAddress) {
196+
scanResults.append(address).append(": ").append(sensorAddr.get(Integer.parseInt(address))).append("\n");
197+
}
198+
tvSensorScan.setText(scanResults);
189199
}
190200

191-
// Add all sensors, even if not detected
201+
// Ensure the full list of sensors is displayed, even if not connected
192202
for (List<String> sensors : sensorAddr.values()) {
193-
for (String sensor : sensors) {
194-
if (!dataName.contains(sensor)) {
195-
dataName.add(sensor);
196-
}
203+
if (!dataName.containsAll(sensors)) {
204+
dataName.addAll(sensors);
197205
}
198206
}
199-
Collections.sort(dataName);
200207

201-
if (scienceLab.isConnected()) {
202-
tvSensorScan.setText(getString(R.string.not_connected));
203-
} else {
208+
// Update scan message if no sensors were detected
209+
if (detectedSensors == null || detectedSensors.isEmpty()) {
204210
tvSensorScan.setText(getString(R.string.not_connected));
205211
}
206-
212+
dataName.sort(String::compareTo);
207213
adapter.notifyDataSetChanged();
208214
buttonSensorAutoScan.setClickable(true);
209215
}

0 commit comments

Comments
 (0)