-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyMax30205.h
176 lines (135 loc) · 4.6 KB
/
myMax30205.h
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#pragma once
#include "esphome.h"
typedef enum{ //For configuration registers
SHUTDOWN, // shutdwon mode to reduce power consumption <3.5uA
COMPARATOR, // Bit 0 = operate OS in comparator mode, 1= INTERRUPT MODE
OS_POLARITY, // Polarity bit ;Bit 0 = Active low output, Bit 1 = Active high
FAULT_QUEUE_0, // Fault indication bits
FAULT_QUEUE_1, // Fault indication bits
DATA_FORMAT, //Data Format
TIME_OUT, //Time out
ONE_SHOT //1= One shot, 0 = Continuos
}configuration;
/**
* @brief chip register definition
*/
#define MAX30205_ADDRESS 0x4c // my MAX30205 component
#define MAX30205_ADDRESS1 0x49 // 8bit address converted to 7bit
#define MAX30205_ADDRESS2 0x48 // 8bit address converted to 7bit
// Registers
#define MAX30205_TEMPERATURE 0x00 /**< temperature register */
#define MAX30205_CONFIGURATION 0x01 /**< configure register */
#define MAX30205_THYST 0x02 /**< thyst register */
#define MAX30205_TOS 0x03 /**< tos register */
static const char *const TAG_MAX30205 = "max30205.sensor";
class myMax30205 : public sensor::Sensor, public PollingComponent, public i2c::I2CDevice
{
public:
float fLastTemperature = 0; // Last temperature
uint8_t sensorAddress = MAX30205_ADDRESS;
public:
// constructor
myMax30205() : PollingComponent(60000)
{
}
// ========== INTERNAL METHODS ==========
// (In most use cases you won't need these)
void setup() override
{
begin();
}
/*
void loop() override
{
}
*/
void dump_config()
{
LOG_SENSOR("", "MAX30205", this);
LOG_I2C_DEVICE(this);
if (this->is_failed())
{
ESP_LOGE(TAG_MAX30205, "Communication with BH1750 failed!");
}
LOG_UPDATE_INTERVAL(this);
}
void update() override
{
float fCurrentTemperature = getTemperature();
if (fCurrentTemperature!=this->fLastTemperature)
{
publish_state(fCurrentTemperature);
this->fLastTemperature = fCurrentTemperature;
}
}
float get_setup_priority() const override
{
return esphome::setup_priority::BUS;
}
protected:
bool scanAvailableSensors(void){
bool sensorFound = false;
Wire.beginTransmission (MAX30205_ADDRESS1);
if (Wire.endTransmission () == 0){
sensorAddress = MAX30205_ADDRESS1;
sensorFound = true;
}
Wire.beginTransmission (MAX30205_ADDRESS2);
if(Wire.endTransmission () == 0){
sensorAddress = MAX30205_ADDRESS2;
sensorFound = true;
}
return sensorFound;
}
float getTemperature(void){
uint8_t readRaw[2] = {0};
I2CreadBytes( sensorAddress, MAX30205_TEMPERATURE, &readRaw[0] ,2); // read two bytes
int16_t raw = readRaw[0] << 8 | readRaw[1]; //combine two bytes
float _temperature = ceil( 10 * raw * 0.00390625 ) / 10; // convert to temperature
return _temperature;
}
void shutdown(void){
uint8_t reg = I2CreadByte(sensorAddress, MAX30205_CONFIGURATION); // Get the current register
I2CwriteByte(sensorAddress, MAX30205_CONFIGURATION, reg | 0x80);
}
void begin(void){
I2CwriteByte(sensorAddress, MAX30205_CONFIGURATION, 0x00); //mode config
I2CwriteByte(sensorAddress, MAX30205_THYST , 0x00); // set threshold
I2CwriteByte(sensorAddress, MAX30205_TOS, 0x00); //
}
void printRegisters(void){
Serial.println(I2CreadByte(sensorAddress, MAX30205_TEMPERATURE), BIN);
Serial.println(I2CreadByte(sensorAddress, MAX30205_CONFIGURATION), BIN);
Serial.println(I2CreadByte(sensorAddress, MAX30205_THYST), BIN);
Serial.println(I2CreadByte(sensorAddress, MAX30205_TOS), BIN);
}
// Wire.h read and write protocols
void I2CwriteByte(uint8_t address, uint8_t subAddress, uint8_t data){
Wire.beginTransmission(address); // Initialize the Tx buffer
Wire.write(subAddress); // Put slave register address in Tx buffer
Wire.write(data); // Put data in Tx buffer
Wire.endTransmission(); // Send the Tx buffer
}
uint8_t I2CreadByte(uint8_t address, uint8_t subAddress){
uint8_t data; // `data` will store the register data
Wire.beginTransmission(address);
Wire.write(subAddress);
Wire.endTransmission(false);
Wire.requestFrom(address, (uint8_t) 1);
data = Wire.read();
return data;
}
void I2CreadBytes(uint8_t address, uint8_t subAddress, uint8_t * dest, uint8_t count){
Wire.beginTransmission(address); // Initialize the Tx buffer
// Next send the register to be read. OR with 0x80 to indicate multi-read.
Wire.write(subAddress);
Wire.endTransmission(false);
uint8_t i = 0;
Wire.requestFrom(address, count); // Read bytes from slave register address
while (Wire.available())
{
uint8_t bRead;
dest[i++] = Wire.read();
}
}
};