Skip to content

Commit

Permalink
Add os_utils lua binding to get access to gettimeofday() and getPID().
Browse files Browse the repository at this point in the history
Also small changes to deal with c/c++ errors/warnings.
  • Loading branch information
woutgg committed May 7, 2016
1 parent f680ce3 commit 9175956
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 7 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ define Package/print3d
SECTION:=mods
CATEGORY:=Doodle3D
TITLE:=3D printer driver
DEPENDS:= +libuci +uclibcxx +kmod-usb-acm +kmod-usb-serial +kmod-usb-serial-ftdi +@BUSYBOX_CUSTOM +@BUSYBOX_CONFIG_INOTIFYD
DEPENDS:= +libuci +liblua +uclibcxx +kmod-usb-acm +kmod-usb-serial +kmod-usb-serial-ftdi +@BUSYBOX_CUSTOM +@BUSYBOX_CONFIG_INOTIFYD
endef

define Package/print3d/description
Expand All @@ -41,6 +41,7 @@ define Package/print3d/install
$(INSTALL_BIN) $(PKG_BUILD_DIR)/server/print3d $(1)/bin/
$(INSTALL_BIN) $(PKG_BUILD_DIR)/frontends/cmdline/p3d $(1)/bin/
$(INSTALL_BIN) $(PKG_BUILD_DIR)/frontends/lua/print3d.so $(1)/usr/lib/lua/
$(INSTALL_BIN) $(PKG_BUILD_DIR)/frontends/lua/os_utils.so $(1)/usr/lib/lua/

$(INSTALL_BIN) $(PKG_BUILD_DIR)/script/print3d_init $(1)/etc/init.d/print3d
$(INSTALL_BIN) $(PKG_BUILD_DIR)/script/print3d-runner.sh $(1)/usr/libexec/
Expand Down
5 changes: 3 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ cmake_minimum_required(VERSION 2.6)
project(print3d)

if(APPLE)
#create multi-architecture files on OSX (mainly for library loading in older 32 bit lua binaries)
set(CMAKE_OSX_ARCHITECTURES x86_64;i386)
#previously, i386 was included here as well, but that has been removed to get os_utils to link
#set(CMAKE_OSX_ARCHITECTURES x86_64;i386)
set(CMAKE_OSX_ARCHITECTURES x86_64)
endif()

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
Expand Down
2 changes: 0 additions & 2 deletions src/frontends/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
cmake_minimum_required(VERSION 2.6)
project(print3d)

add_definitions(--std=gnu99)

option(CMDLINE_FRONTEND "build command-line frontend" ON)
option(LUA_FRONTEND "build Lua frontend" ON)

Expand Down
2 changes: 2 additions & 0 deletions src/frontends/cmdline/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ project(print3d)
set(SOURCES fe_cmdline.c fe_cmdline.h actions.c)
set(HEADERS)

add_definitions(--std=gnu99)

if(CMDLINE_FRONTEND)
set(EXEC_NAME p3d)
add_executable(${EXEC_NAME} ${SOURCES} ${HEADERS})
Expand Down
4 changes: 3 additions & 1 deletion src/frontends/communicator.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <fcntl.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
Expand Down Expand Up @@ -444,7 +445,8 @@ int comm_sendGCodeData(const char *gcode, int32_t total_lines, ipc_gcode_metadat
}

#ifdef DEBUG_GCODE_FRAGMENTATION
for (const char *sp = startP; sp <= endP; ++sp) if (*sp == '\n') lineNum++;
const char *sp;
for (sp = startP; sp <= endP; ++sp) if (*sp == '\n') lineNum++;
#endif

int scmdlen, rcmdlen;
Expand Down
11 changes: 10 additions & 1 deletion src/frontends/lua/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ if(NOT LUA_CFLAGS)
endif()
endif()

