Line data Source code
1 :
2 : #ifndef magAOXMaths_hpp
3 : #define magAOXMaths_hpp
4 :
5 : #include "../../libMagAOX/libMagAOX.hpp" //Note this is included on command line to trigger pch
6 : #include "../../magaox_git_version.h"
7 :
8 : #include <limits>
9 :
10 : namespace MagAOX
11 : {
12 : namespace app
13 : {
14 :
15 : /*
16 : void externalLog ( const std::string & name,
17 : const int & code,
18 : const std::string & valueStr,
19 : const std::string & source
20 : )
21 : {
22 : std::cerr << name << " " << code << " " << valueStr << " " << source << "\n";
23 : }*/
24 :
25 :
26 : /** MagAO-X application to do math on some numbers
27 : *
28 : */
29 : class magAOXMaths : public MagAOXApp<>
30 : {
31 :
32 : protected:
33 : double m_val {0};
34 :
35 : // declare our properties
36 : pcf::IndiProperty m_indiP_myVal;
37 : pcf::IndiProperty m_indiP_myVal_maths;
38 :
39 : pcf::IndiProperty m_indiP_otherVal;
40 :
41 : pcf::IndiProperty m_indiP_setOtherVal;
42 :
43 : std::string m_myVal {"x"};
44 : std::string m_otherDevName;
45 : std::string m_otherValName;
46 :
47 : int updateVals();
48 :
49 : public:
50 :
51 : /// Default c'tor.
52 : magAOXMaths();
53 :
54 0 : ~magAOXMaths() noexcept
55 0 : {
56 0 : }
57 :
58 :
59 : /// Setup the configuration system (called by MagAOXApp::setup())
60 : virtual void setupConfig();
61 :
62 : /// Load the configuration system results (called by MagAOXApp::setup())
63 : virtual void loadConfig();
64 :
65 : /// Checks if the device was found during loadConfig.
66 : virtual int appStartup();
67 :
68 : /// Implementation of the FSM for the maths.
69 : virtual int appLogic();
70 :
71 : /// Do any needed shutdown tasks. Currently nothing in this app.
72 : virtual int appShutdown();
73 :
74 :
75 0 : INDI_NEWCALLBACK_DECL(magAOXMaths, m_indiP_myVal);
76 :
77 0 : INDI_SETCALLBACK_DECL(magAOXMaths, m_indiP_otherVal);
78 :
79 0 : INDI_NEWCALLBACK_DECL(magAOXMaths, m_indiP_setOtherVal);
80 :
81 : };
82 :
83 0 : magAOXMaths::magAOXMaths() : MagAOXApp(MAGAOX_CURRENT_SHA1, MAGAOX_REPO_MODIFIED)
84 : {
85 0 : return;
86 0 : }
87 :
88 0 : void magAOXMaths::setupConfig()
89 : {
90 0 : config.add("myVal", "", "myVal", argType::Required, "", "myVal", false, "string", "The name of this app's value.");
91 0 : config.add("otherDevName", "", "otherDevName", argType::Required, "", "otherDevName", false, "string", "The name of the other app name.");
92 0 : config.add("otherValName", "", "otherValName", argType::Required, "", "otherValName", false, "string", "The name of the other val name.");
93 0 : }
94 :
95 0 : void magAOXMaths::loadConfig()
96 : {
97 0 : config(m_myVal, "myVal");
98 0 : config(m_otherDevName, "otherDevName");
99 0 : config(m_otherValName, "otherValName");
100 :
101 0 : }
102 :
103 0 : int magAOXMaths::appStartup()
104 : {
105 : // set up the x input property
106 0 : REG_INDI_NEWPROP(m_indiP_myVal, m_myVal, pcf::IndiProperty::Number);
107 0 : indi::addNumberElement<double>( m_indiP_myVal, "value", std::numeric_limits<double>::min(), std::numeric_limits<double>::max(), 1.0, "%f", "");
108 0 : m_indiP_myVal["value"].set<double>(0.0);
109 :
110 :
111 : // set up the result maths property
112 0 : REG_INDI_NEWPROP_NOCB(m_indiP_myVal_maths, "maths", pcf::IndiProperty::Number);
113 0 : indi::addNumberElement<double>(m_indiP_myVal_maths,"value", std::numeric_limits<double>::min(), std::numeric_limits<double>::max(), 1.0, "%f", "");
114 0 : indi::addNumberElement<double>(m_indiP_myVal_maths, "sqr", std::numeric_limits<double>::min(), std::numeric_limits<double>::max(), 1.0, "%f", "");
115 0 : indi::addNumberElement<double>(m_indiP_myVal_maths, "sqrt", std::numeric_limits<double>::min(), std::numeric_limits<double>::max(), 1.0, "%f", "");
116 0 : indi::addNumberElement<double>(m_indiP_myVal_maths, "abs", std::numeric_limits<double>::min(), std::numeric_limits<double>::max(), 1.0, "%f", "");
117 0 : indi::addNumberElement<double>(m_indiP_myVal_maths, "prod", std::numeric_limits<double>::min(), std::numeric_limits<double>::max(), 1.0, "%f", "");
118 :
119 0 : REG_INDI_SETPROP(m_indiP_otherVal, m_otherDevName, m_otherValName);
120 0 : m_indiP_otherVal.add (pcf::IndiElement("value"));
121 0 : m_indiP_otherVal["value"].set<double>(0.0);
122 :
123 0 : createStandardIndiNumber<double>( m_indiP_setOtherVal, "other_val", -1e50, 1e50, 0, "%0.f");
124 0 : m_indiP_setOtherVal["current"].set<double>(0.0);
125 0 : m_indiP_setOtherVal["target"].set<double>(0.0);
126 0 : registerIndiPropertyNew( m_indiP_setOtherVal, INDI_NEWCALLBACK( m_indiP_setOtherVal));
127 :
128 0 : updateVals();
129 0 : state(stateCodes::READY);
130 0 : return 0;
131 : }
132 :
133 0 : int magAOXMaths::appLogic()
134 : {
135 0 : return 0;
136 :
137 : }
138 :
139 0 : int magAOXMaths::appShutdown()
140 : {
141 :
142 0 : return 0;
143 : }
144 :
145 0 : int magAOXMaths::updateVals()
146 : {
147 : // extract value
148 0 : double v = m_indiP_myVal["value"].get<double>();
149 :
150 0 : if(v == -1) log<text_log>( "value set to -1!", logPrio::LOG_WARNING);
151 0 : if(v == -2) log<text_log>( "value set to -2!", logPrio::LOG_ERROR);
152 0 : if(v == -3) log<text_log>( "value set to -3!", logPrio::LOG_CRITICAL);
153 0 : if(v == -4) log<text_log>( "value set to -4!", logPrio::LOG_ALERT);
154 0 : if(v == -5) log<text_log>( "value set to -5!", logPrio::LOG_EMERGENCY);
155 :
156 : // fill maths
157 0 : m_indiP_myVal_maths["value"] = v;
158 0 : m_indiP_myVal_maths["sqr"] = v*v;
159 0 : m_indiP_myVal_maths["sqrt"] = sqrt(v);
160 0 : m_indiP_myVal_maths["abs"] = fabs(v);
161 :
162 0 : m_indiP_myVal_maths["prod"] = v*m_indiP_otherVal["value"].get<double>();
163 0 : updateIfChanged( m_indiP_setOtherVal, "current", m_indiP_otherVal["value"].get<double>());
164 :
165 0 : log<text_log>("set new value: " + std::to_string(v), logPrio::LOG_NOTICE);
166 : // publish maths
167 0 : m_indiP_myVal_maths.setState (pcf::IndiProperty::Ok);
168 0 : if(m_indiDriver) m_indiDriver->sendSetProperty (m_indiP_myVal_maths);
169 :
170 0 : return 0;
171 : }
172 :
173 0 : INDI_NEWCALLBACK_DEFN(magAOXMaths, m_indiP_myVal)(const pcf::IndiProperty &ipRecv)
174 : {
175 :
176 0 : if (ipRecv.getName() == m_indiP_myVal.getName())
177 : {
178 : // received a new value for property val
179 0 : m_indiP_myVal["value"] = ipRecv["value"].get<double>();
180 0 : m_indiP_myVal.setState (pcf::IndiProperty::Ok);
181 0 : m_indiDriver->sendSetProperty (m_indiP_myVal);
182 :
183 0 : updateVals();
184 :
185 0 : return 0;
186 : }
187 0 : return -1;
188 : }
189 :
190 0 : INDI_SETCALLBACK_DEFN(magAOXMaths, m_indiP_otherVal)(const pcf::IndiProperty &ipRecv)
191 : {
192 0 : m_indiP_otherVal = ipRecv;
193 :
194 0 : updateVals();
195 0 : return 0;
196 : }
197 :
198 0 : INDI_NEWCALLBACK_DEFN(magAOXMaths, m_indiP_setOtherVal)(const pcf::IndiProperty &ipRecv)
199 : {
200 :
201 0 : if (ipRecv.getName() == m_indiP_setOtherVal.getName())
202 : {
203 0 : std::cerr << " m_indiP_setOtherVal\n";
204 :
205 : // received a new value for property val
206 0 : m_indiP_setOtherVal["target"] = ipRecv["target"].get<double>();
207 0 : m_indiP_setOtherVal.setState (pcf::IndiProperty::Ok);
208 : //m_indiDriver->sendSetProperty (m_indiP_myVal);
209 :
210 0 : sendNewProperty(m_indiP_otherVal, "value", m_indiP_setOtherVal["target"].get<double>());
211 :
212 0 : updateVals();
213 :
214 0 : return 0;
215 : }
216 0 : return -1;
217 : }
218 :
219 : } //namespace app
220 : } //namespace MagAOX
221 :
222 : #endif //magAOXMaths_hpp
|