Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/share/examples/lua Example files showing Lua module usage.



details:   https://anonhg.NetBSD.org/src/rev/550dc82a26be
branches:  trunk
changeset: 770391:550dc82a26be
user:      mbalmer <mbalmer%NetBSD.org@localhost>
date:      Sat Oct 15 12:58:43 2011 +0000

description:
Example files showing Lua module usage.

diffstat:

 share/examples/lua/Makefile   |  12 ++++++++
 share/examples/lua/README     |   3 ++
 share/examples/lua/gpio.lua   |  31 ++++++++++++++++++++++
 share/examples/lua/sqlite.lua |  60 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 106 insertions(+), 0 deletions(-)

diffs (122 lines):

diff -r 37f4ed032563 -r 550dc82a26be share/examples/lua/Makefile
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/share/examples/lua/Makefile       Sat Oct 15 12:58:43 2011 +0000
@@ -0,0 +1,12 @@
+# $NetBSD: Makefile,v 1.1 2011/10/15 12:58:43 mbalmer Exp $
+
+NOOBJ=         # defined
+
+.include <bsd.own.mk>
+
+.if ${MKSHARE} != "no"
+FILES=         README gpio.lua sqlite.lua
+FILESDIR=      /usr/share/examples/lua
+.endif
+
+.include <bsd.prog.mk>
diff -r 37f4ed032563 -r 550dc82a26be share/examples/lua/README
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/share/examples/lua/README Sat Oct 15 12:58:43 2011 +0000
@@ -0,0 +1,3 @@
+       $NetBSD: README,v 1.1 2011/10/15 12:58:43 mbalmer Exp $
+
+This directory contains example code that illustrates how to use Lua modules.
diff -r 37f4ed032563 -r 550dc82a26be share/examples/lua/gpio.lua
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/share/examples/lua/gpio.lua       Sat Oct 15 12:58:43 2011 +0000
@@ -0,0 +1,31 @@
+-- $NetBSD: gpio.lua,v 1.1 2011/10/15 12:58:43 mbalmer Exp $
+
+require 'gpio'
+
+print(gpio._VERSION .. ' - ' .. gpio._DESCRIPTION)
+print(gpio._COPYRIGHT)
+print()
+
+g = gpio.open('/dev/gpio0')
+
+local npins = g:info()
+
+print('gpio0 has ' .. npins .. ' pins.')
+
+for n = 1, npins do
+       print('pin ' .. n .. ': ' .. g:read(n))
+end
+
+local oldval = g:write(32, gpio.PIN_HIGH)
+print('pin 32: ' .. oldval .. ' -> ' .. g:read(32))
+
+oldval = g:toggle(32)
+print('pin 32: ' .. oldval .. ' -> ' .. g:read(32))
+
+g:pulse(32, 1, 50)
+g:write(1, gpio.PIN_LOW)
+
+g:write(32, gpio.PIN_LOW)
+
+g:write(32, 5)
+
diff -r 37f4ed032563 -r 550dc82a26be share/examples/lua/sqlite.lua
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/share/examples/lua/sqlite.lua     Sat Oct 15 12:58:43 2011 +0000
@@ -0,0 +1,60 @@
+-- $NetBSD: sqlite.lua,v 1.1 2011/10/15 12:58:43 mbalmer Exp $
+
+require 'sqlite'
+
+print(sqlite._VERSION .. ' - ' .. sqlite._DESCRIPTION)
+print(sqlite._COPYRIGHT)
+print()
+
+print('initialize sqlite')
+sqlite.initialize()
+
+print('this is sqlite ' .. sqlite.libversion() .. ' (' ..
+    sqlite.libversion_number() .. ')')
+print('sourceid ' .. sqlite.sourceid())
+
+db, state = sqlite.open('/tmp/db.sqlite',
+    sqlite.OPEN_READWRITE + sqlite.OPEN_CREATE)
+
+if state ~= sqlite.OK then
+       print('db open failed')
+else
+       err = db:exec('create table test (name varchar(32))')
+
+       if err ~= sqlite.OK then
+               print('table creation failed')
+               print('error code ' .. db:errcode() .. ' msg ' .. db:errmsg())
+       end
+
+       db:exec("insert into test values('Balmer')")
+       print('last command changed ' .. db:changes() .. ' rows')
+
+       stmt = db:prepare("insert into test values(:name)")
+
+       print('statement has ' .. stmt:bind_parameter_count() .. ' parameters')
+       print('param 1 name: ' .. stmt:bind_parameter_name(1))
+       print('param name is at index ' .. stmt:bind_parameter_index('name'))
+
+       stmt:bind(1, 'Hardmeier')
+       stmt:step()
+       stmt:reset()
+       stmt:bind(1, 'Keller')
+       stmt:step()
+       stmt:finalize()
+
+       s2 = db:prepare('select name from test')
+
+       while s2:step() == sqlite.ROW do
+               print('name = ' .. s2:column(1))
+       end
+       s2:finalize()
+
+       stmt = db:prepare('drop table testx')
+       stmt:step()
+       stmt:finalize()
+       db:close()
+end
+
+print('shutdown sqlite')
+sqlite.shutdown()
+



Home | Main Index | Thread Index | Old Index