ev3dev / ev3dev-lang-python

Pure python bindings for ev3dev
MIT License
431 stars 145 forks source link

Communicate with a custom i2c sensor #325

Closed Idechix closed 7 years ago

Idechix commented 7 years ago

Hi everyone! I made that sensor: http://www.mindstormsrobots.com/lego-mindstorms/mindstorms-8-input-rcx-touch-sensor-multiplexer/

I can read its values using smbus, but I wonder if there is a way to only use ev3dev python classes to get the same result.

Here is what I do for now:

import smbus

bus = smbus.SMBus(3) # input port 1

# PCF8574 address is 0x20
# 0xFF All pins configured as inputs
bus.write_byte(0x20, 0xFF)

data = bus.read_byte(0x20)

I really appreciate any help you can provide :)

WasabiFan commented 7 years ago

Hmmm... we have an I2cSensor class, but that's primarily for LEGO sensors which use the I2C protocol but have a proper ev3dev driver to operate them. That class doesn't actually provide any generic read/write capabilities.

My inclination is to say that the way you do it now is the best, barring writing your own kernel driver for this multiplexer. You could wrap it in your own class to make the interface nicer, but there aren't any pre-built classes for that to my knowledge.

Idechix commented 7 years ago

Ok, thank you :) I think I will use that code, but I am curious and I would like to go a bit further.

In my research I found that: http://docs.ev3dev.org/projects/lego-linux-drivers/en/ev3dev-jessie/sensor_data.html#pcf8574

I don't really know if that means a driver for that chip really exists (and how to use it?).

dlech commented 7 years ago

It means there is not an ev3dev driver for that chip. There is a regular Linux kernel driver that exposes the device using the Linux gpio device class. To use it, would have to add something to /etc/modules-load.d/ and /etc/modprobe.d/ to get the driver to automatically load correctly. Then you would have to write a udev rule so that non-root users could use those gpios. Then you would have to figure out how to use the gpios from python. Using smbus is probably easier than all of this. 😄

Idechix commented 7 years ago

Yes, smbus will be just fine ;) Thank you guys!