43#ifdef ARDUINO_ARCH_SAMD
46 _maxBufferSize = I2C_BUFFER_LENGTH;
78#if !(defined(ESP8266) || \
79 (defined(ARDUINO_ARCH_AVR) && !defined(WIRE_HAS_END)) || \
80 defined(ARDUINO_ARCH_ESP32))
93 if (!_begun && !
begin()) {
98 _wire->beginTransmission(_addr);
100 DEBUG_SERIAL.print(F(
"Address 0x"));
101 DEBUG_SERIAL.print(_addr);
103 if (_wire->endTransmission() == 0) {
105 DEBUG_SERIAL.println(F(
" Detected"));
110 DEBUG_SERIAL.println(F(
" Not detected"));
129 const uint8_t *prefix_buffer,
136 DEBUG_SERIAL.println(F(
"\tI2CDevice could not write such a large buffer"));
141 _wire->beginTransmission(_addr);
144 if ((prefix_len != 0) && (prefix_buffer !=
nullptr)) {
145 if (_wire->write(prefix_buffer, prefix_len) != prefix_len) {
147 DEBUG_SERIAL.println(F(
"\tI2CDevice failed to write"));
154 if (_wire->write(buffer, len) != len) {
156 DEBUG_SERIAL.println(F(
"\tI2CDevice failed to write"));
163 DEBUG_SERIAL.print(F(
"\tI2CWRITE @ 0x"));
164 DEBUG_SERIAL.print(_addr, HEX);
165 DEBUG_SERIAL.print(F(
" :: "));
166 if ((prefix_len != 0) && (prefix_buffer !=
nullptr)) {
167 for (uint16_t i = 0; i < prefix_len; i++) {
168 DEBUG_SERIAL.print(F(
"0x"));
169 DEBUG_SERIAL.print(prefix_buffer[i], HEX);
170 DEBUG_SERIAL.print(F(
", "));
173 for (uint16_t i = 0; i < len; i++) {
174 DEBUG_SERIAL.print(F(
"0x"));
175 DEBUG_SERIAL.print(buffer[i], HEX);
176 DEBUG_SERIAL.print(F(
", "));
178 DEBUG_SERIAL.println();
183 DEBUG_SERIAL.print(
"\tSTOP");
187 if (_wire->endTransmission(stop) == 0) {
189 DEBUG_SERIAL.println();
195 DEBUG_SERIAL.println(
"\tFailed to send!");
214 bool read_stop = (pos < (len - read_len)) ?
false : stop;
215 if (!_read(buffer + pos, read_len, read_stop))
222bool Adafruit_I2CDevice::_read(uint8_t *buffer,
size_t len,
bool stop) {
223#if defined(TinyWireM_h)
224 size_t recv = _wire->requestFrom((uint8_t)_addr, (uint8_t)len);
225#elif defined(ARDUINO_ARCH_MEGAAVR)
226 size_t recv = _wire->requestFrom(_addr, len, stop);
228 size_t recv = _wire->requestFrom((uint8_t)_addr, (uint8_t)len, (uint8_t)stop);
234 DEBUG_SERIAL.print(F(
"\tI2CDevice did not receive enough data: "));
235 DEBUG_SERIAL.println(recv);
240 for (uint16_t i = 0; i < len; i++) {
241 buffer[i] = _wire->read();
245 DEBUG_SERIAL.print(F(
"\tI2CREAD @ 0x"));
246 DEBUG_SERIAL.print(_addr, HEX);
247 DEBUG_SERIAL.print(F(
" :: "));
248 for (uint16_t i = 0; i < len; i++) {
249 DEBUG_SERIAL.print(F(
"0x"));
250 DEBUG_SERIAL.print(buffer[i], HEX);
251 DEBUG_SERIAL.print(F(
", "));
252 if (len % 32 == 31) {
253 DEBUG_SERIAL.println();
256 DEBUG_SERIAL.println();
274 size_t write_len, uint8_t *read_buffer,
275 size_t read_len,
bool stop) {
276 if (!
write(write_buffer, write_len, stop)) {
280 return read(read_buffer, read_len);
297#if defined(__AVR_ATmega328__) || \
298 defined(__AVR_ATmega328P__)
301 if ((F_CPU / 18) < desiredclk) {
303 Serial.println(F(
"I2C.setSpeed too high."));
307 uint32_t atwbr = ((F_CPU / desiredclk) - 16) / 2;
310 Serial.println(F(
"I2C.setSpeed too low."));
318 }
else if (atwbr <= 1020) {
321 }
else if (atwbr <= 4080) {
331 Serial.print(F(
"TWSR prescaler = "));
332 Serial.println(pow(4, TWSR));
333 Serial.print(F(
"TWBR = "));
334 Serial.println(atwbr);
337#elif (ARDUINO >= 157) && !defined(ARDUINO_STM32_FEATHER) && \
338 !defined(TinyWireM_h)
339 _wire->setClock(desiredclk);
Adafruit SSD1306 dependency code for I2C.
size_t maxBufferSize()
How many bytes we can read in a transaction.
bool setSpeed(uint32_t desiredclk)
Change the I2C clock speed to desired (relies on underlying Wire support!
bool write(const uint8_t *buffer, size_t len, bool stop=true, const uint8_t *prefix_buffer=nullptr, size_t prefix_len=0)
Write a buffer or two to the I2C device. Cannot be more than maxBufferSize() bytes.
bool detected(void)
Scans I2C for the address - note will give a false-positive if there's no pullups on I2C.
bool begin(bool addr_detect=true)
Initializes and does basic address detection.
uint8_t address(void)
Returns the 7-bit address of this device.
bool write_then_read(const uint8_t *write_buffer, size_t write_len, uint8_t *read_buffer, size_t read_len, bool stop=false)
Write some data, then read some data from I2C into another buffer. Cannot be more than maxBufferSize(...
void end(void)
De-initialize device, turn off the Wire interface.
Adafruit_I2CDevice(uint8_t addr, TwoWire *theWire=&Wire)
Create an I2C device at a given address.
bool read(uint8_t *buffer, size_t len, bool stop=true)
Read from I2C into a buffer from the I2C device. Cannot be more than maxBufferSize() bytes.