Quellcode durchsuchen

hm11: Initial implementation

This module implements support for the Grove BLE (Bluetooth Low
Energy) device.  It is implemented as a UART device accepting an "AT"
command set.

Signed-off-by: Jon Trulson <jtrulson@ics.com>
Signed-off-by: Zion Orent <zorent@ics.com>
Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
Jon Trulson vor 9 Jahren
Ursprung
Commit
d776edbab2

+ 3
- 0
examples/c++/CMakeLists.txt Datei anzeigen

@@ -117,6 +117,7 @@ add_executable (isd1820-example isd1820.cxx)
117 117
 add_executable (sx6119-example sx6119.cxx)
118 118
 add_executable (si114x-example si114x.cxx)
119 119
 add_executable (maxsonarez-example maxsonarez.cxx)
120
+add_executable (hm11-example hm11.cxx)
120 121
 
121 122
 include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
122 123
 include_directories (${PROJECT_SOURCE_DIR}/src/grove)
@@ -212,6 +213,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/isd1820)
212 213
 include_directories (${PROJECT_SOURCE_DIR}/src/sx6119)
213 214
 include_directories (${PROJECT_SOURCE_DIR}/src/si114x)
214 215
 include_directories (${PROJECT_SOURCE_DIR}/src/maxsonarez)
216
+include_directories (${PROJECT_SOURCE_DIR}/src/hm11)
215 217
 
216 218
 target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
217 219
 target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
@@ -330,3 +332,4 @@ target_link_libraries (isd1820-example isd1820 ${CMAKE_THREAD_LIBS_INIT})
330 332
 target_link_libraries (sx6119-example sx6119 ${CMAKE_THREAD_LIBS_INIT})
331 333
 target_link_libraries (si114x-example si114x ${CMAKE_THREAD_LIBS_INIT})
332 334
 target_link_libraries (maxsonarez-example maxsonarez ${CMAKE_THREAD_LIBS_INIT})
335
+target_link_libraries (hm11-example hm11 ${CMAKE_THREAD_LIBS_INIT})

+ 123
- 0
examples/c++/hm11.cxx Datei anzeigen

@@ -0,0 +1,123 @@
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 <stdio.h>
29
+#include "hm11.h"
30
+
31
+using namespace std;
32
+using namespace upm;
33
+
34
+void printUsage(char *progname)
35
+{
36
+  cout << "Usage: " << progname << " [AT command]" << endl;
37
+  cout << endl;
38
+
39
+  cout << "If an argument is supplied on the command line, that argument is"
40
+       << endl;
41
+  cout << "sent to the module and the response is printed out." << endl;
42
+  cout << endl;
43
+  cout << "If no argument is used, then the address and PIN of the module" 
44
+       << endl;
45
+  cout << "are queried and the results printed out." << endl;
46
+  cout << endl;
47
+  cout << endl;
48
+}
49
+
50
+// simple helper function to send a command and wait for a response
51
+void sendCommand(upm::HM11* ble, char *cmd)
52
+{
53
+  char buffer[BUFSIZ];
54
+  ble->writeData(cmd, strlen(cmd));
55
+  
56
+  // wait up to 1 second
57
+  if (ble->dataAvailable(1000))
58
+    {
59
+      memset(buffer, 0, BUFSIZ);
60
+      
61
+      ble->readData(buffer, BUFSIZ);
62
+      cout << "Returned: " << buffer << endl;
63
+    }
64
+  else
65
+    {
66
+      cerr << "Timed out waiting for response" << endl;
67
+    }
68
+}
69
+
70
+
71
+int main (int argc, char **argv)
72
+{
73
+//! [Interesting]
74
+  char buffer[BUFSIZ];
75
+  // Instantiate a HM11 BLE Module on UART 0
76
+
77
+  upm::HM11* ble = new upm::HM11(0);
78
+
79
+  // make sure port is initialized properly.  9600 baud is the default.
80
+  if (!ble->setupTty(B9600))
81
+    {
82
+      cerr << "Failed to setup tty port parameters" << endl;
83
+      return 1;
84
+    }
85
+
86
+  printUsage(argv[0]);
87
+
88
+  if (argc > 1)
89
+    {
90
+      cout << "Sending command line argument (" << argv[1] << ")..." << endl;
91
+      sendCommand(ble, argv[1]);
92
+    }
93
+  else
94
+    {
95
+      // query the module address
96
+      char addr[] = "AT+ADDR?";
97
+      cout << "Querying module address (" << addr << ")..." << endl;
98
+      sendCommand(ble, addr);
99
+
100
+      sleep(1);
101
+
102
+      // query the module address
103
+      char pin[] = "AT+PASS?";
104
+      cout << "Querying module PIN (" << pin << ")..." << endl;
105
+      sendCommand(ble, pin);
106
+
107
+      // Other potentially useful commands are:
108
+      //
109
+      // AT+VERS? - query module version
110
+      // AT+ROLE0 - set as slave
111
+      // AT+ROLE1 - set as master
112
+      // AT+CLEAR - clear all previous settings
113
+      // AT+RESET - restart the device
114
+      //
115
+      // A comprehensive list is available from the datasheet at:
116
+      // http://www.seeedstudio.com/wiki/images/c/cd/Bluetooth4_en.pdf
117
+    }
118
+
119
+//! [Interesting]
120
+
121
+  delete ble;
122
+  return 0;
123
+}

