Replies: 9 comments
-
You could read the config register first. Modify only the bit for your pin and write the contents to the chip. All threads should use the same practice to avoid your conflict. The link below is an example mcp23017 implementation. It maybe more function [than you need but it can provide example code |
Beta Was this translation helpful? Give feedback.
-
Hello, thanks for taking your time and answer to the question. I gave this a try, but instead of reading the device ports status, every thread that changes a port status has to update a Still, when i run the program, something weird is happening. I am guessing that, from time to time, threads read the same register situation from the map, but they write to the port slightly shifted in time. So 1 thread ends up cannibalizing another thread update. What i think i could do, if a mehtod such as the one i have described in the question does not exist, is to write to the device every millisecond, or every 10 milliseconds or so... So every thread offers it's own value of the pin it controls, but only one thread writes to the device. |
Beta Was this translation helpful? Give feedback.
-
I know singletons are not a "best practice", but maybe can be a solution here. |
Beta Was this translation helpful? Give feedback.
-
Hello, thanks for taking your time and answer to the question. I will try to make it work this evening and i will post here the outcome. Just to be clear: the method i wish to have does not exist right? The one i gave an example in the question. Thanks again |
Beta Was this translation helpful? Give feedback.
-
These methods exist in PiGpioI2C |
Beta Was this translation helpful? Give feedback.
-
I will try this And let you know. Thanks again! |
Beta Was this translation helpful? Give feedback.
-
I tested the above method. Unfortunately it's just a convenience method to achieve the same result as It seems that there is not method that writes a single bit to a specific port. So, as far as i know, you can only write 8 bits to the register |
Beta Was this translation helpful? Give feedback.
-
What you are looking for, is a locking mechanism. You don't need to use one thread to write to the register. Just create an object, which uses a lock to write to the registers, first reading the current register, updating the bit, then writing it, and the in the end releasing the lock. Something like this (code from Use a reentrant lock: The pi4j-v1 had an execute() method on the bus, allowing you to do this, but the pi4j-v2 sadly doesn't. We'll need to add this in the future. |
Beta Was this translation helpful? Give feedback.
-
And yes only 8 bit read/write. From that example device I mentioned, one way to get the register modify and write. |
Beta Was this translation helpful? Give feedback.
-
Hello,
i am new to pi4j and electronics in general. I am usng I2C for a small project i am doing.
So far i am able to use I2C and use the ports of my MCP23017 device as outputs. I can successfully write to them with this code.
mCP23017.writeRegister(register, Integer.parseInt("1000000", 2));
My problem is that i have several threads altering the same MCP23017's register, and this is a problem because everytime i write to the register from within a thread, i have to provide a value (0 or 1) for each port of that register, which, in the meantime. might have been altered by another thread.
I would like to use a method (if it exists) that allows me to alter a specific port on the MCP23017 device, without having to worry about the values of the other ports. Something like:
mCP23017.writeRegister("A", 5, 1); --> //this should be: write HIGH on port 5 of register A
Does this method exist? Does this break how I2C work?
Thanks
Beta Was this translation helpful? Give feedback.
All reactions