OSDN Git Service

Update ecrobot to nxtOSEK_v212.zip
[nxt-jsp/etrobo-atk.git] / nxtOSEK / ecrobot / c++ / device / Bluetooth.cpp
diff --git a/nxtOSEK/ecrobot/c++/device/Bluetooth.cpp b/nxtOSEK/ecrobot/c++/device/Bluetooth.cpp
new file mode 100644 (file)
index 0000000..b0ea5ee
--- /dev/null
@@ -0,0 +1,141 @@
+//\r
+// Bluetooth.cpp\r
+//\r
+// Copyright 2009 by Takashi Chikamasa, Jon C. Martin and Robert W. Kramer\r
+//\r
+\r
+#include "Bluetooth.h"\r
+using namespace ecrobot;\r
+\r
+\r
+//=============================================================================\r
+// Constructor\r
+Bluetooth::Bluetooth(void)\r
+:\r
+mIsConnected(false),\r
+mCancelConnection(false)\r
+{\r
+       AssertDeviceConstructor("Bt Assert");\r
+}\r
+\r
+//=============================================================================\r
+// Destructor (close existing Bluetooth connection).\r
+Bluetooth::~Bluetooth(void)\r
+{\r
+       this->close(); // close Bluetooth communication\r
+}\r
+\r
+//=============================================================================\r
+// connect as slave device\r
+bool Bluetooth::waitForConnection(const CHAR* passkey, U32 duration)\r
+{\r
+       U32 start = systick_get_ms();\r
+       while(!mCancelConnection\r
+                 && !mIsConnected \r
+                 && (duration == 0 || (systick_get_ms() - start) <= duration))\r
+       {\r
+               ecrobot_init_bt_slave(passkey);\r
+\r
+               if (ecrobot_get_bt_status() == BT_STREAM)\r
+               {\r
+                       mIsConnected = true;\r
+                       return mIsConnected;\r
+               }\r
+       }\r
+       return mIsConnected;\r
+}\r
+\r
+//=============================================================================\r
+// Cancel of waiting for a connection.\r
+//\r
+void Bluetooth::cancelWaitForConnection(void)\r
+{\r
+       mCancelConnection = true;\r
+}\r
+\r
+//=============================================================================\r
+// connect as master device\r
+bool Bluetooth::waitForConnection(const CHAR* passkey, const U8 address[7], U32 duration)\r
+{\r
+       U32 start = systick_get_ms();\r
+       while(!mCancelConnection\r
+                 && !mIsConnected \r
+                 && (duration == 0 || (systick_get_ms() - start) <= duration))\r
+       {\r
+               ecrobot_init_bt_master(address, passkey);\r
+\r
+               if (ecrobot_get_bt_status() == BT_STREAM)\r
+               {\r
+                       mIsConnected = true;\r
+                       return mIsConnected;\r
+               }\r
+       }\r
+       return mIsConnected;\r
+}\r
+\r
+//=============================================================================\r
+// Get the Bluetooth Device Address of the device\r
+bool Bluetooth::getDeviceAddress(U8 address[7]) const\r
+{\r
+       return static_cast<bool>(ecrobot_get_bt_device_address(address));\r
+}\r
+\r
+//=============================================================================\r
+// Get the friendly name of the device.\r
+bool Bluetooth::getFriendlyName(CHAR* name) const\r
+{\r
+       return static_cast<bool>(ecrobot_get_bt_device_name(name));\r
+}\r
+\r
+//=============================================================================\r
+// Set the friendly name of the device.\r
+bool Bluetooth::setFriendlyName(const CHAR* name)\r
+{\r
+       return static_cast<bool>(ecrobot_set_bt_device_name(name));\r
+}\r
+\r
+//=============================================================================\r
+// get connection status\r
+bool Bluetooth::isConnected(void) const\r
+{\r
+       return mIsConnected;\r
+}\r
+\r
+//=============================================================================\r
+// send packet data\r
+U32 Bluetooth::send(U8* data, U32 length)\r
+{\r
+       if (length > MAX_BT_DATA_LENGTH)\r
+       {\r
+               return 0;\r
+       }\r
+       return ecrobot_send_bt_packet(data, length);\r
+}\r
+\r
+//=============================================================================\r
+// receive packet data\r
+U32 Bluetooth::receive(U8* data, U32 length) const\r
+{\r
+       if (length > MAX_BT_DATA_LENGTH)\r
+       {\r
+               return 0;\r
+       }\r
+       return ecrobot_read_bt_packet(data, length);\r
+}\r
+\r
+//=============================================================================\r
+// Reset all settings in the persistent settings in the BlueCore chip.\r
+void Bluetooth::setFactorySettings(void)\r
+{\r
+       ecrobot_set_bt_factory_settings();\r
+       this->close();\r
+}\r
+\r
+//=============================================================================\r
+// close the connection\r
+void Bluetooth::close(void)\r
+{\r
+       ecrobot_term_bt_connection();\r
+       mIsConnected = false;\r
+}\r
+\r