From 7ba92d8df84999a0d8f82934ced1766e745dc788 Mon Sep 17 00:00:00 2001 From: Anashuman Singh Date: Wed, 14 Aug 2024 12:37:01 +0530 Subject: [PATCH] fix: fixed CCS811 --- .../pslab/communication/sensors/CCS811.java | 25 +++++++++++-------- .../java/io/pslab/sensors/SensorCCS811.java | 10 ++++---- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/io/pslab/communication/sensors/CCS811.java b/app/src/main/java/io/pslab/communication/sensors/CCS811.java index 9bb027c38..1d7b97d67 100644 --- a/app/src/main/java/io/pslab/communication/sensors/CCS811.java +++ b/app/src/main/java/io/pslab/communication/sensors/CCS811.java @@ -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); @@ -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) { @@ -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 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}); } } diff --git a/app/src/main/java/io/pslab/sensors/SensorCCS811.java b/app/src/main/java/io/pslab/sensors/SensorCCS811.java index 45274ed5e..c2218009e 100644 --- a/app/src/main/java/io/pslab/sensors/SensorCCS811.java +++ b/app/src/main/java/io/pslab/sensors/SensorCCS811.java @@ -282,9 +282,9 @@ public void onStopTrackingTouch(SeekBar seekBar) { private class SensorDataFetch extends AsyncTask { - private double[] dataCS811; - private Double dataCCS811eCO2; - private Double dataCCS811TVOC; + private int[] dataCS811; + private int dataCCS811eCO2; + private int dataCCS811TVOC; private long timeElapsed; @Override @@ -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; }