Listening to i2c input changes with python

531 views Asked by At

Let's say I have a raspberry pi here and I want to write a Python script that turns on the light as soon as a i2c signal reaches the pi and some pin gets high. I do not want to use polling for this task, as it could slow down the process (I know just a bit but I'ts just bad practice and puts load on the CPU and so on, so I don't want to permacycle asking for the input state)

Is there any kind of server or callback function I could use to realize this with a python script? Which library could I use to reach such a behaviour?

First Ideas are enviromental variables/the i2c Interface in the Linux system that I could listen to constantly somehow and catch it to make it do what I want it to.

1

There are 1 answers

0
Avoid On

As I see it no need to use python but I don't see whole picture so I don't know if this will help You,
just regarding this part of question:

Is there any kind of server or callback function...

rpio.poll(pin, callback());

Watch pin for changes and execute the callback callback() on events. callback() takes a single argument, the pin which triggered the callback.

The optional direction argument can be used to watch for specific events:

rpio.POLL_LOW: poll for falling edge transitions to low.
rpio.POLL_HIGH: poll for rising edge transitions to high.
rpio.POLL_BOTH: poll for both transitions (the default).

Complete documentation - this is the npm module documentation

My example of configuring node.js server