-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathI2C.cpp
104 lines (85 loc) · 2.44 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
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
#include "I2C.h"
#include "EZB.h"
I2CClass::I2CClass(EZB* ezb){
m_ezb = ezb;
}
bool I2CClass::Poll(){
unsigned char* retval = m_ezb->SendCommand(EZB::I2CPoll, 1);
bool status = false;
if(retval[0] == 1)
status = true;
delete [] retval;
return status;
}
unsigned char I2CClass::Read(bool ack){
unsigned char* retval = Read(ack, 1);
unsigned char val = retval[0];
delete [] retval;
return val;
}
unsigned char* I2CClass::Read(bool ack, unsigned char expected_ret_bytes){
unsigned char args[2];
if(ack)
args[0] = 1;
else
args[1] = 0;
args[1] = expected_ret_bytes;
return m_ezb->SendCommand(EZB::I2CRead, args, 2, expected_ret_bytes);
}
unsigned char I2CClass::Read(unsigned char device_addr, unsigned char registered_addr){
unsigned char args[2];
args[0] = device_addr << 1;
args[1] = registered_addr;
Start();
Write(args, 2);
Restart();
Write((device_addr << 1) + 1);
unsigned char val = Read(false);
Stop();
return val;
}
unsigned char* I2CClass::ReadAutoAck(unsigned char expected_ret_bytes){
if(!m_ezb->IsConnected())
return NULL;
unsigned char args[1];
args[0] = expected_ret_bytes;
return m_ezb->SendCommand(EZB::I2CReadAutoAck, args, 1, expected_ret_bytes);
}
void I2CClass::Restart(){
m_ezb->SendCommand(EZB::I2CRestart);
}
void I2CClass::Start(){
m_ezb->SendCommand(EZB::I2CStart);
}
void I2CClass::Stop(){
m_ezb->SendCommand(EZB::I2CStop);
}
void I2CClass::Write(unsigned char* bytes, int len){
if (m_ezb->IsConnected()) {
if (len > 255){
throw std::runtime_error("Can not send more than 255 bytes over I2C");
}
m_ezb->SendCommand(EZB::I2CWrite, bytes, len);
}
}
void I2CClass::Write(unsigned char byte){
unsigned char value[1];
value[0] = byte;
Write(value, 1);
}
void I2CClass::Write(unsigned char device_addr, unsigned char* data, int len){
if (len > 255){
throw std::runtime_error("Can not send more than 255 bytes over I2C");
}
unsigned char* towrite = new unsigned char[len+1];
towrite[0] = device_addr << 1;
memcpy(&towrite[1], data, len);
Start();
Write(towrite, len+1);
delete [] towrite;
Stop();
}
void I2CClass::WriteBinary(unsigned char b7, unsigned char b6, unsigned char b5, unsigned char b4, unsigned char b3, unsigned char b2, unsigned char b1, unsigned char b0){
unsigned char b = (unsigned char) (((((((b0 + (b1 * 2)) + (b2 * 4)) + (b3 * 8)) + (b4 * 0x10)) + (b5 * 0x20)) + (b6 * 0x40)) + (b7 * 0x80));
Write(b);
}