Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
28 changes: 14 additions & 14 deletions app/src/main/java/io/pslab/activity/SensorActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
scienceLab = ScienceLabCommon.scienceLab;

i2c = scienceLab.i2c;
sensorAddr.put(0x60, "MCP4728");
sensorAddr.put(0x48, "ADS1115");
sensorAddr.put(0x23, "BH1750");
sensorAddr.put(0x77, "BMP180");
sensorAddr.put(0x5A, "MLX90614");
sensorAddr.put(0x1E, "HMC5883L");
Expand All @@ -77,13 +75,9 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
buttonSensorAutoScan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (scienceLab.isConnected()) {
buttonSensorAutoScan.setClickable(false);
tvSensorScan.setText(getResources().getString(R.string.scanning));
new PopulateSensors().execute();
} else {
Toast.makeText(getApplicationContext(), getString(R.string.device_not_connected), Toast.LENGTH_SHORT).show();
}
buttonSensorAutoScan.setClickable(false);
tvSensorScan.setText(getResources().getString(R.string.scanning));
new PopulateSensors().execute();
}
});
lvSensor.setOnItemClickListener(new AdapterView.OnItemClickListener() {
Expand Down Expand Up @@ -139,10 +133,12 @@ protected Void doInBackground(Void... voids) {
data = new ArrayList<>();
dataName.clear();
dataAddress.clear();
try {
data = i2c.scan(null);
} catch (IOException | NullPointerException e) {
e.printStackTrace();
if (scienceLab.isConnected()) {
try {
data = i2c.scan(null);
} catch (IOException | NullPointerException e) {
e.printStackTrace();
}
}
return null;
}
Expand Down Expand Up @@ -171,7 +167,11 @@ protected void onPostExecute(Void aVoid) {
dataName.add(sensorAddr.get(key));
}

tvSensorScan.setText(tvData);
if (scienceLab.isConnected()) {
tvSensorScan.setText(tvData);
} else {
tvSensorScan.setText(getString(R.string.not_connected));
}
adapter.notifyDataSetChanged();
buttonSensorAutoScan.setClickable(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public void onClick(@NonNull MaterialDialog dialog, @NonNull final DialogAction
@Override
public void run() {
try {
MPU6050 sensorMPU6050 = new MPU6050(i2c);
MPU6050 sensorMPU6050 = new MPU6050(i2c, scienceLab);
while (loggingThreadRunning) {
TaskMPU6050 taskMPU6050 = new TaskMPU6050(sensorMPU6050);
taskMPU6050.execute();
Expand Down
54 changes: 29 additions & 25 deletions app/src/main/java/io/pslab/communication/sensors/BMP180.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.util.Log;

import io.pslab.communication.ScienceLab;
import io.pslab.communication.peripherals.I2C;

import java.io.IOException;
Expand All @@ -16,6 +17,7 @@
*/

public class BMP180 {

private String TAG = "BMP180";
private int ADDRESS = 0x77;
private int REG_CONTROL = 0xF4;
Expand All @@ -36,32 +38,34 @@ public class BMP180 {
private double c3, c4, b1, c5, c6, mc, md, x0, x1, x2, y0, y1, y2, p0, p1, p2, temperature, pressure, baseline;
private ArrayList<Integer> setOverSampling = new ArrayList<>(Arrays.asList(0, 1, 2, 3));

public BMP180(I2C i2c) throws IOException, InterruptedException {
public BMP180(I2C i2c, ScienceLab scienceLab) throws IOException, InterruptedException {
this.i2c = i2c;
MB = readInt(0xBA);
c3 = 160 * pow(2, -15) * readInt(0xAE);
c4 = pow(10, -3) * pow(2, -15) * readUInt(0xB0);
b1 = pow(160, 2) * pow(2, -30) * readInt(0xB6);
c5 = (pow(2, -15) / 160) * readUInt(0xB2);
c6 = readUInt(0xB4);
mc = (pow(2, 11) / pow(160, 2)) * readInt(0xBC);
md = readInt(0xBE) / 160.0;
x0 = readInt(0xAA);
x1 = 160.0 * pow(2, -13) * readInt(0xAC);
x2 = pow(160, 2) * pow(2, -25) * readInt(0xB8);
y0 = c4 * pow(2, 15);
y1 = c4 * c3;
y2 = c4 * b1;
p0 = (3791.0 - 8.0) / 1600.0;
p1 = 1.0 - 7357.0 * pow(2, -20);
p2 = 3038.0 * 100.0 * pow(2, -36);
temperature = 25;

Log.v("calib", Arrays.toString((new double[]{c3, c4, b1, c5, c6, mc, md, x0, x1, x2, y0, y1, p0, p1, p2})));
initTemperature();
readTemperature();
initPressure();
baseline = readPressure();
if (scienceLab.isConnected()) {
MB = readInt(0xBA);
c3 = 160 * pow(2, -15) * readInt(0xAE);
c4 = pow(10, -3) * pow(2, -15) * readUInt(0xB0);
b1 = pow(160, 2) * pow(2, -30) * readInt(0xB6);
c5 = (pow(2, -15) / 160) * readUInt(0xB2);
c6 = readUInt(0xB4);
mc = (pow(2, 11) / pow(160, 2)) * readInt(0xBC);
md = readInt(0xBE) / 160.0;
x0 = readInt(0xAA);
x1 = 160.0 * pow(2, -13) * readInt(0xAC);
x2 = pow(160, 2) * pow(2, -25) * readInt(0xB8);
y0 = c4 * pow(2, 15);
y1 = c4 * c3;
y2 = c4 * b1;
p0 = (3791.0 - 8.0) / 1600.0;
p1 = 1.0 - 7357.0 * pow(2, -20);
p2 = 3038.0 * 100.0 * pow(2, -36);
temperature = 25;

Log.v("calib", Arrays.toString((new double[]{c3, c4, b1, c5, c6, mc, md, x0, x1, x2, y0, y1, p0, p1, p2})));
initTemperature();
readTemperature();
initPressure();
baseline = readPressure();
}
}

private short readInt(int address) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.pslab.communication.sensors;

import io.pslab.communication.ScienceLab;
import io.pslab.communication.peripherals.I2C;

import java.io.IOException;
Expand Down Expand Up @@ -41,9 +42,11 @@ public class HMC5883L {

private I2C i2c;

public HMC5883L(I2C i2c) throws IOException {
public HMC5883L(I2C i2c, ScienceLab scienceLab) throws IOException {
this.i2c = i2c;
init();
if (scienceLab.isConnected()) {
init();
}
}

private void init() throws IOException {
Expand Down
11 changes: 7 additions & 4 deletions app/src/main/java/io/pslab/communication/sensors/MPU6050.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.apache.commons.math3.stat.StatUtils;
import org.apache.commons.math3.util.FastMath;
import io.pslab.communication.ScienceLab;
import io.pslab.communication.peripherals.I2C;

import java.io.IOException;
Expand Down Expand Up @@ -32,11 +33,13 @@ public class MPU6050 {
private ArrayList<Integer> setAccelRange = new ArrayList<>(Arrays.asList(2, 4, 8, 16));
private ArrayList<Double> kalmanFilter = new ArrayList<>(Arrays.asList(0.01, 0.1, 1.0, 10.0, 100.0, 1000.0, 10000.0, 0.0));

public MPU6050(I2C i2c) throws IOException {
public MPU6050(I2C i2c, ScienceLab scienceLab) throws IOException {
this.i2c = i2c;
setGyroRange(2000);
setAccelerationRange(16);
powerUp();
if (scienceLab.isConnected()) {
setGyroRange(2000);
setAccelerationRange(16);
powerUp();
}
}

public void kalmanFilter(Double opt) throws IOException, NullPointerException {
Expand Down
7 changes: 5 additions & 2 deletions app/src/main/java/io/pslab/communication/sensors/SHT21.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.util.Log;

import io.pslab.communication.ScienceLab;
import io.pslab.communication.peripherals.I2C;

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

public SHT21(I2C i2c) throws IOException, InterruptedException {
public SHT21(I2C i2c, ScienceLab scienceLab) throws IOException, InterruptedException {
this.i2c = i2c;
init();
if (scienceLab.isConnected()) {
init();
}
}

private void init() throws IOException, InterruptedException {
Expand Down
29 changes: 16 additions & 13 deletions app/src/main/java/io/pslab/communication/sensors/TSL2561.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.util.Log;

import io.pslab.communication.ScienceLab;
import io.pslab.communication.peripherals.I2C;

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

public TSL2561(I2C i2c) throws IOException, InterruptedException {
public TSL2561(I2C i2c, ScienceLab scienceLab) throws IOException, InterruptedException {
this.i2c = i2c;
// set timing 101ms & 16x gain
enable();
_wait();
i2c.writeBulk(ADDRESS, new int[]{0x80 | 0x01, 0x01 | 0x10});
//full scale luminosity
infraList = i2c.readBulk(ADDRESS, 0x80 | 0x20 | 0x0E, 2);
fullList = i2c.readBulk(ADDRESS, 0x80 | 0x20 | 0x0C, 2);
full = (fullList.get(1) << 8) | fullList.get(0);
infra = (infraList.get(1) << 8) | infraList.get(0);

Log.v(TAG, "Full - " + Integer.toString(full));
Log.v(TAG, "Infrared - " + Integer.toString(infra));
Log.v(TAG, "Visible -" + Integer.toString(full - infra));
if (scienceLab.isConnected()) {
enable();
_wait();
i2c.writeBulk(ADDRESS, new int[]{0x80 | 0x01, 0x01 | 0x10});
//full scale luminosity
infraList = i2c.readBulk(ADDRESS, 0x80 | 0x20 | 0x0E, 2);
fullList = i2c.readBulk(ADDRESS, 0x80 | 0x20 | 0x0C, 2);
full = (fullList.get(1) << 8) | fullList.get(0);
infra = (infraList.get(1) << 8) | infraList.get(0);

Log.v(TAG, "Full - " + Integer.toString(full));
Log.v(TAG, "Infrared - " + Integer.toString(infra));
Log.v(TAG, "Visible -" + Integer.toString(full - infra));
}
}

public int getID() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ public void onItemSelected(AdapterView<?> parent, View view, int position, long
try {
data = i2c.scan(null);
if (data.contains(0x39)) {
tsl2561 = new TSL2561(i2c);
tsl2561 = new TSL2561(i2c, scienceLab);
gainValue.setEnabled(true);
LuxMeterFragmentConfig.selectedSensor = 2;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public void onCreate(Bundle savedInstanceState) {
if (scienceLab.isConnected()) {
try {
I2C i2c = scienceLab.i2c;
sensorTSL2561 = new TSL2561(i2c);
sensorTSL2561 = new TSL2561(i2c, scienceLab);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/java/io/pslab/sensors/SensorADS1115.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,10 @@ private void setSensorDock() {
playPauseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (play) {
if (play && scienceLab.isConnected()) {
playPauseButton.setImageResource(R.drawable.play);
play = false;
} else if (!scienceLab.isConnected()) {
playPauseButton.setImageResource(R.drawable.play);
play = false;
} else {
Expand Down
11 changes: 7 additions & 4 deletions app/src/main/java/io/pslab/sensors/SensorBMP180.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void onCreate(Bundle savedInstanceState) {
scienceLab = ScienceLabCommon.scienceLab;
I2C i2c = scienceLab.i2c;
try {
sensorBMP180 = new BMP180(i2c);
sensorBMP180 = new BMP180(i2c, scienceLab);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -231,7 +231,7 @@ public void run() {
}

private boolean shouldPlay() {
if (play) {
if (play && scienceLab.isConnected()) {
if (indefiniteSamplesCheckBox.isChecked())
return true;
else if (counter >= 0) {
Expand All @@ -257,7 +257,10 @@ private void setSensorDock() {
playPauseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (play) {
if (play && scienceLab.isConnected()) {
playPauseButton.setImageResource(R.drawable.play);
play = false;
} else if (!scienceLab.isConnected()) {
playPauseButton.setImageResource(R.drawable.play);
play = false;
} else {
Expand Down Expand Up @@ -314,7 +317,7 @@ private class SensorDataFetch extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
if (sensorBMP180 != null) {
if (sensorBMP180 != null && scienceLab.isConnected()) {
dataBMP180 = sensorBMP180.getRaw();
}
} catch (IOException | InterruptedException e) {
Expand Down
7 changes: 5 additions & 2 deletions app/src/main/java/io/pslab/sensors/SensorHMC5883L.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void onCreate(Bundle savedInstanceState) {
scienceLab = ScienceLabCommon.scienceLab;
I2C i2c = scienceLab.i2c;
try {
sensorHMC5883L = new HMC5883L(i2c);
sensorHMC5883L = new HMC5883L(i2c, scienceLab);
} catch (IOException e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -183,7 +183,10 @@ private void setSensorDock() {
playPauseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (play) {
if (play && scienceLab.isConnected()) {
playPauseButton.setImageResource(R.drawable.play);
play = false;
} else if (!scienceLab.isConnected()) {
playPauseButton.setImageResource(R.drawable.play);
play = false;
} else {
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/java/io/pslab/sensors/SensorMLX90614.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,10 @@ private void setSensorDock() {
playPauseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (play) {
if (play && scienceLab.isConnected()) {
playPauseButton.setImageResource(R.drawable.play);
play = false;
} else if (!scienceLab.isConnected()) {
playPauseButton.setImageResource(R.drawable.play);
play = false;
} else {
Expand Down
Loading