/
DS2406.cpp
44 lines (38 loc) · 1008 Bytes
/
DS2406.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
#include <DS2406.h>
OneWireSwitch::OneWireSwitch(OneWire *b, uint8_t *addr) {
bus = b;
for(int i = 0; i<8; i++) {
address[i] = addr[i];
}
}
bool OneWireSwitch::getSwitchState(int port) {
uint8_t status[DS2406_STATE_BUF_LEN];
readStatus(status);
return (status[7] & port) == 0;
}
void OneWireSwitch::setSwitchState(bool pio_a, bool pio_b) {
uint8_t state = (!pio_a << 5) | (!pio_b << 6) | 0xf;
bus->reset();
bus->select(address);
bus->write(DS2406_WRITE_STATUS);
bus->write(0x07);
bus->write(0);
bus->write(state);
// Read the CRC data
for (int i = 0; i < 6; i++) {
bus->read();
}
// Write the status back.
bus->write(0xFF,1);
}
void OneWireSwitch::readStatus(uint8_t *buffer) {
bus->reset();
bus->select(address);
bus->write(DS2406_READ_STATUS, 1);
bus->write(0, 1);
bus->write(0, 1);
for(int i = 0; i<DS2406_STATE_BUF_LEN; i++) {
buffer[i] = bus->read();
}
bus->reset();
}