GithubHelp home page GithubHelp logo

Multithread access about i2crelay HOT 6 OPEN

oweidner avatar oweidner commented on August 27, 2024
Multithread access

from i2crelay.

Comments (6)

oweidner avatar oweidner commented on August 27, 2024

Hi @Misiu -

The i2crelay library is really just a very thin wrapper around the Python i2c library, and unless that's thread safe (I doubt it), my library is not thread-safe either. I haven never used the library in a multi-threaded context, so I have never tested it.

If you end up adding thread-safety to it, I'd be more than happy to include it!

from i2crelay.

jpcornil-git avatar jpcornil-git commented on August 27, 2024

For information, I just created a pull request addressing the threading issue for mcp23017 and using smbus2 i2c library as well, see home-assistant/core#42928

=> It should be pretty easy to add a new pcf8574 integration to home assistant using the same framework:

Cheers
jpc

from i2crelay.

Misiu avatar Misiu commented on August 27, 2024

@jpcornil-git thanks for the info. Your PR looks awesome 🚀
I'll wait until your PR is merged and then I'll try to create something. I'll ask you for a review when I'm ready.
BTW is your PR supporting interrupts? I'm sure it's possible to do in Python (like here: https://github.com/wryan67/mcp23x17_rpi_lib)

from i2crelay.

jpcornil-git avatar jpcornil-git commented on August 27, 2024

Handling of interrupt is platform specific and therefore not implemented here.

You could implement a RPi-specific version of this component quite easily (e.g. rpi_mcp23017) by leveraging the rpi_gpio HA package and modify mcp23017/__init__.py;

  • move code under MCP23017.run(self) (polled from i2c subsystem; just replace content by a pass statement) to a new MCP23017.interrupt_handler(self) method.
  • connect above 'interrupt_handler' using rpi_gpio.edge_detect in MCP23017.__init__(bus, address) and setup MCP23017 for interrupt

You can keep both interrupt and polling implementations as well:

  • add a rpi_interrupt_pin parameter to MCP23017.__init__(bus, address) -> MCP23017.__init__(bus, address, rpi_interrupt_pin=None)
  • in MCP23017.run(self), replace above 'pass' statement with 'if rpi_interrupt_pin == None: self.interrupt_handler()'

Now, interrupt is probably not important unless you need very short reaction time; for push-button applications, (multiple of) 100ms polling period like it is the case with this implementation gives you an extremely reactive system already and not loading the system/bus at all (very different from being polled from HA every second as it is the case today)

We could also make a component that is generic for polling but platform-specific when interrupt is required of course (I could add that once the 1st PR is over)

Cheers
jpc

from i2crelay.

Misiu avatar Misiu commented on August 27, 2024

@jpcornil-git let's wait for your PR to be merged.
My scenario is a simple doorbell example. I've spent hours pulling my hair out, because with a simple python script I got instant notifications (correct readings), but In HA I got a lot of false-positives. I want to connect max 8 sensors, so PCF8574 was my first choice, but MCP23017 will be just fine.

from i2crelay.

jpcornil-git avatar jpcornil-git commented on August 27, 2024

You can easily use the i2c/mcp23017 code supplied in the PR without HA to test reactivity:

import homeassistant.components.i2c as i2c
import homeassistant.components.mcp23017 as mcp23017

# Create an I2C bus instance managing /dev/i2c-1 (the one available on RPi GPIOs) and a 100ms polling time
bus = i2c.I2cDeviceManager(1,100)
bus.start_polling()

# Add a MCP23017 device at i2c address 0x27 and a scan_multiple of 1 (1x100ms => 100ms)
d=bus.register_device(mcp23017.MCP23017, 0x27, 1)

# Configure MCP23017 pin 8 (pin0 of bank B) as input with pullup
d.set_input(8, True)
d.set_pullup(8, True)

# Create a simple call back
def callback(value):
    print(value)

# Register above callback to MCP pin 8 
d.register_input_callback(8, callback)

Play with pin 8

bus.stop_polling()
exit()

from i2crelay.

Related Issues (2)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.