Venkata Siva Krishna Madala / ENS160_Library

Dependents:   ECE4180FinalProjectFall22

Revision:
4:cb50c2f7e2b2
Parent:
3:63ff52373e71
--- a/ens160_i2c.cpp	Tue Dec 06 21:21:28 2022 +0000
+++ b/ens160_i2c.cpp	Thu Dec 08 16:39:40 2022 +0000
@@ -6,44 +6,63 @@
     this->i2c_address = i2c_device_address;
 }
 
-bool ENS160::ping(uint8_t address)
+int32_t ENS160::readRegisterRegion(uint8_t reg, char *data)
 {
-    int ret;
-    char rxdata;
-    ret = i2c.read(this->i2c_address, &rxdata, 1);
-    if (ret)
-        return true;
-    return false;
+    uint8_t retVal;
+    char temp[1] = {reg};
+    char tempVal[1] = {0xFF};
+    this->i2c.write(this->i2c_address, temp, 1);
+    wait(0.01);
+    retVal = this->i2c.read(this->i2c_address, tempVal, 1);
+    wait(0.01);
+    data[0] = tempVal[0];
+    if (retVal != 0)
+    	return -1;
+    return 0;
 }
 
-uint8_t ENS160::readRegisterRegion(uint8_t reg, uint8_t *data, uint8_t length)
+int32_t ENS160::readRegisterRegion(uint8_t reg, char *data, uint8_t length)
 {
     int i;
+    int32_t retVal;
     char temp_dest[length];
+    for (i=0; i < length; i++)
+    {
+        temp_dest[i] = 0xFF;
+    }
     char temp[1] = {reg};
-    i2c.write(this->i2c_address, temp, 1);
-    i2c.read(this->i2c_address, temp_dest, length);
+    this->i2c.write(this->i2c_address, temp, 1);
+    wait(0.01);
+    retVal = this->i2c.read(this->i2c_address, temp_dest, length);
+    wait(0.01);
     for (i=0; i < length; i++)
     {
         data[i] = temp_dest[i];
     }
-    return length;
+    if (retVal !=0)
+    	return -1;
+    return 0;
 }
 
-int32_t ENS160::writeRegisterRegion(uint8_t *data, uint8_t length)
+int32_t ENS160::writeRegisterRegion(char *data, uint8_t length)
 {
-    char temp_dest[length];
-    for (int i = 0; i < length; i++)
-    {
-        temp_dest[i] = *(data + i);
-    }
-    return i2c.write(this->i2c_address, temp_dest, length);
+    int32_t retVal;
+    retVal = this->i2c.write(this->i2c_address, data, length);
+    wait(0.01);
+    if(retVal !=0)
+    	return -1;
+	return 0;
 }
 
-int32_t ENS160::writeRegisterRegion(uint8_t reg, uint8_t data)
+int32_t ENS160::writeRegisterRegion(uint8_t reg, char data)
 {
+    int32_t retVal;
     char temp_data[2] = {reg, data};
-    return i2c.write(this->i2c_address, temp_data, 2);
+    retVal = this->i2c.write(this->i2c_address, temp_data, 2);
+    wait(0.01);
+    if (retVal != 0)
+    	return -1;
+	return 0;
 }
 
 //////////////////////////////////////////////////////////////////////////////
