Skip to content

Commit 09da99c

Browse files
committed
fix: sensor views shown without PSLab device
ands
1 parent fdff6d0 commit 09da99c

17 files changed

+127
-82
lines changed

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
5555
scienceLab = ScienceLabCommon.scienceLab;
5656

5757
i2c = scienceLab.i2c;
58-
sensorAddr.put(0x60, "MCP4728");
5958
sensorAddr.put(0x48, "ADS1115");
60-
sensorAddr.put(0x23, "BH1750");
6159
sensorAddr.put(0x77, "BMP180");
6260
sensorAddr.put(0x5A, "MLX90614");
6361
sensorAddr.put(0x1E, "HMC5883L");
@@ -77,13 +75,9 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
7775
buttonSensorAutoScan.setOnClickListener(new View.OnClickListener() {
7876
@Override
7977
public void onClick(View v) {
80-
if (scienceLab.isConnected()) {
81-
buttonSensorAutoScan.setClickable(false);
82-
tvSensorScan.setText(getResources().getString(R.string.scanning));
83-
new PopulateSensors().execute();
84-
} else {
85-
Toast.makeText(getApplicationContext(), getString(R.string.device_not_connected), Toast.LENGTH_SHORT).show();
86-
}
78+
buttonSensorAutoScan.setClickable(false);
79+
tvSensorScan.setText(getResources().getString(R.string.scanning));
80+
new PopulateSensors().execute();
8781
}
8882
});
8983
lvSensor.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@@ -139,10 +133,12 @@ protected Void doInBackground(Void... voids) {
139133
data = new ArrayList<>();
140134
dataName.clear();
141135
dataAddress.clear();
142-
try {
143-
data = i2c.scan(null);
144-
} catch (IOException | NullPointerException e) {
145-
e.printStackTrace();
136+
if (scienceLab.isConnected()) {
137+
try {
138+
data = i2c.scan(null);
139+
} catch (IOException | NullPointerException e) {
140+
e.printStackTrace();
141+
}
146142
}
147143
return null;
148144
}
@@ -171,7 +167,11 @@ protected void onPostExecute(Void aVoid) {
171167
dataName.add(sensorAddr.get(key));
172168
}
173169

174-
tvSensorScan.setText(tvData);
170+
if (scienceLab.isConnected()) {
171+
tvSensorScan.setText(tvData);
172+
} else {
173+
tvSensorScan.setText(getString(R.string.not_connected));
174+
}
175175
adapter.notifyDataSetChanged();
176176
buttonSensorAutoScan.setClickable(true);
177177
}

