@@ -57,7 +57,7 @@ class I2CDevice:
57
57
device.write(bytes_read)
58
58
"""
59
59
60
- def __init__ (self , i2c , device_address ):
60
+ def __init__ (self , i2c , device_address , * , debug = False ):
61
61
"""
62
62
Try to read a byte from an address,
63
63
if you get an OSError it means the device is not there
@@ -79,6 +79,7 @@ def __init__(self, i2c, device_address):
79
79
80
80
self .i2c = i2c
81
81
self .device_address = device_address
82
+ self ._debug = debug
82
83
83
84
def readinto (self , buf , ** kwargs ):
84
85
"""
@@ -94,6 +95,8 @@ def readinto(self, buf, **kwargs):
94
95
:param int end: Index to write up to but not include
95
96
"""
96
97
self .i2c .readfrom_into (self .device_address , buf , ** kwargs )
98
+ if self ._debug :
99
+ print ("i2c_device.readinto:" , [hex (i ) for i in buf ])
97
100
98
101
def write (self , buf , ** kwargs ):
99
102
"""
@@ -110,6 +113,8 @@ def write(self, buf, **kwargs):
110
113
:param bool stop: If true, output an I2C stop condition after the buffer is written
111
114
"""
112
115
self .i2c .writeto (self .device_address , buf , ** kwargs )
116
+ if self ._debug :
117
+ print ("i2c_device.write:" , [hex (i ) for i in buf ])
113
118
114
119
#pylint: disable-msg=too-many-arguments
115
120
def write_then_readinto (self , out_buffer , in_buffer , * ,
@@ -147,10 +152,19 @@ def write_then_readinto(self, out_buffer, in_buffer, *,
147
152
self .i2c .writeto_then_readfrom (self .device_address , out_buffer , in_buffer ,
148
153
out_start = out_start , out_end = out_end ,
149
154
in_start = in_start , in_end = in_end , stop = stop )
155
+ if self ._debug :
156
+ print ("i2c_device.writeto_then_readfrom.out_buffer:" , [hex (i ) for i in out_buffer ])
157
+ print ("i2c_device.writeto_then_readfrom.in_buffer:" , [hex (i ) for i in in_buffer ])
150
158
else :
151
159
# If we don't have a special implementation, we can fake it with two calls
152
160
self .write (out_buffer , start = out_start , end = out_end , stop = stop )
161
+ if self ._debug :
162
+ print ("i2c_device.write_then_readinto.write.out_buffer:" ,
163
+ [hex (i ) for i in out_buffer ])
153
164
self .readinto (in_buffer , start = in_start , end = in_end )
165
+ if self ._debug :
166
+ print ("i2c_device.write_then_readinto.readinto.in_buffer:" ,
167
+ [hex (i ) for i in in_buffer ])
154
168
155
169
#pylint: enable-msg=too-many-arguments
156
170
0 commit comments