]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/controller.cpp
Rename the project to R²C²
[r2c2.git] / source / libr2c2 / controller.cpp
diff --git a/source/libr2c2/controller.cpp b/source/libr2c2/controller.cpp
new file mode 100644 (file)
index 0000000..480abc2
--- /dev/null
@@ -0,0 +1,76 @@
+/* $Id$
+
+This file is part of R²C²
+Copyright © 2010  Mikkosoft Productions, Mikko Rasa
+Distributed under the GPL
+*/
+
+#include <cmath>
+#include <msp/core/except.h>
+#include "controller.h"
+
+using namespace std;
+using namespace Msp;
+
+namespace R2C2 {
+
+void Controller::Control::set(float v)
+{
+       if(v<min_value)
+               v = min_value;
+       else if(v>max_value)
+               v = max_value;
+       else if(type==BINARY)
+               value = v ? 1 : 0;
+       else if(type==DISCRETE)
+               value = min_value+floor((v-min_value)/step)*step;
+       else if(type==CONTINUOUS)
+               value = v;
+}
+
+Controller::Control Controller::Control::binary(const string &n)
+{
+       Controller::Control tc;
+       tc.name = n;
+       tc.type = BINARY;
+       tc.min_value = 0;
+       tc.max_value = 1;
+       tc.step = 1;
+       tc.value = 0;
+
+       return tc;
+}
+
+Controller::Control Controller::Control::discrete(const string &n, float m, float x, float s)
+{
+       if(x<m)
+               throw InvalidParameterValue("Max value must be greater than min value");
+
+       Controller::Control tc;
+       tc.name = n;
+       tc.type = DISCRETE;
+       tc.min_value = m;
+       tc.max_value = m+floor((x-m)/s)*s;
+       tc.step = s;
+       tc.value = m;
+
+       return tc;
+}
+
+Controller::Control Controller::Control::continuous(const string &n, float m, float x)
+{
+       if(x<m)
+               throw InvalidParameterValue("Max value must be greater than min value");
+
+       Controller::Control tc;
+       tc.name = n;
+       tc.type = CONTINUOUS;
+       tc.min_value = m;
+       tc.max_value = x;
+       tc.step = 0;
+       tc.value = m;
+
+       return tc;
+}
+
+} // namespace R2C2