trying out only 1 file handle
#!/usr/bin/env python
-# Copyright 2017 Kubos Corporation
+# Copyright 2018 Kubos Corporation
# Licensed under the Apache License, Version 2.0
# See LICENSE file for details.
# Referenced from: https://www.raspberrypi.org/forums/viewtopic.php?t=162248&p=1049717
@@ -19,11 +19,10 @@ class I2C:
"""
Retrieves the read/write file handles for the device and sets the device address
"""
- self.readfile = io.open("/dev/i2c-"+str(bus), "rb", buffering=0)
- self.writefile = io.open("/dev/i2c-"+str(bus), "wb", buffering=0)
- fcntl.ioctl(self.readfile, I2C_SLAVE, device)
- fcntl.ioctl(self.writefile, I2C_SLAVE, device)
+ self.devfile = io.open("/dev/i2c-"+str(bus), "r+b", buffering=0)
+
+ fcntl.ioctl(self.devfile, I2C_SLAVE, device)
def write(self, data):
"""
@@ -37,21 +36,20 @@ class I2C:
pass
else:
raise('Invalid data format: '+str(type(data))+', must be string or list')
- self.writefile.write(data)
+ self.devfile.write(data)
return True,data
def read(self, count):
"""
Reads the specified number of bytes from the device.
"""
- return self.readfile.read(count)
+ return self.devfile.read(count)
def close(self):
"""
Closes the files.
"""
- self.writefile.close()
- self.readfile.close()
+ self.devfile.close()
\ No newline at end of file
