Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cut down in libs #1

Merged
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
57 changes: 49 additions & 8 deletions lib/lib_i2c/ScioSense_ENS16x/src/ScioSense_ENS16x.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ ScioSense_ENS16x::ScioSense_ENS16x(uint8_t slaveaddr) {
this->_nCS = 0;
}

#ifndef ENS16x_DISABLE_ENHANCED_FEATURES
ScioSense_ENS16x::ScioSense_ENS16x(uint8_t ADDR, uint8_t nCS, uint8_t nINT) {
this->_slaveaddr = ENS16x_I2CADDR_0;

Expand All @@ -44,10 +45,12 @@ void ScioSense_ENS16x::setI2C(uint8_t sda, uint8_t scl) {
this->_sdaPin = sda;
this->_sclPin = scl;
}
#endif

// Init I2C communication, resets ENS16x and checks its PART_ID. Returns false on I2C problems or wrong PART_ID.
bool ScioSense_ENS16x::begin(bool debug)
{
#ifndef ENS16x_DISABLE_DEBUG
debugENS16x = debug;

//Set pin levels
Expand All @@ -60,12 +63,15 @@ bool ScioSense_ENS16x::begin(bool debug)
pinMode(this->_nCS, OUTPUT);
digitalWrite(this->_nCS, HIGH);
}

#endif

//init I2C
_i2c_init();
#ifndef ENS16x_DISABLE_DEBUG
if (debugENS16x) {
Serial.println("begin() - I2C init done");
}
#endif
delay(ENS16x_BOOTING); // Wait to boot after reset

this->_available = false;
Expand All @@ -78,9 +84,11 @@ bool ScioSense_ENS16x::begin(bool debug)
this->_available = this->clearCommand();
this->_available = this->getFirmware();
}
#ifndef ENS16x_DISABLE_DEBUG
if (debugENS16x) {
Serial.println("ENS16x in idle mode");
}
#endif
return this->_available;
}

