/* ************************************************************ * NXT-BT-Master.nxc * CH, Chen(Taiwan) * 2012.5.27 * ************************************************************ */ #define BT_CHANNEL 2 #define ARDUINO_SLAVE "Bluetooth_Bee_V" /* ************************************************************ * Build_Connection() function * 1. Search index of slave device name in bluetooth contact list * 2. Call SysCommExecuteFunction(CommExecuteFunctionType cefArgs) * to connect with slave device * ************************************************************ */ #define MUST_WAIT_3_SECONDS 3 bool build_Connection(byte BtChannel, const string BtDevname) { string msg; bool isOK = FALSE; short idx = 0, cmpLen = strlen(BtDevname); ClearScreen(); TextOut(0, LCD_LINE1, "Build Connection"); TextOut(0, LCD_LINE2, StrCat(" with ", BtDevname)); // Search index of Slave device in BT contact list TextOut(0, LCD_LINE4, "Searching Slave "); for(short xii=0; xii0; xjj--) { NumOut(42, LCD_LINE5, xjj); //PlayTone(440,100); Wait(1000); } NumOut(42, LCD_LINE5, 0); //PlayTone(440,1000); Wait(1000); isOK = TRUE; break; } else { TextOut(0, LCD_LINE6, "Re-try: "); NumOut(48, LCD_LINE6, xii); TextOut(0, LCD_LINE7, "Result: "); NumOut(48, LCD_LINE7, cefArgs.Result); } } if(!isOK) { TextOut(0, LCD_LINE5, " Failed!! "); TextOut(0, LCD_LINE8, " Enter to Stop "); until(ButtonPressed(BTNCENTER, TRUE)); Stop(TRUE); } return isOK; } /* ************************************************************ * stop_Connection() function * ************************************************************ */ void stop_Connection(byte BtChannel) { CommExecuteFunctionType cefArgs; cefArgs.Cmd = INTF_DISCONNECT; cefArgs.Param1 = BtChannel; ClearScreen(); for(short xii=0; xii<20; xii++) { SysCommExecuteFunction(cefArgs); if(cefArgs.Result == LDR_SUCCESS) { TextOut(0, LCD_LINE4, "Disconnect Slave"); TextOut(0, LCD_LINE5, " after Seconds"); for(short xjj=MUST_WAIT_3_SECONDS; xjj>=0; xjj--) { NumOut(42, LCD_LINE5, xjj); Wait(1000); } break; } else { TextOut(0, LCD_LINE6, "Re-try: "); NumOut(48, LCD_LINE6, xii); TextOut(0, LCD_LINE7, "Result: "); NumOut(48, LCD_LINE7, cefArgs.Result); } } } /* ************************************************************ * Send_Command function * ************************************************************ */ void send_Command(string remoteCommand) { SendRemoteString(BT_CHANNEL, MAILBOX1, remoteCommand); while(BluetoothStatus(BT_CHANNEL)== STAT_COMM_PENDING); } /* ************************************************************ * main task * ************************************************************ */ #define WAIT_FOR_NEXT_READING 1500 task main() { string helloString = "Hello Arduino"; string remoteString; if(!(BluetoothStatus(BT_CHANNEL) == NO_ERR)) build_Connection(BT_CHANNEL, ARDUINO_SLAVE); ClearScreen(); TextOut(0, LCD_LINE1," Say Hello "); TextOut(0, LCD_LINE2," to Arduino "); TextOut(0, LCD_LINE3," via Bluetooth "); TextOut(0, LCD_LINE8," Exit"); int xcnt=1; while(TRUE) { remoteString = StrCat("(", NumToStr(xcnt), ") ", helloString); TextOut(0, LCD_LINE5, StrCat("(", NumToStr(xcnt), ") ")); TextOut(0, LCD_LINE6, helloString); send_Command(remoteString); Wait(WAIT_FOR_NEXT_READING); TextOut(0, LCD_LINE5," "); TextOut(0, LCD_LINE6," "); xcnt++; if(ButtonPressed(BTNRIGHT, TRUE)) { stop_Connection(BT_CHANNEL); Stop(TRUE); } } }