-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCCS811.h
117 lines (95 loc) · 2.97 KB
/
CCS811.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
#ifndef IAQ_CCS811_H
#define IAQ_CCS811_H
#include <cstring>
#include <memory>
#include <string>
#include <utility>
#include <unistd.h>
#include <vector>
#include <iostream>
#include <thread>
// CCS811 interface per specifications in
// https://cdn.sparkfun.com/assets/learn_tutorials/1/4/3/CCS811_Datasheet-DS000459.pdf
class CCS811 {
public:
CCS811(std::string i2c_dev_name, uint8_t ccs811_addr);
~CCS811();
void read_sensors();
uint16_t get_co2();
uint16_t get_tvoc();
void set_env_data(double rel_humidity, double temperature);
struct MailboxInfo {
uint8_t id;
size_t size;
bool readable;
bool writeable;
};
enum Mailbox {
STATUS,
MEAS_MODE,
ALG_RESULT_DATA,
RAW_DATA,
ENV_DATA,
NTC,
THRESHOLDS,
BASELINE,
HW_ID,
HW_VERSION,
FW_BOOT_VERSION,
FW_APP_VERSION,
ERROR_ID,
SW_RESET
};
enum Commands {
APP_START = 0xF4
};
private:
const std::string i2c_dev_name;
const uint8_t ccs811_addr;
int i2c_fd = -1;
time_t last_measurement = 0;
uint16_t co2 = 0;
uint16_t tvoc = 0;
MailboxInfo mailbox_info(Mailbox m) {
// These values should correspond to the Mailbox values above.
static MailboxInfo mailbox_info[] = {
/* addr, size, readable, writeable */
/* STATUS */ {0x0, 1, true, false},
/* MEAS_MODE */
{0x1, 1, true, true},
/* ALG_RESULT_DATA */
{0x2, 8, true, false},
/* RAW_DATA */
{0x3, 2, true, false},
/* ENV_DATA */
{0x5, 4, false, true},
/* NTC */
{0x6, 4, true, false},
/* THRESHOLDS */
{0x10, 5, false, true},
/* BASELINE */
{0x11, 2, true, true},
/* HW_ID */
{0x20, 1, true, false},
/* HW_VERSION */
{0x21, 1, true, false},
/* FW_BOOT_VERSION */
{0x23, 2, true, false},
/* FW_APP_VERSION */
{0x24, 2, true, false},
/* ERROR_ID */
{0xE0, 1, true, false},
/* SW_RESET */
{0xFF, 4, false, true}
};
return mailbox_info[m];
}
void init();
void open_device();
std::unique_ptr<std::vector<uint8_t>> read_mailbox(Mailbox m);
void write_to_mailbox(Mailbox m, uint8_t *buffer, size_t buffer_len);
void write_data(uint8_t *buffer, size_t buffer_len);
int version_to_str(uint8_t version, char *buffer);
void close_device() const;
};
#endif //IAQ_CCS811_H