create an i2c device with an address and be able to detect it
This commit is contained in:
parent
337886fe76
commit
694010c57b
|
|
@ -0,0 +1,33 @@
|
||||||
|
#include <Adafruit_I2CDevice.h>
|
||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
Adafruit_I2CDevice::Adafruit_I2CDevice(uint8_t addr, TwoWire *theWire) {
|
||||||
|
_addr = addr;
|
||||||
|
_wire = theWire;
|
||||||
|
_begun = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Adafruit_I2CDevice::begin(void) {
|
||||||
|
_wire->begin();
|
||||||
|
_begun = true;
|
||||||
|
|
||||||
|
return detected();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Adafruit_I2CDevice::detected(void) {
|
||||||
|
// Init I2C if not done yet
|
||||||
|
if (!_begun && !begin()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// A basic scanner, see if it ACK's
|
||||||
|
_wire->beginTransmission(_addr);
|
||||||
|
if (_wire->endTransmission () == 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t Adafruit_I2CDevice::address(void) {
|
||||||
|
return _addr;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,15 @@
|
||||||
|
#include <Wire.h>
|
||||||
|
|
||||||
|
class Adafruit_I2CDevice {
|
||||||
|
|
||||||
|
private:
|
||||||
|
uint8_t _addr;
|
||||||
|
TwoWire *_wire;
|
||||||
|
bool _begun;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Adafruit_I2CDevice(uint8_t addr, TwoWire *theWire=&Wire);
|
||||||
|
uint8_t address(void);
|
||||||
|
bool begin(void);
|
||||||
|
bool detected(void);
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
#include <Adafruit_I2CDevice.h>
|
||||||
|
|
||||||
|
Adafruit_I2CDevice i2c_dev = Adafruit_I2CDevice(0x10);
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
while (!Serial) { delay(10); }
|
||||||
|
Serial.begin(115200);
|
||||||
|
Serial.println("I2C address detection test");
|
||||||
|
|
||||||
|
if (!i2c_dev.begin()) {
|
||||||
|
Serial.print("Did not find device at 0x");
|
||||||
|
Serial.println(i2c_dev.address(), HEX);
|
||||||
|
while (1);
|
||||||
|
}
|
||||||
|
Serial.print("Device found on address 0x");
|
||||||
|
Serial.println(i2c_dev.address(), HEX);
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue