Browse Source

Expose SerialScale to host environment

Neal Wilson 11 years ago
parent
commit
3fe40821ef
2 changed files with 60 additions and 5 deletions
  1. 4
    2
      src/Typica.pro
  2. 56
    3
      src/scales.w

+ 4
- 2
src/Typica.pro View File

@@ -21,13 +21,15 @@ HEADERS += moc_typica.cpp \
21 21
     abouttypica.h \
22 22
     units.h \
23 23
     webview.h \
24
-    webelement.h
24
+    webelement.h \
25
+    scale.h
25 26
 SOURCES += typica.cpp \
26 27
     helpmenu.cpp \
27 28
     abouttypica.cpp \
28 29
     units.cpp \
29 30
     webview.cpp \
30
-    webelement.cpp
31
+    webelement.cpp \
32
+    scale.cpp
31 33
 
32 34
 RESOURCES += \
33 35
     resources.qrc

+ 56
- 3
src/scales.w View File

@@ -65,6 +65,7 @@ scale, buffering and interpreting the response, and signaling new measurements.
65 65
 #define TypicaScaleInclude
66 66
 
67 67
 #include "3rdparty/qextserialport/src/qextserialport.h"
68
+#include "units.h"
68 69
 
69 70
 class SerialScale : public QextSerialPort
70 71
 {
@@ -89,6 +90,8 @@ a signal to buffer data..
89 90
 
90 91
 @(scale.cpp@>=
91 92
 #include "scale.h"
93
+#include <QStringList>
94
+
92 95
 
93 96
 SerialScale::SerialScale(const QString &port) :
94 97
 	QextSerialPort(port, QextSerialPort::EventDriven)
@@ -158,11 +161,11 @@ else if(responseParts[1] == "kg")
158 161
 }
159 162
 else if(responseParts[1] == "g")
160 163
 {
161
-	unit = Units::gram;
164
+	unit = Units::Gram;
162 165
 }
163 166
 else if(responseParts[1] == "oz")
164 167
 {
165
-	unit = Units::ounce;
168
+	unit = Units::Ounce;
166 169
 }
167 170
 emit newMeasurement(weight, unit);
168 171
 
@@ -176,7 +179,57 @@ void SerialScale::tare()
176 179
 	write("!KT\x0D");
177 180
 }
178 181
 
179
-void SerialScale::weight()
182
+void SerialScale::weigh()
180 183
 {
181 184
 	write("!KP\x0D");
182 185
 }
186
+
187
+@ This must be available to the host environment.
188
+
189
+@<Function prototypes for scripting@>=
190
+QScriptValue constructSerialScale(QScriptContext *context, QScriptEngine *engine);
191
+void setSerialScaleProperties(QScriptValue value, QScriptEngine *engine);
192
+
193
+@ These functions are made known to the scripting engine in the usual way.
194
+
195
+@<Set up the scripting engine@>=
196
+constructor = engine->newFunction(constructSerialScale);
197
+value = engine->newQMetaObject(&SerialScale::staticMetaObject, constructor);
198
+engine->globalObject().setProperty("SerialScale", value);
199
+
200
+@ In order to make this class available to the host environment, we must also
201
+include the appropriate header file.
202
+
203
+@<Header files to include@>=
204
+#include "scale.h"
205
+
206
+@ Most of the properties of interest should be added automatically, however
207
+there are non-slot methods in |QIODevice| that we require.
208
+
209
+@<Functions for scripting@>=
210
+void setSerialScaleProperties(QScriptValue value, QScriptEngine *engine)
211
+{
212
+	setQIODeviceProperties(value, engine);
213
+}
214
+
215
+@ The script constructor should seem familiar.
216
+
217
+@<Functions for scripting@>=
218
+QScriptValue constructSerialScale(QScriptContext *context, QScriptEngine *engine)
219
+{
220
+	QScriptValue object;
221
+	if(context->argumentCount() == 1)
222
+	{
223
+		object = engine->newQObject(new SerialScale(argument<QString>(0, context)));
224
+		setSerialScaleProperties(object, engine);
225
+	}
226
+	else
227
+	{
228
+		context->throwError("Incorrect number of arguments passed to "
229
+		                    "SerialScale. The constructor takes one string "
230
+		                    "as an argument specifying a port name.");
231
+	}
232
+	return object;
233
+}
234
+
235
+@ While the |SerialScale| class will always 

Loading…
Cancel
Save