+ 136
- 0
examples/javascript/hm11.js Datei anzeigen

@@ -0,0 +1,136 @@
1
+/*jslint node:true, vars:true, bitwise:true, unparam:true */
2
+/*jshint unused:true */
3
+
4
+/*
5
+* Author: Zion Orent <zorent@ics.com>
6
+* Copyright (c) 2015 Intel Corporation.
7
+*
8
+* Permission is hereby granted, free of charge, to any person obtaining
9
+* a copy of this software and associated documentation files (the
10
+* "Software"), to deal in the Software without restriction, including
11
+* without limitation the rights to use, copy, modify, merge, publish,
12
+* distribute, sublicense, and/or sell copies of the Software, and to
13
+* permit persons to whom the Software is furnished to do so, subject to
14
+* the following conditions:
15
+*
16
+* The above copyright notice and this permission notice shall be
17
+* included in all copies or substantial portions of the Software.
18
+*
19
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20
+* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
+* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23
+* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24
+* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25
+* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
+*/
27
+
28
+
29
+/************** Variables **************/
30
+// normal read/write mode
31
+var bufferLength = 256;
32
+
33
+var ble = require('jsupm_hm11');
34
+
35
+
36
+/************** Functions **************/
37
+function printUsage(progname)
38
+{
39
+	var outputStr = "Usage: " + progname + " [AT command]\n\n" +
40
+	"If an argument is supplied on the command line, that argument is\n" +
41
+	"sent to the module and the response is printed out.\n\n" +
42
+	"If no argument is used, then the address and PIN of the module\n" +
43
+	"are queried and the results printed out.\n\n"
44
+	console.log(outputStr);
45
+}
46
+
47
+// simple helper function to send a command and wait for a response
48
+function sendCommand(bleObj, cmd, callback)
49
+{
50
+	var bleBuffer = new ble.charArray(bufferLength);
51
+	bleObj.writeData(cmd, cmd.length);
52
+
53
+	// wait up to 1 second
54
+	if (bleObj.dataAvailable(1000))
55
+    {
56
+		bleObj.readData(bleBuffer, bufferLength);
57
+		var bleData = "";
58
+		// read only the number of characters
59
+		// specified by myGPSSensor.readData
60
+		for (var x = 0; x < bufferLength; x++)
61
+		{
62
+			if (bleBuffer.getitem(x) == '\0')
63
+				break;
64
+			else
65
+				bleData += bleBuffer.getitem(x);
66
+		}
67
+		console.log(bleData);
68
+    }
69
+	else
70
+		console.log("Timed out waiting for response");
71
+	if (callback)
72
+		callback();
73
+}
74
+
75
+/************** Main code **************/
76
+// Instantiate a HM11 BLE Module on UART 0
77
+var my_ble_obj = new ble.HM11(0);
78
+
79
+// make sure port is initialized properly. 9600 baud is the default.
80
+if (!my_ble_obj.setupTty(ble.int_B9600))
81
+{
82
+	console.log("Failed to setup tty port parameters");
83
+	process.exit(0);
84
+}
85
+
86
+printUsage(process.argv[1]);
87
+
88
+// Note: in nodeJS, command-line argument 0 is "node".
89
+// Command-line argument 1 is "hm11.js"
90
+// If you have a third argument, then it's a command for BLE
91
+if (process.argv.length > 2)
92
+{
93
+	console.log("Sending command line argument (" + process.argv[2] + ")...");
94
+	sendCommand(my_ble_obj, process.argv[2]);
95
+}
96
+else
97
+{
98
+	// query the module address
99
+	var addr = "AT+ADDR?";
100
+	console.log("Querying module address (" + addr + ")...");
101
+
102
+	// sending this command as a synchronous callback ensures better timing
103
+	var callbackFunc = function()
104
+	{
105
+		setTimeout(function()
106
+		{
107
+			// query the module address
108
+			var pin = "AT+PASS?";
109
+			console.log("Querying module PIN (" + pin + ")...");
110
+			sendCommand(my_ble_obj, pin);
111
+
112
+			// Other potentially useful commands are:
113
+			//
114
+			// AT+VERS? - query module version
115
+			// AT+ROLE0 - set as slave
116
+			// AT+ROLE1 - set as master
117
+			// AT+CLEAR - clear all previous settings
118
+			// AT+RESET - restart the device
119
+			//
120
+			// A comprehensive list is available from the datasheet at:
121
+			// http://www.seeedstudio.com/wiki/images/c/cd/Bluetooth4_en.pdf
122
+		}, 1000);
123
+	};
124
+	sendCommand(my_ble_obj, addr, callbackFunc);
125
+}
126
+
127
+
128
+/************** Exit code **************/
129
+process.on('SIGINT', function()
130
+{
131
+	my_ble_obj = null;
132
+	ble.cleanUp();
133
+	ble = null;
134
+	console.log("Exiting...");
135
+	process.exit(0);
136
+});

