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

Add MCP9601 with K-Type Thermocouple #565

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/components/i2c/WipperSnapper_I2C.cpp
Expand Up @@ -420,6 +420,18 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
_si7021->configureDriver(msgDeviceInitReq);
drivers.push_back(_si7021);
WS_DEBUG_PRINTLN("SI7021/SHT20 Initialized Successfully!");
} else if (strcmp("mcp9601", msgDeviceInitReq->i2c_device_name) == 0) {
_mcp9601 = new WipperSnapper_I2C_Driver_MCP9601(this->_i2c, i2cAddress);
if (!_mcp9601->begin()) {
WS_DEBUG_PRINTLN("ERROR: Failed to initialize MCP9601!");
_busStatusResponse =
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
return false;
}
_mcp9601->configureDriver(msgDeviceInitReq);
drivers.push_back(_mcp9601);
WS_DEBUG_PRINTLN(
"MCP9601 Initialized with K-Type thermocouple successfully!");
} else if (strcmp("mcp9808", msgDeviceInitReq->i2c_device_name) == 0) {
_mcp9808 = new WipperSnapper_I2C_Driver_MCP9808(this->_i2c, i2cAddress);
if (!_mcp9808->begin()) {
Expand Down
2 changes: 2 additions & 0 deletions src/components/i2c/WipperSnapper_I2C.h
Expand Up @@ -40,6 +40,7 @@
#include "drivers/WipperSnapper_I2C_Driver_LTR329_LTR303.h"
#include "drivers/WipperSnapper_I2C_Driver_LTR390.h"
#include "drivers/WipperSnapper_I2C_Driver_MAX17048.h"
#include "drivers/WipperSnapper_I2C_Driver_MCP9601.h"
#include "drivers/WipperSnapper_I2C_Driver_MCP9808.h"
#include "drivers/WipperSnapper_I2C_Driver_MPL115A2.h"
#include "drivers/WipperSnapper_I2C_Driver_MPRLS.h"
Expand Down Expand Up @@ -127,6 +128,7 @@ class WipperSnapper_Component_I2C {
WipperSnapper_I2C_Driver_INA219 *_ina219 = nullptr;
WipperSnapper_I2C_Driver_LTR329_LTR303 *_ltr329 = nullptr;
WipperSnapper_I2C_Driver_LTR390 *_ltr390 = nullptr;
WipperSnapper_I2C_Driver_MCP9601 *_mcp9601 = nullptr;
WipperSnapper_I2C_Driver_MCP9808 *_mcp9808 = nullptr;
WipperSnapper_I2C_Driver_MPL115A2 *_mpl115a2 = nullptr;
WipperSnapper_I2C_Driver_MPRLS *_mprls = nullptr;
Expand Down
111 changes: 111 additions & 0 deletions src/components/i2c/drivers/WipperSnapper_I2C_Driver_MCP9601.h
@@ -0,0 +1,111 @@
/*!
* @file WipperSnapper_I2C_Driver_MCP9601.h
*
* Device driver for the MCP9601 Temperature sensor.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Copyright (c) Brent Rubell 2022 for Adafruit Industries.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add your name and current year here

*
* MIT license, all text here must be included in any redistribution.
*
*/
#ifndef WipperSnapper_I2C_Driver_MCP9601_H
#define WipperSnapper_I2C_Driver_MCP9601_H

#include "WipperSnapper_I2C_Driver.h"
#include <Adafruit_MCP9601.h>

/**************************************************************************/
/*!
@brief Class that provides a driver interface for a MCP9601 sensor.
*/
/**************************************************************************/
class WipperSnapper_I2C_Driver_MCP9601 : public WipperSnapper_I2C_Driver {
public:
/*******************************************************************************/
/*!
@brief Constructor for a MCP9601 sensor.
@param i2c
The I2C interface.
@param sensorAddress
7-bit device address.
*/
/*******************************************************************************/
WipperSnapper_I2C_Driver_MCP9601(TwoWire *i2c, uint16_t sensorAddress)
: WipperSnapper_I2C_Driver(i2c, sensorAddress) {
_i2c = i2c;
_sensorAddress = sensorAddress;
}

/*******************************************************************************/
/*!
@brief Destructor for an MCP9601 sensor.
*/
/*******************************************************************************/
~WipperSnapper_I2C_Driver_MCP9601() {
// Called when a MCP9601 component is deleted.
delete _MCP9601;
}

/*******************************************************************************/
/*!
@brief Initializes the MCP9601 sensor and begins I2C.
@returns True if initialized successfully, False otherwise.
*/
/*******************************************************************************/
bool begin() {
_MCP9601 = new Adafruit_MCP9601();
if (!_MCP9601->begin((uint8_t)_sensorAddress, _i2c)) {
return false;
}
_MCP9601->setADCresolution(MCP9600_ADCRESOLUTION_18);
_MCP9601->setThermocoupleType(MCP9600_TYPE_K);
_MCP9601->setFilterCoefficient(3);
return true;
}

/*******************************************************************************/
/*!
@brief Gets the MCP9601's current ambient temperature.
@param tempEvent
Pointer to an Adafruit_Sensor event.
@returns True if the temperature was obtained successfully, False
otherwise.
*/
/*******************************************************************************/
bool getEventAmbientTemp(sensors_event_t *tempEvent) {
tempEvent->temperature = _MCP9601->readAmbient();
return true;
}

/*******************************************************************************/
/*!
@brief Gets the MCP9601's current thermocouple temperature.
@param rawEvent
Pointer to an Adafruit_Sensor event.
@returns True if the temperature was obtained successfully, False
otherwise.
*/
/*******************************************************************************/
bool getEventRaw(sensors_event_t *rawEvent) {
uint8_t status = _MCP9601->getStatus();
if (status & MCP9601_STATUS_OPENCIRCUIT) {
WS_DEBUG_PRINTLN("Thermocouple open!");
return false;
}
if (status & MCP9601_STATUS_SHORTCIRCUIT) {
WS_DEBUG_PRINTLN("Thermocouple shorted to ground!");
return false;
}
rawEvent->data[0] = _MCP9601->readThermocouple();
return true;
}

protected:
Adafruit_MCP9601 *_MCP9601; ///< Pointer to MCP9601 temperature sensor object
};

#endif // WipperSnapper_I2C_Driver_MCP9601