summaryrefslogtreecommitdiffstats
path: root/src/control.c
blob: d952f7fe3dda75b90be1729184ce470187bbd2fb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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;
}