+ 110
- 0
examples/python/hm11.py Datei anzeigen

@@ -0,0 +1,110 @@
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_hm11 as upmHm11
26
+
27
+# Instantiate a HM11 BLE Module on UART 0
28
+my_ble_obj = upmHm11.HM11(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 my_ble_obj
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
+bufferLength = 256
48
+
49
+# make sure port is initialized properly. 9600 baud is the default.
50
+if (not my_ble_obj.setupTty(upmHm11.cvar.int_B9600)):
51
+	print "Failed to setup tty port parameters"
52
+	sys.exit(0)
53
+
54
+
55
+usageStr = ("Usage:\n"
56
+"Pass a commandline argument (any argument) to this program\n"
57
+"to query the radio configuration and output it.  NOTE: the\n"
58
+"radio must be in CONFIG mode for this to work.\n\n"
59
+"Running this program without arguments will simply transmit\n"
60
+"'Hello World!' every second, and output any data received from\n"
61
+"another radio.\n\n")
62
+print usageStr
63
+
64
+# simple helper function to send a command and wait for a response
65
+def sendCommand(bleObj, cmd):
66
+	bleBuffer = upmHm11.charArray(bufferLength)
67
+	bleObj.writeData(cmd, len(cmd))
68
+
69
+	# wait up to 1 second
70
+	if (bleObj.dataAvailable(1000)):
71
+		bleObj.readData(bleBuffer, bufferLength)
72
+		bleData = ""
73
+		# read only the number of characters
74
+		# specified by myGPSSensor.readData
75
+		for x in range(0, bufferLength):
76
+			if (bleBuffer.__getitem__(x) == '\0'):
77
+				break
78
+			else:
79
+				bleData += bleBuffer.__getitem__(x)
80
+		print bleData
81
+	else:
82
+		print "Timed out waiting for response"
83
+
84
+
85
+if (len(sys.argv) > 1):
86
+	print "Sending command line argument (" + sys.argv[1] + ")..."
87
+	sendCommand(my_ble_obj, sys.argv[1])
88
+else:
89
+	# query the module address
90
+	addr = "AT+ADDR?";
91
+	print "Querying module address (" + addr + ")..."
92
+
93
+	sendCommand(my_ble_obj, addr)
94
+	time.sleep(1)
95
+	# query the module address
96
+	pin = "AT+PASS?";
97
+	print "Querying module PIN (" + pin + ")..."
98
+	sendCommand(my_ble_obj, pin)
99
+
100
+	# Other potentially useful commands are:
101
+	#
102
+	# AT+VERS? - query module version
103
+	# AT+ROLE0 - set as slave
104
+	# AT+ROLE1 - set as master
105
+	# AT+CLEAR - clear all previous settings
106
+	# AT+RESET - restart the device
107
+	#
108
+	# A comprehensive list is available from the datasheet at:
109
+	# http://www.seeedstudio.com/wiki/images/c/cd/Bluetooth4_en.pdf
110
+

+ 5
- 0
src/hm11/CMakeLists.txt Datei anzeigen

@@ -0,0 +1,5 @@
1
+set (libname "hm11")
2
+set (libdescription "upm grove hm11 bluetooth low energy module")
3
+set (module_src ${libname}.cxx)
4
+set (module_h ${libname}.h)
5
+upm_module_init()

+ 154
- 0
src/hm11/hm11.cxx Datei anzeigen

@@ -0,0 +1,154 @@
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 <iostream>
26
+
27
+#include "hm11.h"
28
+
29
+using namespace upm;
30
+using namespace std;
31
+
32
+static const int defaultDelay = 100;     // max wait time for read
33
+
34
+HM11::HM11(int uart)
35
+{
36
+  m_ttyFd = -1;
37
+
38
+  if ( !(m_uart = mraa_uart_init(uart)) )
39
+    {
40
+      cerr << __FUNCTION__ << ": mraa_uart_init() failed" << endl;
41
+      return;
42
+    }
43
+
44
+  // This requires a recent MRAA (1/2015)
45
+  char *devPath = mraa_uart_get_dev_path(m_uart);
46
+
47
+  if (!devPath)
48
+    {
49
+      cerr << __FUNCTION__ << ": mraa_uart_get_dev_path() failed" << endl;
50
+      return;
51
+    }
52
+
53
+  // now open the tty
54
+  if ( (m_ttyFd = open(devPath, O_RDWR)) == -1)
55
+    {
56
+      cerr << __FUNCTION__ << ": open of " << devPath << " failed: " 
57
+           << strerror(errno) << endl;
58
+      return;
59
+    }
60
+}
61
+
62
+HM11::~HM11()
63
+{
64
+  if (m_ttyFd != -1)
65
+    close(m_ttyFd);
66
+}
67
+
68
+bool HM11::dataAvailable(unsigned int millis)
69
+{
70
+  if (m_ttyFd == -1)
71
+    return false;
72
+
73
+  struct timeval timeout;
74
+
75
+  // no waiting
76
+  timeout.tv_sec = 0;
77
+  timeout.tv_usec = millis * 1000;
78
+
79
+  int nfds;  
80
+  fd_set readfds;
81
+
82
+  FD_ZERO(&readfds);
83
+
84
+  FD_SET(m_ttyFd, &readfds);
85
+  
86
+  if (select(m_ttyFd + 1, &readfds, NULL, NULL, &timeout) > 0)
87
+    return true;                // data is ready
88
+  else
89
+    return false;
90
+}
91
+
92
+int HM11::readData(char *buffer, size_t len)
93
+{
94
+  if (m_ttyFd == -1)
95
+    return(-1);
96
+
97
+  int rv = read(m_ttyFd, buffer, len);
98
+
99
+  if (rv < 0)
100
+    cerr << __FUNCTION__ << ": read failed: " << strerror(errno) << endl;
101
+
102
+  return rv;
103
+}
104
+
105
+int HM11::writeData(char *buffer, size_t len)
106
+{
107
+  if (m_ttyFd == -1)
108
+    return(-1);
109
+
110
+  // first, flush any pending but unread input
111
+
112
+  tcflush(m_ttyFd, TCIFLUSH);
113
+
114
+  int rv = write(m_ttyFd, buffer, len);
115
+
116
+  if (rv < 0)
117
+    {
118
+      cerr << __FUNCTION__ << ": write failed: " << strerror(errno) << endl;
119
+      return rv;
120
+    }
121
+
122
+  tcdrain(m_ttyFd);
123
+
124
+  return rv;
125
+}
126
+
127
+bool HM11::setupTty(speed_t baud)
128
+{
129
+  if (m_ttyFd == -1)
130
+    return(false);
131
+  
132
+  struct termios termio;
133
+
134
+  // get current modes
135
+  tcgetattr(m_ttyFd, &termio);
136
+
137
+  // setup for a 'raw' mode.  81N, no echo or special character
138
+  // handling, such as flow control.
139
+  cfmakeraw(&termio);
140
+
141
+  // set our baud rates
142
+  cfsetispeed(&termio, baud);
143
+  cfsetospeed(&termio, baud);
144
+
145
+  // make it so
146
+  if (tcsetattr(m_ttyFd, TCSAFLUSH, &termio) < 0)
147
+    {
148
+      cerr << __FUNCTION__ << ": tcsetattr failed: " << strerror(errno) << endl;
149
+      return false;
150
+    }
151
+
152
+  return true;
153
+}
154
+

+ 143
- 0
src/hm11/hm11.h Datei anzeigen

@@ -0,0 +1,143 @@
1
+/*
2
+ * Author: Jon Trulson <jtrulson@ics.com>
3
+ * Copyright (c) 2015 Intel Corporation.
4
+ *
5
+ * Thanks to Adafruit for supplying a google translated version of the
6
+ * Chinese datasheet and some clues in their code.
7
+ *
8
+ * Permission is hereby granted, free of charge, to any person obtaining
9
+ * a copy of this software and associated documentation files (the
10
+ * "Software"), to deal in the Software without restriction, including
11
+ * without limitation the rights to use, copy, modify, merge, publish,
12
+ * distribute, sublicense, and/or sell copies of the Software, and to
13
+ * permit persons to whom the Software is furnished to do so, subject to
14
+ * the following conditions:
15
+ *
16
+ * The above copyright notice and this permission notice shall be
17
+ * included in all copies or substantial portions of the Software.
18
+ *
19
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
+ */
27
+#pragma once
28
+
29
+#include <string>
30
+#include <iostream>
31
+
32
+#include <stdint.h>
33
+#include <stdlib.h>
34
+#include <unistd.h>
35
+#include <string.h>
36
+#include <fcntl.h>
37
+#include <errno.h>
38
+#include <termios.h>
39
+#include <sys/time.h>
40
+#include <sys/select.h>
41
+#include <sys/types.h>
42
+#include <sys/stat.h>
43
+
44
+#include <mraa/uart.h>
45
+
46
+#define HM11_DEFAULT_UART 0
47
+
48
+namespace upm {
49
+    /**
50
+     * @brief UPM library for the HM-11 Bluetooth 4.0 Low Energy Module
51
+     * @defgroup hm11 libupm-hm11
52
+     * @ingroup seeed serial wifi
53
+     */
54
+
55
+    /**
56
+     * @library hm11
57
+     * @sensor hm11
58
+     * @comname HM-11 Bluetooth 4.0 Low Energy Module
59
+     * @altname HM-10, HM-12
60
+     * @category wifi
61
+     * @manufacturer seeed
62
+     * @connection uart
63
+     * @web http://www.seeedstudio.com/wiki/images/c/cd/Bluetooth4_en.pdf
64
+     *
65
+     * @brief C++ API HM-11 4.0 Bluetooth Low Energy Module
66
+     *
67
+     * The driver was tested with the Grove BLE module.  Its an HM-11
68
+     * BLE 4.0 module based on a TI CC2541 chip.  It operates using a
69
+     * standard 'AT' command set.  See the datasheet for a full list
70
+     * of available commands and their possible responses.
71
+     *
72
+     * http://www.seeedstudio.com/wiki/images/c/cd/Bluetooth4_en.pdf
73
+     *
74
+     * It is connected via a UART at 9600 baud.
75
+     * 
76
+     * @snippet hm11.cxx Interesting
77
+     */
78
+
79
+  class HM11 {
80
+  public:
81
+
82
+    /**
83
+     * HM11 module constructor
84
+     *
85
+     * @param uart default uart to use (0 or 1)
86
+     */
87
+    HM11(int uart);
88
+
89
+    /**
90
+     * HM11 module Destructor
91
+     */
92
+    ~HM11();
93
+
94
+    /**
95
+     * check to see if there is data available for reading
96
+     *
97
+     * @param millis number of milliseconds to wait, 0 means no wait.
98
+     * @return true if there is data available to be read
99
+     */
100
+    bool dataAvailable(unsigned int millis);
101
+
102
+    /**
103
+     * read any available data into a user-supplied buffer.  Note, the
104
+     * call will block until data is available to be read.  Use
105
+     * dataAvailable() to determine whether there is data available
106
+     * beforehand, to avoid blocking.
107
+     *
108
+     * @param buffer the buffer to hold the data read
109
+     * @param len the length of the buffer
110
+     * @return the number of bytes read
111
+     */
112
+    int readData(char *buffer, size_t len);
113
+
114
+    /**
115
+     * write the data in buffer to the device
116
+     *
117
+     * @param buffer the buffer to hold the data read
118
+     * @param len the length of the buffer
119
+     * @return the number of bytes written
120
+     */
121
+    int writeData(char *buffer, size_t len);
122
+
123
+    /**
124
+     * setup the proper tty i/o modes and the baudrate.  The default
125
+     * baud rate is 9600 (B9600) for this device.
126
+     *
127
+     * @param baud the desired baud rate.  
128
+     * @return true if successful
129
+     */
130
+    bool setupTty(speed_t baud=B9600);
131
+
132
+
133
+  protected:
134
+    int ttyFd() { return m_ttyFd; };
135
+    int setTtyFd(int fd) { m_ttyFd = fd; };
136
+
137
+  private:
138
+    mraa_uart_context m_uart;
139
+    int m_ttyFd;
140
+  };
141
+}
142
+
143
+

+ 12
- 0
src/hm11/jsupm_hm11.i Datei anzeigen

@@ -0,0 +1,12 @@
1
+%module jsupm_hm11
2
+%include "../upm.i"
3
+%include "carrays.i"
4
+
5
+%{
6
+    #include "hm11.h"
7
+    speed_t int_B9600 = B9600;
8
+%}
9
+
10
+%include "hm11.h"
11
+speed_t int_B9600 = B9600;
12
+%array_class(char, charArray);

+ 13
- 0
src/hm11/pyupm_hm11.i Datei anzeigen

@@ -0,0 +1,13 @@
1
+%module pyupm_hm11
2
+%include "../upm.i"
3
+%include "carrays.i"
4
+
5
+%feature("autodoc", "3");
6
+
7
+%{
8
+    #include "hm11.h"
9
+    speed_t int_B9600 = B9600;
10
+%}
11
+%include "hm11.h"
12
+speed_t int_B9600 = B9600;
13
+%array_class(char, charArray);