i2c.ino 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #ifdef ENABLE_I2C_INTERFACE
  2. void i2cscan() {
  3. byte error, address;
  4. int nDevices;
  5. snprintf(logBuf, LOG_BUFFER_SIZE, "Scanning I2C devices...");
  6. sendLog(logBuf, LOGLEVEL_INFO);
  7. nDevices = 0;
  8. for(address = 1; address < 127; address++ )
  9. {
  10. // The i2c_scanner uses the return value of
  11. // the Write.endTransmisstion to see if
  12. // a device did acknowledge to the address.
  13. Wire.beginTransmission(address);
  14. error = Wire.endTransmission();
  15. if (error == 0)
  16. {
  17. //Serial.print("I2C device found at address 0x");
  18. //if (address<16)
  19. // Serial.print("0");
  20. //Serial.print(address,HEX);
  21. //Serial.println(" !");
  22. snprintf(logBuf, LOG_BUFFER_SIZE, " device found at address 0x%x", address);
  23. sendLog(logBuf, LOGLEVEL_INFO);
  24. nDevices++;
  25. }
  26. else if (error==4)
  27. {
  28. //Serial.print(" unknown error at address 0x");
  29. //if (address<16)
  30. // Serial.print("0");
  31. //Serial.println(address,HEX);
  32. snprintf(logBuf, LOG_BUFFER_SIZE, " unknown error at address 0x%x", address);
  33. sendLog(logBuf, LOGLEVEL_INFO);
  34. }
  35. }
  36. if (nDevices == 0) {
  37. //Serial.println("No I2C devices found\n");
  38. snprintf(logBuf, LOG_BUFFER_SIZE, " No I2C devices found.");
  39. sendLog(logBuf, LOGLEVEL_INFO);
  40. }
  41. else {
  42. //Serial.println("done\n");
  43. snprintf(logBuf, LOG_BUFFER_SIZE, " DONE!");
  44. sendLog(logBuf, LOGLEVEL_INFO);
  45. }
  46. } // i2cscan()
  47. #endif