Repository navigation
Expand file tree
/
Copy pathI2Class.cpp
More file actions
73 lines (56 loc) · 1.93 KB
/
Copy pathI2Class.cpp
File metadata and controls
73 lines (56 loc) · 1.93 KB
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
#include <I2Class.h>
uint8_t I2Class::I2CReadByte(uint8_t chipadr, uint8_t regadr, uint8_t* temp, uint16_t timeout){
return this->I2CReadBytes(chipadr, regadr, temp, 1, timeout);
}
uint8_t I2Class::I2CReadBytes(uint8_t chipadr, uint8_t regadr, uint8_t* temp, uint8_t length, uint16_t timeout){
if(!this->isSet){
DEBUG_PRINTLN(F("I2C ayarlamasi yapilmadi..."));
return -1;
}
uint8_t cnt = 0;
uint32_t t1 = millis();
for(uint8_t check = 0; check < length; check += minimum(length, BUFFER_LENGTH)){
this->wireI2C->beginTransmission(chipadr);
this->wireI2C->write(regadr + check);
this->wireI2C->endTransmission(false);
this->wireI2C->requestFrom(chipadr, minimum(length - check, BUFFER_LENGTH));
uint32_t start = millis();
while(this->wireI2C->available()){
if(timeout == 0 || millis() - start > timeout){
DEBUG_PRINTLN(F("I2C ReadByte timeout süresini asti..."));
return -1;
}
temp[cnt++] = this->wireI2C->read();
}
}
return cnt;
}
bool I2Class::I2CWriteByte(uint8_t chipadr, uint8_t regadr, uint8_t data){
if(!this->isSet){
DEBUG_PRINTLN(F("I2C ayarlamasi yapilmadi..."));
return false;
}
int8_t result;
this->wireI2C->beginTransmission(chipadr);
this->wireI2C->write(regadr);
this->wireI2C->write(data);
result = this->wireI2C->endTransmission(true);
return result == 0;
}
uint8_t I2Class::minimum(uint8_t x, uint8_t y){
return (x < y) ? x : y;
}
I2Class::I2Class() : created(false){
}
I2Class::I2Class(uint8_t sda, uint8_t scl) : isSet(true), created(true){
this->wireI2C = new TwoWire();
this->wireI2C->setSDA(sda);
this->wireI2C->setSCL(scl);
this->wireI2C->begin();
this->wireI2C->setClock(400000);
}
I2Class::~I2Class(){
if(created && this->wireI2C != nullptr){
delete this->wireI2C;
}
}