fix compile with openmp
[liba.git] / quickjs / src / pid.c
blob800c6f4df8294bb289f03e26424cec66cc1a2e10
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_kpid(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
41 (void)argc;
42 a_pid *const self = (a_pid *)JS_GetOpaque2(ctx, this_val, liba_pid_class_id);
43 if (!self) { return JS_EXCEPTION; }
44 double args[] = {0, 0, 0};
45 for (unsigned int i = 0; i < A_LEN(args); ++i)
47 if (JS_ToFloat64(ctx, &args[i], argv[i])) { return JS_EXCEPTION; }
49 a_pid_kpid(self, (a_float)args[0], (a_float)args[1], (a_float)args[2]);
50 return JS_UNDEFINED;
53 static JSValue liba_pid_zero(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
55 (void)argc;
56 (void)argv;
57 a_pid *const self = (a_pid *)JS_GetOpaque2(ctx, this_val, liba_pid_class_id);
58 if (!self) { return JS_EXCEPTION; }
59 a_pid_zero(self);
60 return JS_UNDEFINED;
63 static JSValue liba_pid_run(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
65 (void)argc;
66 a_pid *const self = (a_pid *)JS_GetOpaque2(ctx, this_val, liba_pid_class_id);
67 if (!self) { return JS_EXCEPTION; }
68 double args[] = {0, 0};
69 for (unsigned int i = 0; i < A_LEN(args); ++i)
71 if (JS_ToFloat64(ctx, &args[i], argv[i])) { return JS_EXCEPTION; }
73 return JS_NewFloat64(ctx, (double)a_pid_run(self, (a_float)args[0], (a_float)args[1]));
76 static JSValue liba_pid_pos(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
78 (void)argc;
79 a_pid *const self = (a_pid *)JS_GetOpaque2(ctx, this_val, liba_pid_class_id);
80 if (!self) { return JS_EXCEPTION; }
81 double args[] = {0, 0};
82 for (unsigned int i = 0; i < A_LEN(args); ++i)
84 if (JS_ToFloat64(ctx, &args[i], argv[i])) { return JS_EXCEPTION; }
86 return JS_NewFloat64(ctx, (double)a_pid_pos(self, (a_float)args[0], (a_float)args[1]));
89 static JSValue liba_pid_inc(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
91 (void)argc;
92 a_pid *const self = (a_pid *)JS_GetOpaque2(ctx, this_val, liba_pid_class_id);
93 if (!self) { return JS_EXCEPTION; }
94 double args[] = {0, 0};
95 for (unsigned int i = 0; i < A_LEN(args); ++i)
97 if (JS_ToFloat64(ctx, &args[i], argv[i])) { return JS_EXCEPTION; }
99 return JS_NewFloat64(ctx, (double)a_pid_inc(self, (a_float)args[0], (a_float)args[1]));
102 enum
104 self_kp_,
105 self_ki_,
106 self_kd_,
107 self_summax_,
108 self_summin_,
109 self_sum_,
110 self_outmax_,
111 self_outmin_,
112 self_out_,
113 self_fdb_,
114 self_err_,
117 static JSValue liba_pid_get(JSContext *ctx, JSValueConst this_val, int magic)
119 a_pid *const self = (a_pid *)JS_GetOpaque2(ctx, this_val, liba_pid_class_id);
120 if (!self) { return JS_EXCEPTION; }
121 double x;
122 switch (magic)
124 case self_kp_: x = (double)self->kp; break;
125 case self_ki_: x = (double)self->ki; break;
126 case self_kd_: x = (double)self->kd; break;
127 case self_summax_: x = (double)self->summax; break;
128 case self_summin_: x = (double)self->summin; break;
129 case self_sum_: x = (double)self->sum; break;
130 case self_outmax_: x = (double)self->outmax; break;
131 case self_outmin_: x = (double)self->outmin; break;
132 case self_out_: x = (double)self->out; break;
133 case self_fdb_: x = (double)self->fdb; break;
134 case self_err_: x = (double)self->err; break;
135 default: return JS_UNDEFINED;
137 return JS_NewFloat64(ctx, x);
140 static JSValue liba_pid_set(JSContext *ctx, JSValueConst this_val, JSValueConst val, int magic)
142 a_pid *const self = (a_pid *)JS_GetOpaque2(ctx, this_val, liba_pid_class_id);
143 if (!self) { return JS_EXCEPTION; }
144 double x;
145 if (JS_ToFloat64(ctx, &x, val)) { return JS_EXCEPTION; }
146 switch (magic)
148 case self_kp_: self->kp = (a_float)x; break;
149 case self_ki_: self->ki = (a_float)x; break;
150 case self_kd_: self->kd = (a_float)x; break;
151 case self_summax_: self->summax = (a_float)x; break;
152 case self_summin_: self->summin = (a_float)x; break;
153 case self_outmax_: self->outmax = (a_float)x; break;
154 case self_outmin_: self->outmin = (a_float)x; break;
155 default: break;
157 return JS_UNDEFINED;
160 static JSCFunctionListEntry const liba_pid_proto[] = {
161 JS_PROP_STRING_DEF("[Symbol.toStringTag]", "a.pid", 0),
162 JS_CGETSET_MAGIC_DEF("kp", liba_pid_get, liba_pid_set, self_kp_),
163 JS_CGETSET_MAGIC_DEF("ki", liba_pid_get, liba_pid_set, self_ki_),
164 JS_CGETSET_MAGIC_DEF("kd", liba_pid_get, liba_pid_set, self_kd_),
165 JS_CGETSET_MAGIC_DEF("summax", liba_pid_get, liba_pid_set, self_summax_),
166 JS_CGETSET_MAGIC_DEF("summin", liba_pid_get, liba_pid_set, self_summin_),
167 JS_CGETSET_MAGIC_DEF("sum", liba_pid_get, NULL, self_sum_),
168 JS_CGETSET_MAGIC_DEF("outmax", liba_pid_get, liba_pid_set, self_outmax_),
169 JS_CGETSET_MAGIC_DEF("outmin", liba_pid_get, liba_pid_set, self_outmin_),
170 JS_CGETSET_MAGIC_DEF("out", liba_pid_get, NULL, self_out_),
171 JS_CGETSET_MAGIC_DEF("fdb", liba_pid_get, NULL, self_fdb_),
172 JS_CGETSET_MAGIC_DEF("err", liba_pid_get, NULL, self_err_),
173 JS_CFUNC_DEF("kpid", 3, liba_pid_kpid),
174 JS_CFUNC_DEF("zero", 0, liba_pid_zero),
175 JS_CFUNC_DEF("run", 2, liba_pid_run),
176 JS_CFUNC_DEF("pos", 2, liba_pid_pos),
177 JS_CFUNC_DEF("inc", 2, liba_pid_inc),
180 int js_liba_pid_init(JSContext *ctx, JSModuleDef *m)
182 JS_NewClassID(&liba_pid_class_id);
183 JS_NewClass(JS_GetRuntime(ctx), liba_pid_class_id, &liba_pid_class);
185 JSValue const proto = JS_NewObject(ctx);
186 JS_SetPropertyFunctionList(ctx, proto, liba_pid_proto, A_LEN(liba_pid_proto));
188 JSValue const clazz = JS_NewCFunction2(ctx, liba_pid_ctor, "pid", 3, JS_CFUNC_constructor, 0);
189 JS_SetConstructor(ctx, clazz, proto);
190 JS_SetClassProto(ctx, liba_pid_class_id, proto);
192 return JS_SetModuleExport(ctx, m, "pid", clazz);