]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/controller.cpp
Strip Id tags and copyright notices from files
[r2c2.git] / source / libr2c2 / controller.cpp
1 #include <cmath>
2 #include <msp/core/except.h>
3 #include "controller.h"
4
5 using namespace std;
6 using namespace Msp;
7
8 namespace R2C2 {
9
10 void Controller::Control::set(float v)
11 {
12         if(v<min_value)
13                 v = min_value;
14         else if(v>max_value)
15                 v = max_value;
16         else if(type==BINARY)
17                 value = v ? 1 : 0;
18         else if(type==DISCRETE)
19                 value = min_value+floor((v-min_value)/step)*step;
20         else if(type==CONTINUOUS)
21                 value = v;
22 }
23
24 Controller::Control Controller::Control::binary(const string &n)
25 {
26         Controller::Control tc;
27         tc.name = n;
28         tc.type = BINARY;
29         tc.min_value = 0;
30         tc.max_value = 1;
31         tc.step = 1;
32         tc.value = 0;
33
34         return tc;
35 }
36
37 Controller::Control Controller::Control::discrete(const string &n, float m, float x, float s)
38 {
39         if(x<m)
40                 throw InvalidParameterValue("Max value must be greater than min value");
41
42         Controller::Control tc;
43         tc.name = n;
44         tc.type = DISCRETE;
45         tc.min_value = m;
46         tc.max_value = m+floor((x-m)/s)*s;
47         tc.step = s;
48         tc.value = m;
49
50         return tc;
51 }
52
53 Controller::Control Controller::Control::continuous(const string &n, float m, float x)
54 {
55         if(x<m)
56                 throw InvalidParameterValue("Max value must be greater than min value");
57
58         Controller::Control tc;
59         tc.name = n;
60         tc.type = CONTINUOUS;
61         tc.min_value = m;
62         tc.max_value = x;
63         tc.step = 0;
64         tc.value = m;
65
66         return tc;
67 }
68
69 } // namespace R2C2