-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi2c.cpp
More file actions
67 lines (60 loc) · 2.19 KB
/
Copy pathi2c.cpp
File metadata and controls
67 lines (60 loc) · 2.19 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
/*
* Copyright 2021 Marcus Behel
*
* This file is part of ArPiRobot-ArduinoFirmware.
*
* ArPiRobot-ArduinoFirmware is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation;
* (at your option) any later version.
*
* ArPiRobot-ArduinoFirmware 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with ArPiRobot-ArduinoFirmware. If not, see <https://www.gnu.org/licenses/>.
*/
#include "i2c.h"
uint8_t I2CHelper::write(TwoWire &wire, uint8_t address, uint8_t val, bool stop){
wire.beginTransmission(address);
wire.write(val);
return wire.endTransmission(stop);
}
uint8_t I2CHelper::writeByte(TwoWire &wire, uint8_t address, uint8_t reg, uint8_t val, bool stop){
wire.beginTransmission(address);
wire.write(reg);
wire.write(val);
return wire.endTransmission(stop);
}
int16_t I2CHelper::readByte(TwoWire &wire, uint8_t address, uint8_t reg, bool stop){
wire.beginTransmission(address);
wire.write(reg);
if(wire.endTransmission(stop) != 0)
return -1;
if(wire.requestFrom(address, (size_t)1) != 1)
return -1;
return wire.read();
}
uint8_t I2CHelper::readBytes(TwoWire &wire, uint8_t address, uint8_t *data, uint8_t count){
uint8_t len = wire.requestFrom(address, (size_t)count);
for(uint8_t i = 0; i < len; ++i){
data[i] = wire.read();
}
return len;
}
uint8_t I2CHelper::replaceBits(uint8_t originalValue, uint8_t newValue, uint8_t width, uint8_t shift){
// Mask out all but last 'width' bits of new data
uint8_t mask = (1 << width) - 1;
newValue &= mask;
// Mask out bits to replace in old data
mask <<= shift;
originalValue &= ~mask;
// Combine
return originalValue | (newValue << shift);
}
uint8_t I2CHelper::getBits(uint8_t value, uint8_t width, uint8_t shift){
value >>= shift;
return value & ((1 << width) - 1);
}