GY-63_MS5611/libraries/DS28CM00/DS28CM00.cpp

98 lines
1.8 KiB
C++
Raw Normal View History

2017-07-24 08:02:11 -04:00
//
// FILE: DS28CM00.cpp
// AUTHOR: Rob Tillaart
// PURPOSE: Library for the DS28CM00 unique identification chip.
2022-11-02 06:55:17 -04:00
// VERSION: 0.2.5
2020-11-27 05:10:47 -05:00
// URL: https://github.com/RobTillaart/DS28CM00
2017-07-24 08:02:11 -04:00
//
2022-11-02 06:55:17 -04:00
// HISTORY: see changelog.md
2021-10-26 08:02:32 -04:00
2017-07-24 08:02:11 -04:00
#include "DS28CM00.h"
2022-11-02 06:55:17 -04:00
2021-01-29 06:31:58 -05:00
#define DS28CM00_DEVICEADDRESS 0x50
#define DS28CM00_UIDREGISTER 0x00
#define DS28CM00_CONTROLREGISTER 0x08
2017-07-24 08:02:11 -04:00
2020-11-27 05:10:47 -05:00
DS28CM00::DS28CM00(TwoWire *wire)
{
2021-01-29 06:31:58 -05:00
_wire = wire;
2017-07-24 08:02:11 -04:00
}
2021-10-26 08:02:32 -04:00
2020-11-27 05:10:47 -05:00
#if defined(ESP8266) || defined(ESP32)
DS28CM00::DS28CM00(const uint8_t dataPin, const uint8_t clockPin)
{
2021-01-29 06:31:58 -05:00
_wire = &Wire;
if ((dataPin < 255) && (clockPin < 255))
{
_wire->begin(dataPin, clockPin);
} else {
_wire->begin();
}
2020-11-27 05:10:47 -05:00
}
#endif
2021-12-17 07:06:20 -05:00
bool DS28CM00::begin()
2017-07-24 08:02:11 -04:00
{
2020-11-27 05:10:47 -05:00
_wire->begin();
2021-12-17 07:06:20 -05:00
return setI2CMode();
}
bool DS28CM00::isConnected()
{
_wire->beginTransmission(DS28CM00_DEVICEADDRESS);
int rv = _wire->endTransmission();
return rv == 0;
2017-07-24 08:02:11 -04:00
}
2021-01-29 06:31:58 -05:00
2017-07-24 08:02:11 -04:00
bool DS28CM00::setMode(uint8_t mode)
{
2020-11-27 05:10:47 -05:00
_wire->beginTransmission(DS28CM00_DEVICEADDRESS);
_wire->write(DS28CM00_CONTROLREGISTER);
_wire->write(mode);
int rv = _wire->endTransmission();
2017-07-24 08:02:11 -04:00
return rv == 0;
}
2021-01-29 06:31:58 -05:00
2017-07-24 08:02:11 -04:00
bool DS28CM00::getMode(uint8_t &mode)
{
2021-01-29 06:31:58 -05:00
mode = DS28CM00_MODE_UNKNOWN;
2020-11-27 05:10:47 -05:00
_wire->beginTransmission(DS28CM00_DEVICEADDRESS);
_wire->write(DS28CM00_CONTROLREGISTER);
int rv = _wire->endTransmission();
2017-07-24 08:02:11 -04:00
if (rv != 0) return false;
2020-11-27 05:10:47 -05:00
uint8_t read = _wire->requestFrom(DS28CM00_DEVICEADDRESS, 1);
2017-07-24 08:02:11 -04:00
if (read < 1) return false;
2020-11-27 05:10:47 -05:00
mode = _wire->read();
2017-07-24 08:02:11 -04:00
return true;
}
2021-01-29 06:31:58 -05:00
2017-07-24 08:02:11 -04:00
bool DS28CM00::getUID(uint8_t *buffer)
{
2020-11-27 05:10:47 -05:00
_wire->beginTransmission(DS28CM00_DEVICEADDRESS);
_wire->write(DS28CM00_UIDREGISTER);
int rv = _wire->endTransmission();
2017-07-24 08:02:11 -04:00
if (rv != 0) return false;
2020-11-27 05:10:47 -05:00
uint8_t read = _wire->requestFrom(DS28CM00_DEVICEADDRESS, 8);
2017-07-24 08:02:11 -04:00
if (read < 8) return false;
2020-11-27 05:10:47 -05:00
for (uint8_t i = 0; i < 8; i++) buffer[i] = _wire->read();
2017-07-24 08:02:11 -04:00
return true;
}
2021-10-26 08:02:32 -04:00
2020-11-27 05:10:47 -05:00
// -- END OF FILE --
2021-12-17 07:06:20 -05:00