Skip to content

Commit

Permalink
fix: fixed CCS811
Browse files Browse the repository at this point in the history
  • Loading branch information
AsCress committed Aug 14, 2024
1 parent 91e8666 commit 7ba92d8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
25 changes: 15 additions & 10 deletions app/src/main/java/io/pslab/communication/sensors/CCS811.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,31 @@ public class CCS811 {
private static final int HW_ID = 0x20; // HW_ID # R 1 byte Hardware ID. The value is 0x81
private static final int FW_BOOT_VERSION = 0x23; // FW_Boot_Version # R 2 bytes firmware version number for the boot code. Firmware Application Version. The first 2 bytes contain
private static final int FW_APP_VERSION = 0x24; // FW_App_Version # R 2 bytes the firmware version number for the application code
private static final int SW_RESET = 0xFF; // SW_RESET # W 4 bytes to this register in a single sequence the device will reset and return to BOOT mode.

// Figure 25: CCS811 Bootloader Register Map
// Address Register R/W Size Description
private static final int HW_Version = 0x21;
private static final int APP_START = 0xF4;

public CCS811(I2C i2c, ScienceLab scienceLab) throws Exception {
this.i2c = i2c;
if (scienceLab.isConnected()) {
fetchID();
softwareReset();
appStart();
}
}

private void softwareReset() throws IOException {
i2c.write(ADDRESS, new int[]{0x11, 0xE5, 0x72, 0x8A}, SW_RESET);
}

private void fetchID() throws IOException, InterruptedException {
i2c.write(ADDRESS, new int[]{}, HW_ID);
int hardwareId = i2c.read(ADDRESS, 1, HW_ID).get(0) & 0xFF;
Thread.sleep(20);
i2c.write(ADDRESS, new int[]{}, HW_Version);
int hardwareVersion = i2c.read(ADDRESS, 1, HW_Version).get(0) & 0xFF;
Thread.sleep(20);
i2c.write(ADDRESS, new int[]{}, FW_BOOT_VERSION);
int bootVersion = i2c.read(ADDRESS, 2, FW_BOOT_VERSION).get(0) & 0xFF;
Thread.sleep(20);
i2c.write(ADDRESS, new int[]{}, FW_APP_VERSION);
int appVersion = i2c.read(ADDRESS, 2, FW_APP_VERSION).get(0) & 0xFF;
Thread.sleep(20);

Expand All @@ -51,6 +51,10 @@ private void fetchID() throws IOException, InterruptedException {
Log.d("CCS811", "App Version: " + appVersion);
}

private void appStart() throws IOException {
i2c.write(ADDRESS, new int[]{}, APP_START);
}

private String decodeError(int error) {
String e = "";
if ((error & (1)) > 0) {
Expand All @@ -74,15 +78,16 @@ private String decodeError(int error) {
return "Error: " + e.substring(2);
}

public double[] getRaw() throws IOException {
public int[] getRaw() throws IOException {
i2c.write(ADDRESS, new int[]{}, ALG_RESULT_DATA);
ArrayList<Integer> data = i2c.read(ADDRESS, 8, ALG_RESULT_DATA);
double eCO2 = (data.get(0) & 0xFF) * 256 + (data.get(1) & 0xFF);
double TVOC = (data.get(2) & 0xFF) * 256 + (data.get(3) & 0xFF);
int eCO2 = ((data.get(0) & 0xFF) << 8) | (data.get(1) & 0xFF);
int TVOC = ((data.get(2) & 0xFF) << 8) | (data.get(3) & 0xFF);
int errorId = data.get(5) & 0xFF;

if (errorId > 0) {
Log.d("CCS811", decodeError(errorId));
}
return (new double[]{eCO2, TVOC});
return (new int[]{eCO2, TVOC});
}
}
10 changes: 5 additions & 5 deletions app/src/main/java/io/pslab/sensors/SensorCCS811.java
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,9 @@ public void onStopTrackingTouch(SeekBar seekBar) {

private class SensorDataFetch extends AsyncTask<Void, Void, Void> {

private double[] dataCS811;
private Double dataCCS811eCO2;
private Double dataCCS811TVOC;
private int[] dataCS811;
private int dataCCS811eCO2;
private int dataCCS811TVOC;
private long timeElapsed;

@Override
Expand All @@ -299,8 +299,8 @@ protected Void doInBackground(Void... params) {
e.printStackTrace();
}
timeElapsed = (System.currentTimeMillis() - startTime) / 1000;
entrieseCO2.add(new Entry((float) timeElapsed, dataCCS811eCO2.floatValue()));
entriesTVOC.add(new Entry((float) timeElapsed, dataCCS811TVOC.floatValue()));
entrieseCO2.add(new Entry((float) timeElapsed, dataCCS811eCO2));
entriesTVOC.add(new Entry((float) timeElapsed, dataCCS811TVOC));
return null;
}

Expand Down

0 comments on commit 7ba92d8

Please sign in to comment.