nodemcu-firmware/tests/NTest_gpio_env.lua
Nathaniel Wesley Filardo 6316b33296
More NTest prep work for eventual test harness (#3353)
* Rename to tests/README.md

* Expand tests/README.md a bit

* NTest: remove report() in favor of named fields

Use a metatable to provide defaults which can be shadowed by the calling
code.

* NTest: remove old interface flag

I think we have few enough tests that we can verify not needing this
alert for ourselves.

* NTest tests: new standard prelude

Allow for NTest constructor to be passed in to the test itself.
The test harness can use this to provide a wrapper that will
pre-configure NTest itself.

* NTest output handler for TAP messages

* expect tests: core library functions

* expect tests: file xfer TCL module

* expect tests: add TAP-based test runner

* Begin documenting TCL goo

* Add .gitattributes to make sure lineends are correct ...

... if checked out under windows and executed under linux (say docker)

* tests/README: enumerate dependencies

* tests: more README.md

Co-authored-by: Gregor Hartmann <HHHartmann@users.noreply.github.com>
2021-01-16 21:26:22 +00:00

90 lines
2.0 KiB
Lua

-- Walk the GPIO subsystem through its paces, using the attached I2C GPIO chip
--
-- Node GPIO 13 (index 7) is connected to I2C expander channel B6; node OUT
-- Node GPIO 15 (index 8) is connected to I2C expander channel B7; node IN
local N = ...
N = (N or require "NTest")("gpio-env")
-- TODO: Preflight test that we are in the correct environment with an I2C
-- expander in the right place with the right connections.
-- TODO: Use the mcp23017 module in the main tree rather than hand-coding
-- the commands
N.test('setup', function()
-- Set gpio pin directions
gpio.mode(8, gpio.INPUT)
gpio.mode(7, gpio.OUTPUT, gpio.FLOAT)
-- Configure the I2C bus
i2c.setup(0, 2, 1, i2c.FAST)
-- Set the IO expander port B to channel 7 as output, 6 as input
i2c.start(0)
ok(i2c.address(0, 0x20, i2c.TRANSMITTER))
i2c.write(0, 0x01, 0x7F)
i2c.stop(0)
end)
local function seti2cb7(v)
i2c.start(0)
i2c.address(0, 0x20, i2c.TRANSMITTER)
i2c.write(0, 0x15, v and 0x80 or 0x00)
i2c.stop(0)
end
local function geti2cb6()
i2c.start(0)
i2c.address(0, 0x20, i2c.TRANSMITTER)
i2c.write(0, 0x13)
i2c.start(0)
i2c.address(0, 0x20, i2c.RECEIVER)
local v = i2c.read(0, 1):byte(1)
i2c.stop(0)
return (bit.band(v,0x40) ~= 0)
end
N.test('gpio read 0', function()
seti2cb7(false)
ok(eq(0, gpio.read(8)))
end)
N.test('gpio read 1', function()
seti2cb7(true)
ok(eq(1, gpio.read(8)))
end)
N.test('i2c read 0', function()
gpio.write(7, 0)
ok(eq(false, geti2cb6()))
end)
N.test('i2c read 1', function()
gpio.write(7, 1)
ok(eq(true, geti2cb6()))
end)
N.testasync('gpio toggle trigger 1', function(next)
seti2cb7(false)
tmr.delay(10)
gpio.trig(8, "both", function(l,_,c)
ok(c == 1 and l == 1)
return next()
end)
seti2cb7(true)
end, true)
N.testasync('gpio toggle trigger 2', function(next)
gpio.trig(8, "both", function(l,_,c)
ok(c == 1 and l == 0)
return next()
end)
seti2cb7(false)
end, true)
N.test('gpio toggle trigger end', function()
gpio.trig(8, "none")
ok(true)
end)