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

Lua bindings #527

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ build/
# Object files
*.o
*.lib
*.so.*
*.so
*.lo
*.la
Expand All @@ -20,3 +21,16 @@ package.json
binding.gyp
READMEFIRST
npm-debug.log

# lua files
src/lua/mraaLUA_wrap.cxx
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm kind of confused why all these are needed, if you use a build/ directory then everything will get ignored. Let's keep this stuff out unless there's a good reason

Copy link
Author

@dedok dedok Jun 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i do not use build/ directory :)
Okay if you don't like these changes I can remove them out.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

building in tree is not recommended for cmake (or any other builds really tbh), any good reason why you can't just build somewhere outside of git or simply add an ignore for your build directory? In any case such stuff doesn't belong in commits adding Lua support so please remove.


# CMake
CMakeFiles
CMakeCache.txt
cmake_install.cmake
CTestTestfile.cmake
Makefile

# Gen files
arch.c
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ option (BUILDSWIG "Build swig modules." ON)
option (BUILDSWIGPYTHON "Build swig python modules." ON)
option (BUILDSWIGNODE "Build swig node modules." ON)
option (BUILDSWIGJAVA "Build Java API." OFF)
option (BUILDSWIGLUA "Build Lua API." OFF)
option (USBPLAT "Detection USB platform." OFF)
option (FIRMATA "Add Firmata support to mraa." OFF)
option (ONEWIRE "Add Onewire support to mraa." ON)
Expand All @@ -87,6 +88,7 @@ if (NOT BUILDSWIG)
set (BUILDSWIGPYTHON OFF)
set (BUILDSWIGNODE OFF)
set (BUILDSWIGJAVA OFF)
set (BUILDSWIGLUA OFF)
endif()

if (NOT BUILDARCH)
Expand Down
31 changes: 31 additions & 0 deletions examples/lua/aio.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env lua

-- Author: Vasiliy Soshnikov <[email protected], [email protected]>
-- Copyright (c) 2016 Intel Corporation.
--
-- Permission is hereby granted, free of charge, to any person obtaining
-- a copy of this software and associated documentation files (the
-- "Software"), to deal in the Software without restriction, including
-- without limitation the rights to use, copy, modify, merge, publish,
-- distribute, sublicense, and/or sell copies of the Software, and to
-- permit persons to whom the Software is furnished to do so, subject to
-- the following conditions:
--
-- The above copyright notice and this permission notice shall be
-- included in all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

mraa = require('mraa')

print (mraa.getVersion())

x = mraa.Aio(0)
print (x:read())
print ("%.5f" % x:readFloat())
42 changes: 42 additions & 0 deletions examples/lua/blink-io8.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env lua

-- Author: Vasiliy Soshnikov <[email protected], [email protected]>
-- Copyright (c) 2016 Intel Corporation.
--
-- Permission is hereby granted, free of charge, to any person obtaining
-- a copy of this software and associated documentation files (the
-- "Software"), to deal in the Software without restriction, including
-- without limitation the rights to use, copy, modify, merge, publish,
-- distribute, sublicense, and/or sell copies of the Software, and to
-- permit persons to whom the Software is furnished to do so, subject to
-- the following conditions:
--
-- The above copyright notice and this permission notice shall be
-- included in all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

mraa = require('mraa')
os = require('os')

x = mraa.Gpio(8)
x:dir(mraa.DIR_OUT)

local function my_sleep(sec)
os.execute('sleep ' .. tonumber(sec))
end

while true do
-- On
x:write(1)
my_sleep(0.2)
-- off
x:write(0)
my_sleep(0.2)
end
44 changes: 44 additions & 0 deletions examples/lua/cycle-pwm3.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env lua

-- Author: Vasiliy Soshnikov <[email protected], [email protected]>
-- Copyright (c) 2016 Intel Corporation.
--
-- Permission is hereby granted, free of charge, to any person obtaining
-- a copy of this software and associated documentation files (the
-- "Software"), to deal in the Software without restriction, including
-- without limitation the rights to use, copy, modify, merge, publish,
-- distribute, sublicense, and/or sell copies of the Software, and to
-- permit persons to whom the Software is furnished to do so, subject to
-- the following conditions:
--
-- The above copyright notice and this permission notice shall be
-- included in all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

mraa = require('mraa')
os = require('os')

local function my_sleep(sec)
os.execute('sleep ' .. tonumber(sec))
end

x = mraa.Pwm(3)
x:period_us(700)
x:enable(true)
value = 0.0

while true do
x:write(value)
my_sleep(0.05)
value = value + 0.01
if value >= 1 then
value = 0.0
end
end
29 changes: 29 additions & 0 deletions examples/lua/firmata.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env lua

-- Author: Soshnikov Vasiliy <[email protected], [email protected]>
-- Copyright (c) 2016 Intel Corporation.
--
-- Permission is hereby granted, free of charge, to any person obtaining
-- a copy of this software and associated documentation files (the
-- "Software"), to deal in the Software without restriction, including
-- without limitation the rights to use, copy, modify, merge, publish,
-- distribute, sublicense, and/or sell copies of the Software, and to
-- permit persons to whom the Software is furnished to do so, subject to
-- the following conditions:
--
-- The above copyright notice and this permission notice shall be
-- included in all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

