RN41 Bluetooth Module Library
Embed:
(wiki syntax)
Show/hide line numbers
RN41.cpp
00001 #include "RN41.h" 00002 #include <string> 00003 00004 RN41::RN41(PinName tx, PinName rx) : _RN41(tx, rx){ 00005 _baud = 115200; 00006 _RN41.baud(_baud); 00007 _commandMode = false; 00008 } 00009 00010 //Resets The Device 00011 //Also Exits Command Mode 00012 //Device Must Be Reset After A Config Change To Take Effect 00013 bool RN41::reset(){ 00014 enterCommandMode(); 00015 sendString("R,1\r\n"); 00016 if(getString() == "Reboot!\r\n"){ 00017 _commandMode = false; 00018 return true; 00019 }else{ 00020 return false; 00021 } 00022 } 00023 00024 //Sets Device Name 00025 //Maximum 20 characters 00026 bool RN41::setDeviceName(string name){ 00027 enterCommandMode(); 00028 sendString("SN," + name + "\r\n"); 00029 if(getString() == "AOK\r\n"){ 00030 reset(); 00031 return true; 00032 }else{ 00033 return false; 00034 } 00035 } 00036 00037 //Set Authentication Mode 00038 //Available Modes: 00039 //0 - Open Mode 00040 //1 - SSP Keyboard I/O Mode 00041 //2 - SSP "Just Works" Mode 00042 //4 - Pin Code 00043 //Default Mode: 1 00044 bool RN41::setAuthenticationMode(int authMode){ 00045 enterCommandMode(); 00046 if(authMode < 0 or authMode > 4 or authMode == 3){ 00047 return false; 00048 } 00049 char buf[1]; 00050 sprintf(buf,"SA,%d\r\n", authMode); 00051 string msg = buf; 00052 sendString(msg); 00053 if(getString() == "AOK\r\n"){ 00054 reset(); 00055 return true; 00056 }else{ 00057 return false; 00058 } 00059 } 00060 00061 //Sets Device Mode 00062 //Available Modes: 00063 //0 - Slave Mode 00064 //1 - Master Mode 00065 //2 - Trigger Mode 00066 //3 - Auto-Connect Master Mode 00067 //4 - Auto-Connect DTR Mode 00068 //5 - Auto-Connect Any Mode 00069 //6 - Pairing Mode 00070 //Default Mode: 4 00071 bool RN41::setMode(int mode){ 00072 enterCommandMode(); 00073 if(mode < 0 or mode > 6){ 00074 return false; 00075 } 00076 char buf[1]; 00077 sprintf(buf,"SM,%d\r\n", mode); 00078 string msg = buf; 00079 sendString(msg); 00080 if(getString() == "AOK\r\n"){ 00081 reset(); 00082 return true; 00083 }else{ 00084 return false; 00085 } 00086 } 00087 00088 //Gets Bluetooth Address 00089 //returns the 12 didgite MAC ID 00090 string RN41::getBluetoothAddress(){ 00091 enterCommandMode(); 00092 sendString("GB\r\n"); 00093 string result = getString(); 00094 string address = result.substr(0, result.length() - 2); 00095 exitCommandMode(); 00096 return address; 00097 } 00098 00099 //Gets Connection Status 00100 //0,0,0 = Not Connected 00101 //1,0,0 = Connected 00102 bool RN41::getConnectionStatus(){ 00103 enterCommandMode(); 00104 sendString("GK\r\n"); 00105 if(getString() == "1,0,0\r\n"){ 00106 exitCommandMode(); 00107 return true; 00108 }else{ 00109 exitCommandMode(); 00110 return false; 00111 } 00112 } 00113 00114 //Get The Device's Firmware Version 00115 string RN41::getFirmwareVersion(){ 00116 enterCommandMode(); 00117 sendString("V\r\n"); 00118 string version = getString(); 00119 exitCommandMode(); 00120 return version; 00121 } 00122 00123 bool RN41::sendMessage(string message, char terminationChar){ 00124 if(_commandMode){return false;} 00125 string msg = message + terminationChar; 00126 _RN41.printf("%s", msg); 00127 return true; 00128 } 00129 00130 string RN41::recieveMessage(char terminationChar){ 00131 return getString(terminationChar); 00132 if(_commandMode){return "*ERROR*";} 00133 } 00134 00135 //Private Functions Below 00136 00137 //Enter Command Mode 00138 bool RN41::enterCommandMode(){ 00139 if(_commandMode == true){ 00140 return true; 00141 } 00142 sendString("$$$"); 00143 if(getString() == "CMD\r\n"){ 00144 _commandMode = true; 00145 return true; 00146 }else{ 00147 return false; 00148 } 00149 } 00150 00151 //Exit Command Mode 00152 bool RN41::exitCommandMode(){ 00153 if(_commandMode == false){ 00154 return true; 00155 } 00156 sendString("---\r\n"); 00157 if(getString() == "END\r\n"){ 00158 _commandMode = false; 00159 return true; 00160 }else{ 00161 return false; 00162 } 00163 } 00164 00165 void RN41::sendString(string msg){ 00166 _RN41.printf("%s",msg); 00167 } 00168 00169 bool RN41::readable(){ 00170 if(_RN41.readable()){return true;}else{return false;} 00171 } 00172 00173 string RN41::getString(){ 00174 string msg = ""; 00175 char prev = ' '; 00176 char curr = ' '; 00177 while(1){ 00178 if(_RN41.readable()){ 00179 prev = curr; 00180 curr = getChar(); 00181 msg += curr; 00182 if(prev=='\r' and curr=='\n'){ 00183 break; 00184 } 00185 } 00186 } 00187 00188 return msg; 00189 } 00190 00191 string RN41::getString(char terminationChar){ 00192 string msg = ""; 00193 char curr = ' '; 00194 while(1){ 00195 if(_RN41.readable()){ 00196 curr = getChar(); 00197 msg += curr; 00198 if(curr == terminationChar){ 00199 break; 00200 } 00201 } 00202 } 00203 return msg; 00204 } 00205 00206 char RN41::getChar(){ 00207 return _RN41.getc(); 00208 }
Generated on Fri Jul 15 2022 13:51:48 by 1.7.2