Skip to content
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: Display all implemented sensors in SensorActivity #2584 #2589

Open
wants to merge 5 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ dependencies {
implementation("com.github.mirrajabi:search-dialog:1.2.4")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rollback this whole file as discussed earlier.

implementation(files("../libs/croller-release.aar"))
implementation("com.github.BeppiMenozzi:Knob:1.9.0")
implementation("com.github.warkiz:IndicatorSeekBar:v2.1.1")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some changes which arn't required. You can just rollback build.gradle.kts.

implementation("com.github.Vatican-Cameos:CarouselPicker:1.2")
implementation("com.github.anastr:speedviewlib:1.6.1")
implementation("com.github.GoodieBag:ProtractorView:v1.2")
Expand Down Expand Up @@ -118,5 +117,7 @@ dependencies {

// OSS license plugin
implementation("com.google.android.gms:play-services-oss-licenses:17.1.0")
implementation ("com.github.warkiz:IndicatorSeekBar:2.1.1")

}

}
73 changes: 37 additions & 36 deletions app/src/main/java/io/pslab/activity/SensorActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,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;
Expand All @@ -74,17 +69,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);

Expand All @@ -99,6 +96,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;
Expand Down Expand Up @@ -154,17 +152,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();
}
Expand All @@ -176,31 +179,29 @@ 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));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have to get this removed. tvData displayes the addresses of the connected devices.
Kindly rollback the changes from lines 186 to 192.

}

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);
}
}
}

if (scienceLab.isConnected()) {
tvSensorScan.setText(tvData);
tvSensorScan.setText(getString(R.string.not_connected));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed. See comment above.

} else {
tvSensorScan.setText(getString(R.string.not_connected));
}

adapter.notifyDataSetChanged();
buttonSensorAutoScan.setClickable(true);
}
Expand Down
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ buildscript {
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rollback as discussed earlier.


plugins {
id("com.android.application") version "8.7.3" apply false
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, some changes that arn't required. You can rollback these.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay @AsCress Thanks for Explaining me. I will do the needful !

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AsCress Other than the SensorActivity the changes I have made because I was unable to run the application without making those changes. But after testing I will rollback it.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AsCress to resolve this issue as you have said, I need to make changes in the method "PopulateSensors" to handle the cases where both having same addresses.
should I ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samruddhi-Rahegaonkar The problem here is with the data structure that we are using to store the sensor addresses.
A Map in Java doesn't allow duplicate keys to be inserted. When we try to do that in lines 86 and 87 of SensorActivity.java, it overwrites the previous entries, and hence the previous two sensors (MLX90614 and TSL2561) actually never get inserted in the Map.
The idea here would be to migrate to a different data structure which would allow storage of duplicate entries, and then using that in PopulateSensors to display them.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AsCress Instead of Map<Integer, String>, we can use a Map<Integer, List<String>> to associate multiple sensor names with the same address.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samruddhi-Rahegaonkar Yep, that's one way. Go ahead and maybe try to implement whatever you find best in this PR.

}
id("com.android.application") version "8.5.1" apply false
}
Loading