Skip to content

Commit

Permalink
Add changes to SensorActivity.java
Browse files Browse the repository at this point in the history
  • Loading branch information
samruddhi-Rahegaonkar committed Jan 2, 2025
1 parent 9321d17 commit 929990e
Showing 1 changed file with 44 additions and 30 deletions.
74 changes: 44 additions & 30 deletions app/src/main/java/io/pslab/activity/SensorActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,16 @@
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, List<String>> sensorAddr = new LinkedHashMap<>();
private final List<String> dataAddress = new ArrayList<>();
private final List<String> dataName = new ArrayList<>();
private ArrayAdapter<String> adapter;
private ListView lvSensor;
Expand All @@ -70,18 +75,16 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {

i2c = scienceLab.i2c;

// 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");
// Populate sensor addresses with multiple sensors mapped to the same address
sensorAddr.put(0x48, List.of("ADS1115"));
sensorAddr.put(0x77, List.of("BMP180"));
sensorAddr.put(0x5A, List.of("MLX90614", "CCS811"));
sensorAddr.put(0x1E, List.of("HMC5883L"));
sensorAddr.put(0x68, List.of("MPU6050"));
sensorAddr.put(0x40, List.of("SHT21"));
sensorAddr.put(0x39, List.of("TSL2561", "APDS9960"));
sensorAddr.put(0x69, List.of("MPU925x"));
sensorAddr.put(0x29, List.of("VL53L0X"));

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

Expand Down Expand Up @@ -150,55 +153,66 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
"Sensor Not Supported", null, null, Snackbar.LENGTH_SHORT);
}
});
}

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);
// Populate the full list of sensors initially
for (List<String> sensors : sensorAddr.values()) {
dataName.addAll(sensors);
}
adapter.notifyDataSetChanged();
}

private class PopulateSensors extends AsyncTask<Void, Void, Void> {
private List<Integer> detectedAddresses;
private List<Integer> detectedSensors;

@Override
protected Void doInBackground(Void... voids) {
detectedAddresses = new ArrayList<>();
detectedSensors = new ArrayList<>();
dataAddress.clear();
dataName.clear();

if (scienceLab.isConnected()) {
try {
detectedAddresses = i2c.scan(null);
// Perform I2C scan to detect connected sensors
detectedSensors = i2c.scan(null);
} catch (IOException | NullPointerException e) {
e.printStackTrace();
}
}

return null;
}

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);

if (scienceLab.isConnected() && detectedAddresses != null) {
for (Integer address : detectedAddresses) {
if (sensorAddr.containsKey(address)) {
StringBuilder scanResults = new StringBuilder();

// Populate the list with detected sensors
if (detectedSensors != null && scienceLab.isConnected()) {
for (Integer address : detectedSensors) {
if (address != null && sensorAddr.containsKey(address)) {
dataAddress.add(String.valueOf(address));
dataName.addAll(sensorAddr.get(address));
}
}

// Build scan results for detected sensors
for (final String address : dataAddress) {
scanResults.append(address).append(": ").append(sensorAddr.get(Integer.parseInt(address))).append("\n");
}
tvSensorScan.setText(scanResults);
}

// Add all sensors, even if not detected
// Ensure the full list of sensors is displayed, even if not connected
for (List<String> sensors : sensorAddr.values()) {
for (String sensor : sensors) {
if (!dataName.contains(sensor)) {
dataName.add(sensor);
}
if (!dataName.containsAll(sensors)) {
dataName.addAll(sensors);
}
}

if (scienceLab.isConnected()) {
tvSensorScan.setText(getString(R.string.not_connected));
} else {
// Update scan message if no sensors were detected
if (detectedSensors == null || detectedSensors.isEmpty()) {
tvSensorScan.setText(getString(R.string.not_connected));
}

Expand Down

0 comments on commit 929990e

Please sign in to comment.