@@ -52,10 +71,10 @@
 uint16_t ENS160::getUniqueID()
 {
     int32_t retVal;
-	uint8_t tempVal[2] = {0}; 
+	char tempVal[2] = {0}; 
 	uint16_t id; 
 
-    retVal = readRegisterRegion(SFE_ENS160_PART_ID, tempVal, 2);
+    retVal = this->readRegisterRegion(SFE_ENS160_PART_ID, tempVal, 2);
 
 	id = tempVal[0];
 	id |= tempVal[1] << 8;
@@ -83,8 +102,6 @@
 
 bool ENS160::init()
 {
-    if(!this->ping(this->i2c_address))
-        return false;
     return this->isConnected();
 }
 
@@ -118,14 +135,14 @@
 int8_t ENS160::getOperatingMode()
 {
 	int32_t retVal;
-	uint8_t tempVal; 
+	char tempVal[1] = {0x00}; 
 
-	retVal = readRegisterRegion(SFE_ENS160_OP_MODE, &tempVal, 1);
+	retVal = this->readRegisterRegion(SFE_ENS160_OP_MODE, tempVal);
 
 	if( retVal != 0 )
 		return -1;
 
-	return tempVal; 
+	return tempVal[0]; 
 }
 
 //////////////////////////////////////////////////////////////////////////////
@@ -141,7 +158,7 @@
 {
 	int32_t retVal;
 
-	retVal = writeRegisterRegion(SFE_ENS160_CONFIG, val);
+	retVal = this->writeRegisterRegion(SFE_ENS160_CONFIG, val);
 
 	if( retVal != 0 )
 		return false;
@@ -162,16 +179,16 @@
 bool ENS160::enableInterrupt(bool enable)
 {
 	int32_t retVal;
-	uint8_t tempVal; 
+	char tempVal[1] = {0x00}; 
 
-	retVal = readRegisterRegion(SFE_ENS160_CONFIG, &tempVal, 1);
+	retVal = this->readRegisterRegion(SFE_ENS160_CONFIG, tempVal);
 
 	if( retVal != 0 )
 		return false;
 	
-	tempVal = (tempVal | (uint8_t)enable); 
+	tempVal[0] = (tempVal[0] | (uint8_t)enable); 
 
-	retVal = writeRegisterRegion(SFE_ENS160_CONFIG, tempVal);
+	retVal = this->writeRegisterRegion(SFE_ENS160_CONFIG, tempVal[0]);
 
 	if( retVal != 0 )
 		return false;
@@ -192,16 +209,16 @@
 bool ENS160::setInterruptPolarity(bool activeHigh)
 {
 	int32_t retVal;
-	uint8_t tempVal; 
+	char tempVal[1] = {0x00}; 
 
-	retVal = readRegisterRegion(SFE_ENS160_CONFIG, &tempVal, 1);
+	retVal = this->readRegisterRegion(SFE_ENS160_CONFIG, tempVal);
 	
 	if( retVal != 0 )
 		return false;
 
-	tempVal = (tempVal | (activeHigh << 6)); 
+	tempVal[0] = (tempVal[0] | (activeHigh << 6)); 
 
-	retVal = writeRegisterRegion(SFE_ENS160_CONFIG, tempVal);
+	retVal = this->writeRegisterRegion(SFE_ENS160_CONFIG, tempVal[0]);
 
 	if( retVal != 0 )
 		return false;
@@ -217,16 +234,16 @@
 int8_t ENS160::getInterruptPolarity()
 {
 	int32_t retVal;
-	uint8_t tempVal; 
+	char tempVal[1] = {0x00}; 
 
-	retVal = readRegisterRegion(SFE_ENS160_CONFIG, &tempVal, 1);
+	retVal = this->readRegisterRegion(SFE_ENS160_CONFIG, tempVal);
 	
 	if( retVal != 0 )
 		return -1;
 
-	tempVal &= 0x40;
+	tempVal[0] &= 0x40;
 
-	return (tempVal >> 6);
+	return (tempVal[0] >> 6);
 }
 
 //////////////////////////////////////////////////////////////////////////////
@@ -241,16 +258,16 @@
 bool ENS160::setInterruptDrive(bool pushPull)
 {
 	int32_t retVal;
-	uint8_t tempVal; 
+	char tempVal[1] = {0x00}; 
 
-	retVal = readRegisterRegion(SFE_ENS160_CONFIG, &tempVal, 1);
+	retVal = this->readRegisterRegion(SFE_ENS160_CONFIG, tempVal);
 
 	if( retVal != 0 )
 		return false;
 	
-	tempVal = (tempVal | (uint8_t)(pushPull << 5)); 
+	tempVal[0] = (tempVal[0] | (uint8_t)(pushPull << 5)); 
 
-	retVal = writeRegisterRegion(SFE_ENS160_CONFIG, tempVal);
+	retVal = this->writeRegisterRegion(SFE_ENS160_CONFIG, tempVal[0]);
 
 	if( retVal != 0 )
 		return false;
@@ -271,16 +288,16 @@
 bool ENS160::setDataInterrupt(bool enable)
 {
 	int32_t retVal;
-	uint8_t tempVal; 
+	char tempVal[1] = {0x00}; 
 
-	retVal = readRegisterRegion(SFE_ENS160_CONFIG, &tempVal, 1);
+	retVal = this->readRegisterRegion(SFE_ENS160_CONFIG, tempVal);
 
 	if( retVal != 0 )
 		return false;
 	
-	tempVal = (tempVal | (uint8_t)(enable << 1)); 
+	tempVal[0] = (tempVal[0] | (uint8_t)(enable << 1)); 
 
-	retVal = writeRegisterRegion(SFE_ENS160_CONFIG, tempVal);
+	retVal = this->writeRegisterRegion(SFE_ENS160_CONFIG, tempVal[0]);
 
 	if( retVal != 0 )
 		return false;
@@ -301,16 +318,16 @@
 bool ENS160::setGPRInterrupt(bool enable)
 {
 	int32_t retVal;
-	uint8_t tempVal; 
+	char tempVal[1] = {0x00}; 
 
-	retVal = readRegisterRegion(SFE_ENS160_CONFIG, &tempVal, 1);
+	retVal = this->readRegisterRegion(SFE_ENS160_CONFIG, tempVal);
 
 	if( retVal != 0 )
 		return false;
 	
-	tempVal = (tempVal | (uint8_t)(enable << 3)); 
+	tempVal[0] = (tempVal[0] | (uint8_t)(enable << 3)); 
 	
-	retVal = writeRegisterRegion(SFE_ENS160_CONFIG, tempVal);
+	retVal = this->writeRegisterRegion(SFE_ENS160_CONFIG, tempVal[0]);
 
 	if( retVal != 0 )
 		return false;
@@ -327,10 +344,10 @@
 uint32_t ENS160::getAppVer()
 {
 	int32_t retVal;
-	uint8_t tempVal[3] = {0};
+	char tempVal[3] = {0};
 	uint32_t version;
 
-	retVal = readRegisterRegion(SFE_ENS160_GPR_READ4, tempVal, 3);
+	retVal = this->readRegisterRegion(SFE_ENS160_GPR_READ4, tempVal, 3);
 
 	if( retVal != 0 )
 		return 0;
@@ -354,7 +371,7 @@
 bool ENS160::setTempCompensation(float tempKelvin)
 {
 	int32_t retVal;
-	uint8_t tempVal[3] = {0};
+	char tempVal[3] = {0};
 	uint16_t kelvinConversion = tempKelvin; 
 
 	kelvinConversion = kelvinConversion * 64; // convert value - fixed equation pg. 29 of datasheet
@@ -362,7 +379,7 @@
 	tempVal[1] = (kelvinConversion & 0x00FF);
 	tempVal[2] = (kelvinConversion & 0xFF00) >> 8;
 
-	retVal = writeRegisterRegion(tempVal, 2);
+	retVal = this->writeRegisterRegion(tempVal, 3);
 
 	if( retVal != 0 )
 		return false;
@@ -403,14 +420,14 @@
 bool ENS160::setRHCompensation(uint16_t humidity)
 {
 	int32_t retVal;
-	uint8_t tempVal[3] = {0};
+	char tempVal[3] = {0};
 
 	humidity = humidity * 512; // convert value - fixed equation pg. 29 in datasheet. 
     tempVal[0] = SFE_ENS160_RH_IN;
 	tempVal[1] = (humidity & 0x00FF);
 	tempVal[2] = (humidity & 0xFF00) >> 8;
 
-	retVal = writeRegisterRegion(tempVal, 2);
+	retVal = this->writeRegisterRegion(tempVal, 3);
 
 	if( retVal != 0 )
 		return false;
@@ -446,16 +463,16 @@
 bool ENS160::checkDataStatus()
 {
 	int32_t retVal;
-	uint8_t tempVal; 
+	char tempVal[1] = {0x00}; 
 
-	retVal = readRegisterRegion(SFE_ENS160_DEVICE_STATUS, &tempVal, 1);
+	retVal = this->readRegisterRegion(SFE_ENS160_DEVICE_STATUS, tempVal);
 
 	if( retVal != 0 )
 		return false; 
 
-	tempVal &= 0x02; 
+	tempVal[0] &= 0x02; 
 
-	if( tempVal == 0x02 )
+	if( tempVal[0] == 0x02 )
 		return true;
 
 	return false;
@@ -472,16 +489,16 @@
 bool ENS160::checkGPRStatus()
 {
 	int32_t retVal;
-	uint8_t tempVal; 
+	char tempVal[1] = {0x00}; 
 
-	retVal = readRegisterRegion(SFE_ENS160_DEVICE_STATUS, &tempVal, 1);
+	retVal = this->readRegisterRegion(SFE_ENS160_DEVICE_STATUS, tempVal);
 
 	if( retVal != 0 )
 		return false; 
 
-	tempVal &= 0x01;
+	tempVal[0] &= 0x01;
 
-	if( tempVal == 0x01 )
+	if( tempVal[0] == 0x01 )
 		return true;
 
 	return false;
@@ -496,29 +513,25 @@
 uint8_t ENS160::getFlags()
 {
 	int32_t retVal;
-	uint8_t tempVal;
+	char tempVal[1] = {0x00};
 
-	retVal = readRegisterRegion(SFE_ENS160_DEVICE_STATUS, &tempVal, 1);
+	retVal = this->readRegisterRegion(SFE_ENS160_DEVICE_STATUS, tempVal);
 
 	if( retVal != 0 )
 		return 0xFF; // Change to general error
 
-	tempVal = (tempVal & 0x0C) >> 2; 
+	tempVal[0] = (tempVal[0] & 0x0C) >> 2; 
 
-	switch( tempVal )
+	switch( tempVal[0] )
 	{
 		case 0: // Normal operation
 			return 0;
-			break;
 		case 1: // Warm-up phase
 			return 1;
-			break;
 		case 2: // Initial Start-Up Phase
 			return 2;
-			break;
 		case 3: // Invalid Output
 			return 3;
-			break;
 		default:
 			return 0xFF;
 	}
@@ -534,16 +547,16 @@
 bool ENS160::checkOperationStatus()
 {
 	int32_t retVal;
-	uint8_t tempVal;
+	char tempVal[1] = {0x00};
 
-	retVal = readRegisterRegion(SFE_ENS160_DEVICE_STATUS, &tempVal, 1);
+	retVal = this->readRegisterRegion(SFE_ENS160_DEVICE_STATUS, tempVal);
 
 	if( retVal != 0 )
 		return false; 
 
-	tempVal &= 0x80;
+	tempVal[0] &= 0x80;
 
-	if( tempVal == 0x80 )
+	if( tempVal[0] == 0x80 )
 		return true;
 
 	return false;
@@ -558,16 +571,16 @@
 bool ENS160::getOperationError()
 {
 	int32_t retVal;
-	uint8_t tempVal; 
+	char tempVal[1] = {0x00}; 
 
-	retVal = readRegisterRegion(SFE_ENS160_DEVICE_STATUS, &tempVal, 1);
+	retVal = this->readRegisterRegion(SFE_ENS160_DEVICE_STATUS, tempVal);
 
 	if( retVal != 0 )
 		return false; 
 
-	tempVal &= 0x40;
+	tempVal[0] &= 0x40;
 
-	if( tempVal == 0x40 )
+	if( tempVal[0] == 0x40 )
 		return true;
 
 	return false;
@@ -587,16 +600,16 @@
 uint8_t ENS160::getAQI()
 {
 	int32_t retVal;
-	uint8_t tempVal; 
+	char tempVal[1] = {0x00}; 
 
-	retVal = readRegisterRegion(SFE_ENS160_DATA_AQI, &tempVal, 1);
+	retVal = this->readRegisterRegion(SFE_ENS160_DATA_AQI, tempVal);
 
 	if( retVal != 0 )
 		return 0;
 	
-	tempVal = (tempVal & 0x03);
+	tempVal[0] = (tempVal[0] & 0x03);
 
-	return tempVal;
+	return tempVal[0];
 }
 
 //////////////////////////////////////////////////////////////////////////////
@@ -608,9 +621,9 @@
 {
 	int32_t retVal;
 	uint16_t tvoc; 
-	uint8_t tempVal[2] = {0}; 
+	char tempVal[2] = {0}; 
 
-	retVal = readRegisterRegion(SFE_ENS160_DATA_TVOC, tempVal, 2);
+	retVal = this->readRegisterRegion(SFE_ENS160_DATA_TVOC, tempVal, 2);
 
 	if( retVal != 0 )
 		return 0;
@@ -634,9 +647,9 @@
 {
 	int32_t retVal;
 	uint16_t ethanol; 
-	uint8_t tempVal[2] = {0}; 
+	char tempVal[2] = {0}; 
 
-	retVal = readRegisterRegion(SFE_ENS160_DATA_ETOH, tempVal, 2);
+	retVal = this->readRegisterRegion(SFE_ENS160_DATA_ETOH, tempVal, 2);
 
 	if( retVal != 0 )
 		return 0;
@@ -657,9 +670,9 @@
 {
 	int32_t retVal;
 	uint16_t eco; 
-	uint8_t tempVal[2] = {0}; 
+	char tempVal[2] = {0}; 
 
-	retVal = readRegisterRegion(SFE_ENS160_DATA_ECO2, tempVal, 2);
+	retVal = this->readRegisterRegion(SFE_ENS160_DATA_ECO2, tempVal, 2);
 
 	if( retVal != 0 )
 		return 0;
@@ -681,12 +694,12 @@
 	int32_t retVal;
 	float temperature; 
 	int16_t tempConversion; 
-	uint8_t tempVal[2] = {0}; 
+	char tempVal[2] = {0}; 
 
-	retVal = readRegisterRegion(SFE_ENS160_DATA_T, tempVal, 2);
+	retVal = this->readRegisterRegion(SFE_ENS160_DATA_T, tempVal, 2);
 
 	if( retVal != 0 )
-		return 0;
+		return -1.0;
 	
 	tempConversion = tempVal[0];
 	tempConversion |= (tempVal[1] << 8);
@@ -722,12 +735,12 @@
 {
 	int32_t retVal;
 	uint16_t rh; 
-	uint8_t tempVal[2] = {0}; 
+	char tempVal[2] = {0}; 
 
-	retVal = readRegisterRegion(SFE_ENS160_DATA_RH, tempVal, 2);
+	retVal = this->readRegisterRegion(SFE_ENS160_DATA_RH, tempVal, 2);
 
 	if( retVal != 0 )
-		return 0;
+		return -1.0;
 	
 	rh = tempVal[0];
 	rh |= tempVal[1] << 8;