Expand All @@ -89,10 +97,12 @@ bool ScioSense_ENS16x::reset(void)
{
uint8_t result = this->write8(_slaveaddr, ENS16x_REG_OPMODE, ENS16x_OPMODE_RESET);

#ifndef ENS16x_DISABLE_DEBUG
if (debugENS16x) {
Serial.print("reset() result: ");
Serial.println(result == 0 ? "ok" : "nok");
}
#endif
delay(ENS16x_BOOTING); // Wait to boot after reset

return result == 0;
Expand All @@ -107,12 +117,14 @@ bool ScioSense_ENS16x::checkPartID(void) {
this->read(_slaveaddr, ENS16x_REG_PART_ID, i2cbuf, 2);
part_id = i2cbuf[0] | ((uint16_t)i2cbuf[1] << 8);

#ifndef ENS16x_DISABLE_DEBUG
if (debugENS16x) {
Serial.print("checkPartID() result: ");
if (part_id == ENS160_PARTID) Serial.println("ENS160 ok");
else if (part_id == ENS161_PARTID) Serial.println("ENS161 ok");
else Serial.println("nok");
}
#endif
delay(ENS16x_BOOTING); // Wait to boot after reset

if (part_id == ENS160_PARTID) { this->_revENS16x = 0; result = true; }
Expand All @@ -128,17 +140,21 @@ bool ScioSense_ENS16x::clearCommand(void) {

result = this->write8(_slaveaddr, ENS16x_REG_COMMAND, ENS16x_COMMAND_NOP);
result = this->write8(_slaveaddr, ENS16x_REG_COMMAND, ENS16x_COMMAND_CLRGPR);
#ifndef ENS16x_DISABLE_DEBUG
if (debugENS16x) {
Serial.print("clearCommand() result: ");
Serial.println(result == 0 ? "ok" : "nok");
}
#endif
delay(ENS16x_BOOTING); // Wait to boot after reset

status = this->read8(_slaveaddr, ENS16x_REG_DATA_STATUS);
#ifndef ENS16x_DISABLE_DEBUG
if (debugENS16x) {
Serial.print("clearCommand() status: 0x");
Serial.println(status, HEX);
}
#endif
delay(ENS16x_BOOTING); // Wait to boot after reset

return result == 0;
Expand All @@ -163,13 +179,15 @@ bool ScioSense_ENS16x::getFirmware() {
if (this->_fw_ver_major > 6) this->_revENS16x = 1;
else this->_revENS16x = 0;

#ifndef ENS16x_DISABLE_DEBUG
if (debugENS16x) {
Serial.println(this->_fw_ver_major);
Serial.println(this->_fw_ver_minor);
Serial.println(this->_fw_ver_build);
Serial.print("getFirmware() result: ");
Serial.println(result == 0 ? "ok" : "nok");
}
#endif
delay(ENS16x_BOOTING); // Wait to boot after reset

return result == 0;
Expand All @@ -183,16 +201,18 @@ bool ScioSense_ENS16x::setMode(uint8_t mode) {
if ((mode == ENS161_OPMODE_LP) and (_revENS16x == 0)) result = 1;
else result = this->write8(_slaveaddr, ENS16x_REG_OPMODE, mode);

#ifndef ENS16x_DISABLE_DEBUG
//if (debugENS16x) {
Serial.print("setMode() activate result: ");
Serial.println(result == 0 ? "ok" : "nok");
//}

#endif
delay(ENS16x_BOOTING); // Wait to boot after reset

return result == 0;
}

#ifndef ENS16x_DISABLE_ENHANCED_FEATURES
// Initialize definition of custom mode with <n> steps
bool ScioSense_ENS16x::initCustomMode(uint16_t stepNum) {
uint8_t result;
Expand All @@ -211,16 +231,20 @@ bool ScioSense_ENS16x::initCustomMode(uint16_t stepNum) {

return result == 0;
}
#endif

#ifndef ENS16x_DISABLE_ENHANCED_FEATURES
// Add a step to custom measurement profile with definition of duration, enabled data acquisition and temperature for each hotplate
bool ScioSense_ENS16x::addCustomStep(uint16_t time, bool measureHP0, bool measureHP1, bool measureHP2, bool measureHP3, uint16_t tempHP0, uint16_t tempHP1, uint16_t tempHP2, uint16_t tempHP3) {
uint8_t seq_ack;
uint8_t temp;

#ifndef ENS16x_DISABLE_DEBUG
if (debugENS16x) {
Serial.print("setCustomMode() write step ");
Serial.println(this->_stepCount);
}
#endif
delay(ENS16x_BOOTING); // Wait to boot after reset

temp = (uint8_t)(((time / 24)-1) << 6);
Expand Down Expand Up @@ -258,6 +282,7 @@ bool ScioSense_ENS16x::addCustomStep(uint16_t time, bool measureHP0, bool measur
}

}
#endif

// Perform prediction measurement and stores result in internal variables
bool ScioSense_ENS16x::measure(bool waitForNew) {
Expand All @@ -266,18 +291,21 @@ bool ScioSense_ENS16x::measure(bool waitForNew) {
bool newData = false;

// Set default status for early bail out
#ifndef ENS16x_DISABLE_DEBUG
if (debugENS16x) Serial.println("Start measurement");

#endif

if (waitForNew) {
do {
delay(10);
status = this->read8(_slaveaddr, ENS16x_REG_DATA_STATUS);

#ifndef ENS16x_DISABLE_DEBUG
if (debugENS16x) {
Serial.print("Status: ");
Serial.println(status);
}

#endif
} while (!IS_NEWDAT(status));
} else {
status = this->read8(_slaveaddr, ENS16x_REG_DATA_STATUS);
Expand All @@ -297,24 +325,29 @@ bool ScioSense_ENS16x::measure(bool waitForNew) {
return newData;
}

#ifndef ENS16x_DISABLE_ENHANCED_FEATURES
// Perfrom raw measurement and stores result in internal variables
bool ScioSense_ENS16x::measureRaw(bool waitForNew) {
uint8_t i2cbuf[8];
uint8_t status;
bool newData = false;

// Set default status for early bail out
#ifndef ENS16x_DISABLE_DEBUG
if (debugENS16x) Serial.println("Start measurement");

#endif

if (waitForNew) {
do {
delay(10);
status = this->read8(_slaveaddr, ENS16x_REG_DATA_STATUS);

#ifndef ENS16x_DISABLE_DEBUG
if (debugENS16x) {
Serial.print("Status: ");
Serial.println(status);
}
#endif
} while (!IS_NEWGPR(status));
} else {
status = this->read8(_slaveaddr, ENS16x_REG_DATA_STATUS);
Expand Down Expand Up @@ -344,7 +377,6 @@ bool ScioSense_ENS16x::measureRaw(bool waitForNew) {
return newData;
}


// Writes t (degC) and h (%rh) to ENV_DATA. Returns false on I2C problems.
bool ScioSense_ENS16x::set_envdata(float t, float h) {

Expand Down Expand Up @@ -372,12 +404,17 @@ bool ScioSense_ENS16x::set_envdata210(uint16_t t, uint16_t h) {

return result;
}
#endif

/**************************************************************************/

void ScioSense_ENS16x::_i2c_init() {
if (this->_sdaPin != this->_sclPin) Wire.begin(this->_sdaPin, this->_sclPin);
else Wire.begin();
#ifndef ENS16x_DISABLE_ENHANCED_FEATURES
if (this->_sdaPin != this->_sclPin)
Wire.begin(this->_sdaPin, this->_sclPin);
else
#endif
Wire.begin();
}

/**************************************************************************/
Expand All @@ -393,12 +430,14 @@ uint8_t ScioSense_ENS16x::read(uint8_t addr, uint8_t reg, uint8_t *buf, uint8_t
uint8_t pos = 0;
uint8_t result = 0;

#ifndef ENS16x_DISABLE_DEBUG
if (debugENS16x) {
Serial.print("I2C read address: 0x");
Serial.print(addr, HEX);
Serial.print(" - register: 0x");
Serial.println(reg, HEX);
}
#endif

//on arduino we need to read in 32 byte chunks
while(pos < num){
Expand Down Expand Up @@ -426,6 +465,7 @@ uint8_t ScioSense_ENS16x::write8(uint8_t addr, byte reg, byte value) {
}

uint8_t ScioSense_ENS16x::write(uint8_t addr, uint8_t reg, uint8_t *buf, uint8_t num) {
#ifndef ENS16x_DISABLE_DEBUG
if (debugENS16x) {
Serial.print("I2C write address: 0x");
Serial.print(addr, HEX);
Expand All @@ -438,6 +478,7 @@ uint8_t ScioSense_ENS16x::write(uint8_t addr, uint8_t reg, uint8_t *buf, uint8_t
}
Serial.println();
}
#endif

Wire.beginTransmission((uint8_t)addr);
Wire.write((uint8_t)reg);
Expand Down
13 changes: 12 additions & 1 deletion lib/lib_i2c/ScioSense_ENS16x/src/ScioSense_ENS16x.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
#ifndef __SCIOSENSE_ENS16x_H_
#define __SCIOSENSE_ENS16x_H_

#define ENS16x_DISABLE_DEBUG
#define ENS16x_DISABLE_ENHANCED_FEATURES

#if (ARDUINO >= 100)
#include "Arduino.h"
#else
Expand Down Expand Up @@ -103,8 +106,10 @@ class ScioSense_ENS16x {

public:
ScioSense_ENS16x(uint8_t slaveaddr = ENS16x_I2CADDR_0); // Constructor using slave address (5A or 5B)
#ifndef ENS16x_DISABLE_ENHANCED_FEATURES
ScioSense_ENS16x(uint8_t ADDR, uint8_t nCS, uint8_t nINT); // Constructor with pin definition
ScioSense_ENS16x(uint8_t slaveaddr, uint8_t ADDR, uint8_t nCS, uint8_t nINT); // Constructor with slave address and pin definition
#endif

void setI2C(uint8_t sda, uint8_t scl); // Function to redefine I2C pins

Expand All @@ -113,14 +118,18 @@ class ScioSense_ENS16x {
uint8_t revENS16x() { return this->_revENS16x; } // Report version of sensor (0: ENS16x, 1: ENS161)
bool setMode(uint8_t mode); // Set operation mode of sensor

#ifndef ENS16x_DISABLE_ENHANCED_FEATURES
bool initCustomMode(uint16_t stepNum); // Initialize definition of custom mode with <n> steps
bool addCustomStep(uint16_t time, bool measureHP0, bool measureHP1, bool measureHP2, bool measureHP3, uint16_t tempHP0, uint16_t tempHP1, uint16_t tempHP2, uint16_t tempHP3);
// Add a step to custom measurement profile with definition of duration, enabled data acquisition and temperature for each hotplate

#endif

bool measure(bool waitForNew = true); // Perform measurement and stores result in internal variables
#ifndef ENS16x_DISABLE_ENHANCED_FEATURES
bool measureRaw(bool waitForNew = true); // Perform raw measurement and stores result in internal variables
bool set_envdata(float t, float h); // Writes t (degC) and h (%rh) to ENV_DATA. Returns "0" if I2C transmission is successful
bool set_envdata210(uint16_t t, uint16_t h); // Writes t and h (in ENS210 format) to ENV_DATA. Returns "0" if I2C transmission is successful
#endif
uint8_t getMajorRev() { return this->_fw_ver_major; } // Get major revision number of used firmware
uint8_t getMinorRev() { return this->_fw_ver_minor; } // Get minor revision number of used firmware
uint8_t getBuild() { return this->_fw_ver_build; } // Get build revision number of used firmware
Expand All @@ -146,7 +155,9 @@ class ScioSense_ENS16x {
uint8_t _sdaPin = 0;
uint8_t _sclPin = 0;

#ifndef ENS16x_DISABLE_DEBUG
bool debugENS16x = false;
#endif

bool reset(); // Sends a reset to the ENS16x. Returns false on I2C problems.
bool checkPartID(); // Reads the part ID and confirms valid sensor
Expand Down
Loading