GithubHelp home page GithubHelp logo

another i2c PE button question about button2 HOT 8 OPEN

sercona avatar sercona commented on June 30, 2024
another i2c PE button question

from button2.

Comments (8)

LennartHennigs avatar LennartHennigs commented on June 30, 2024

Hey,
good question.
Well, I'd rather not provide an option to call non-button code in my loop() function.
Some people don't understand the non-blocking loop concept.
This would allow people to slow down the loop, leading to issues that don't pertain to my class per se.

Can you check whether data has changed via I2C, or can you only set the pin mode and read /write?
I'd just add a simple loop function for the port expander to get the word and bitshift to the virtual pin values, I think.
Does that make sense?

from button2.

sercona avatar sercona commented on June 30, 2024

I'm not sure which way to go; I can see the efficiency of reading a whole port expander byte (or word) at once and caching that for a short time. but in some apps, it may be necessary to always ask for 'fresh' data even if you have to re-fetch a whole byte/word.

I guess to allow max flex, when you do the equiv of a digitalRead(), you could add a param that forces a refetch of data, or will allow cached data. and if you leave off that last param, it would have a reasonable default (whatever that is).

dont know if I'm overthinking it or not; but i2c bus traffic has to be managed between all the things on the bus. I have an i2c oled display that I'm trying to keep updated quickly and I also want to scan for keyboard changes and not feel sluggish on the key scan. if I do an i2c request for each keypress check, it would clutter the bus. that's why I'm trying to think of the best way to deal with port expanders and fetching a whole bunch of data even if the user only wanted one bit of it.

thinking aloud, maybe something like:

digitalRead(pin, cache_allowed=true);

I'm thinking that hiding the fetch of the byte/word and the cache timing is something I'd like to hide from the app writer and just let them call the abstract digitalRead(). if they dont have a good reason to disable it, the cache would be used and some holding time would be in a ctor, somewhere.

from button2.

sercona avatar sercona commented on June 30, 2024

oh, and there would be a 'digitalReadWholePort()' or something, for when the user really wanted a complete set of bits and is going to do their own AND and OR stuff from that. that would also have a cache_allowed flag, to match the single bit reads. while it might seem less useful to keep the cache if you are asking for all the bits, but you may want to throttle how often the port is polled and this cache thing could be one way to limit how fast the i2c port is being queried.

(this might belong more on the port-expander class, though.)

from button2.

LennartHennigs avatar LennartHennigs commented on June 30, 2024

out of curiosity which port expander are you using?

You could use the debounce time of the buttons as the expander's interval to read data.
Would pass it to either the constructor or the loop(unsigned long interval_ms)
And I'd invert the digitalRead()parameter to digitalRead(pin, force_read=false) if you really want live data

from button2.

sercona avatar sercona commented on June 30, 2024

I tend to use mcp23008 and pcf8574 (or 75) mostly.

from button2.

LennartHennigs avatar LennartHennigs commented on June 30, 2024

Have one of the MCPs lying around and never used it so far.
Might attach a button or two to it to see. But this might take a while for me to try this.

from button2.

sercona avatar sercona commented on June 30, 2024

I often dont have a spare gpio to allocate to the interrupt pin on the PE chip so I have to poll and compare old word to new word, etc.

usually there are not enough pins. hey, isn't that way we use PEs? lol

from button2.

ryancasler avatar ryancasler commented on June 30, 2024

I setup button2 using an MCP23008 and an ESP32. I used a timer to update the state of 8 different buttons and used a global array to store that state of each of the buttons. Inside the button2 custom state handler rather than reading from the MCP I am reading from the array in memory. The fact that button2 "reads" the state of the button from the array more frequently than the timer updates the array doesn't really matter in the end. Both are happening so frequently that it self-mitigates any delays in updating the state. I was able to accurately capture single, double and triple presses and helds accurately.
For example, if you are only updating the state of the buttons into the array every 100 ms, even if the button loop was to read at the exactly worst possible time, you're only going to get a maximum of 100ms delay.
If you did want to use the interrupt on the MCP23008, you could still do the same thing except without the timer. The fact that the button2 loop is reading a "stale" array that hasn't been updated in a long time doesn't really matter to button2.
With an ESP32, unless your sketch is huge, you are going to run through the loop and hit the button2 loop functions more frequently than you are ever going to want to poll an i2c device. So it is easier just to decouple the two of them.

from button2.

Related Issues (20)

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.