@@ -25,10 +25,12 @@ class I2CDevice:
25
25
Represents a single I2C device and manages locking the bus and the device
26
26
address.
27
27
28
- :param I2C i2c: The I2C bus the device is on
28
+ :param ~nativeio. I2C i2c: The I2C bus the device is on
29
29
:param int device_address: The 7 bit device address
30
30
31
- Example::
31
+ Example:
32
+
33
+ .. code-block:: python
32
34
33
35
import nativeio
34
36
from board import *
@@ -44,6 +46,14 @@ class I2CDevice:
44
46
device.writeto(bytes_read)
45
47
"""
46
48
def __init__ (self , i2c , device_address ):
49
+ # Verify that a deivce with that address exists.
50
+ while not i2c .try_lock ():
51
+ pass
52
+ scan = i2c .scan ()
53
+ i2c .unlock ()
54
+ if device_address not in scan :
55
+ raise ValueError ("No i2c device at address: " + str (hex (device_address )))
56
+
47
57
self .i2c = i2c
48
58
self .device_address = device_address
49
59
@@ -56,10 +66,10 @@ def readfrom_into(self, buf, **kwargs):
56
66
as if ``buf[start:end]``. This will not cause an allocation like
57
67
``buf[start:end]`` will so it saves memory.
58
68
59
- :param int address: 7-bit device address
60
- :param bytearray buffer: buffer to write into
61
- :param int start: Index to start writing at
62
- :param int end: Index to write up to but not include
69
+ :param int address: 7-bit device address
70
+ :param bytearray buffer: buffer to write into
71
+ :param int start: Index to start writing at
72
+ :param int end: Index to write up to but not include
63
73
"""
64
74
self .i2c .readfrom_into (self .device_address , buf , ** kwargs )
65
75
@@ -72,11 +82,10 @@ def writeto(self, buf, **kwargs):
72
82
as if ``buffer[start:end]``. This will not cause an allocation like
73
83
``buffer[start:end]`` will so it saves memory.
74
84
75
- :param bytearray buffer: buffer containing the bytes to write
76
- :param int start: Index to start writing from
77
- :param int end: Index to read up to but not include
78
- :param bool stop: If true, output an I2C stop condition after the
79
- buffer is written
85
+ :param bytearray buffer: buffer containing the bytes to write
86
+ :param int start: Index to start writing from
87
+ :param int end: Index to read up to but not include
88
+ :param bool stop: If true, output an I2C stop condition after the buffer is written
80
89
"""
81
90
self .i2c .writeto (self .device_address , buf , ** kwargs )
82
91
0 commit comments