mirror of
https://github.com/elua/elua.git
synced 2025-01-08 20:56:17 +08:00
Forgot to commit this.
This commit is contained in:
parent
e006aaaaa9
commit
ed31980cb8
84
src/modules/can.c
Normal file
84
src/modules/can.c
Normal file
@ -0,0 +1,84 @@
|
||||
// Module for interfacing Lua code with a Controller Area Network (CAN)
|
||||
|
||||
#include "lua.h"
|
||||
#include "lualib.h"
|
||||
#include "lauxlib.h"
|
||||
#include "platform.h"
|
||||
#include "auxmods.h"
|
||||
#include "lrotable.h"
|
||||
|
||||
// Lua: setup( id, clock )
|
||||
static int can_setup( lua_State* L )
|
||||
{
|
||||
unsigned id;
|
||||
u32 clock, res;
|
||||
|
||||
id = luaL_checkinteger( L, 1 );
|
||||
MOD_CHECK_ID( can, id );
|
||||
clock = luaL_checkinteger( L, 2 );
|
||||
res = platform_can_setup( id, clock );
|
||||
lua_pushinteger( L, res );
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Lua: send( id, canid, canidtype, message )
|
||||
static int can_send( lua_State* L )
|
||||
{
|
||||
size_t len;
|
||||
int id, canid, idtype;
|
||||
const char *data;
|
||||
|
||||
id = luaL_checkinteger( L, 1 );
|
||||
MOD_CHECK_ID( can, id );
|
||||
canid = luaL_checkinteger( L, 2 );
|
||||
idtype = luaL_checkinteger( L, 3 );
|
||||
data = luaL_checklstring (L, 4, &len);
|
||||
if ( len > PLATFORM_CAN_MAXLEN )
|
||||
return luaL_error( L, "message exceeds max length" );
|
||||
|
||||
platform_can_send( id, canid, idtype, len, ( const u8 * )data );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Lua: canid, canidtype, message = recv( id )
|
||||
static int can_recv( lua_State* L )
|
||||
{
|
||||
u8 len;
|
||||
int id;
|
||||
u32 canid;
|
||||
u8 idtype, data[ 8 ];
|
||||
|
||||
id = luaL_checkinteger( L, 1 );
|
||||
MOD_CHECK_ID( can, id );
|
||||
|
||||
platform_can_recv( id, &canid, &idtype, &len, data );
|
||||
lua_pushinteger( L, canid );
|
||||
lua_pushinteger( L, idtype );
|
||||
lua_pushlstring (L, ( const char * )data, ( size_t )len);
|
||||
|
||||
return 3;
|
||||
}
|
||||
|
||||
|
||||
// Module function map
|
||||
#define MIN_OPT_LEVEL 2
|
||||
#include "lrodefs.h"
|
||||
const LUA_REG_TYPE can_map[] =
|
||||
{
|
||||
{ LSTRKEY( "setup" ), LFUNCVAL( can_setup ) },
|
||||
{ LSTRKEY( "send" ), LFUNCVAL( can_send ) },
|
||||
{ LSTRKEY( "recv" ), LFUNCVAL( can_recv ) },
|
||||
{ LNILKEY, LNILVAL }
|
||||
};
|
||||
|
||||
LUALIB_API int luaopen_can( lua_State *L )
|
||||
{
|
||||
#if LUA_OPTIMIZE_MEMORY > 0
|
||||
return 0;
|
||||
#else // #if LUA_OPTIMIZE_MEMORY > 0
|
||||
luaL_register( L, AUXLIB_CAN, can_map );
|
||||
|
||||
return 1;
|
||||
#endif // #if LUA_OPTIMIZE_MEMORY > 0
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user