Commit 8922946b authored by Léo-Paul Géneau's avatar Léo-Paul Géneau 👾

Add position class

Return a position object instead of using various getters.
parent fb8d3662
...@@ -32,6 +32,12 @@ typedef struct { ...@@ -32,6 +32,12 @@ typedef struct {
UA_UInt32 messageId; UA_UInt32 messageId;
} JSDroneData; } JSDroneData;
typedef struct {
UA_Double x;
UA_Double y;
UA_Double z;
} JSPositionData;
typedef struct { typedef struct {
char* name; char* name;
char* typeName; char* typeName;
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
# define ANSI_COLOR_RESET "\x1b[0m" # define ANSI_COLOR_RESET "\x1b[0m"
static JSClassID jsDroneClassId; static JSClassID jsDroneClassId;
static JSClassID jsPositionClassId;
static UA_Boolean pubsubShouldRun = true; static UA_Boolean pubsubShouldRun = true;
static UA_Boolean pubsubExited = true; static UA_Boolean pubsubExited = true;
...@@ -44,6 +45,65 @@ const char *logCategoryNames[10] = ...@@ -44,6 +45,65 @@ const char *logCategoryNames[10] =
{"network", "channel", "session", "server", "client", {"network", "channel", "session", "server", "client",
"userland", "securitypolicy", "eventloop", "pubsub", "discovery"}; "userland", "securitypolicy", "eventloop", "pubsub", "discovery"};
// Position class functions
static void js_position_finalizer(JSRuntime *rt, JSValue val)
{
JSPositionData *s = (JSPositionData *) JS_GetOpaque(val, jsPositionClassId);
js_free_rt(rt, s);
}
static JSValue js_new_position(JSContext *ctx, JSValueConst thisVal,
int argc, JSValueConst *argv)
{
JSPositionData *s;
JSValue obj;
UA_Double *positionArray;
obj = JS_NewObjectClass(ctx, (int) jsPositionClassId);
if (JS_IsException(obj))
return obj;
s = (JSPositionData *) js_mallocz(ctx, sizeof(*s));
if (!s) {
JS_FreeValue(ctx, obj);
return JS_EXCEPTION;
}
positionArray = getPositionArray();
s->x = positionArray[0];
s->y = positionArray[1];
s->z = positionArray[3];
JS_SetOpaque(obj, s);
free(positionArray);
return obj;
}
static JSValue js_position_get(JSContext *ctx, JSValueConst thisVal, int magic)
{
JSPositionData *s = (JSPositionData *) JS_GetOpaque2(ctx, thisVal, jsPositionClassId);
if (!s)
return JS_EXCEPTION;
switch(magic) {
case 0:
return JS_NewFloat64(ctx, s->x);
case 1:
return JS_NewFloat64(ctx, s->y);
case 2:
return JS_NewFloat64(ctx, s->z);
default:
return JS_EXCEPTION;
}
}
static JSClassDef jsPositionClass = {
"Position",
.finalizer = js_position_finalizer,
};
static const JSCFunctionListEntry js_position_proto_funcs[] = {
JS_CGETSET_MAGIC_DEF("x", js_position_get, NULL, 0),
JS_CGETSET_MAGIC_DEF("y", js_position_get, NULL, 1),
JS_CGETSET_MAGIC_DEF("z", js_position_get, NULL, 2),
};
// Drone class functions // Drone class functions
static void js_drone_finalizer(JSRuntime *rt, JSValue val) static void js_drone_finalizer(JSRuntime *rt, JSValue val)
...@@ -627,12 +687,6 @@ static JSValue js_getAltitude(JSContext *ctx, JSValueConst thisVal, ...@@ -627,12 +687,6 @@ static JSValue js_getAltitude(JSContext *ctx, JSValueConst thisVal,
return JS_NewFloat64(ctx, getAltitude()); return JS_NewFloat64(ctx, getAltitude());
} }
static JSValue js_getAltitudeRel(JSContext *ctx, JSValueConst thisVal,
int argc, JSValueConst *argv)
{
return JS_NewFloat64(ctx, getAltitudeRel());
}
static JSValue js_getInitialAltitude(JSContext *ctx, static JSValue js_getInitialAltitude(JSContext *ctx,
JSValueConst thisVal, JSValueConst thisVal,
int argc, JSValueConst *argv) int argc, JSValueConst *argv)
...@@ -654,18 +708,6 @@ static JSValue js_getInitialLongitude(JSContext *ctx, ...@@ -654,18 +708,6 @@ static JSValue js_getInitialLongitude(JSContext *ctx,
return JS_NewFloat64(ctx, getInitialLongitude()); return JS_NewFloat64(ctx, getInitialLongitude());
} }
static JSValue js_getLatitude(JSContext *ctx, JSValueConst thisVal,
int argc, JSValueConst *argv)
{
return JS_NewFloat64(ctx, getLatitude());
}
static JSValue js_getLongitude(JSContext *ctx, JSValueConst thisVal,
int argc, JSValueConst *argv)
{
return JS_NewFloat64(ctx, getLongitude());
}
static JSValue js_getTakeOffAltitude(JSContext *ctx, static JSValue js_getTakeOffAltitude(JSContext *ctx,
JSValueConst thisVal, JSValueConst thisVal,
int argc, JSValueConst *argv) int argc, JSValueConst *argv)
...@@ -717,13 +759,11 @@ static const JSCFunctionListEntry js_funcs[] = { ...@@ -717,13 +759,11 @@ static const JSCFunctionListEntry js_funcs[] = {
JS_CFUNC_DEF("loiter", 4, js_loiter ), JS_CFUNC_DEF("loiter", 4, js_loiter ),
JS_CFUNC_DEF("setAirSpeed", 1, js_setAirSpeed ), JS_CFUNC_DEF("setAirSpeed", 1, js_setAirSpeed ),
JS_CFUNC_DEF("setTargetCoordinates", 3, js_setTargetCoordinates ), JS_CFUNC_DEF("setTargetCoordinates", 3, js_setTargetCoordinates ),
JS_CFUNC_DEF("getPosition", 0, js_new_position ),
JS_CFUNC_DEF("getAltitude", 0, js_getAltitude ), JS_CFUNC_DEF("getAltitude", 0, js_getAltitude ),
JS_CFUNC_DEF("getAltitudeRel", 0, js_getAltitudeRel ),
JS_CFUNC_DEF("getInitialAltitude", 0, js_getInitialAltitude ), JS_CFUNC_DEF("getInitialAltitude", 0, js_getInitialAltitude ),
JS_CFUNC_DEF("getInitialLatitude", 0, js_getInitialLatitude ), JS_CFUNC_DEF("getInitialLatitude", 0, js_getInitialLatitude ),
JS_CFUNC_DEF("getInitialLongitude", 0, js_getInitialLongitude ), JS_CFUNC_DEF("getInitialLongitude", 0, js_getInitialLongitude ),
JS_CFUNC_DEF("getLatitude", 0, js_getLatitude ),
JS_CFUNC_DEF("getLongitude", 0, js_getLongitude ),
JS_CFUNC_DEF("getTakeOffAltitude", 0, js_getTakeOffAltitude ), JS_CFUNC_DEF("getTakeOffAltitude", 0, js_getTakeOffAltitude ),
JS_CFUNC_DEF("getYaw", 0, js_getYaw ), JS_CFUNC_DEF("getYaw", 0, js_getYaw ),
JS_CFUNC_DEF("getAirspeed", 0, js_getSpeed ), JS_CFUNC_DEF("getAirspeed", 0, js_getSpeed ),
...@@ -736,19 +776,25 @@ static const JSCFunctionListEntry js_funcs[] = { ...@@ -736,19 +776,25 @@ static const JSCFunctionListEntry js_funcs[] = {
static int js_init(JSContext *ctx, JSModuleDef *m) static int js_init(JSContext *ctx, JSModuleDef *m)
{ {
JSValue droneProto, droneClass; JSValue droneProto, droneClass, positionProto;
JS_NewClassID(&jsDroneClassId); JS_NewClassID(&jsDroneClassId);
JS_NewClassID(&jsPositionClassId);
JS_NewClass(JS_GetRuntime(ctx), jsDroneClassId, &jsDroneClass); JS_NewClass(JS_GetRuntime(ctx), jsDroneClassId, &jsDroneClass);
JS_NewClass(JS_GetRuntime(ctx), jsPositionClassId, &jsPositionClass);
droneProto = JS_NewObject(ctx); droneProto = JS_NewObject(ctx);
JS_SetPropertyFunctionList(ctx, droneProto, js_drone_proto_funcs, JS_SetPropertyFunctionList(ctx, droneProto, js_drone_proto_funcs,
countof(js_drone_proto_funcs)); countof(js_drone_proto_funcs));
positionProto = JS_NewObject(ctx);
JS_SetPropertyFunctionList(ctx, positionProto, js_position_proto_funcs,
countof(js_position_proto_funcs));
droneClass = JS_NewCFunction2(ctx, js_drone_ctor, "Drone", 1, droneClass = JS_NewCFunction2(ctx, js_drone_ctor, "Drone", 1,
JS_CFUNC_constructor, 0); JS_CFUNC_constructor, 0);
JS_SetConstructor(ctx, droneClass, droneProto); JS_SetConstructor(ctx, droneClass, droneProto);
JS_SetClassProto(ctx, jsDroneClassId, droneProto); JS_SetClassProto(ctx, jsDroneClassId, droneProto);
JS_SetClassProto(ctx, jsPositionClassId, positionProto);
JS_SetModuleExport(ctx, m, "Drone", droneClass); JS_SetModuleExport(ctx, m, "Drone", droneClass);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment