forked from simplefoc/Arduino-FOC-drivers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
I2CCommanderMaster.cpp
59 lines (40 loc) · 1.61 KB
/
I2CCommanderMaster.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
#include "I2CCommanderMaster.h"
I2CCommanderMaster::I2CCommanderMaster(int maxMotors) : maxMotors(maxMotors), motors(new I2CRemoteMotor[maxMotors]) {
};
I2CCommanderMaster::~I2CCommanderMaster(){
};
void I2CCommanderMaster::init(){
};
// TODO handle multiple motors per target
void I2CCommanderMaster::addI2CMotors(uint8_t i2cAddress, uint8_t motorCount, TwoWire *wire){
for (int i=0;i<motorCount;i++){
if (numMotors<maxMotors){
motors[numMotors] = I2CRemoteMotor{ .wire=wire, .address=i2cAddress };
numMotors++;
}
}
};
int I2CCommanderMaster::writeRegister(int motor, I2CCommander_Register registerNum, void* data, uint8_t size){
motors[motor].wire->beginTransmission(motors[motor].address);
motors[motor].wire->write((uint8_t)registerNum);
motors[motor].wire->write((uint8_t*)data, size);
motors[motor].wire->endTransmission();
return size;
};
int I2CCommanderMaster::readRegister(int motor, I2CCommander_Register registerNum, void* data, uint8_t size){
motors[motor].wire->beginTransmission(motors[motor].address);
int numWrite = motors[motor].wire->write((uint8_t)registerNum); // TODO check return value
motors[motor].wire->endTransmission();
if (numWrite==1)
return readLastUsedRegister(motor, data, size);
return 0;
};
int I2CCommanderMaster::readLastUsedRegister(int motor, void* data, uint8_t size){
int numRead = motors[motor].wire->requestFrom(motors[motor].address, size);
if (numRead==size)
motors[motor].wire->readBytes((uint8_t*)data, size);
else {
return 0;
}
return numRead;
};