]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/controller.cpp
Make LCD output selectable at runtime through an extra I/O pin
[r2c2.git] / source / libmarklin / controller.cpp
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2010  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <cmath>
9 #include <msp/core/except.h>
10 #include "controller.h"
11
12 using namespace std;
13 using namespace Msp;
14
15 namespace Marklin {
16
17 void Controller::Control::set(float v)
18 {
19         if(v<min_value)
20                 v = min_value;
21         else if(v>max_value)
22                 v = max_value;
23         else if(type==BINARY)
24                 value = v ? 1 : 0;
25         else if(type==DISCRETE)
26                 value = min_value+floor((v-min_value)/step)*step;
27         else if(type==CONTINUOUS)
28                 value = v;
29 }
30
31 Controller::Control Controller::Control::binary(const string &n)
32 {
33         Controller::Control tc;
34         tc.name = n;
35         tc.type = BINARY;
36         tc.min_value = 0;
37         tc.max_value = 1;
38         tc.step = 1;
39         tc.value = 0;
40
41         return tc;
42 }
43
44 Controller::Control Controller::Control::discrete(const string &n, float m, float x, float s)
45 {
46         if(x<m)
47                 throw InvalidParameterValue("Max value must be greater than min value");
48
49         Controller::Control tc;
50         tc.name = n;
51         tc.type = DISCRETE;
52         tc.min_value = m;
53         tc.max_value = m+floor((x-m)/s)*s;
54         tc.step = s;
55         tc.value = m;
56
57         return tc;
58 }
59
60 Controller::Control Controller::Control::continuous(const string &n, float m, float x)
61 {
62         if(x<m)
63                 throw InvalidParameterValue("Max value must be greater than min value");
64
65         Controller::Control tc;
66         tc.name = n;
67         tc.type = CONTINUOUS;
68         tc.min_value = m;
69         tc.max_value = x;
70         tc.step = 0;
71         tc.value = m;
72
73         return tc;
74 }
75
76 } // namespace Marklin