RN41/42 control lib. RN41/42 is bluetooth module with class 1 or 2.

Dependencies:   StrLib myTimer RingBuffer

Dependents:   Theremin

Revision:
1:5ed110051e39
Parent:
0:812e6b59aa54
Child:
2:3a28bc9332b6
--- a/RN41.h	Tue Oct 20 01:32:17 2015 +0000
+++ b/RN41.h	Thu Oct 22 05:02:36 2015 +0000
@@ -2,11 +2,6 @@
  *  RN42 refer to http://akizukidenshi.com/catalog/g/gM-07612/ .
  */
 
-#pragma once
-
-#include "mbed.h"
-#include "StrLib.h"
-
 /*
  * cmdで送る改行コードは、CRのみ。LFは文字として認識されるので?となる。
  * RN41 Returns (default); AOK(valid cmd), ERR(invalid cmod), ?(unrecongnized cmd).
@@ -15,6 +10,53 @@
 
 */
 
+/**
+ @code
+ #include "mbed.h"
+#include <string>
+#include "RN41.h"
+RN41 rn41(p13, p14, 9600);
+DigitalOut led[]= {LED1, LED2, LED3, LED4};
+void connectRN41(string tmpAddr);
+int main()
+{
+    while(true) {
+        led[0]= !led[0];
+        connectRN41("target BT address");
+        rn41.sendLine("test");
+        wait_ms(100);
+        printf(rn41.getLine().c_str());
+        if(rn41.disconnect())
+            printf("disconnect");
+        wait(5.0);
+    }
+}
+void connectRN41(string tmpAddr)
+{
+    printf("Connect to: %s.\r\n", tmpAddr);
+    int status= rn41.connect(tmpAddr);
+    if(status == RN41::OK)                      printf("OK, Connect.");
+    else if(status == RN41::ERR_AddrUnder12)    printf("NG, Connect. Under12 Addr.");
+    else if(status == RN41::ERR_AddrOver12)     printf("NG, Connect. Over12 Addr.");
+    else if(status == RN41::ERR_EnterCmdMode)   printf("Error, failed in to enter Cmd-mode.");
+    else if(status == RN41::ERR_Disconnect)     printf("Error, failed in DisConnect.");
+    else if(status == RN41::ERR_Connect)        printf("Error, Connect.");
+    else if(status == RN41::FAIL_Connect)       printf("Fail, Connect.");
+    else if(status == RN41::ERR_Timeout )       printf("Error, TimeOut.");
+    else                                        printf("Error Connection: %d.",status);
+    return;
+}
+ @endcode
+*/
+
+
+#pragma once
+
+#include "mbed.h"
+#include "StrLib.h"
+#include "myTimer.h"
+
+
 
 /**     Bluetooth module RN41/42 control class.
  *
@@ -26,7 +68,7 @@
 //    static const int
     static const int NG, OK;//=                  1;
     static const int ERR_AddrUnder12, ERR_AddrOver12, ERR_Connect, FAIL_Connect;
-    static const int ERR_EnterCmdMode, ERR_Disconnect;
+    static const int ERR_EnterCmdMode, ERR_Disconnect, ERR_Timeout;
 
 //    static const int None, Odd, Even;
     static const int ERR_NameSize;
@@ -49,11 +91,11 @@
      *
      */
     void sendCMD( string str, bool addCR=true);
-    void sendLINE(string str, bool addCR=true);
+    void sendLine(string str, bool addCR=true);
 
 
     int setDev_Name(string str, bool usingAddr=true);
-    int setDev_UART(int boad, int stop=1, int parity=SerialBase::None);
+    int setDev_UART(int baud, int bit=8, int parity=SerialBase::None, int stop=1);
 
 
 // Command to RN41
@@ -63,10 +105,11 @@
 
 private:
     Serial rn41;
-    string CR;
+    string cr;
     string rxStr, str4GetLine;      // 内部バッファ。read()での保管用と、GetLineまでの保管用。
 
-    Timer timerLocal;
+//    Timer timerLocal;
+    myTimer timer;
 
     static const int findCmp1, findCmp2, findCmp12;
     /** check reply from RN41.