-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathi2c.cpp
75 lines (61 loc) · 2.06 KB
/
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
/*
i2c.cpp -
Part of Grbl: An embedded CNC Controller with rs274/ngc (g-code) support
Copyright (c) 2010 Albert Daffe
Grbl is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Grbl is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Grbl. If not, see <http://www.gnu.org/licenses/>.
*/
#include <WProgram.h> //all things wiring / arduino
#include <Wire.h>
extern "C" void i2c_init();
extern "C" void i2c_write_value(byte addr, byte config, int32_t value);
extern "C" void i2c_report_position(void);
extern "C" void i2c_get_buttons(void);
extern "C" int32_t actual_position[3]; // The current actual position of the tool in absolute steps
extern "C" int32_t position[3]; // The current target position of the tool in absolute steps
char buttons[4];
int i2c_line=0;
void i2c_init()
{
Wire.begin(); // join i2c bus (address optional for master)
}
void i2c_write_value(byte addr, byte config, int32_t value)
{
Wire.beginTransmission(addr);
Wire.send(config);
Wire.send((char)((value >> 24) & 0xFF));
Wire.send((char)((value >> 16) & 0xFF));
Wire.send((char)((value >> 8) & 0xFF));
Wire.send((char)(value & 0xFF));
Wire.endTransmission();
}
void i2c_report_position(void)
{
if (i2c_line<3){
i2c_write_value(4,128+i2c_line,actual_position[i2c_line]);
} else {
i2c_write_value(4,128+i2c_line,position[i2c_line-3]);
}
i2c_line++;
if (i2c_line>5) i2c_line=0;
}
void i2c_get_buttons(void)
{
Wire.requestFrom(4, 4); // request 4 bytes from slave device #4
char i;
for(i=0;i<4;i++){
if (Wire.available()) {
buttons[i] = Wire.receive();
} else{
buttons[4] = 255;
}
}
}