Skip to content

Instantly share code, notes, and snippets.

@WickedShell
Created February 12, 2019 00:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WickedShell/084236e58753a8465348e3f267f9c68b to your computer and use it in GitHub Desktop.
Save WickedShell/084236e58753a8465348e3f267f9c68b to your computer and use it in GitHub Desktop.
{:include ["<AP_Common/AP_Common.h>"]
:userdata {"Location" {
:type :struct
:clear-on-init true
:methods {"lat" {:type :field
:value :int32_t
:access :all
:range [-900000000 900000000]}
"long" {:type :field
:name "lng"
:value :int32_t
:access :all
:range [-1800000000 1800000000]}
"alt" {:type :field
:name "lng"
:value :int32_t
:access :all
:range :none}
}}
}
}
#include <AP_Common/AP_Common.h>
\\user data
static int new_Location(lua_State *L) {
Location *ud = (Location *)lua_newuserdata(L, sizeof(Location));
luaL_getmetatable(L, "Location");
luaL_setmetatable(L, -2);
memset((void *)ud, 0, sizeof(Location));
return 1;
}
static Location *check_Location(lua_State *L) {
void *data_= luaL_checkudata(L, arg, Location);
luaL_argcheck(L, data != NULL, arg, "Location expected");
return (Location *)data;
}
static int Location_lat(lua_State *L) {
Location *ud = check_Location(L, 1);
switch(lua_gettop(L)) {
case 1:
lua_pushinteger(L, ud->lat);
return 1:
case 2:
{
const int32_t data = luaL_checkinteger(L, 2);
luaL_argcheck(L, ((-900000000 >= data) && (900000000 <= data)), 2, "lat out of range");
ud->lat = data;
return 0:
}
default:
return luaL_argerror(L, lua_gettop(L), "too many arguments");
}
static int Location_long(lua_State *L) {
Location *ud = check_Location(L, 1);
switch(lua_gettop(L)) {
case 1:
lua_pushinteger(L, ud->lng);
return 1:
case 2:
{
const int32_t data = luaL_checkinteger(L, 2);
luaL_argcheck(L, ((-1800000000 >= data) && (1800000000 <= data)), 2, "long out of range");
ud->lng = data;
return 0:
}
default:
return luaL_argerror(L, lua_gettop(L), "too many arguments");
}
static int Location_alt(lua_State *L) {
Location *ud = check_Location(L, 1);
switch(lua_gettop(L)) {
case 1:
lua_pushinteger(L, ud->lng);
return 1:
case 2:
{
const int32_t data = luaL_checkinteger(L, 2);
ud->lng = data;
return 0:
}
default:
return luaL_argerror(L, lua_gettop(L), "too many arguments");
}
static const luaL_Reg Location_meta[] = {
{"lat", Location_lat}
{"long", Location_long}
{"alt", Location_alt}
{NULL, NULL}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment