-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTwoWireSimulator.cpp
49 lines (41 loc) · 1.36 KB
/
TwoWireSimulator.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
#include <Wire.h>
#include "TwoWireSimulator.h"
///////////////////////// wire.cpp /////////////////////////
TwoWireSimulator::TwoWireSimulator(){
}
void TwoWireSimulator::begin(uint8_t address, uint8_t mask)
{
/*
* ATmega datasheet section 26.9.6:
* The TWAMR can be loaded with a 7-bit Slave Address mask.
* Each of the bits in TWAMR can mask (disable) the corresponding
* address bits in the TWI Address Register (TWAR). If the mask bit
* is set to one then the address match logic ignores the compare
* between the incoming address bit and the corresponding bit in TWAR.
*/
// set mask address
TWAMR = mask << 1;
TwoWire::begin(address);
}
void TwoWireSimulator::begin(int address, int mask)
{
TwoWireSimulator::begin((uint8_t)address, (uint8_t)mask);
}
void TwoWireSimulator::begin(const uint8_t *addresses, const uint8_t sizeAddresses)
{
uint8_t mask = 0;
// get all bits that change between each address
for(int i=1; i<sizeAddresses; ++i){
mask |= (addresses[0] ^ addresses[i]);
}
// set twi mask address
TWAMR = mask << 1;
TwoWire::begin(addresses[0]);
}
void TwoWireSimulator::begin(const uint8_t *addressesArray, const int sizeAddressesArray)
{
TwoWireSimulator::begin(addressesArray, (uint8_t)sizeAddressesArray);
}
char TwoWireSimulator::lastAddress(void){
return TWDR >> 1; // retrieve address from last byte on the bus
}