add_definitions(--std=gnu99 -I.. ${LUA_CFLAGS})
add_definitions(-I.. ${LUA_CFLAGS})

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
#As advised on http://lua-users.org/wiki/BindingCodeToLua
add_definitions(-DLUA_USE_APICHECK)
Expand Down Expand Up @@ -55,3 +56,11 @@ if(LUA_FRONTEND)

install(TARGETS print3d_lua LIBRARY DESTINATION ${LUAPATH})
endif()

add_library(os_utils_lua MODULE os_utils.c lua_compat.h)
target_link_libraries(os_utils_lua lua)
set_target_properties(os_utils_lua PROPERTIES
OUTPUT_NAME os_utils
PREFIX ""
)
install(TARGETS os_utils_lua LIBRARY DESTINATION ${LUAPATH})
106 changes: 106 additions & 0 deletions src/frontends/lua/os_utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* This file is part of the Doodle3D project (http://doodle3d.com).
*
* Copyright (c) 2013-2014, Doodle3D
* This software is licensed under the terms of the GNU GPL v2 or later.
* See file LICENSE.txt or visit http://www.gnu.org/licenses/gpl.html for full license details.
*/

#define LUA_LIB

#include <unistd.h>
#include <sys/time.h>
#include "lua_compat.h"


/*********************
* UTILITY FUNCTIONS *
* *******************
*
* For microsecond-precision time and obtaining PID.
* Note: this should really be in the firmware repository but that would require
* a C(++) dependency on that package as well as duplicating lua_compat.h.
*/

/*
* --- Get the current time with high precision.
* -- The returned values are as returned by the C function `gettimeofday()`,
* -- so it is in local time since epoch (1-1-1970).
* --
* -- @return Seconds since epoch.
* -- @return Microseconds since epoch.
* -- @name getTime
*/
static int l_getTime(lua_State *L) {
struct timeval now;

gettimeofday(&now, NULL);

lua_pushinteger(L, now.tv_sec);
lua_pushinteger(L, now.tv_usec);
return 2;
}

/*
* --- Get both the time elapsed since the given time in milliseconds.
* -- Additionally, the current time is returned in the same fashion as getTime,
* -- in order to allow for easy repeated measuring of elapsed time.
* -- If less than two arguments are passed, nil+msg is returned.
* --
* -- @see getTime
* -- @param sec Seconds since epoch to measure elapsed time from.
* -- @param usec Microseconds since epoch to measure elapsed time from.
* -- @return The number of milliseconds elapsed since the given time.
* -- @return Current seconds since epoch.
* -- @return Current microseconds since epoch.
* -- @name getElapsedTime
*/
static int l_getElapsedTime(lua_State *L) {
struct timeval begin, now;

if (lua_gettop(L) >= 2) {
begin.tv_sec = luaL_checkinteger(L, 1);
begin.tv_usec = luaL_checkinteger(L, 2);
} else {
lua_pushnil(L);
lua_pushstring(L, "two arguments required: seconds and microseconds");
return 2;
}

gettimeofday(&now, NULL);
//usec must be cast to int because if it has rolled over, the subtraction can be negative
int msec_diff = (now.tv_sec - begin.tv_sec) * 1000 + ((int)now.tv_usec - (int)begin.tv_usec) / 1000;

lua_pushinteger(L, msec_diff);
lua_pushinteger(L, now.tv_sec);
lua_pushinteger(L, now.tv_usec);
return 3;
}

/*
* --- Return the process ID of the current process.
* --
* -- @return The PID as returned by the C function `getPID()`.
* -- @name getPID
*/
static int l_getPID(lua_State *L) {
lua_pushinteger(L, getpid());
return 1;
}


/**************************
* REGISTRATION FUNCTIONS *
**************************/

static const struct luaL_Reg os_utils_f[] = {
{"getTime", l_getTime},
{"getElapsedTime", l_getElapsedTime},
{"getPID", l_getPID},
{NULL, NULL}
};

int luaopen_os_utils(lua_State *L) {
luaL_newlib(L, os_utils_f);
return 1;
}

0 comments on commit 9175956

Please sign in to comment.