Hello everybody!
Ive gone against better judgement and figured i need to build a library in order to learn some finer things in programming, like structure, classes, constructors and instances. ![]()
As a test-library im thinking a middle-man between sketch code and i2c pin extender libraries.
Im using Adafruits GREAT library for mcp23008 and a handfull of these ic´s for a bunch of projects, some devices are used both for inputs and outputs but its somewhat cumbersome to read or write pin-by-pin when youre using more than a couple of them..
Its a LOT more effective to read or write the GPIO byte x times/second than to read each button every x ms & write each output every time it changes.
The aim is to set up a structure of inputs and outputs, link them with their respective pins and devices and keep track of when they were last updated.
The end-user would then be able to check if i2c.button1.isPressed (true/false) or i2c.button3.clickType (int return type, 1= is_pressed, 2=shortclicked, 3=longclicked).
The outputs would be used with something like "i2c.lights2.on/off/blink(500)"
The middle-man library would configure the pin extenders and keep record of their last read/write time (counted with millis()) and store the last read value between update intervals.
So if the update-interval is set to 50ms, if i poll the button state every 10ms, i would get 5 identical replies, between each i2c extender update cycle.
I could then have different timers for inputs and outputs, checking inputs every 50ms and setting outputs every 120ms for example.
The middle-man library would then act the same way, but in the other direction to handle the outputs.
I would "simply" define each output channel with its chip "id" and "pin#", allowing multiple outputs for each channel, so "i2c.light1.on" effects all the output pins (on their respective i2c device) configured as "light1".
I would read and set inputs/outputs by reading/writing (to) the whole pin registry, instead of speaking to each pin separately.
This would save a LOT of i2c bus traffic...
Now, i have very limited knowledge of the needed ingredients here, but this is how my learningprocess works, i find something i need to do and learn how to do it along the way.
So could i please ask for a constructive tone on the replies? ![]()
First big question;
How do i "link" a hypothetical "button1" with a specific i2c-pin-extender and pin#?
i´ll be using a byte to store each pin extenders buffer, so i can bitRead/bitSet/bitClear the pin state for each pin# in each pin-extender-buffer, but i´ll have to figure out how to setup a constructor to do this for me and then instance it for each button.
To express the question in another format; how do i make "i2c.button1.read" into "bitRead(buffer1, 3)" (if its the pin# = 3) with a constructor and instance?
Oh and i almost forgot to even ask;
How do i setup the config bit of the equation?
I figure that the end user (me) would want to modify each pin extender i2c address, as well as setting up inputs/outputs..
Im trying to read thru the source files of other libraries, but most do use so advanced languages and dont explain the "basics", but cover only the higher level logics in their occational code commets..
So any and all hints, advices and links are welcome, if they help me learn what im lacking here.. ![]()