Periphery for GopherLua
A native Go implementation of lua-periphery for the GopherLua VM.
Feature Status
Using
Loading Modules
import (
"github.com/nubix-io/gluaperiphery"
)
// Bring up a GopherLua VM
L := lua.NewState()
defer L.Close()
// Preload periphery modules
gluaperiphery.Preload(L)
Blinking an LED via a GPIO pin
The following code blinks an LED connected to GPIO18
(header J8 pin #12) of a Raspberry Pi 3 B+:
import (
"github.com/nubix-io/gluaperiphery"
"github.com/nubix-io/gluasocket"
)
L := lua.NewState()
defer L.Close()
gluaperiphery.Preload(L)
gluasocket.Preload(L)
L.DoString(`
local periphery = require 'periphery'
local socket = require 'socket'
local gpio18 = periphery.GPIO(18, 'out')
for j=1,10 do
gpio18:write(true)
socket.sleep(.5)
gpio18:write(false)
socket.sleep(.5)
end`)
Reading a GPIO pin
Using a Raspberry Pi 3 B+ with a jumper bridge between GPIO17
and GPIO18
, the following code sets a high and low state at GPIO18
used as an output, then reads the state back using GPIO17
used as an input.
import (
"github.com/nubix-io/gluaperiphery"
)
L := lua.NewState()
defer L.Close()
gluaperiphery.Preload(L)
L.DoString(`
local periphery = require 'periphery'
local gpio17 = periphery.GPIO(17, 'in')
local gpio18 = periphery.GPIO(18, 'out')
gpio18:write(false)
assert(gpio17:read() == false)
gpio18:write(true)
assert(gpio17:read() == true)
`)
Opening an I²C bus device
import (
"github.com/nubix-io/gluaperiphery"
)
L := lua.NewState()
defer L.Close()
gluaperiphery.Preload(L)
L.DoString(`
local periphery = require 'periphery';
local i2c = periphery.I2C('/dev/i2c-1');
return i2c`)
i2c := L.CheckUserData(-1)
Using with lua-bme280
import (
"github.com/nubix-io/gluaperiphery"
)
L := lua.NewState()
defer L.Close()
gluaperiphery.Preload(L)
L.DoString(`package.preload['bme280'] = function(...)
(... full inline contents of https://github.com/nubix-io/lua-bme280/blob/0.1.0/src/bme280.lua ...)
end`)
L.DoString(`
local bme280 = require 'bme280'
local periphery = require 'periphery'
local i2c = periphery.I2C('/dev/i2c-1')
local coeff = bme280.readCoefficients(i2c)
local tempC = bme280.readTemperatureC(i2c, bme280.AccuracyMode.STANDARD, coeff)
local pressPa = bme280.readPressurePa(i2c, bme280.AccuracyMode.STANDARD, coeff)
local humRH = bme280.readHumidityRH(i2c, bme280.AccuracyMode.STANDARD, coeff)
return tempC, pressPa, humRH`)
tempC, pressPa, humRH := L.CheckNumber(-3), L.CheckNumber(-2), L.CheckNumber(-1)
Testing
$ dep ensure
$ go test -v github.com/nubix-io/gluaperiphery...
=== RUN TestModuleTableExportsI2C
--- PASS: TestModuleTableExportsI2C (0.00s)
PASS
ok github.com/nubix-io/gluaperiphery (cached)
=== RUN TestBME280
--- SKIP: TestBME280 (0.00s)
bme280_test.go:16: A BME280 is not detected
=== RUN TestLoader
--- PASS: TestLoader (0.00s)
=== RUN TestConstantExports
--- PASS: TestConstantExports (0.00s)
=== RUN TestOpenMalformedDevice
--- PASS: TestOpenMalformedDevice (0.00s)
=== RUN TestOpenNonExistentDevice
--- PASS: TestOpenNonExistentDevice (0.00s)
PASS
ok github.com/nubix-io/gluaperiphery/i2c 0.021s