-
Notifications
You must be signed in to change notification settings - Fork 0
/
i2c_utils.cpp
220 lines (184 loc) · 6.39 KB
/
i2c_utils.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include "Arduino.h" //import byte and Serial definitions
#include <Wire.h>
#include "i2c_utils.h"
//---------------------------------------------
// I2C
//---------------------------------------------
const int SLAVE_ADDRESS = 42;
/*
int sendCommandByte (int cmd, const int responseSize)
{
Wire.beginTransmission (SLAVE_ADDRESS);
//Wire.write (cmd);
Wire.write ((byte *) &cmd, sizeof(cmd));
Wire.endTransmission ();
int received = Wire.requestFrom (SLAVE_ADDRESS, responseSize);
Serial.printf("received size:%d \r\n",received);
return received;
}
*/
// Send Command without values, just to retrieve, for GETS
bool sendCommand (int code, int responseSize)
{
return sendCommandIFF(code,0.0,0.0,responseSize);
}
// Send Command with 2 float values
bool sendCommandIFF (int code,float v1,float v2, int responseSize)
{
struct {int code;float v1;float v2;} command = {code,v1,v2};
//Serial.printf("Master sendCommand:%d,%0.3f,%0.3f, size: %d \r\n",command.code,command.v1,command.v2,sizeof(command));
//Serial.printf("time: %d, sendCommandIFF:%d,",millis(),command.code);
//Serial.printf("sendCommandIFF: %d",command.code);
Wire.beginTransmission (SLAVE_ADDRESS);
Wire.write ((byte *) &command, sizeof(command));//4+4+4
if (responseSize==0) return Wire.endTransmission()==0;
//https://www.arduino.cc/reference/en/language/functions/communication/wire/endtransmission/
//Returns
//0: success.
//1: data too long to fit in transmit buffer.
//2: received NACK on transmit of address.
//3: received NACK on transmit of data.
//4: other error.
//5: timeout
//responseSize > 0
Wire.endTransmission();
// Verify responseSize == receivedSize
return responseSize == Wire.requestFrom(SLAVE_ADDRESS, responseSize);
}
/*
bool sendandreceiveWResponseIFF (int code,float v1,float v2, int responseSize)
{
struct {int code;float v1;float v2;} command = {code,v1,v2};
//Serial.printf("Master sendCommand:%d,%0.3f,%0.3f, size: %d \r\n",command.code,command.v1,command.v2,sizeof(command));
//Serial.printf("time: %d, sendCommandIFF:%d,",millis(),command.code);
//Serial.printf("sendCommandIFF: %d",command.code);
Wire.beginTransmission (SLAVE_ADDRESS);
Wire.write ((byte *) &command, sizeof(command));//4+4+4
if (Wire.endTransmission()!==0)
return false
//https://www.arduino.cc/reference/en/language/functions/communication/wire/endtransmission/
//Returns
//0: success.
//1: data too long to fit in transmit buffer.
//2: received NACK on transmit of address.
//3: received NACK on transmit of data.
//4: other error.
//5: timeout
// Verify responseSize == receivedSize
return responseSize == Wire.requestFrom(SLAVE_ADDRESS, responseSize);
}
*/
//---------------------------------------------
// I2C Setters and Getters
//---------------------------------------------
//setValue return ack
bool setValue2F(int code,float v1,float v2){
struct {int v1;int v2; } buffer2I ={0,0}; //Esp32: int=4 and long=4
int const responseSize = 8;// Mega2560: long=4 * 2
if(sendCommandIFF(code,v1,v2,responseSize))//8 force request, expect (1,code)
{
Wire.readBytes((byte*)&buffer2I,responseSize);
Serial.printf(" setValue2F, ack:%d, code:%d \r\n" ,buffer2I.v1,buffer2I.v2);
return buffer2I.v1==1;
}
else
{
Serial.println(" FAIL to setValue");
return false;
}
}
//---------------------------------------------------
// For I2C_ROUND_TRIP, send and receive same values+PI
bool setgetValue2F(int code,float v1,float v2, float & out1,float & out2){
struct {float v1;float v2; } buffer2F ={0,0};
int const responseSize = 8;// Mega2560 sizeof(float) * 2
if(sendCommandIFF(code,v1,v2,responseSize))
{
Wire.readBytes((byte*)&buffer2F,responseSize);
//Serial.printf(" getValue: %f:%f \r\n" ,buffer2F.v1,buffer2F.v2);
out1 = buffer2F.v1;
out2 = buffer2F.v2;
return true;
}
else
{
Serial.println(" FAIL to setgetValue2F");
return false;
}
}
//---------------------------------------------------
bool getValue2F(int code,float & f1,float & f2){
struct {float v1;float v2; } buffer2F ={0,0};
int const responseSize = 8;// Mega2560 sizeof(float) * 2
if(sendCommand(code,responseSize))
{
Wire.readBytes((byte*)&buffer2F,responseSize);
//Serial.printf(" getValue: %f:%f \r\n" ,buffer2F.v1,buffer2F.v2);
f1 = buffer2F.v1;
f2 = buffer2F.v2;
return true;
}
else
{
Serial.printf(" FAIL to getValue2F, code: %d \r\n", code);
return false;
}
}
//---------------------------------------------------
bool getValue3F(int code,float & f1,float & f2,float & f3 ){
struct {float v1;float v2;float v3; } buffer3F ={0,0,0};
int const responseSize = 12;// Mega2560 sizeof(float) 4 * 3
if(sendCommand(code,responseSize))
{
Wire.readBytes((byte*)&buffer3F,responseSize);
//Serial.printf(" getValue3F: %f:%f:%f \r\n" ,buffer3F.v1,buffer3F.v2,buffer3F.v3);
f1 = buffer3F.v1;
f2 = buffer3F.v2;
f3 = buffer3F.v3;
return true;
}
else
{
Serial.printf(" FAIL to getValue2F, code: %d \r\n", code);
return false;
}
}
//---------------------------------------------------
//GET: Mode, EndStops, Losed Steps
bool getValue2I(int code,int & a, int & b){
struct {int v1;int v2; } buffer2I ={0,0}; //Esp32 int=4 and long=4
int const responseSize = 8;// Mega2560 sizeof(long) * 2
if(sendCommand(code,responseSize))
{
Wire.readBytes((byte*)&buffer2I,responseSize);
//Serial.printf(" getValue2I: %d:%d \r\n" ,buffer2I.v1,buffer2I.v2);
a = buffer2I.v1;
b = buffer2I.v2;
return true;
}
else
{
Serial.printf(" FAIL to getValue2I, code: %d \r\n", code);
return false;
}
}
bool getValue3I(int code, int & a, int & b, int & c){
struct {int v1; int v2; int v3; } buffer3I ={0,0,0}; //Esp32 int=4 and long=4
int const responseSize = 12;// Mega2560 sizeof(long 4 ) * 3
if(sendCommand(code,responseSize))
{
Wire.readBytes((byte*)&buffer3I,responseSize);
//Serial.printf(" getValue3I buffer3I: %d:%d:%d \r\n" ,buffer3I.v1,buffer3I.v2,buffer3I.v3);
a = buffer3I.v1;
b = buffer3I.v2;
c = buffer3I.v3;
return true;
}
else
{
Serial.printf(" FAIL to getValue3I, code: %d \r\n", code);
return false;
}
}
//bool getValue2I(int code,int & a, int & b, int & c){
//}