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

P164 gases ens160 #4910

Merged
merged 19 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
17 changes: 11 additions & 6 deletions src/_P164_gases_ens160.ino
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ boolean Plugin_164(uint8_t function, struct EventStruct *event, String& string)
Device[deviceCount].TimerOption = true;
Device[deviceCount].GlobalSyncOption = true;
Device[deviceCount].PluginStats = true;
Device[deviceCount].I2CNoDeviceCheck = true; //TODO
Device[deviceCount].I2CNoDeviceCheck = true;
break;
}

Expand All @@ -61,7 +61,7 @@ boolean Plugin_164(uint8_t function, struct EventStruct *event, String& string)
case PLUGIN_I2C_HAS_ADDRESS:
case PLUGIN_WEBFORM_SHOW_I2C_PARAMS:
{
const uint8_t i2cAddressValues[] = { ENS160_I2CADDR_0, ENS160_I2CADDR_1 };
const uint8_t i2cAddressValues[] = { P164_ENS160_I2CADDR_0, P164_ENS160_I2CADDR_1 };
constexpr int nrAddressOptions = NR_ELEMENTS(i2cAddressValues);

if (function == PLUGIN_WEBFORM_SHOW_I2C_PARAMS) {
Expand All @@ -85,7 +85,7 @@ boolean Plugin_164(uint8_t function, struct EventStruct *event, String& string)

case PLUGIN_SET_DEFAULTS:
{
P164_PCONFIG_I2C_ADDR = ENS160_I2CADDR_1;
P164_PCONFIG_I2C_ADDR = P164_ENS160_I2CADDR_1;
success = true;
break;
}
Expand All @@ -104,19 +104,24 @@ boolean Plugin_164(uint8_t function, struct EventStruct *event, String& string)
P164_data_struct *P164_data = static_cast<P164_data_struct *>(getPluginTaskData(event->TaskIndex));

if (nullptr == P164_data) {
addLogMove(LOG_LEVEL_ERROR, "P164: plugin_read NULLPTR");
addLogMove(LOG_LEVEL_ERROR, F("P164: plugin_read NULLPTR"));
break;
}

float temperature = 20.0f; // A reasonable value in case temperature source task is invalid
float humidity = 50.0f; // A reasonable value in case humidity source task is invalid
float tvoc = 0.0f; // tvoc value to be retrieved from device
float eco2 = 0.0f; // eCO2 value to be retrieved from device

if (validTaskIndex(P164_PCONFIG_TEMP_TASK) && validTaskIndex(P164_PCONFIG_HUM_TASK))
{
// we're checking a var from another task, so calculate that basevar
// we're checking a value from other tasks
temperature = UserVar.getFloat(P164_PCONFIG_TEMP_TASK, P164_PCONFIG_TEMP_VAL); // in degrees C
humidity = UserVar.getFloat(P164_PCONFIG_HUM_TASK, P164_PCONFIG_HUM_VAL); // in % relative
}
success = P164_data->read(UserVar[event->BaseVarIndex], UserVar[event->BaseVarIndex + 1], temperature, humidity);
success = P164_data->read(tvoc, eco2, temperature, humidity);
UserVar.setFloat(event->TaskIndex, 0, tvoc);
UserVar.setFloat(event->TaskIndex, 1, eco2);
break;
}

Expand Down
19 changes: 14 additions & 5 deletions src/src/PluginStructs/P164_data_struct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

// A curious delay inserted in the original code [ms]
#define ENS160_BOOTING 10
// Max time for device to react on a reset [ms]
#define ENS160_MAXBOOTING 2000

// ENS160 registers for version V0
#define ENS160_REG_PART_ID 0x00 // 2 byte register for part identification
Expand Down Expand Up @@ -79,7 +81,7 @@
#define ENS160_OPMODE_ULP 0x04 // ULTRA LOW POWER (ENS161 only)
#define ENS160_OPMODE_CUSTOM 0xC0 // Not specified in datasheet

// ENS160 undefined bitfields?
// ENS160 unspecified bitfields?
#define ENS160_BL_CMD_START 0x02
#define ENS160_BL_CMD_ERASE_APP 0x04
#define ENS160_BL_CMD_ERASE_BLINE 0x06
Expand All @@ -89,17 +91,21 @@
#define ENS160_BL_CMD_GET_APPVER 0x0E
#define ENS160_BL_CMD_EXITBL 0x12

// ENS160 undefined bitfields?
// ENS160 unspecified bitfields?
#define ENS160_SEQ_ACK_NOTCOMPLETE 0x80
#define ENS160_SEQ_ACK_COMPLETE 0xC0

#define IS_ENS160_SEQ_ACK_NOT_COMPLETE(x) (ENS160_SEQ_ACK_NOTCOMPLETE == (ENS160_SEQ_ACK_NOTCOMPLETE & (x)))
#define IS_ENS160_SEQ_ACK_COMPLETE(x) (ENS160_SEQ_ACK_COMPLETE == (ENS160_SEQ_ACK_COMPLETE & (x)))

// ENS160 STATUS bitfields
#define ENS160_STATUS_STATAS 0x80 // STATAS: Indicates that an OPMODE is rumming
#define ENS160_STATUS_STATAS 0x80 // STATAS: Indicates that an OPMODE is running
#define ENS160_STATUS_STATER 0x40 // STATER: High indicated that an error is detected
#define ENS160_STATUS_VALIDITY 0x0C // VALIDITY FLAG
#define ENS160_STATUS_VAL_NORM 0x00 // 0: Normal operation
#define ENS160_STATUS_VAL_WARM 0x01 // 1: Warm-Up phase
#define ENS160_STATUS_VAL_NOUSE 0x02 // 2: Not used
#define ENS160_STATUS_VAL_INVAL 0x03 // 3: Invalid output
#define ENS160_STATUS_NEWDAT 0x02 // NEWDAT: 1= New data in data registers available
#define ENS160_STATUS_NEWGPR 0x01 // NEWGRP: 1= New data in GRP_READ registers available

Expand Down Expand Up @@ -350,13 +356,16 @@ bool P164_data_struct::evaluateState()
{
newState = P164_STATE_STARTING1;
}
else if (timePassedSince(this->_lastChange) > ENS160_MAXBOOTING) {
newState = P164_STATE_ERROR;
}
}
break;
case P164_STATE_STARTING1:
// A valid device is found, check if its status is ready to continue
this->_available = false;
this->getStatus();
if (GET_STATUS_VALIDITY(this->_statusReg) == 0)
if (GET_STATUS_VALIDITY(this->_statusReg) == ENS160_STATUS_VAL_NORM)
{
this->writeMode(ENS160_OPMODE_IDLE);
this->clearCommand();
Expand All @@ -366,7 +375,7 @@ bool P164_data_struct::evaluateState()
case P164_STATE_STARTING2:
this->_available = false;
this->getStatus();
if (GET_STATUS_VALIDITY(this->_statusReg) == 0) {
if (GET_STATUS_VALIDITY(this->_statusReg) == ENS160_STATUS_VAL_NORM) {
this->getFirmware();
newState = P164_STATE_IDLE;
}
Expand Down
6 changes: 3 additions & 3 deletions src/src/PluginStructs/P164_data_struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
#define P164_PCONFIG_HUM_VAL PCONFIG(4)

// 7-bit I2C slave address of the ENS160
#define ENS160_I2CADDR_0 0x52 //ADDR low
#define ENS160_I2CADDR_1 0x53 //ADDR high
#define P164_ENS160_I2CADDR_0 0x52 //ADDR low
#define P164_ENS160_I2CADDR_1 0x53 //ADDR high

// Use a state machine to avoid blocking the CPU while waiting for the response
// See P164_data_struct.cpp for detailed description
Expand Down Expand Up @@ -96,7 +96,7 @@ struct P164_data_struct : public PluginTaskData_base {
bool writeMode(uint8_t mode); // Write the opmode register
void moveToState(P164_state newState); // Trigger a state change

uint8_t i2cAddress = ENS160_I2CADDR_0; // The I2C address of the connected device
uint8_t i2cAddress = P164_ENS160_I2CADDR_0; // The I2C address of the connected device

P164_state _state = P164_STATE_INITIAL; // General state of the software
ulong _lastChange = 0u; // Timestamp of last state transition
Expand Down