Kaynağa Gözat

Device script can now produce measurements.

Neal Wilson 10 yıl önce
ebeveyn
işleme
8d3f170b4b
3 değiştirilmiş dosya ile 105 ekleme ve 1 silme
  1. 72
    1
      src/measurement.w
  2. 9
    0
      src/typica.w
  3. 24
    0
      src/unsupportedserial.w

+ 72
- 1
src/measurement.w Dosyayı Görüntüle

@@ -121,4 +121,75 @@ public default constructor (already defined above), a public copy constructor,
121 121
 and a public destructor. These latter two are default generated.
122 122
 
123 123
 @<Register meta-types@>=
124
-qRegisterMetaType<Measurement>("Measurement");
124
+qRegisterMetaType<Measurement>("Measurement");
125
+
126
+@ A little more is required to use |Measurement| objects in scripts.
127
+
128
+@<Class declarations@>=
129
+Q_DECLARE_METATYPE(Measurement)
130
+
131
+@ The only thing unusual here is the conversion to and from script values.
132
+
133
+@<Function prototypes for scripting@>=
134
+QScriptValue constructMeasurement(QScriptContext *context, QScriptEngine *engine);
135
+void setMeasurementProperties(QScriptValue value, QScriptEngine *engine);
136
+QScriptValue Measurement_toScriptValue(QScriptEngine *engine, const Measurement &measurement);
137
+void Measurement_fromScriptValue(const QScriptValue &value, Measurement &measurement);
138
+
139
+@ This follows much the same pattern as other types not derived from |QObject|.
140
+
141
+@<Set up the scripting engine@>=
142
+constructor = engine->newFunction(constructMeasurement);
143
+engine->globalObject().setProperty("Measurement", constructor);
144
+qScriptRegisterMetaType(engine, Measurement_toScriptValue, Measurement_fromScriptValue);
145
+
146
+@ The constructor takes two or three arguments. If the third arguement is not
147
+supplied, we will assume that the measurements are in degrees Fahrenheit.
148
+
149
+@<Functions for scripting@>=
150
+QScriptValue constructMeasurement(QScriptContext *context, QScriptEngine *engine)
151
+{
152
+	QScriptValue object;
153
+	if(context->argumentCount() == 2 || context->argumentCount() == 3)
154
+	{
155
+		double measurement = argument<double>(0, context);
156
+		QTime timestamp = argument<QTime>(1, context);
157
+		Units::Unit unit = Units::Fahrenheit;
158
+		if(context->argumentCount() == 3)
159
+		{
160
+			unit = argument<Units::Unit>(2, context);
161
+		}
162
+		object = engine->toScriptValue<Measurement>(Measurement(measurement, timestamp, unit));
163
+		setMeasurementProperties(object, engine);
164
+	}
165
+	else
166
+	{
167
+		context->throwError("Incorrect number of arguments passed to "@|
168
+		                    "Measurement::Measurement(). This method takes two "@|
169
+		                    "or three arguments.");
170
+	}
171
+	return object;
172
+}
173
+
174
+@ No additional properties are currently needed, but if they were, they would go here.
175
+
176
+@<Functions for scripting@>=
177
+void setMeasurementProperties(QScriptValue, QScriptEngine *)
178
+{
179
+	/* Nothing needs to be done here. */
180
+}
181
+
182
+@ The script value conversions are reasonably straightforward.
183
+
184
+@<Functions for scripting@>=
185
+QScriptValue Measurement_toScriptValue(QScriptEngine *engine, const Measurement &measurement)
186
+{
187
+	QVariant var;
188
+	var.setValue(measurement);
189
+	return engine->newVariant(var);
190
+}
191
+
192
+void Measurement_fromScriptValue(const QScriptValue &value, Measurement &measurement)
193
+{
194
+	measurement = value.toVariant().value<Measurement>();
195
+}

+ 9
- 0
src/typica.w Dosyayı Görüntüle

@@ -3034,6 +3034,15 @@ QScriptValue QTime_fromString(QScriptContext *context, QScriptEngine *engine)
3034 3034
 	return object;
3035 3035
 }
3036 3036
 
3037
+@ In order to pass |QTime| objects back from a script, we also need to overload
3038
+|argument()| for this type.
3039
+
3040
+@<Functions for scripting@>=
3041
+template<> QTime argument(int arg, QScriptContext *context)
3042
+{
3043
+	return qscriptvalue_cast<QTime>(context->argument(arg));
3044
+}
3045
+
3037 3046
 @* Scripting Item View Classes.
3038 3047
 
3039 3048
 \noindent |QAbstractScrollArea| is a |QFrame| that serves as the base class for

+ 24
- 0
src/unsupportedserial.w Dosyayı Görüntüle

@@ -609,6 +609,30 @@ void JavaScriptDevice::setAnnotationColumn(int ncol)
609 609
 	annotationNoteColumn = ncol;
610 610
 }
611 611
 
612
+@ Device scripts must be able to produce measurements on a channel. To do this,
613
+a function is provided for obtaining a timestamp. The returned timestamp should
614
+not be examined as future changes may break assumptions about the content of
615
+the timestamp.
616
+
617
+@<Function prototypes for scripting@>=
618
+QScriptValue getMeasurementTimestamp(QScriptContext *context, QScriptEngine *engine);
619
+
620
+@ That method is made available to the scripting engine.
621
+
622
+@<Set up the scripting engine@>=
623
+engine->globalObject().setProperty("getMeasurementTimestamp",
624
+                                   engine->newFunction(getMeasurementTimestamp));
625
+
626
+@ At present this simply obtains the current system time. It is planned to
627
+switch to a better quality clock in the future, but this should be done for
628
+everything that uses |Measurement| objects at once.
629
+
630
+@<Functions for scripting@>=
631
+QScriptValue getMeasurementTimestamp(QScriptContext *, QScriptEngine *engine)@/
632
+{
633
+	return engine->toScriptValue<QTime>(QTime::currentTime());
634
+}
635
+
612 636
 @ At present, implementations are not broken out to a separate file. This
613 637
 should be changed at some point.
614 638
 

Loading…
İptal
Kaydet