app/src/main/java/org/pslab/activity/SensorDataLoggerActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public void onClick(@NonNull MaterialDialog dialog, @NonNull final DialogAction
176176
@Override
177177
public void run() {
178178
try {
179-
MPU6050 sensorMPU6050 = new MPU6050(i2c);
179+
MPU6050 sensorMPU6050 = new MPU6050(i2c, scienceLab);
180180
while (loggingThreadRunning) {
181181
TaskMPU6050 taskMPU6050 = new TaskMPU6050(sensorMPU6050);
182182
taskMPU6050.execute();

app/src/main/java/org/pslab/communication/sensors/BMP180.java

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.util.Log;
44

5+
import org.pslab.communication.ScienceLab;
56
import org.pslab.communication.peripherals.I2C;
67

78
import java.io.IOException;
@@ -16,6 +17,9 @@
1617
*/
1718

1819
public class BMP180 {
20+
21+
private ScienceLab scienceLab;
22+
1923
private String TAG = "BMP180";
2024
private int ADDRESS = 0x77;
2125
private int REG_CONTROL = 0xF4;
@@ -36,32 +40,35 @@ public class BMP180 {
3640
private double c3, c4, b1, c5, c6, mc, md, x0, x1, x2, y0, y1, y2, p0, p1, p2, temperature, pressure, baseline;
3741
private ArrayList<Integer> setOverSampling = new ArrayList<>(Arrays.asList(0, 1, 2, 3));
3842

39-
public BMP180(I2C i2c) throws IOException, InterruptedException {
43+
public BMP180(I2C i2c, ScienceLab scienceLab) throws IOException, InterruptedException {
4044
this.i2c = i2c;
41-
MB = readInt(0xBA);
42-
c3 = 160 * pow(2, -15) * readInt(0xAE);
43-
c4 = pow(10, -3) * pow(2, -15) * readUInt(0xB0);
44-
b1 = pow(160, 2) * pow(2, -30) * readInt(0xB6);
45-
c5 = (pow(2, -15) / 160) * readUInt(0xB2);
46-
c6 = readUInt(0xB4);
47-
mc = (pow(2, 11) / pow(160, 2)) * readInt(0xBC);
48-
md = readInt(0xBE) / 160.0;
49-
x0 = readInt(0xAA);
50-
x1 = 160.0 * pow(2, -13) * readInt(0xAC);
51-
x2 = pow(160, 2) * pow(2, -25) * readInt(0xB8);
52-
y0 = c4 * pow(2, 15);
53-
y1 = c4 * c3;
54-
y2 = c4 * b1;
55-
p0 = (3791.0 - 8.0) / 1600.0;
56-
p1 = 1.0 - 7357.0 * pow(2, -20);
57-
p2 = 3038.0 * 100.0 * pow(2, -36);
58-
temperature = 25;
59-
60-
Log.v("calib", Arrays.toString((new double[]{c3, c4, b1, c5, c6, mc, md, x0, x1, x2, y0, y1, p0, p1, p2})));
61-
initTemperature();
62-
readTemperature();
63-
initPressure();
64-
baseline = readPressure();
45+
this.scienceLab = scienceLab;
46+
if (scienceLab.isConnected()) {
47+
MB = readInt(0xBA);
48+
c3 = 160 * pow(2, -15) * readInt(0xAE);
49+
c4 = pow(10, -3) * pow(2, -15) * readUInt(0xB0);
50+
b1 = pow(160, 2) * pow(2, -30) * readInt(0xB6);
51+
c5 = (pow(2, -15) / 160) * readUInt(0xB2);
52+
c6 = readUInt(0xB4);
53+
mc = (pow(2, 11) / pow(160, 2)) * readInt(0xBC);
54+
md = readInt(0xBE) / 160.0;
55+
x0 = readInt(0xAA);
56+
x1 = 160.0 * pow(2, -13) * readInt(0xAC);
57+
x2 = pow(160, 2) * pow(2, -25) * readInt(0xB8);
58+
y0 = c4 * pow(2, 15);
59+
y1 = c4 * c3;
60+
y2 = c4 * b1;
61+
p0 = (3791.0 - 8.0) / 1600.0;
62+
p1 = 1.0 - 7357.0 * pow(2, -20);
63+
p2 = 3038.0 * 100.0 * pow(2, -36);
64+
temperature = 25;
65+
66+
Log.v("calib", Arrays.toString((new double[]{c3, c4, b1, c5, c6, mc, md, x0, x1, x2, y0, y1, p0, p1, p2})));
67+
initTemperature();
68+
readTemperature();
69+
initPressure();
70+
baseline = readPressure();
71+
}
6572
}
6673

6774
private short readInt(int address) throws IOException {

app/src/main/java/org/pslab/communication/sensors/HMC5883L.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.pslab.communication.sensors;
22

3+
import org.pslab.communication.ScienceLab;
34
import org.pslab.communication.peripherals.I2C;
45

56
import java.io.IOException;
@@ -41,9 +42,11 @@ public class HMC5883L {
4142

4243
private I2C i2c;
4344

44-
public HMC5883L(I2C i2c) throws IOException {
45+
public HMC5883L(I2C i2c, ScienceLab scienceLab) throws IOException {
4546
this.i2c = i2c;
46-
init();
47+
if (scienceLab.isConnected()) {
48+
init();
49+
}
4750
}
4851

4952
private void init() throws IOException {

app/src/main/java/org/pslab/communication/sensors/MPU6050.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.apache.commons.math3.stat.StatUtils;
44
import org.apache.commons.math3.util.FastMath;
5+
import org.pslab.communication.ScienceLab;
56
import org.pslab.communication.peripherals.I2C;
67

78
import java.io.IOException;
@@ -28,15 +29,19 @@ public class MPU6050 {
2829
private String name = "Accel/gyro";
2930
private ArrayList<KalmanFilter> K = new ArrayList<>(); //K is the list of KalmanFilter object
3031
private I2C i2c;
32+
private ScienceLab scienceLab;
3133
private ArrayList<Integer> setGyroRange = new ArrayList<>(Arrays.asList(250, 500, 1000, 2000));
3234
private ArrayList<Integer> setAccelRange = new ArrayList<>(Arrays.asList(2, 4, 8, 16));
3335
private ArrayList<Double> kalmanFilter = new ArrayList<>(Arrays.asList(0.01, 0.1, 1.0, 10.0, 100.0, 1000.0, 10000.0, 0.0));
3436

35-
public MPU6050(I2C i2c) throws IOException {
37+
public MPU6050(I2C i2c, ScienceLab scienceLab) throws IOException {
3638
this.i2c = i2c;
37-
setGyroRange(2000);
38-
setAccelerationRange(16);
39-
powerUp();
39+
this.scienceLab = scienceLab;
40+
if (scienceLab.isConnected()) {
41+
setGyroRange(2000);
42+
setAccelerationRange(16);
43+
powerUp();
44+
}
4045
}
4146

4247
public void kalmanFilter(Double opt) throws IOException, NullPointerException {

app/src/main/java/org/pslab/communication/sensors/SHT21.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.util.Log;
44

5+
import org.pslab.communication.ScienceLab;
56
import org.pslab.communication.peripherals.I2C;
67

78
import java.io.IOException;
@@ -28,9 +29,11 @@ public class SHT21 {
2829
public ArrayList<String> selectParameter = new ArrayList<>(Arrays.asList("temperature", "humidity"));
2930
private I2C i2c;
3031

31-
public SHT21(I2C i2c) throws IOException, InterruptedException {
32+
public SHT21(I2C i2c, ScienceLab scienceLab) throws IOException, InterruptedException {
3233
this.i2c = i2c;
33-
init();
34+
if (scienceLab.isConnected()) {
35+
init();
36+
}
3437
}
3538

3639
private void init() throws IOException, InterruptedException {

app/src/main/java/org/pslab/communication/sensors/TSL2561.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.util.Log;
44

5+
import org.pslab.communication.ScienceLab;
56
import org.pslab.communication.peripherals.I2C;
67

78
import java.io.IOException;
@@ -51,21 +52,23 @@ public class TSL2561 {
5152
private ArrayList<java.io.Serializable> setGain = new ArrayList<java.io.Serializable>(Arrays.asList("1x", "16x"));
5253
private ArrayList<java.io.Serializable> setTiming = new ArrayList<java.io.Serializable>(Arrays.asList(0, 1, 2));
5354

54-
public TSL2561(I2C i2c) throws IOException, InterruptedException {
55+
public TSL2561(I2C i2c, ScienceLab scienceLab) throws IOException, InterruptedException {
5556
this.i2c = i2c;
5657
// set timing 101ms & 16x gain
57-
enable();
58-
_wait();
59-
i2c.writeBulk(ADDRESS, new int[]{0x80 | 0x01, 0x01 | 0x10});
60-
//full scale luminosity
61-
infraList = i2c.readBulk(ADDRESS, 0x80 | 0x20 | 0x0E, 2);
62-
fullList = i2c.readBulk(ADDRESS, 0x80 | 0x20 | 0x0C, 2);
63-
full = (fullList.get(1) << 8) | fullList.get(0);
64-
infra = (infraList.get(1) << 8) | infraList.get(0);
65-
66-
Log.v(TAG, "Full - " + Integer.toString(full));
67-
Log.v(TAG, "Infrared - " + Integer.toString(infra));
68-
Log.v(TAG, "Visible -" + Integer.toString(full - infra));
58+
if (scienceLab.isConnected()) {
59+
enable();
60+
_wait();
61+
i2c.writeBulk(ADDRESS, new int[]{0x80 | 0x01, 0x01 | 0x10});
62+
//full scale luminosity
63+
infraList = i2c.readBulk(ADDRESS, 0x80 | 0x20 | 0x0E, 2);
64+
fullList = i2c.readBulk(ADDRESS, 0x80 | 0x20 | 0x0C, 2);
65+
full = (fullList.get(1) << 8) | fullList.get(0);
66+
infra = (infraList.get(1) << 8) | infraList.get(0);
67+
68+
Log.v(TAG, "Full - " + Integer.toString(full));
69+
Log.v(TAG, "Infrared - " + Integer.toString(infra));
70+
Log.v(TAG, "Visible -" + Integer.toString(full - infra));
71+
}
6972
}
7073

7174
public int getID() throws IOException {

app/src/main/java/org/pslab/fragment/LuxMeterFragmentConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ public void onItemSelected(AdapterView<?> parent, View view, int position, long
259259
try {
260260
data = i2c.scan(null);
261261
if (data.contains(0x39)) {
262-
tsl2561 = new TSL2561(i2c);
262+
tsl2561 = new TSL2561(i2c, scienceLab);
263263
gainValue.setEnabled(true);
264264
LuxMeterFragmentConfig.selectedSensor = 2;
265265
}

app/src/main/java/org/pslab/fragment/LuxMeterFragmentData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public void onCreate(Bundle savedInstanceState) {
122122
if (scienceLab.isConnected()) {
123123
try {
124124
I2C i2c = scienceLab.i2c;
125-
sensorTSL2561 = new TSL2561(i2c);
125+
sensorTSL2561 = new TSL2561(i2c, scienceLab);
126126
} catch (IOException | InterruptedException e) {
127127
e.printStackTrace();
128128
}

app/src/main/java/org/pslab/sensors/SensorADS1115.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,10 @@ private void setSensorDock() {
188188
playPauseButton.setOnClickListener(new View.OnClickListener() {
189189
@Override
190190
public void onClick(View v) {
191-
if (play) {
191+
if (play && scienceLab.isConnected()) {
192+
playPauseButton.setImageResource(R.drawable.play);
193+
play = false;
194+
} else if (!scienceLab.isConnected()) {
192195
playPauseButton.setImageResource(R.drawable.play);
193196
play = false;
194197
} else {

0 commit comments

Comments
 (0)