-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add mcp23017 Lua module #3197
add mcp23017 Lua module #3197
Conversation
Please add mcp23017_example.lua from previous PR. |
|
@plomi-net Travis build is failing because of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I decided to make a review for this PR and I've newer done any before, so I'm sorry if the review isn't as good as it should be. I have an MCP23017 somewhere so I will try to test the hardware itself in near future.
@galjonsfigur thank you for the advice with Travis build check. It is good to know. I refactor the function so the check was passed now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your help. I hope you mean this error:
PANIC: unprotected error in call to Lua API (script4.lua:65: attempt to concatenate a nil value)
I added an condition to catch this error.
Yes - that one. I think the module is ready to be merged. The only thing that I could to is to add the note about untested possibility to work with MCP23018 IC, but other than that LGTM. Thanks for the work on this!
lua_modules/mcp23017/mcp23017.lua
Outdated
self.HIGH = true | ||
self.LOW = false | ||
|
||
self:setup(address, i2cId) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not jazzed about this print
ing and then always returning a table (that just happens to have deviceOK
set to false
on the case of an error). I would prefer that checkAddress
and checkDevice
be moved to the example code and setup
removed from the driver code. This has the added benefit that all the deviceOK
checks can also be dropped.
A few comments:.
local isINPUT, isGBP, isHIGH = true, true, true -- use upvals in inline code to avoid self lookups
-- The metatable initialisation becomes
local mcp23017 = {
__index = mcp23017,
INPUT = isINPUT, OUTPUT = not isINPUT,
GPA = not isGBP, GPB = isGPB,
HIGH = isHIGH, LOW = not isHIGH
}
-- Below is an example of how we could strip down the redundancy. The other
-- functions could either be left as class methods acquring state from self
-- or move to lazy closures with the state also passed in as upvals.
return function(address, i2cId)
local self = setmetatable({}, mcp23017)
if setup(self, address, i2cId) then
self.writeIODIR = function(self, bReg, newByte)
writeByte(address, i2cId, -- upvals
bReg == isGBP and MCP23017_IODIRB or MCP23017_IODIRA, newByte)
end
self.writeGPIO = function(self, bReg, newByte)
writeByte(address, i2cId, -- upvals
bReg == isGBP and MCP23017_GPIOB or MCP23017_GPIOA, newByte)
end
self.readGPIO = function(self, bReg)
readByte(address, i2cId, -- upvals
bReg == isGBP and MCP23017_GPIOB or MCP23017_GPIOA)
end
return self
end
return nil
end Anyway, most style-type comments are a matter of personal preference. This is intended mostly as food for thought rather than mandatory requests, but get back to me if you think that I am talking out of my arse or you don't understand my points. 😄 |
@plomi-net Do you plan to address Terry's comments? |
@marcelstoer Sorry, time passes quickly. I would like to respond to the comments from Terry, which are really helpful. I had already fixed a few little things. |
…ding error and exception handling
@TerryE and @nwf Thank you for your detailed comments. One of your first comments is that a function is returned instead of a table. Furthermore, there are certainly many advantages and disadvantages to using a metatable or allowing multi instances. The MCP23017 can theoretically be used with 8 devices that need different instances. I look forward to your feedback and thank you for your help. |
New version of the code works like it should (tested quickly on a breadboard with DIP version of MCP23017) It takes considerably less RAM (at least when being run from flash and not using LFS). I will test how it behaves on LFS and how much RAM usage differs between current and previous version of the module. |
The neat thing about making function that after being called returns table with all the module functions and constants is that running the module from LFS is extremely simple and requires changing line or two of code - in this particular case from: local mcp23017 = require "mcp23017"
...
local mcp = mcp23017(address, i2cId) to local mcp = node.LFS.get("mcp23017")()(address, i2cId) The difference between the previous and current version of the module in case of RAM usage when in LFS is about 100 bytes less with current version - for me a clear improvement! |
@galjonsfigur an improvement of 100 Bytes is amazing. How much RAM does the program use now? |
Fixes #3057
Make sure all boxes are checked (add x inside the brackets) when you submit your contribution, remove this sentence before doing so.
dev
branch rather than formaster
.docs/*
.Add MCP23017 port expander support as a lua module. See issue #3057 and old pull request #3067
(rebase doesn't working correctly, sry)