Skip to content
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

mcp23017 Lua module #3067

Closed
wants to merge 66 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
14c1b8f
Lua 5.1 to 5.3 realignement phase 1
TerryE Jul 18, 2019
ba03cb0
Add telnet example
TerryE Jul 19, 2019
bc98174
Updates following JM review
TerryE Jul 19, 2019
39bb60e
Rebased against current dev
TerryE Jul 23, 2019
522b1d0
Rebased against current dev, tweaks for clean compile
TerryE Jul 23, 2019
fff9f95
Merge pull request #2836 from TerryE/dev-new-lua.c
TerryE Sep 7, 2019
32ad759
Add streaming support for hx711 device (#2793)
pjsg Sep 10, 2019
4c3d45e
Revert "Add streaming support for hx711 device (#2793)" (#2914)
marcelstoer Sep 13, 2019
81e213a
Remove superfluous module def
marcelstoer Sep 21, 2019
04287ac
Fix invalid smartconfig include
marcelstoer Sep 29, 2019
043046d
Deprecate crypto.to{Hex,Base64} (#2929)
nwf Sep 30, 2019
f3044e1
Fix typos
marcelstoer Oct 30, 2019
0e34f7d
fix lfs upload issue
Nov 13, 2019
f3b4984
Merge pull request #2961 from seregaxvm/lfsfix
TerryE Nov 14, 2019
e3935de
Improve MQTT documentation (#2967)
stromnet Nov 21, 2019
9a2579d
Improve httpserver documentation (#2971)
HHHartmann Nov 26, 2019
c7d0f83
Update sensor driver for BME680 to 3.5.9 (#2969)
vsky279 Dec 9, 2019
b179f30
Fixes for `ws2812` and `ws2812_effects` (#2953)
HHHartmann Dec 9, 2019
4fc2b85
Streaming support for hx711 (#2915)
marcelstoer Dec 9, 2019
f85c278
New `net.if.info` call to show LwIP information (#2862)
nwf Dec 9, 2019
d84da36
fixed missing forward declaration (#2975)
glumanda Dec 13, 2019
bf8f14b
SSL rampage (#2938)
nwf Dec 27, 2019
52a1587
Initial version of software UART C module (#2673)
galjonsfigur Dec 27, 2019
1652c0c
Add DCC decoder module (#2905)
vsky279 Dec 28, 2019
bcb669a
Polish Lua examples (#2846)
galjonsfigur Dec 30, 2019
7ed3d00
Minor typo and wording fixes (#2991)
mk-pmb Jan 1, 2020
8c08e76
Add CRC check (#2992)
vsky279 Jan 2, 2020
5278944
Fix compile warning and set correct order in module LROT (#2996)
galjonsfigur Jan 3, 2020
1c83f02
Expose CPU CCOUNT register as tmr function (#2906)
fikin Jan 4, 2020
49f25bd
Clarify LFS build info is its size (#3022)
mk-pmb Feb 1, 2020
76e9f1a
Document tls.cert.auth (#3010)
HHHartmann Feb 1, 2020
9fb8a2f
Add flag to disable start-up banner (#3026)
532910 Feb 9, 2020
df83908
Documentation fixes for pipe (#3041)
HHHartmann Feb 18, 2020
ec8622f
Fix timing of softuart module RX (#3029)
galjonsfigur Feb 18, 2020
1851a73
Update sqlite3.md (#3042)
HHHartmann Feb 21, 2020
31d4714
net: rename net.if.info to net.ifinfo (#3033)
nwf Feb 23, 2020
1577988
Bugfix conn metatable method call (#3012)
edzius Feb 23, 2020
c116d9d
Add Lua module for Gossip protocol (#3013)
alexandruantochi Mar 6, 2020
787ac7c
WIP: MQTT fixes (#2986)
nwf Mar 14, 2020
f265867
Fix connect() params in example
marcelstoer Mar 15, 2020
75db62f
Fix init.lua link
marcelstoer Mar 15, 2020
a9991c6
Update getting-started docs hrefs for LFS examples (#3048)
cerasmus Mar 22, 2020
f45a9ef
Add hint about locally built docs
marcelstoer Mar 22, 2020
cb2636d
Add page about Lua module directory
marcelstoer Mar 22, 2020
e358c4a
adding mcp23017 support as a lua module
plomi-net Apr 10, 2020
1d7e7d9
adding mcp23017 support as a lua module
plomi-net Apr 10, 2020
a4c30e4
Correcting functions for a/b register, update doc and remove unnecess…
plomi-net Apr 22, 2020
c39949b
Correcting functions for a/b register, update doc and remove unnecess…
plomi-net Apr 22, 2020
ceadbbe
remove mandatory i2c setup from mcp setup function, add optional setu…
plomi-net Apr 22, 2020
b77d865
update setup syntax in doc
plomi-net Apr 22, 2020
e0bd9a4
changing mcp23017 module to factory style
plomi-net Jul 5, 2020
6766acb
mcp23017 module: update example and docs for factory style
plomi-net Jul 5, 2020
974ae7f
Update from nodemcu dev
plomi-net Jul 5, 2020
af386af
update from dev
plomi-net Jul 5, 2020
90258c4
Delete telnet_pipe.lua
plomi-net Jul 5, 2020
e653d94
adding mcp23017 support as a lua module
plomi-net Apr 10, 2020
d88fcbf
adding mcp23017 support as a lua module
plomi-net Apr 10, 2020
5c3774c
Correcting functions for a/b register, update doc and remove unnecess…
plomi-net Apr 22, 2020
8c5845e
Correcting functions for a/b register, update doc and remove unnecess…
plomi-net Apr 22, 2020
2ac2f63
remove mandatory i2c setup from mcp setup function, add optional setu…
plomi-net Apr 22, 2020
b3c5a34
update setup syntax in doc
plomi-net Apr 22, 2020
75c8cf6
changing mcp23017 module to factory style
plomi-net Jul 5, 2020
60963ea
mcp23017 module: update example and docs for factory style
plomi-net Jul 5, 2020
0b9431d
Merge remote-tracking branch 'origin/dev' into dev
plomi-net Jul 5, 2020
a1e00c8
update from dev
plomi-net Jul 5, 2020
1c34e2d
Merge remote-tracking branch 'origin/feature-mcp23017' into feature-m…
plomi-net Jul 5, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
230 changes: 230 additions & 0 deletions docs/lua-modules/mcp23017.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
# Lua MCP23017 Module for NodeMCU / ESP8266

| Since | Origin / Contributor | Maintainer | Source |
| :----- | :-------------------- | :---------- | :------ |
| 2020-04-10 | [Marcel P.](https://github.com/plomi-net) | [Marcel P.](https://github.com/plomi-net) | [mcp23017.lua](../../lua_modules/mcp23017/mcp23017.lua) |


This Lua module provides access to the MCP23017 module.

The [MCP23017](http://ww1.microchip.com/downloads/en/devicedoc/20001952c.pdf) is a port expander and provides 16 channels for inputs and outputs.
Up to 8 devices (128 channels) are possible by the configurable address (A0 - A2).

Due to the 16 channels, 2 bytes are required for switching outputs or reading input signals. These are A and B.
A single pin can be set or a whole byte.

The numbering of the individual pins starts at 0 and ends with 7.
The numbers are for each register GPIO A and GPIO B.


!!! important
The module requires `i2c` and `bit` C module built into firmware.


### Require
```lua
mcp = require "mcp23017"
```

## Example Script
The example script can be found [here](../../lua_examples/mcp23017/mcp23017_example.lua)

## setup()
Configures the address of the module and tests the connection to the i2c bus.
The i2c id is required for an existing i2c interface, alternatively the sda and scl pins can be specified.
Then this function will establish the connection.
Automatically resets the device state (see `mcp23017:reset()`)

#### Syntax
`mcp23017:setup(address, i2c_id)`

#### Parameter
- `address` address for MCP23017, default: 0x20 (should be between 0x20 and 0x27)
- `i2c_id` id for the i2c bus connection (i2c.setup separately)

#### Return
`true` if device found, otherwise `false`.

#### possible Errors
- `MCP23017 device on address not found`
- `MCP23017 address is out of range`

#### Example
```lua
local mcp23017 = require "mcp23017"

local address = 0x20
local cSCL = 1
local cSDA = 2
local i2c_instance = 0

-- setup i2c bus and create instance for mcp23017 (assigned to mcp)
i2c.setup(i2c_instance, cSDA, cSCL, i2c.SLOW)
local mcp = mcp23017(address, i2c_instance)
```

## setMode()
Set the mode of a single channel. This can be OUTPUT or INPUT.

#### Syntax
`mcp23017:setMode(register, pin, mode)`

#### Parameter
- `register` the side of channels (GPA or GPB)
- `pin` the number to be set for the channel (0-15)
- `mode` the mode for the channel. This can be `mcp23017.INPUT` or `mcp23017.OUTPUT`

#### Return
`true`, in case of error `nil`.

#### Example
```lua
-- set pin 7 and 8 to output (GPA7 and GPB0) and GPB1 to input
mcp:setMode(mcp23017.GPA, 7, mcp23017.OUTPUT)
mcp:setMode(mcp23017.GPB, 0, mcp23017.OUTPUT)
mcp:setMode(mcp23017.GPB, 1, mcp23017.INPUT)
```

## setPin()
Set the state of a single channel. This can be HIGH or LOW.

#### Syntax
`mcp23017:setMode(register, pin, state)`

#### Parameter
- `register` the side of channels (GPA or GPB)
- `pin` the number to be set for the channel (0-15)
- `state` the state for the channel. This can be `mcp23017.HIGH` or `mcp23017.LOW`

#### Return
`true`, in case of error `nil`.

#### Example
```lua
-- set pin 7 to high (GPA7)
mcp:setPin(mcp23017.GPA, 7, mcp23017.HIGH)
-- set pin 8 to low (GPB0)
mcp:setPin(mcp23017.GPB, 0, mcp23017.LOW)
```

## getPinState()
get the state for a single channel. This can be HIGH or LOW.

#### Syntax
`mcp23017:getPinState(register, pin)`

#### Parameter
- `register` the side of channels (GPA or GPB)
- `pin` the number for which a state is to be queried (0-15)

#### Return
`true` for HIGH, `false` for LOW, in case of error `nil`.

#### Example
```lua
-- get the state for pin 9 (GPB1)
print(mcp:getPinState(mcp23017.GPB, 1))
```

## reset()
By calling this function, a safe state is established.
All channels are set to input.
This function can be used for a panic program.

#### Syntax
`mcp23017:reset()`

#### Parameter
None

#### Return
None

#### Example
```lua
-- reset the mcp23017 to startup defaults
mcp:reset()
```

## setInternalPullUp()
Enable or disable the internal pullup resistors.

#### Syntax
`mcp23017:setInternalPullUp(register, byte)`

#### Parameter
- `register` the side of channels (GPA or GPB)
- `byte` byte to set the pullup resistors

#### Return
None

#### Example
```lua
-- enable all pullup resistors for GPA
print(mcp:setInternalPullUp(mcp23017.GPA, 0xFF))
-- disable all pullup resistors for GPA
print(mcp:setInternalPullUp(mcp23017.GPA, 0x00))
```

## writeIODIR()
Setup the mode of the channels with a whole byte.


#### Syntax
`mcp23017:writeIODIR(register, byte)`

#### Parameter
- `register` the side of channels (GPA or GPB)
- `byte` byte to set the mode for all channels for this register

#### Return
None

#### Example
```lua
-- set all GPA to input
print(mcp:writeIODIR(mcp23017.GPA, 0xFF))
-- set all GPA to output
print(mcp:writeIODIR(mcp23017.GPA, 0x00))
```

## writeGPIO()
Setup the output state of the channels with a whole byte.

#### Syntax
`mcp23017:writeGPIO(register, byte)`

#### Parameter
- `register` the side of channels (GPA or GPB)
- `byte` byte to set the state for all channels for this register

#### Return
None

#### Example
```lua
-- set all GPA to HIGH
print(mcp:writeGPIO(mcp23017.GPA, 0xFF))
-- set all GPA to LOW
print(mcp:writeGPIO(mcp23017.GPA, 0x00))
```

## readGPIO()
Read the input states of the channels with a whole byte.

#### Syntax
`mcp23017:readGPIO(register)`

#### Parameter
- `register` the side of channels (GPA or GPB)

#### Return
byte with states

#### Example
```lua
-- get states for GPA
print(mcp:readGPIO(mcp23017.GPA))
```

110 changes: 110 additions & 0 deletions lua_examples/mcp23017/mcp23017_example.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
--[[

This example demonstrates how to use the different functions of the mcp23017 lua module.

@author Marcel P. | Plomi.net
@github https://github.com/plomi-net

@version 1.0.0

]]





--[[

initialize and setup

]]

-- initialize module
local mcp23017 = require "mcp23017"

-- set the address for MCP23017
local address = 0x20

-- SCL pin = 1 = D1 / GPIO 5 (ESP8266)
local cSCL = 1

-- SDA pin = 2 = D2 / GPIO4 (ESP8266)
local cSDA = 2

local i2cId = 0

-- setup i2c bus and create instance for mcp23017 (assigned to mcp)
i2c.setup(i2cId, cSDA, cSCL, i2c.SLOW)
local mcp = mcp23017(address, i2cId)


--[[

set output and input channels

]]

-- set pin 7 and 8 to output (GPA7 and GPB0) and GPB1 to input
mcp:setMode(mcp.GPA, 7, mcp.OUTPUT)
mcp:setMode(mcp.GPB, 0, mcp.OUTPUT)
mcp:setMode(mcp.GPB, 1, mcp.INPUT)



--[[

set output channels to high and low

]]

-- set pin 7 to high (GPA7)
mcp:setPin(mcp.GPA, 7, mcp.HIGH)
-- set pin 8 to low (GPB0)
mcp:setPin(mcp.GPB, 0, mcp.LOW)




--[[

toggle pin 6 channel state every second (blinking)

]]

local currentPin = 6
local currentState = false

mcp:setMode(mcp.GPA, currentPin, mcp.OUTPUT)

tmr.create():alarm(1000, tmr.ALARM_AUTO, function()
if currentState == true then
-- print("set to low")
mcp:setPin(mcp.GPA, currentPin, mcp.LOW)
currentState = false
else
-- print("set to high")
mcp:setPin(mcp.GPA, currentPin, mcp.HIGH)
currentState = true
end
end)





--[[

read input channels and display every 7 seconds

]]

-- read input register
tmr.create():alarm(7000, tmr.ALARM_AUTO, function()
local a = mcp:readGPIO(mcp.GPA)
print(" ")
print("GPIO A input states: " .. a)

local b = mcp:readGPIO(mcp.GPB)
print("GPIO B input states: " .. b)
print(" ")
end)
3 changes: 3 additions & 0 deletions lua_modules/mcp23017/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# MCP23017 Module

Documentation for this Lua module is available in the [mcp23017.md](../../docs/lua-modules/mcp23017.md) file and in the [Official NodeMCU Documentation](https://nodemcu.readthedocs.io/) in `Lua Modules` section.
Loading