rename other to that
[liba.git] / quickjs / src / pid.c
blob640ae6243faa7b61763bba6173b0a412a0b2b0db
1 #include "a.h"
2 #include "a/pid.h"
4 static JSClassID liba_pid_class_id;
6 static void liba_pid_finalizer(JSRuntime *rt, JSValue val)
8 js_free_rt(rt, JS_GetOpaque(val, liba_pid_class_id));
11 static JSClassDef liba_pid_class = {"pid", .finalizer = liba_pid_finalizer};
13 static JSValue liba_pid_ctor(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst *argv)
15 (void)argc;
16 (void)argv;
17 JSValue proto, clazz = JS_UNDEFINED;
18 a_pid *const self = (a_pid *)js_mallocz(ctx, sizeof(a_pid));
19 if (!self) { return JS_EXCEPTION; }
20 self->kp = 1;
21 self->summax = +A_FLOAT_INF;
22 self->summin = -A_FLOAT_INF;
23 self->outmax = +A_FLOAT_INF;
24 self->outmin = -A_FLOAT_INF;
25 a_pid_init(self);
26 proto = JS_GetPropertyStr(ctx, new_target, "prototype");
27 if (JS_IsException(proto)) { goto fail; }
28 clazz = JS_NewObjectProtoClass(ctx, proto, liba_pid_class_id);
29 JS_FreeValue(ctx, proto);
30 if (JS_IsException(clazz)) { goto fail; }
31 JS_SetOpaque(clazz, self);
32 return clazz;
33 fail:
34 js_free(ctx, self);
35 JS_FreeValue(ctx, clazz);
36 return JS_EXCEPTION;
39 static JSValue liba_pid_get(JSContext *ctx, JSValueConst this_val, int magic)
41 a_pid *const self = (a_pid *)JS_GetOpaque2(ctx, this_val, liba_pid_class_id);
42 if (!self) { return JS_EXCEPTION; }
43 double x;
44 switch (magic)
46 case 0: x = (double)self->kp; break;
47 case 1: x = (double)self->ki; break;
48 case 2: x = (double)self->kd; break;
49 case 3: x = (double)self->summax; break;
50 case 4: x = (double)self->summin; break;
51 case 5: x = (double)self->outmax; break;
52 case 6: x = (double)self->outmin; break;
53 case 7: x = (double)self->out; break;
54 case 8: x = (double)self->fdb; break;
55 case 9: x = (double)self->err; break;
56 default: return JS_UNDEFINED;
58 return JS_NewFloat64(ctx, x);
61 static JSValue liba_pid_set(JSContext *ctx, JSValueConst this_val, JSValueConst val, int magic)
63 a_pid *const self = (a_pid *)JS_GetOpaque2(ctx, this_val, liba_pid_class_id);
64 if (!self) { return JS_EXCEPTION; }
65 double x;
66 if (JS_ToFloat64(ctx, &x, val)) { return JS_EXCEPTION; }
67 switch (magic)
69 case 0: self->kp = (a_float)x; break;
70 case 1: self->ki = (a_float)x; break;
71 case 2: self->kd = (a_float)x; break;
72 case 3: self->summax = (a_float)x; break;
73 case 4: self->summin = (a_float)x; break;
74 case 5: self->outmax = (a_float)x; break;
75 case 6: self->outmin = (a_float)x; break;
76 default: break;
78 return JS_UNDEFINED;
81 static JSValue liba_pid_zero(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
83 (void)argc;
84 (void)argv;
85 a_pid *const self = (a_pid *)JS_GetOpaque2(ctx, this_val, liba_pid_class_id);
86 if (!self) { return JS_EXCEPTION; }
87 a_pid_zero(self);
88 return JS_UNDEFINED;
91 static JSValue liba_pid_kpid(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
93 (void)argc;
94 a_pid *const self = (a_pid *)JS_GetOpaque2(ctx, this_val, liba_pid_class_id);
95 if (!self) { return JS_EXCEPTION; }
96 double args[] = {0, 0, 0};
97 for (unsigned int i = 0; i < A_LEN(args); ++i)
99 if (JS_ToFloat64(ctx, &args[i], argv[i])) { return JS_EXCEPTION; }
101 a_pid_kpid(self, (a_float)args[0], (a_float)args[1], (a_float)args[2]);
102 return JS_UNDEFINED;
105 static JSValue liba_pid_run(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
107 (void)argc;
108 a_pid *const self = (a_pid *)JS_GetOpaque2(ctx, this_val, liba_pid_class_id);
109 if (!self) { return JS_EXCEPTION; }
110 double args[] = {0, 0};
111 for (unsigned int i = 0; i < A_LEN(args); ++i)
113 if (JS_ToFloat64(ctx, &args[i], argv[i])) { return JS_EXCEPTION; }
115 return JS_NewFloat64(ctx, (double)a_pid_run(self, (a_float)args[0], (a_float)args[1]));
118 static JSValue liba_pid_pos(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
120 (void)argc;
121 a_pid *const self = (a_pid *)JS_GetOpaque2(ctx, this_val, liba_pid_class_id);
122 if (!self) { return JS_EXCEPTION; }
123 double args[] = {0, 0};
124 for (unsigned int i = 0; i < A_LEN(args); ++i)
126 if (JS_ToFloat64(ctx, &args[i], argv[i])) { return JS_EXCEPTION; }
128 return JS_NewFloat64(ctx, (double)a_pid_pos(self, (a_float)args[0], (a_float)args[1]));
131 static JSValue liba_pid_inc(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
133 (void)argc;
134 a_pid *const self = (a_pid *)JS_GetOpaque2(ctx, this_val, liba_pid_class_id);
135 if (!self) { return JS_EXCEPTION; }
136 double args[] = {0, 0};
137 for (unsigned int i = 0; i < A_LEN(args); ++i)
139 if (JS_ToFloat64(ctx, &args[i], argv[i])) { return JS_EXCEPTION; }
141 return JS_NewFloat64(ctx, (double)a_pid_inc(self, (a_float)args[0], (a_float)args[1]));
144 static JSCFunctionListEntry const liba_pid_proto[] = {
145 JS_PROP_STRING_DEF("[Symbol.toStringTag]", "a.pid", 0),
146 JS_CGETSET_MAGIC_DEF("kp", liba_pid_get, liba_pid_set, 0),
147 JS_CGETSET_MAGIC_DEF("ki", liba_pid_get, liba_pid_set, 1),
148 JS_CGETSET_MAGIC_DEF("kd", liba_pid_get, liba_pid_set, 2),
149 JS_CGETSET_MAGIC_DEF("summax", liba_pid_get, liba_pid_set, 3),
150 JS_CGETSET_MAGIC_DEF("summin", liba_pid_get, liba_pid_set, 4),
151 JS_CGETSET_MAGIC_DEF("outmax", liba_pid_get, liba_pid_set, 5),
152 JS_CGETSET_MAGIC_DEF("outmin", liba_pid_get, liba_pid_set, 6),
153 JS_CGETSET_MAGIC_DEF("out", liba_pid_get, NULL, 7),
154 JS_CGETSET_MAGIC_DEF("fdb", liba_pid_get, NULL, 8),
155 JS_CGETSET_MAGIC_DEF("err", liba_pid_get, NULL, 9),
156 JS_CFUNC_DEF("kpid", 3, liba_pid_kpid),
157 JS_CFUNC_DEF("zero", 0, liba_pid_zero),
158 JS_CFUNC_DEF("run", 2, liba_pid_run),
159 JS_CFUNC_DEF("pos", 2, liba_pid_pos),
160 JS_CFUNC_DEF("inc", 2, liba_pid_inc),
163 int js_liba_pid_init(JSContext *ctx, JSModuleDef *m)
165 JS_NewClassID(&liba_pid_class_id);
166 JS_NewClass(JS_GetRuntime(ctx), liba_pid_class_id, &liba_pid_class);
168 JSValue const proto = JS_NewObject(ctx);
169 JS_SetPropertyFunctionList(ctx, proto, liba_pid_proto, A_LEN(liba_pid_proto));
171 JSValue const clazz = JS_NewCFunction2(ctx, liba_pid_ctor, "pid", 3, JS_CFUNC_constructor, 0);
172 JS_SetConstructor(ctx, clazz, proto);
173 JS_SetClassProto(ctx, liba_pid_class_id, proto);
175 return JS_SetModuleExport(ctx, m, "pid", clazz);