summaryrefslogtreecommitdiffstats
path: root/src/control.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/control.c')
-rw-r--r--src/control.c68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/control.c b/src/control.c
new file mode 100644
index 0000000..d952f7f
--- /dev/null
+++ b/src/control.c
@@ -0,0 +1,68 @@
+#include "control.h"
+#include <stdio.h>
+
+int
+cc_step(cc_t *cc, int steps)
+{
+ float tmp = cc->mod;
+ cc->mod += steps * cc->step;
+
+ if (cc->value+cc->mod > cc->max) {
+ cc->mod = 0;
+ cc->target = cc->max;
+ } else if (cc->value+cc->mod < cc->min) {
+ cc->mod = 0;
+ cc->target = cc->min;
+ }
+
+ return cc->mod != tmp;
+}
+
+void
+cc_prep(cc_t *cc)
+{
+ if (cc->mod) {
+ cc->target = cc->value + cc->mod;
+ }
+}
+
+void
+cc_fix(cc_t *cc)
+{
+ cc->value = cc->target;
+ cc->mod = 0;
+}
+
+void
+cc_reset(cc_t *cc)
+{
+ cc->target = cc->def;
+}
+
+float
+cc_iget(cc_t *cc, unsigned int i, unsigned int max)
+{
+ /*
+ v/t_s: value/time start
+ v/t_e: value/time end
+ y = m*x + b
+ m = (v_e - v_s) / (t_e - t_s)
+ b = v_s - m * t_s
+ */
+
+ float m = (cc->target - cc->value) / (max - 0);
+
+ float b = cc->value /* + m * 0 */;
+
+ return m * i + b;
+}
+
+const char *
+cc_to_str(cc_t *cc)
+{
+ static char buf[128];
+
+ snprintf(buf, sizeof buf, "[%s:v:%.2f_m:%.2f_t:%.2f]", cc->name, cc->value, cc->mod, cc->target);
+
+ return buf;
+}