Fix compiler warning and simplify DS2408_Switch example

This commit is contained in:
PaulStoffregen
2017-09-05 04:02:38 -07:00
parent a074897351
commit ebc5f7f503
+34 -37
View File
@@ -12,7 +12,10 @@
* - For reading from a switch, you should use 10K pull-up resisters.
*/
void PrintBytes(uint8_t* addr, uint8_t count, bool newline=0) {
OneWire net(10); // on pin 10
void PrintBytes(const uint8_t* addr, uint8_t count, bool newline=false) {
for (uint8_t i = 0; i < count; i++) {
Serial.print(addr[i]>>4, HEX);
Serial.print(addr[i]&0x0f, HEX);
@@ -21,18 +24,43 @@ void PrintBytes(uint8_t* addr, uint8_t count, bool newline=0) {
Serial.println();
}
void ReadAndReport(OneWire* net, uint8_t* addr) {
void setup(void) {
Serial.begin(9600);
}
void loop(void) {
byte addr[8];
if (!net.search(addr)) {
Serial.print("No more addresses.\n");
net.reset_search();
delay(1000);
return;
}
if (OneWire::crc8(addr, 7) != addr[7]) {
Serial.print("CRC is not valid!\n");
return;
}
if (addr[0] != 0x29) {
PrintBytes(addr, 8);
Serial.print(" is not a DS2408.\n");
return;
}
Serial.print(" Reading DS2408 ");
PrintBytes(addr, 8);
Serial.println();
uint8_t buf[13]; // Put everything in the buffer so we can compute CRC easily.
buf[0] = 0xF0; // Read PIO Registers
buf[1] = 0x88; // LSB address
buf[2] = 0x00; // MSB address
net->write_bytes(buf, 3);
net->read_bytes(buf+3, 10); // 3 cmd bytes, 6 data bytes, 2 0xFF, 2 CRC16
net->reset();
net.write_bytes(buf, 3);
net.read_bytes(buf+3, 10); // 3 cmd bytes, 6 data bytes, 2 0xFF, 2 CRC16
net.reset();
if (!OneWire::check_crc16(buf, 11, &buf[11])) {
Serial.print("CRC failure in DS2408 at ");
@@ -44,34 +72,3 @@ void ReadAndReport(OneWire* net, uint8_t* addr) {
Serial.println(buf[3], BIN);
}
OneWire net(10); // on pin 10
void setup(void) {
Serial.begin(9600);
}
void loop(void) {
byte i;
byte present = 0;
byte addr[8];
if (!net.search(addr)) {
Serial.print("No more addresses.\n");
net.reset_search();
delay(1000);
return;
}
if (OneWire::crc8(addr, 7) != addr[7]) {
Serial.print("CRC is not valid!\n");
return;
}
if (addr[0] != 0x29) {
PrintBytes(addr, 8);
Serial.print(" is not a DS2408.\n");
return;
}
ReadAndReport(&net, addr);
}