Browse Source

my9221: Add C++ and JS examples

These were tested on the Grove LED Bar.

Signed-off-by: Jon Trulson <jtrulson@ics.com>
Signed-off-by: Zion Orent <zorent@ics.com>
Signed-off-by: John Van Drasek <john.r.van.drasek@intel.com>
Jon Trulson 10 years ago
parent
commit
a853733f61
3 changed files with 154 additions and 0 deletions
  1. 3
    0
      examples/CMakeLists.txt
  2. 73
    0
      examples/javascript/my9221.js
  3. 78
    0
      examples/my9221.cxx

+ 3
- 0
examples/CMakeLists.txt View File

@@ -74,6 +74,7 @@ add_executable (mq303a-example mq303a.cxx)
74 74
 add_executable (grovespeaker-example grovespeaker.cxx)
75 75
 add_executable (rfr359f-example rfr359f.cxx)
76 76
 add_executable (biss0001-example biss0001.cxx)
77
+add_executable (my9221-example my9221.cxx)
77 78
 
78 79
 include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
79 80
 include_directories (${PROJECT_SOURCE_DIR}/src/grove)
@@ -135,6 +136,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/mq303a)
135 136
 include_directories (${PROJECT_SOURCE_DIR}/src/grovespeaker)
136 137
 include_directories (${PROJECT_SOURCE_DIR}/src/rfr359f)
137 138
 include_directories (${PROJECT_SOURCE_DIR}/src/biss0001)
139
+include_directories (${PROJECT_SOURCE_DIR}/src/my9221)
138 140
 
139 141
 target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
140 142
 target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
@@ -212,3 +214,4 @@ target_link_libraries (mq303a-example mq303a ${CMAKE_THREAD_LIBS_INIT})
212 214
 target_link_libraries (grovespeaker-example grovespeaker ${CMAKE_THREAD_LIBS_INIT})
213 215
 target_link_libraries (rfr359f-example rfr359f ${CMAKE_THREAD_LIBS_INIT})
214 216
 target_link_libraries (biss0001-example biss0001 ${CMAKE_THREAD_LIBS_INIT})
217
+target_link_libraries (my9221-example my9221 ${CMAKE_THREAD_LIBS_INIT})

+ 73
- 0
examples/javascript/my9221.js View File

@@ -0,0 +1,73 @@
1
+/*jslint node:true, vars:true, bitwise:true, unparam:true */
2
+/*jshint unused:true */
3
+/*
4
+* Author: Zion Orent <zorent@ics.com>
5
+* Copyright (c) 2015 Intel Corporation.
6
+*
7
+* Permission is hereby granted, free of charge, to any person obtaining
8
+* a copy of this software and associated documentation files (the
9
+* "Software"), to deal in the Software without restriction, including
10
+* without limitation the rights to use, copy, modify, merge, publish,
11
+* distribute, sublicense, and/or sell copies of the Software, and to
12
+* permit persons to whom the Software is furnished to do so, subject to
13
+* the following conditions:
14
+*
15
+* The above copyright notice and this permission notice shall be
16
+* included in all copies or substantial portions of the Software.
17
+*
18
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
+* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
+* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
+* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
+* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
+*/
26
+
27
+var LEDBar = require("jsupm_my9221");
28
+
29
+// Instantiate a MY9221, we use D2 for the data, and D3 for the
30
+// data clock.  This was tested with a Grove LED bar.
31
+var myLEDBar = new LEDBar.MY9221(2, 3);
32
+
33
+var directionBool = true;
34
+
35
+// A note on timing:
36
+//	In the C++ example, the system sleeps 10 times for 50 milliseconds
37
+//	between each LED lighting. After the LED has reached the last light
38
+//	of the cycle, the system sleeps again for 1 second.
39
+//	The sleeps are cumulative, so the system has slept for 1.5 seconds
40
+//	sum total for each cycle.
41
+//	setInterval and setTimeout make asynchronous function calls;
42
+//	they aren't cumulative.
43
+//	In order to approximate the behavior of the C++ example, we need
44
+//	to call each iteration 1.5 seconds apart instead of 1 second apart.
45
+var myInterval = setInterval(function()
46
+{
47
+	// start showing LED strip with just the first one lit
48
+	show_LED(1, directionBool);
49
+}, (1000 + (10*50)) );
50
+
51
+function show_LED(level, direction)
52
+{
53
+	// If it's less than 10
54
+	//	light up the LED now
55
+	//	call show_LED again in 50 ms
56
+	if (level <= 10)
57
+	{
58
+		myLEDBar.setBarLevel(level, directionBool);
59
+		setTimeout(show_LED, 50, ++level, directionBool);	
60
+	}
61
+	// Switch LED lighting directions between lighting cycles
62
+	else
63
+		directionBool = !directionBool;
64
+}
65
+
66
+// When exiting: clear LED strip lights, clear interval, print exit message
67
+process.on('SIGINT', function()
68
+{
69
+	myLEDBar.setBarLevel(0, true);
70
+	clearInterval(myInterval);
71
+	console.log("Exiting...");
72
+	process.exit(0);
73
+});

+ 78
- 0
examples/my9221.cxx View File

@@ -0,0 +1,78 @@
1
+/*
2
+ * Author: Jon Trulson <jtrulson@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
+
25
+#include <unistd.h>
26
+#include <iostream>
27
+#include <signal.h>
28
+#include "my9221.h"
29
+
30
+using namespace std;
31
+
32
+int shouldRun = true;
33
+
34
+void sig_handler(int signo)
35
+{
36
+  if (signo == SIGINT)
37
+    shouldRun = false;
38
+}
39
+
40
+
41
+int main ()
42
+{
43
+  signal(SIGINT, sig_handler);
44
+
45
+//! [Interesting] 
46
+
47
+  // Instantiate a MY9221, we use D2 for the data, and D3 for the
48
+  // data clock.  This was tested with a Grove LED bar.
49
+
50
+  upm::MY9221* bar = new upm::MY9221(2, 3);
51
+  
52
+  while (shouldRun)
53
+    {
54
+      // count up from green to red
55
+      for (int i=1; i<=10; i++)
56
+        {
57
+          bar->setBarLevel(i, true);
58
+          usleep(50000);
59
+        }
60
+      sleep(1);
61
+
62
+      // count down from red to green
63
+      for (int i=1; i<=10; i++)
64
+        {
65
+          bar->setBarLevel(i, false);
66
+          usleep(50000);
67
+        }
68
+      sleep(1);
69
+    }
70
+//! [Interesting]
71
+
72
+  cout << "Exiting..." << endl;
73
+  // turn off the LED's
74
+  bar->setBarLevel(0, true);
75
+
76
+  delete bar;
77
+  return 0;
78
+}