-
Notifications
You must be signed in to change notification settings - Fork 39
/
Ezo_i2c.cpp
140 lines (110 loc) · 3.01 KB
/
Ezo_i2c.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
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
#include "Ezo_i2c.h"
#include "Arduino.h"
#include "Wire.h"
#include <stdlib.h>
Ezo_board::Ezo_board(uint8_t address){
this->i2c_address = address;
}
Ezo_board::Ezo_board(uint8_t address, const char* name){
this->i2c_address = address;
this->name = name;
}
Ezo_board::Ezo_board(uint8_t address, TwoWire* wire) : Ezo_board(address){
this->wire = wire;
}
Ezo_board::Ezo_board(uint8_t address, const char* name, TwoWire* wire) : Ezo_board(address, name){
this->wire = wire;
}
const char* Ezo_board::get_name(){
return this->name;
}
void Ezo_board::set_name(const char* name){
this->name = name;
}
uint8_t Ezo_board::get_address(){
return i2c_address;
}
void Ezo_board::set_address(uint8_t address){
this->i2c_address = address;
}
void Ezo_board::send_cmd(const char* command) {
Wire.beginTransmission(this->i2c_address);
#ifdef ESP32
Wire.write((const uint8_t*)command, strlen(command));
#else
Wire.write(command);
#endif
Wire.endTransmission();
this->issued_read = false;
}
void Ezo_board::send_read_cmd(){
send_cmd("r");
this->issued_read = true;
}
void Ezo_board::send_cmd_with_num(const char* cmd, float num, uint8_t decimal_amount){
String temp = String(cmd )+ String(num, (unsigned int)decimal_amount);
const char* pointer = temp.c_str();
send_cmd(pointer);
}
void Ezo_board::send_read_with_temp_comp(float temperature){
send_cmd_with_num("rt,", temperature, 3);
this->issued_read = true;
}
enum Ezo_board::errors Ezo_board::receive_read_cmd(){
char _sensordata[this->bufferlen];
this->error = receive_cmd(_sensordata, bufferlen);
if(this->error == SUCCESS){
if(this->issued_read == false){
this->error = NOT_READ_CMD;
}
else{
this->reading = atof(_sensordata);
}
}
return this->error;
}
bool Ezo_board::is_read_poll(){
return this->issued_read;
}
float Ezo_board:: get_last_received_reading(){
return this->reading;
}
enum Ezo_board::errors Ezo_board::get_error(){
return this->error;
}
enum Ezo_board::errors Ezo_board::receive_cmd( char * sensordata_buffer, uint8_t buffer_len) {
byte sensor_bytes_received = 0;
byte code = 0;
byte in_char = 0;
memset(sensordata_buffer, 0, buffer_len); // clear sensordata array;
wire->requestFrom(this->i2c_address, (uint8_t)(buffer_len-1), (uint8_t)1);
code = wire->read();
//wire->beginTransmission(this->i2c_address);
while (wire->available()) {
in_char = wire->read();
if (in_char == 0) {
//wire->endTransmission();
break;
}
else {
sensordata_buffer[sensor_bytes_received] = in_char;
sensor_bytes_received++;
}
}
//should last array point be set to 0 to stop string overflows?
switch (code) {
case 1:
this->error = SUCCESS;
break;
case 2:
this->error = FAIL;
break;
case 254:
this->error = NOT_READY;
break;
case 255:
this->error = NO_DATA;
break;
}
return this->error;
}