Browse Source

grovevdiv: python example and js modification for grovevdiv voltage divider

Signed-off-by: Zion Orent <zorent@ics.com>
Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
Zion Orent 9 years ago
parent
commit
2ceee8bf26
2 changed files with 61 additions and 4 deletions
  1. 7
    4
      examples/javascript/grovevdiv.js
  2. 54
    0
      examples/python/grovevdiv.py

+ 7
- 4
examples/javascript/grovevdiv.js View File

@@ -1,6 +1,5 @@
1 1
 /*jslint node:true, vars:true, bitwise:true, unparam:true */
2 2
 /*jshint unused:true */
3
-/*global */
4 3
 /*
5 4
 * Author: Zion Orent <zorent@ics.com>
6 5
 * Copyright (c) 2014 Intel Corporation.
@@ -31,11 +30,12 @@ var myVoltageDivider = new voltageDivider.GroveVDiv(0);
31 30
 
32 31
 // collect data and output measured voltage according to the setting
33 32
 // of the scaling switch (3 or 10)
33
+var val, gain3val, gain10val;
34 34
 function getVoltageInfo()
35 35
 {
36
-	var val = myVoltageDivider.value(100);
37
-	var gain3val = myVoltageDivider.computedValue(3, val);
38
-	var gain10val = myVoltageDivider.computedValue(10, val);
36
+	val = myVoltageDivider.value(100);
37
+	gain3val = myVoltageDivider.computedValue(3, val);
38
+	gain10val = myVoltageDivider.computedValue(10, val);
39 39
 	console.log("ADC value: " + val + " Gain 3: " + gain3val 
40 40
 				+ "v Gain 10: " + gain10val + "v");
41 41
 }
@@ -45,6 +45,9 @@ setInterval(getVoltageInfo, 1000);
45 45
 // Print message when exiting
46 46
 process.on('SIGINT', function()
47 47
 {
48
+	myVoltageDivider = null;
49
+	voltageDivider.cleanUp();
50
+	voltageDivider = null;
48 51
 	console.log("Exiting...");
49 52
 	process.exit(0);
50 53
 });

+ 54
- 0
examples/python/grovevdiv.py View File

@@ -0,0 +1,54 @@
1
+#!/usr/bin/python
2
+# Author: Zion Orent <zorent@ics.com>
3
+# Copyright (c) 2015 Intel Corporation.
4
+#
5
+# Permission is hereby granted, free of charge, to any person obtaining
6
+# a copy of this software and associated documentation files (the
7
+# "Software"), to deal in the Software without restriction, including
8
+# without limitation the rights to use, copy, modify, merge, publish,
9
+# distribute, sublicense, and/or sell copies of the Software, and to
10
+# permit persons to whom the Software is furnished to do so, subject to
11
+# the following conditions:
12
+#
13
+# The above copyright notice and this permission notice shall be
14
+# included in all copies or substantial portions of the Software.
15
+#
16
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+import time, sys, signal, atexit
25
+import pyupm_grovevdiv as upmGrovevdiv
26
+
27
+# Instantiate a Grove Voltage Divider sensor on analog pin A0
28
+myVoltageDivider = upmGrovevdiv.GroveVDiv(0)
29
+
30
+
31
+## Exit handlers ##
32
+# This stops python from printing a stacktrace when you hit control-C
33
+def SIGINTHandler(signum, frame):
34
+	raise SystemExit
35
+
36
+# This function lets you run code on exit,
37
+# including functions from myVoltageDivider
38
+def exitHandler():
39
+	print "Exiting"
40
+	sys.exit(0)
41
+
42
+# Register exit handlers
43
+atexit.register(exitHandler)
44
+signal.signal(signal.SIGINT, SIGINTHandler)
45
+
46
+
47
+while(1):
48
+	val = myVoltageDivider.value(100)
49
+	gain3val = myVoltageDivider.computedValue(3, val)
50
+	gain10val = myVoltageDivider.computedValue(10, val)
51
+	print "ADC value: {0} Gain 3: {1}v Gain 10: {2}v".format(
52
+	val,  gain3val, gain10val)
53
+
54
+	time.sleep(1)