#include #include "libad.h" #include "stdlib.h" #include "string.h" #include "Windows.h" static int ad4_open(lua_State *L) { const char * name = luaL_checkstring(L, 1); int32_t ret = ad_open(name); lua_pushinteger(L, ret); return 1; } static int ad4_close(lua_State *L) { int32_t adh = luaL_checkinteger(L, 1); int32_t ret = ad_close(adh); lua_pushinteger(L, ret); return 1; } static int ad4_analog_in(lua_State *L) { float volt; int32_t adh = luaL_checkinteger(L, 1); int32_t cha = luaL_checkinteger(L, 2); int32_t rng = luaL_checkinteger(L, 3); int32_t ret = ad_analog_in (adh, cha, rng, &volt); lua_pushinteger(L, ret); lua_pushnumber(L, volt); return 2; } static int ad4_analog_out(lua_State *L) { int32_t adh = luaL_checkinteger(L, 1); int32_t cha = luaL_checkinteger(L, 2); int32_t rng = luaL_checkinteger(L, 3); float volt= luaL_checknumber(L, 4); int32_t ret = ad_analog_out (adh, cha, rng, volt); lua_pushinteger(L, ret); lua_pushnumber(L, volt); return 2; } static int ad4_digital_in(lua_State *L) { uint32_t data; int32_t adh = luaL_checkinteger(L, 1); int32_t cha = luaL_checkinteger(L, 2); int32_t ret = ad_digital_in (adh, cha, &data); lua_pushinteger(L, ret); lua_pushnumber(L, data); return 2; } static int ad4_digital_out(lua_State *L) { int32_t adh = luaL_checkinteger(L, 1); int32_t cha = luaL_checkinteger(L, 2); uint32_t data = luaL_checkinteger(L, 3); int32_t ret = ad_digital_out (adh, cha, data); lua_pushinteger(L, ret); return 1; } static int ad4_get_version(lua_State *L) { uint32_t v = ad_get_version (); lua_pushinteger(L, AD_MAJOR_VERS(v)); lua_pushinteger(L, AD_MINOR_VERS(v)); lua_pushinteger(L, AD_BUILD_VERS(v)); return 3; } static int ad4_get_drv_version(lua_State *L) { int32_t adh = luaL_checkinteger(L, 1); uint32_t v; int32_t ret = ad_get_drv_version (adh, &v); lua_pushinteger(L, ret); lua_pushinteger(L, AD_MAJOR_VERS(v)); lua_pushinteger(L, AD_MINOR_VERS(v)); lua_pushinteger(L, AD_BUILD_VERS(v)); return 4; } static int getIntField(lua_State *L, int tableStackPos, const char *key, int defaultVal) { int result = defaultVal; if (lua_istable(L,tableStackPos)) // doublecheck { lua_getfield(L,tableStackPos,key); if (!lua_isnil(L,-1)) // is key in table { if (lua_isnumber(L, -1)) { result = (int)lua_tonumber(L, -1); } else { luaL_error(L, "invalid component in table. key: %s", key); } } lua_pop(L, 1); // remove number } return result; } static float getFloatField(lua_State *L, int tableStackPos, const char *key, float defaultVal) { float result = defaultVal; if (lua_istable(L,tableStackPos)) // doublecheck { lua_getfield(L,tableStackPos,key); if (!lua_isnil(L,-1)) // is key in table { if (lua_isnumber(L, -1)) { result = lua_tonumber(L, -1); } else { luaL_error(L, "invalid component in table. key: %s", key); } } lua_pop(L, 1); // remove number } return result; } static int ad4_scan(lua_State *L) { int i,j; int chav_count=0; float * samples = 0; int argc = lua_gettop(L); struct ad_scan_cha_desc * chav = 0; struct ad_scan_desc sd; int32_t rc; int scan_result = 0; int32_t adh = luaL_checkinteger(L, 1); luaL_checktype(L, 2, LUA_TTABLE); if(argc <= 2) luaL_error(L, "table expected"); for(i=3; i<=argc; i++) luaL_checktype(L, i, LUA_TTABLE); chav_count = argc - 2; memset (&sd, 0, sizeof(sd)); sd.sample_rate = getFloatField(L, 2, "rate", 0.01); sd.prehist = getIntField(L, 2, "prehist", 0); sd.posthist = getIntField(L, 2, "posthist", 0); sd.ticks_per_run = sd.prehist + sd.posthist; chav = calloc(sizeof(struct ad_scan_cha_desc), chav_count); for(i=0; i>16; chav[i].trg_par[1] = 0; } rc = ad_calc_run_size (adh, &sd, chav_count, chav); if(rc != 0) goto end; rc = ad_start_mem_scan (adh, &sd, chav_count, chav); if(rc != 0) goto end; samples = (float *) malloc (sd.samples_per_run * sizeof(float)); rc = ad_get_next_run_f (adh, NULL, NULL, samples); if(rc != 0) goto end; rc = ad_stop_scan(adh, &scan_result); if(rc != 0) goto end; end: lua_pushinteger(L, rc); lua_pushinteger(L, scan_result); if(rc == 0) { lua_newtable(L); for(i=0; i