Compare commits
2 commits
9f9141c5f0
...
2038522e0f
Author | SHA1 | Date | |
---|---|---|---|
2038522e0f | |||
6f7f97c26f |
1 changed files with 24 additions and 5 deletions
|
@ -35,14 +35,21 @@ HM330x::HM330x() {};
|
|||
// Initialize the Sensor
|
||||
void HM330x::initialize(void) {
|
||||
uint8_t retryCount = 0;
|
||||
|
||||
DEBUG_PRINTLN("HM330x::initialize");
|
||||
|
||||
// Wait for Sensor to get Ready
|
||||
DEBUG_PRINTLN("HM330x::initialize Waiting for Sensor Startup");
|
||||
delay(30000);
|
||||
|
||||
DEBUG_PRINTLN("HM330x::initialize Trying to send Select");
|
||||
// Send select command
|
||||
while (!sendCmd(HM330x_SELECT)) {
|
||||
if (retryCount++ >= 0) {
|
||||
if (retryCount++ >= 10) {
|
||||
DEBUG_PRINTLN("HM330x::initialize Select Failed!");
|
||||
return;
|
||||
} else {
|
||||
delay(100);
|
||||
delay(500);
|
||||
DEBUG_PRINTLN("HM330x::initialize Retrying Select...");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -56,13 +63,18 @@ uint8_t HM330x::getSensorData(char *payload, uint8_t startbyte) {
|
|||
|
||||
// Initialize Payload with 0s
|
||||
for (uint8_t i=startbyte; i < startbyte+6; i++)
|
||||
payload[i] = 0;
|
||||
payload[i] = 0xFF;
|
||||
|
||||
// Get Data from the Sensors
|
||||
Wire.requestFrom(HM330x_ADDR, HM330x_DATA_LEN);
|
||||
if (Wire.available()) {
|
||||
for (uint8_t i = 0; i<HM330x_DATA_LEN; i++)
|
||||
DEBUG_PRINT("HM330x::getSensorData reading I2C:")
|
||||
for (uint8_t i = 0; i<HM330x_DATA_LEN; i++){
|
||||
data[i] = Wire.read();
|
||||
DEBUG_PRINT(" 0x");
|
||||
DEBUG_PRINT(data[i], HEX);
|
||||
}
|
||||
DEBUG_PRINTLN("");
|
||||
|
||||
if (calcSum(data) == data[HM330x_DATA_LEN-1]) {
|
||||
for (uint8_t pos = 5; pos<8; pos++) {
|
||||
|
@ -71,6 +83,9 @@ uint8_t HM330x::getSensorData(char *payload, uint8_t startbyte) {
|
|||
startbyte += 2;
|
||||
}
|
||||
return startbyte;
|
||||
} else {
|
||||
for (uint8_t i=startbyte; i < startbyte+6; i++)
|
||||
payload[i] = 0xEE;
|
||||
}
|
||||
}
|
||||
return startbyte+6;
|
||||
|
@ -78,6 +93,8 @@ uint8_t HM330x::getSensorData(char *payload, uint8_t startbyte) {
|
|||
|
||||
// Send only a Command
|
||||
bool HM330x::sendCmd(uint8_t cmd) {
|
||||
DEBUG_PRINT("HM330x::sendCmd: 0x")
|
||||
DEBUG_PRINTLN(cmd, HEX);
|
||||
Wire.beginTransmission(HM330x_ADDR);
|
||||
Wire.write(cmd);
|
||||
return Wire.endTransmission();
|
||||
|
@ -89,6 +106,8 @@ uint8_t HM330x::calcSum(uint8_t bytes[]) {
|
|||
for (int i = 0; i < HM330x_DATA_LEN - 1; i++) {
|
||||
checksum += bytes[i];
|
||||
}
|
||||
DEBUG_PRINT("HM330x::calcSum: 0x");
|
||||
DEBUG_PRINTLN(checksum, HEX);
|
||||
return checksum;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue