nodemcu-firmware/lua_examples/mcp23008/mcp23008_buttons.lua
galjonsfigur 6926c66b16 Polish Lua examples (#2846)
* Add missing globals from luacheck config

* Fix luacheck warnings in all lua files

* Re-enable luacheck in Travis

* Speed up Travis by using preinstalled LuaRocks

* Fix more luacheck warnings in httpserver lua module

* Fix DCC module and add appropriate definitions to luacheck config.

* Change inline comments from ignoring block to only ignore specific line

* Add Luacheck for Windows and enable it for both Windows and Linux

* Change luacheck exceptions and fix errors from 1st round of polishing

* Add retry and timeout params to wget
2020-06-09 22:26:52 +02:00

58 lines
1.8 KiB
Lua

---
-- @description Shows how to read 8 GPIO pins/buttons via I2C with the MCP23008 I/O expander.
-- Tested on NodeMCU 0.9.5 build 20150213.
-- @circuit
-- Connect GPIO0 of the ESP8266-01 module to the SCL pin of the MCP23008.
-- Connect GPIO2 of the ESP8266-01 module to the SDA pin of the MCP23008.
-- Use 3.3V for VCC.
-- Connect switches or buttons to the GPIOs of the MCP23008 and GND.
-- Connect two 4.7k pull-up resistors on SDA and SCL
-- We will enable the internal pull up resistors for the GPIOS of the MCP23008.
-- @author Miguel (AllAboutEE)
-- GitHub: https://github.com/AllAboutEE
-- YouTube: https://www.youtube.com/user/AllAboutEE
-- Website: http://AllAboutEE.com
---------------------------------------------------------------------------------------------
local mcp23008 = require ("mcp23008")
-- ESP-01 GPIO Mapping as per GPIO Table in https://github.com/nodemcu/nodemcu-firmware
local gpio0, gpio2 = 3, 4
---
-- @name showButtons
-- @description Shows the state of each GPIO pin
-- @return void
---------------------------------------------------------
local function showButtons()
local gpio = mcp23008.readGPIO() -- read the GPIO/buttons states
-- get/extract the state of one pin at a time
for pin=0,7 do
local pinState = bit.band(bit.rshift(gpio,pin),0x1) -- extract one pin state
-- change to string state (HIGH, LOW) instead of 1 or 0 respectively
if(pinState == mcp23008.HIGH) then
pinState = "HIGH"
else
pinState = "LOW"
end
print("Pin ".. pin .. ": ".. pinState)
end
print("\r\n")
end
do
-- Setup the MCP23008
mcp23008.begin(0x0,gpio2,gpio0,i2c.SLOW)
mcp23008.writeIODIR(0xff)
mcp23008.writeGPPU(0xff)
tmr.create():alarm(2000, tmr.ALARM_AUTO, showButtons) -- run showButtons() every 2 seconds
end