Skip to content

Commit d492b23

Browse files
committed
Implement Adafruit_BusIO-style read/write methods for I2C.
1 parent 2d1a689 commit d492b23

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/wire/WireBusDevice.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,25 @@ IoResponse WireBusDevice::transfer(IoRequest & req)
4141
return WireDispatcher::instance().dispatch(&req, &_config);
4242
}
4343

44+
bool WireBusDevice::read(uint8_t * buffer, size_t len, bool stop)
45+
{
46+
WireBusDeviceConfig config(_config.wire(), _config.slave_addr(), _config.restart(), stop);
47+
IoRequest req(nullptr, 0, buffer, len);
48+
IoResponse rsp = WireDispatcher::instance().dispatch(&req, &config);
49+
rsp->wait();
50+
return true;
51+
}
52+
53+
bool WireBusDevice::write(const uint8_t * buffer, size_t len, bool stop)
54+
{
55+
bool const restart = !stop;
56+
WireBusDeviceConfig config(_config.wire(), _config.slave_addr(), restart, _config.stop());
57+
IoRequest req(buffer, len, nullptr, 0);
58+
IoResponse rsp = WireDispatcher::instance().dispatch(&req, &config);
59+
rsp->wait();
60+
return true;
61+
}
62+
4463
bool WireBusDevice::write_then_read(const uint8_t * write_buffer, size_t write_len, uint8_t * read_buffer, size_t read_len, bool stop)
4564
{
4665
/* Copy the Wire parameters from the device and modify only those

src/wire/WireBusDevice.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class WireBusDevice : public BusDeviceBase
4444

4545
virtual IoResponse transfer(IoRequest & req) override;
4646

47+
bool read(uint8_t * buffer, size_t len, bool stop = true);
48+
bool write(const uint8_t * buffer, size_t len, bool stop = true);
4749
bool write_then_read(const uint8_t * write_buffer, size_t write_len, uint8_t * read_buffer, size_t read_len, bool stop = false);
4850

4951
private:

0 commit comments

Comments
 (0)