forked from jhschwartz/sm933X
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SM9333.cpp
85 lines (71 loc) · 2.28 KB
/
SM9333.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// SM9333.cpp
#include "SM9333.h"
#include <Wire.h>
#include <assert.h>
#include <math.h>
SM9333::SM9333() {}
bool SM9333::begin() {
Wire.begin();
return true;
}
bool SM9333::isConnected() {
// do an actual check that the address can be reached??
return true;
}
double SM9333::readPressure() {
pressureTemperaturePair pair = readBoth();
return pair.pressure;
}
double SM9333::readTemperature() {
pressureTemperaturePair pair = readBoth();
return pair.temperature;
}
pressureTemperaturePair SM9333::readBoth() {
// int command[3] = {0x2E, 0x5B, 0xDB};
// commandSequence seq = {command, 3};
// writer(seq);
int* result = doRead(7, true, 0x2E);
int temp_low_bit = result[0];
int temp_high_bit = result[1];
int pres_low_bit = result[2];
int pres_high_bit = result[3];
pressureTemperaturePair pair = {
calculatePressure(pres_low_bit, pres_high_bit),
calculateTemperature(temp_low_bit, temp_high_bit)
};
return pair;
}
double SM9333::calculatePressure(int pressureLowByte, int pressureHighByte) {
int digital = pressureLowByte | pressureHighByte << 8;
double p_pc = 100.0/(pow(2,16)-1)*double(digital) + 100.0*pow(2,16)/(2.0*(pow(2,16)-1));
// the datasheet calculation of p_p appears to be a mistake. -P_MIN instead of +.
double p_p = (P_MIN-P_MAX)/80.0 * p_pc - P_MIN - (P_MIN-P_MAX)/8.0;
return p_p;
}
double SM9333::calculateTemperature(int temperatureLowBit, int temperatureHighBit) {
int digital = temperatureLowBit | temperatureHighBit << 8;
double temp = (double(digital) - b0)/b1;
return temp;
}
void SM9333::writer(commandSequence seq) {
Wire.beginTransmission(SM9333_UNPROTECTED);
for (int i = 0; i < seq.length; i++) {
int val = seq.sequence[i];
// Wire.write((byte)val);
Wire.write(val);
}
Wire.endTransmission();
}
int* SM9333::doRead(int numBits, bool crcProtected, int location) {
int crcLoc = crcProtected ? SM9333_CRC_PROTECTED : SM9333_UNPROTECTED;
int command[3] = {location, 0x5B, 0xDB};
commandSequence seq = {command, 3};
writer(seq);
Wire.requestFrom(crcLoc, numBits);
static int result[8];
//int* result;
for (int i = 0; i < numBits; i++) {
*(result+i) = Wire.read();
}
return result;
}