mraa = require('mraa')

print (mraa.getVersion())

mraa.addSubplatform(mraa.GENERIC_FIRMATA, "/dev/ttyACM0");
30 changes: 30 additions & 0 deletions examples/lua/hello_gpio.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env lua

-- Author: Vasiliy Soshnikov <[email protected], [email protected]>
-- Copyright (c) 2016 Intel Corporation.
--
-- Permission is hereby granted, free of charge, to any person obtaining
-- a copy of this software and associated documentation files (the
-- "Software"), to deal in the Software without restriction, including
-- without limitation the rights to use, copy, modify, merge, publish,
-- distribute, sublicense, and/or sell copies of the Software, and to
-- permit persons to whom the Software is furnished to do so, subject to
-- the following conditions:
--
-- The above copyright notice and this permission notice shall be
-- included in all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

mraa = require('mraa')

print (mraa.getVersion())
x = mraa.Gpio(13)
x:dir(mraa.DIR_OUT)
x:write(1)
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ if (BUILDSWIG)
message (SEND_ERROR "SWIG is ${SWIG_VERSION}. Please upgrade to 3.0.5+ to build nodejs addon")
endif ()
endif ()
if (BUILDSWIGLUA)
add_subdirectory (lua)
endif()
endif ()
endif ()

Expand Down
50 changes: 50 additions & 0 deletions src/lua/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
find_package (Lua REQUIRED)

include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/..
${LUA_INCLUDE_DIR}
)

set_source_files_properties (mraa.i PROPERTIES CPLUSPLUS ON)
set_source_files_properties (mraa.i PROPERTIES SWIG_FLAGS "-I${CMAKE_BINARY_DIR}/src")
swig_add_module (lua-mraa lua mraa.i)
swig_link_libraries (lua-mraa mraa ${LUA_LIBRARIES})

if (DOXYGEN_FOUND)
foreach (_file ${DOCCLASSES})
add_dependencies (${SWIG_MODULE_lua-mraa_REAL_NAME} ${_file}class_doc_i)
endforeach ()
add_dependencies (${SWIG_MODULE_lua-mraa_REAL_NAME} common_hpp_doc_i)

add_custom_target (pydoc
pydoc -w ${CMAKE_CURRENT_BINARY_DIR}/mraa.py ${CMAKE_CURRENT_BINARY_DIR}/
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Generating API documentation with pydoc" VERBATIM
)
endif ()

set_target_properties (${SWIG_MODULE_lua-mraa_REAL_NAME} PROPERTIES
OUTPUT_NAME mraa
COMPILE_FLAGS "${CMAKE_C_FLAGS} -DSWIG=${SWIG_FOUND}"
)

execute_process (
COMMAND lua -e
"string.gsub(package.path, '[^;]+', function(e)
if e:find('/usr/local') == 1 then
p = e:gsub('(?.lua)', '')
io.write(p)
os.exit(0)
end
end)
os.exit(1)
"
OUTPUT_VARIABLE LUA_SITE_DIR
)

install (FILES ${CMAKE_CURRENT_BINARY_DIR}/mraa.so
DESTINATION ${LUA_SITE_DIR})

message(STATUS "Lua site dir: ${LUA_SITE_DIR}")

add_subdirectory (docs)
41 changes: 41 additions & 0 deletions src/lua/docs/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
if (DOXYGEN_FOUND)
find_package (Sphinx)
if (SPHINX_FOUND)
if (NOT DEFINED SPHINX_THEME)
set (SPHINX_THEME default)
endif ()

if (NOT DEFINED SPHINX_THEME_DIR)
set (SPHINX_THEME_DIR)
endif ()

# configured documentation tools and intermediate build results
set (BINARY_BUILD_DIR "${CMAKE_CURRENT_BINARY_DIR}")

# Sphinx cache with pickled ReST documents
set (SPHINX_CACHE_DIR "${CMAKE_CURRENT_BINARY_DIR}/doctrees")

# HTML output directory
set (SPHINX_HTML_DIR "${CMAKE_CURRENT_BINARY_DIR}/html")

# doc .rst locations
set (SPHINX_DOC_LOATION "${CMAKE_CURRENT_SOURCE_DIR}")

configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/conf.py.in"
"${BINARY_BUILD_DIR}/conf.py"
@ONLY
)

add_custom_target(sphinx ALL
${SPHINX_EXECUTABLE} -b html
-c "${BINARY_BUILD_DIR}"
-d "${SPHINX_CACHE_DIR}"
"${SPHINX_DOC_LOATION}"
"${SPHINX_HTML_DIR}"
COMMENT "Building HTML documentation with Sphinx"
)

add_dependencies (sphinx ${SWIG_MODULE_lua-mraa_REAL_NAME})
endif ()
endif ()
Loading