// // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // +++++ ++ // +++++ When UwTerminal downloads the app it will store it as a filenname ++ // +++++ which consists of all characters up to the first . and excluding ++ // +++++ excluding it ++ // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // // BT900.CHILD.+mac.sb // //****************************************************************************** AssertBT900() //This is to make sure the application doesn't compile //if this sblib file is #included for the wrong module //****************************************************************************** //****************************************************************************** // Definitions //****************************************************************************** //Default BT Friendly Name - name of device seen by other devices #define FRIENDLY_NAME "BT900.CHILD" //Set this to the BT address of the BT classic device #define LOCAL_BT_ADDR "0016A4707E25" //This Module #define PEER_BT_ADDR "0016A40FBEEB" //Eva Module #define OPEN_SPP_ON_STARTUP 1 //If OPEN_SPP_ON_STARTUP is set to 1, on startup //an SPP port will be opened for peer devices to connect to #define CONNECT_ON_STARTUP 1 //If set to 1, on startup the module will //connect to device PEER_BT_ADDR for SPP #define PAIRABLE_ON_STARTUP 1 #define DISCOVERABLE_ON_STARTUP 1 //if OPEN_SPP_ON_STARTUP==1, //discoverablilty will be enabled for DISCOV_TIMEOUT specified #define DISCOV_MODE 0//0 - General, 1 - Limited #define DISCOV_TIMEOUT 120 //Time that module will be discoverable for //in seconds if discoverabilty enabled #define USE_BRIDGE 1 //Set to 0 to use smartBASIC to transfer data //between UART and SPP, //set to 1 to use direct bridging //(faster throughput) //Inquiry config parameters //(See description for BtcInquiryConfig() in BT900 Extention user manual) #define INQR_TMT 10 //Inquiry timeout in seconds #define INQR_TYPE 0 //Inquiry type - general inquiry #define INQR_MODE 2 //Inquiry mode - extended inquiry #define MAX_DEVS_REC_RESP 100 //Max number of devices to receive inquiry responses from #define SAVE_PAIRINGS 1 //For subsequent incoming pair requests, //if set to 1, pairing information will be //saved in bonding manager //APP RUN MODES #define CMD_MODE 0 #define BRIDGE_SPP_MODE 1 #define AUTH_FAILURE 0x05 #define DISCON_TIMER 0 //If this is changed, make sure to change the EvTmrX statement #define DISCON_TIMER_DUR_MS 500 #define UART_BAUDRATE (115200) #define MAX_FRAME_SIZE 192 #define RX_CREDITS 3 //**************************************************************************** // Global Variable Declarations //**************************************************************************** // GPIOBINDEVENT (EventNum(0..3), SIO_NR, Polarity) //------------------------------------------------------------------------ // Where SIO_NR On BT900 only can be SIO 0, 3, 5, 8, 13, 20 or 22. // And SIO0 and SIO3 can only be used as bind or assign events, // if they are not being used as UART pins, i.e. // that the UartClose() function has been called // and the UART has not been reopened. //------------------------------------------------------------------------ // Where Polarity // 0 - Low to high transition, // 1 - High to low transition, // 2 - Either a low to high or high to low transition //------------------------------------------------------------------------ // GPIOASSIGNEVENT EVDETECTCHAN(0..3) // SIO: 0, 3, 5, 8, 13, 20 or 22 // GpioSetFunc(SIO_xx, MainFunction, SubFunction) //------------------------------------- #define EVENTNUM0 0 #define EVENTNUM1 1 #define EVENTNUM2 2 #define EVENTNUM3 3 //------------------------------------- // MainFunction //------------------------------------- #define DIGITAL_IN 1 #define DIGITAL_OUT 2 #define ANALOG_IN 3 //--------------------------------------------------------- // SubFunction DIGITAL_IN //------------------------------------- #define D_IN_PU 2 // or 4 #define D_IN_WakeUpLow 0x10 // 16 #define D_IN_WakeUpHigh 0x20 // 32 #define D_IN_WakeUpCH 0x30 // 48 //--------------------------------------------------------- // SubFunction DIGITAL_OUT //------------------------------------- #define D_OUT_LOW 0 #define D_OUT_HIGH 1 #define D_OUT_PWM 2 #define D_OUT_FREQ 3 //---------------------------------------------------------------------------- // Defining SIO //------------------------------------- #define SIO_00 0 //PIN 44 SIO_00 DIO UART_RX or WKUP2 EVENT #define SIO_01 1 //PIN 45 SIO_01 DIO UART_TX #define SIO_02 2 //PIN 46 SIO_02 DIO UART_RTS #define SIO_03 3 //PIN 01 SIO_03 DIO UART_CTS or WKUP4 or Ext Interrupt #define SIO_04 4 //PIN 02 SIO_04 DIO #define SIO_05 5 //PIN 04 SIO_05 DIO Ext Interrupt EVENT #define SIO_06 6 //PIN 07 SIO_06 DIO SPI MISO #define SIO_07 7 //PIN 08 SIO_07 DIO SPI MOSI #define SIO_08 8 //PIN 09 SIO_08 DIO Ext Interrupt EVENT #define SIO_09 9 //PIN 10 SIO_09 DIO SPI CLK #define SIO_10 10//PIN 11 SIO_10 DIO I2C SDA #define SIO_11 11//PIN 12 SIO_11 DIO I2C SCL #define SIO_12 12//PIN 14 SIO_12 DIO FREQ or PWM #define SIO_13 13//PIN 15 SIO_13 DIO FREQ or PWM EVENT #define SIO_14 14//PIN 17 SIO_14 DIO #define SIO_15 15//PIN 18 SIO_15 DIO #define SIO_16 16//PIN 19 SIO_16 DIO #define SIO_17 17//PIN 20 SIO_17 DIO FREQ or PWM #define SIO_18 18//PIN 21 SIO_18 DIO #define SIO_19 19//PIN 22 SIO_19 DIO VSP #define SIO_20 20//PIN 24 SIO_20 DIO AIN (ADC01) or WKUP1 or Ext Interrupt EVENT #define SIO_21 21//PIN 25 SIO_21 DIO AIN (ADC00) #define SIO_22 22//PIN 03 SIO_22 nAutoRUN or Ext Interrupt EVENT //---------------------------------------------------------------------------- //Number of connections #define NUM_OF_CONNS 8 //--------------------------------------------------------- //APP RUN MODES #define NORMAL_MODE 0 #define BRIDGE_MODE 1 //--------------------------------------------------------- //Size of i[] #define NUM_OF_I_PARAMS (5) //Size of s$[] #define NUM_OF_S_PARAMS (5) #define APP_VERSION "1.0" //Misc #define CAR_PRESS_DELAY 100 //ms to wait between registering entries of '^' //#define NUM_OF_SCAN 8 //--------------------------------------------------------- dim rc dim stRsp$ as string //Uart rx data is stored here dim ok$,er$,pr$, nc$, uc$, t$, e$, p$ dim index[NUM_OF_I_PARAMS] //Index 0 used for return values dim string$[NUM_OF_S_PARAMS] //Must be at least 8 elements dim urtcmd$ //CMD line from uart dim tkn$,tlen //Used by command parser dim carCnt //Count variable for number of times '^' entered dim SppPortHandler //Handler of spp port that a peer will connect to //dim HandlerSppPort //dim hSpp dim SppConHandler //Handler of ssp connecction //SppConHandler = 0 dim StreamBridgeHandler //Handler of UART sB bridge //dim SBHndl //dim handler //Handler of other things dim buffer dim stringbuffer$ dim RecBuf$ //UART buffer dim u$ dim index_array : index_array = 0 dim received$[128] dim adr_array$[8] dim mac_array$[8] //Array of mac-addr dim addr$ //Address of device being connected to dim mac$ dim name$ //Local device name dim devName$ //dim string$ dim NUM_OF_SCAN : NUM_OF_SCAN = 8 dim StrExitFunc$ : StrExitFunc$ = "Exit_Func" dim StrExit$ : StrExit$ = "Exit" dim StrSleep$ : StrSleep$ = "Sleep" //---------------------------------------------------------------------------- //---------------------------------------------------------------------------- dim message$ message$ = "Here I am\n" //**************************************************************************** //============================================================================ //============================================================================ // //============================================================================ //FUNCTION NN() //PRINT "\n Begin\n" //PRINT "\n End\n" //ENDFUNC 1 //============================================================================ //**************************************************************************** // //**************************************************************************** function ClearS() dim j for j = 0 to (NUM_OF_S_PARAMS-1) string$[j]="" next endfunc 0 //**************************************************************************** //**************************************************************************** // //**************************************************************************** function ClearI() dim j for j = 0 to (NUM_OF_I_PARAMS-1) index[j]=0 next endfunc 0 //**************************************************************************** //============================================================================ //---------------------------------------------------------------------------- // Display the error message //---------------------------------------------------------------------------- sub AssertRc(ResCode) if (rc < 0)|| (rc > 1) then print "error: ";integer.h' ResCode;"\n" endif endsub //---------------------------------------------------------------------------- //============================================================================ //---------------------------------------------------------------------------- // For debugging // --- rc = result code // --- ln = line number //---------------------------------------------------------------------------- Sub AssertRCLn(rc,ln) if (rc < 0)|| (rc > 1) then print "\nFail :";integer.h' rc;" at line: ";ln; "\n" //else //print "\nOk: line ";ln endif EndSub //---------------------------------------------------------------------------- //---------------------------------------------------------------------------- // For debugging // --- rc = result code // --- ln = line number //---------------------------------------------------------------------------- Sub AssertRcStr(rc, byval string$ ) if (rc < 0)|| (rc > 1) then print "\nFail :";integer.h' rc;" with msg: ";string$; "\n" //else //print "\nOk: line ";ln endif EndSub //---------------------------------------------------------------------------- // For debugging // --- rc = result code // --- ln = line number //---------------------------------------------------------------------------- Sub AssertStrRc(byval string$,rc ) if (rc != 0) then print string$; " Fail:: ";integer.h'rc; "\n" endif EndSub //---------------------------------------------------------------------------- //============================================================================ //**************************************************************************** // //**************************************************************************** function ExtractStrTokens(u$,stIdx,num) print "ExtractStrTokens\n" while num>0 tlen = ExtractStrToken(u$,string$[stIdx]) if tlen == 0 then exitfunc 3 endif num=num-1 stIdx = stIdx+1 endwhile endfunc 0 //**************************************************************************** //**************************************************************************** // //**************************************************************************** function ExtractHexTokens(u$,stIdx,num) print "ExtractHexTokens\n" while num>0 tlen = ExtractStrToken(u$,tkn$) if tlen == 0 then exitfunc 3 endif string$[stIdx] = StrDehexize$(tkn$) num=num-1 stIdx = stIdx+1 endwhile endfunc 0 //**************************************************************************** //**************************************************************************** // //**************************************************************************** function ExtractIntTokens(u$,stIdx,num) while num>0 tlen = ExtractIntToken(u$,index[stIdx]) if tlen == 0 then exitfunc 4 endif num=num-1 stIdx = stIdx+1 endwhile endfunc 0 //**************************************************************************** //============================================================================ function exit() as integer print "function exit()\n" exitfunc 0 endfunc 1 //============================================================================ //============================================================================ FUNCTION HandlerTimer0() PRINT "\nTimer 0 has expired\n" ENDFUNC 1 //remain blocked in WAITEVENT //============================================================================ //============================================================================ FUNCTION HandlerTimer1() PRINT "\nTimer 1 has expired\n" endfunc 1 //============================================================================ //============================================================================ FUNCTION StartTimer0(time, reuse) PRINT "\nStart Timer 0 \n" // A valid time in milliseconds, between 1 and 1,000,000,000 (11.6 days). // reuse AS INTEGER // Set to 0 for a once-only timer // Set to 1 for reuse timer. TimerStart(0,time,reuse) //start a 500 millisecond recurring timer PRINT "\nWaiting for Timer 0" ENDFUNC 1 //============================================================================ //============================================================================ FUNCTION StartTimer1(time, reuse) PRINT "\nStart Timer 1 \n" TimerStart(1,time,reuse) //start a 1000 millisecond timer PRINT "\nWaiting for Timer 1" ENDFUNC 1 //============================================================================ //============================================================================ FUNCTION StartTimer1_0(time, reuse) PRINT "\nStart Timer 1_0 \n" TimerStart(1,time,reuse) //start a 1000 millisecond timer PRINT "\nWaiting for Timer 1_0\n" endfunc 1 //============================================================================ //============================================================================ // AT I 2003 -> 8 (0-7) antal timere //============================================================================ SUB Wait(time) PRINT "\nWait Begin\n" PRINT integer.h'time;"\n" //TimerStart(7,time,0) while TimerRunning(7) !=0 endwhile PRINT "Waiting is Over\n" ENDSUB //============================================================================ //**************************************************************************** // //**************************************************************************** sub UartRsp(rsp as integer) if rsp == 0 then print ok$;pr$ elseif rsp == -2 then print pr$ elseif rsp == -3 then print ok$ elseif rsp == -4 then print uc$;pr$ elseif rsp > 0 then print er$;integer.h' rsp;pr$ endif endsub //**************************************************************************** //**************************************************************************** // Process a carrat character ^ do decide whether or not to switch to command mode // - When the carCnt value is 3, end func with 1 to switch to command mode // - Think of it like a boolean value //**************************************************************************** function SwitchToCmd() //If ^ was sent after the delay time (valid) increment the counter if TimerRunning(0)==0 then carCnt = carCnt + 1 if carCnt<3 then TimerStart(0,CAR_PRESS_DELAY,0) else exitfunc 1 endif else //Invalid press TimerCancel(0) carCnt = 0 endif endfunc 0 //Don't give response to user //**************************************************************************** //**************************************************************************** //#CMD#// ato OR bridge //**************************************************************************** sub _bridge() rc = SendMsgApp(0,BRIDGE_SPP_MODE) //BRIDGE_SPP_MODE = 1 AssertRC(rc) endsub //**************************************************************************** //**************************************************************************** //#CMD#// at+bti OR inquiry //**************************************************************************** function _inquiry() rc = BtcInquiryStart(INQR_TMT) if rc!=0 then exitfunc rc endif endfunc -1 //Don't give response to user //**************************************************************************** //**************************************************************************** //**************************************************************************** // Find out whether in a connection or not = think of it like a boolean //**************************************************************************** function InConnection() dim bdAddr$ //Will fail if invalid handle - no connection rc = BtcGetBDAddrFromHandle(SppConHandler,bdAddr$) if rc!=0 then exitfunc 0 endif //Length of 'bdAddr$' will be zero if no connection if StrLen(bdAddr$)==0 then exitfunc 0 endif print "BtcGetBDAddrFromHandle: ";StrDehexize$(bdAddr$);"\n" endfunc 1 //**************************************************************************** //**************************************************************************** //#CMD#// spp open //#CMD#// spp close //**************************************************************************** function _spp() tlen = ExtractStrToken(urtcmd$,tkn$) if tlen == 0 then exitfunc -4 //Send unknown command response to user endif if strcmp(tkn$,"open")==0 then //Don't open another SPP port if one is already open if SppPortHandler!=0 then print "\nSPP Port Already Open: (";integer.h'SppPortHandler;")\n" exitfunc -2 endif rc = BtcSppOpen(SppPortHandler) if rc ==0 then print "\nSPP Port open : (";integer.h'SppPortHandler;")\n" rc = BtcSetDiscoverable(1,DISCOV_TIMEOUT) exitfunc 0 else exitfunc rc endif elseif strcmp(tkn$,"close")==0 then if InConnection()==1 then exitfunc -2 endif //Open the spp port. Store handle in the array at acquired index rc = BtcSppClose(SppPortHandler) if rc ==0 then print "\nSPP Port closed : (";integer.h'SppPortHandler;")" SppPortHandler=0 rc = BtcSetDiscoverable(0,0) exitfunc 0 else exitfunc rc endif else UartRsp(-4) //Send unknown command response to user endif endfunc -1 //**************************************************************************** //**************************************************************************** //#CMD#// at+btw #HEXbdAddr6bytes# OR pair #HEXbdAddr6bytes# //**************************************************************************** function _pair() rc = ExtractStrTokens(urtcmd$,1,1) if rc != 0 then exitfunc rc endif string$[1] = StrDehexize$(string$[1]) rc = BtcPair(string$[1], 2) //If incorrect mode if rc ==0x571A then rc = BtcSetPairable(1) AssertRC(rc) rc = BtcSetConnectable(1) AssertRC(rc) //Try pairing again rc = BtcPair(string$[1], 2) AssertRC(rc) endif endfunc rc //**************************************************************************** //**************************************************************************** //#CMD#// atd #HEXbdAddr6bytes# OR connect #HEXbdAddr6bytes# //**************************************************************************** function _connect() if InConnection() == 1 then print "\nALREADY CONNECTED" exitfunc -2 endif rc = ExtractStrTokens(urtcmd$,1,1) //Use #defined/stored BT address if none given if rc!=0 then string$[1] = PEER_BT_ADDR endif string$[1] = StrDehexize$(string$[1]) //Stop inquiring in case rc = BtcInquiryCancel() rc = BtcSppConnect(string$[1]) if rc!=0 then exitfunc rc endif endfunc -1 //**************************************************************************** //**************************************************************************** //#CMD#// at+btd #HEXbdAddr6bytes# //**************************************************************************** function _delete() rc = ExtractStrTokens(urtcmd$,1,1) if rc!=0 then exitfunc -2 endif string$[1] = StrDehexize$(string$[1]) rc = BtcBondingEraseKey(string$[1]) if rc!=0 then exitfunc rc endif endfunc 0 //**************************************************************************** //**************************************************************************** //#CMD#// ath OR disconnect //**************************************************************************** function _disconnect() rc = BtcSppDisconnect(SppConHandler) print "\nBtcSppDisconnect(SppConHandler) : (";integer.h'SppConHandler;")\n" if rc!=0 then exitfunc rc endif endfunc -3 //Don't give response to user //**************************************************************************** //**************************************************************************** //#CMD#// stop (inquiries) //**************************************************************************** function _stop() endfunc BtcInquiryCancel() //**************************************************************************** //**************************************************************************** // #CMD#// at+bls //**************************************************************************** Function _at() StrShiftLeft(urtcmd$,1) //Remove the '+' tlen = ExtractStrToken(urtcmd$,tkn$) if tlen > 0 then //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ if strcmp(tkn$,"bti")==0 then exitfunc _inquiry() //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ elseif strcmp(tkn$,"btw")==0 then exitfunc _pair() endif endif endfunc -4 //Print 'UNKNOWN COMMAND' if incorrect command //**************************************************************************** //**************************************************************************** //#CMD#// misc index clearall //#CMD#// misc index set #INTidx# #INTval# //#CMD#// misc string$ clearall //#CMD#// misc string$ set #INTidx# #STRescapedvalue# //#CMD#// misc string$ sethex #INTidx# #HEXdigitsvalue# //**************************************************************************** function _Misc() tlen = ExtractStrToken(urtcmd$,tkn$) if tlen == 0 then exitfunc 5 endif //================================================= if strcmp(tkn$,"string$")==0 then tlen = ExtractStrToken(urtcmd$,tkn$) if tlen == 0 then exitfunc 5 endif //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ if strcmp(tkn$,"clearall")==0 then exitfunc ClearS() endif //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ if strcmp(tkn$,"set")==0 then //Extract 1 (#INTidx#) and store starting at index[1] rc = ExtractIntTokens(urtcmd$,1,1) if rc != 0 then exitfunc rc endif if (index[1] < 0) || (index[1] >= NUM_OF_S_PARAMS) then exitfunc 6 endif //Extract 1 (#STRescapedvalue#) and store starting at string$[1] rc = ExtractStrTokens(urtcmd$,index[1],1) if rc != 0 then exitfunc rc endif StrDeEscape(string$[index[1]]) exitfunc 0 endif //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ if strcmp(tkn$,"sethex")==0 then //Extract 1 (#INTidx#) and store starting at index[1] rc = ExtractIntTokens(urtcmd$,1,1) if rc != 0 then exitfunc rc endif if (index[1] < 0) || (index[1] >= NUM_OF_S_PARAMS) then exitfunc 6 endif //Extract 1 (#STRescapedvalue#) and store starting at string$[1] rc = ExtractStrTokens(urtcmd$,index[1],1) if rc != 0 then exitfunc rc endif StrDeEscape(string$[index[1]]) exitfunc 0 endif endif //================================================= if strcmp(tkn$,"index")==0 then tlen = ExtractStrToken(urtcmd$,tkn$) if tlen == 0 then exitfunc 5 endif //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ if strcmp(tkn$,"clearall")==0 then exitfunc ClearI() endif //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ if strcmp(tkn$,"set")==0 then dim idx tlen = ExtractIntToken(urtcmd$,idx) if tlen == 0 then exitfunc 5 endif if (idx < 0) || (idx >= NUM_OF_I_PARAMS) then exitfunc 6 endif tlen = ExtractIntToken(urtcmd$,index[idx]) if tlen == 0 then exitfunc 5 endif exitfunc 0 endif endif endfunc 5 //**************************************************************************** //============================================================================ //#CMD#// at+bti OR inquiry //============================================================================ function connect() if InConnection() == 1 then print "\nALREADY CONNECTED" exitfunc -2 endif rc = ExtractStrTokens(urtcmd$,1,1) //Use #defined/stored BT address if none given if rc!=0 then string$[1] = PEER_BT_ADDR endif string$[1] = StrDehexize$(string$[1]) //Stop inquiring in case rc = BtcInquiryCancel() rc = BtcSppConnect(string$[1]) if rc!=0 then exitfunc rc endif endfunc -1 //============================================================================ //============================================================================ // //============================================================================ sub ShowGpioStatus() //PRINT "\n Begin\n" print "--------------------------------\n" print "Pin 01: (";"UART_CTS"; ")\n"//SIO_3 or WKUP4 or Ext Interrupt print "Pin 02: (";GpioRead(SIO_04); ")\n" print "Pin 03: (";GpioRead(SIO_22); ")\n" print "Pin 04: (";GpioRead(SIO_05); ")\n" print "Pin 05: (";"Vcc"; ")\n" print "Pin 06: (";"GND"; ")\n" print "Pin 07: (";GpioRead(SIO_06); ")\n"//SPI MISO print "Pin 08: (";GpioRead(SIO_07); ")\n"//SPI MOSI print "Pin 09: (";GpioRead(SIO_08); ")\n" print "Pin 10: (";GpioRead(SIO_09); ")\n" print "Pin 11: (";GpioRead(SIO_10); ")\n" print "Pin 12: (";GpioRead(SIO_11); ")\n" print "Pin 13: (";"GND"; ")\n" print "Pin 14: (";GpioRead(SIO_12); ")\n" print "Pin 15: (";GpioRead(SIO_13); ")\n" print "Pin 16: (";"RESET"; ")\n" print "Pin 17: (";GpioRead(SIO_14); ")\n" print "Pin 18: (";GpioRead(SIO_15); ")\n" print "Pin 19: (";GpioRead(SIO_16); ")\n" print "Pin 20: (";GpioRead(SIO_17); ")\n" print "Pin 21: (";GpioRead(SIO_18); ")\n" print "Pin 22: (";GpioRead(SIO_19); ")\n" print "Pin 24: (";GpioRead(SIO_20); ")\n"//AIN (ADC01) or WKUP1 or Ext Interrupt print "Pin 25: (";GpioRead(SIO_21); ")\n"//AIN (ADC00) print "Pin 26: (";"AVCC"; ")\n" print "Pin 27: (";"Do not connect"; ")\n" print "Pin 28: (";"AVREF"; ")\n" print "Pin 29: (";"GND"; ")\n" print "Pin 30: (";"GND"; ")\n" print "Pin 31: (";"BT_VREG_IN_HV"; ")\n"//Connected to Vcc print "Pin 32: (";"BT_VREG_OUT_HV"; ")\n"//Leave pin 32 unconnected. print "Pin 33: (";"GND"; ")\n" print "Pin 34: (";"Do not connect"; ")\n" print "Pin 35: (";"Do not connect"; ")\n" print "Pin 36: (";"Do not connect"; ")\n" print "Pin 37: (";"Do not connect"; ")\n" print "Pin 38: (";"Do not connect"; ")\n" print "Pin 39: (";"Do not connect"; ")\n" print "Pin 40: (";"Do not connect"; ")\n" print "Pin 41: (";"BT_#SEL"; ")\n"//100k pull-up print "Pin 42: (";"BT_PRIORITY"; ")\n" print "Pin 43: (";"BT_VDD_IO"; ")\n"//Connected to Vcc, 3.3V, IN only print "Pin 44: (";"UART_RX"; ")\n"//SIO_00 or WKUP2 print "Pin 45: (";"UART_TX"; ")\n"//SIO_1 print "Pin 46: (";"UART_RTS"; ")\n"//SIO_2 print "Pin 47: (";"GND"; ")\n" print "--------------------------------\n" //PRINT "\n End\n" endsub //============================================================================ //============================================================================ // Handler of Detected Changes used by GpioAssignEvent() //============================================================================ FUNCTION HandlerDetectChan() PRINT "\nHandler_of_Detected_Changes Begin\n" OnEvent EVDETECTCHAN0 DISABLE OnEvent EVDETECTCHAN1 DISABLE OnEvent EVDETECTCHAN2 DISABLE OnEvent EVDETECTCHAN3 DISABLE dim i i = GpioRead(SIO_05) print "GpioRead(SIO_05) = "; i; "\n" i = GpioRead(SIO_08) print "GpioRead(SIO_08) = "; i; "\n" i = GpioRead(SIO_13) print "GpioRead(SIO_13) = "; i; "\n" i = GpioRead(SIO_20) print "GpioRead(SIO_20) = "; i; "\n" i = GpioRead(SIO_22) print "GpioRead(SIO_22) = "; i; "\n" PRINT "\nHandler_of_Detected_Changes End\n" ENDFUNC 1 //============================================================================ //============================================================================ // Handler of Gpio Change used by GpioBindEvent() //============================================================================ FUNCTION HandlerGpioChan() print "--------------------------------\n" print "Handler of Gpio Change Begin \n" OnEvent EVGPIOCHAN0 DISABLE OnEvent EVGPIOCHAN1 DISABLE OnEvent EVGPIOCHAN2 DISABLE OnEvent EVGPIOCHAN3 DISABLE print "Handler of Gpio Change End\n" print "--------------------------------\n" ENDFUNC 1 //============================================================================ //============================================================================== // SIO_NR is the sio_number // EVENT_NR is the number 0..3 // Polarity (trigger) = 1 for Low to High transitio, 0 for high to low transition //============================================================================== function DeepSleepIn(SIO_NR, WakeUp) as integer print "Deep-Sleep Begin \n" rc=BtcInquiryCancel() //--------------------------------------------------------- // GpioBindEvent(EventNum, SIO_NR, Polarity) // GpioBindEvent( // EventNum(0..3), // SIO_NR(0, 3, 5, 8, 13, 20 or 22.), // Polarity(0 - Low to high transition, // 1 - High to low transition, // 2 - Either a low to high or high to low transition) // ) //--------------------------------------------------------- // GPIOSETFUNC (SIO_NR, Function, SubFunc) //--------------------------------------------------------- // SIO_NR, // Function: 1 = DIGITAL_IN, 2 = DIGITAL_OUT, 3 = ANALOG_IN // SubFunc: DIGITAL_IN: 2 or 4 – Use pull-up resistor // 16 – Wake up when signal is low // 32 – Wake up when signal is high // 48 – Wake up when signal changes //--------------------------------------------------------- if WakeUp==1 then print "GpioSetFunc: Low to high transition \n" rc = gpiosetfunc(SIO_NR, DIGITAL_IN , D_IN_WakeUpLow) elseif WakeUp==2 then print "GpioSetFunc: Low to high transition \n" rc = gpiosetfunc(SIO_NR, DIGITAL_IN , D_IN_WakeUpHigh) elseif WakeUp==3 then print "GpioSetFunc: Alternate transition \n" rc = gpiosetfunc(SIO_NR, DIGITAL_IN , D_IN_WakeUpCH) else print "Exit Func null" //exitfunc 0 endif //--------------------------------------------------------- //OnEvent EVGPIOCHAN0 call HandlerGpioChan //--------------------------------------------------------- if (rc == 0) then print "\n-- Going to sleep --\n\n" // uartclose() //Closes the UART //--------------------------------------------------------- //The following lines is for BT900 only //rc = gpiosetfunc(01, DIGITAL_OUT, D_OUT_HIGH) //TX - set high on default //rc = gpiosetfunc(02, DIGITAL_OUT, D_OUT_HIGH) //RTS //--------------------------------------------------------- //The following lines is for RM186 only //rc = gpiosetfunc(21, DIGITAL_OUT, D_OUT_HIGH) //TX - set high on default //rc = gpiosetfunc(23, DIGITAL_OUT, D_OUT_HIGH) //RTS //--------------------------------------------------------- rc=BtcSppClose(SppConHandler) rc=BtcDiscoveryConfig(0,0) //general discoverability rc=BtcSetDiscoverable(0,0) //not discoverable rc=BtcSetConnectable(0) //connectable rc= //Put module into deep sleep rc = SystemStateSet(0) else print "Some thing missing in sleep?\n" endif //--------------------------------------------------------- print "Deep-Sleep End \n" endfunc 1 //============================================================================ //============================================================================ // //============================================================================ function OnUartCmd() as integer rc = -2 //Assume there is no error tlen = ExtractStrToken(urtcmd$,tkn$) //Get first token if tlen > 0 then if strcmp(tkn$,"at")==0 then rc = _at() elseif strcmp(tkn$,"spp")==0 then rc = _spp() elseif strcmp(tkn$,"atd")==0 || strcmp(tkn$,"connect")==0 then rc = _connect() elseif StrCmp(tkn$,"ato")==0 || StrCmp(tkn$,"bridge")==0 then _bridge() rc = -1 //Don't give response to user elseif strcmp(tkn$,"at+btd")==0 then rc = _delete() elseif strcmp(tkn$,"inquiry")==0 then rc = _inquiry() elseif strcmp(tkn$,"pair")==0 then rc = _pair() elseif StrCmp(tkn$,"ato")==0 || StrCmp(tkn$,"bridge")==0 then rc = SendMsgApp(0,BRIDGE_SPP_MODE) //BRIDGE_SPP_MODE = 1 rc = -1 //Don't give response to user elseif StrCmp(tkn$,"ath")==0 || StrCmp(tkn$,"disconnect")==0 then rc = _disconnect() elseif strcmp(tkn$,"misc")==0 then rc = _misc() elseif strcmp(tkn$,"stop")==0 then rc = BtcInquiryCancel() elseif strcmp(tkn$,"quit")==0 || strcmp(tkn$,"exit")==0 then rc = BtcSppDisconnect(SppConHandler) rc = BtcSppClose(SppPortHandler) print "\nVersion ";APP_VERSION exitfunc 0 elseif strcmp(tkn$,"setdiscoverable")==0 then //Extract 2 : (#INTenable# #INTtimeout#) and store starting at index[1] rc = ExtractIntTokens(urtcmd$,1,2) if rc != 0 then exitfunc rc endif rc = BtcSetDiscoverable(index[1], index[2]) elseif strcmp(tkn$,"setpairable")==0 then //Extract 1 : (#INTenable#) and store starting at index[1] rc = ExtractIntTokens(urtcmd$,1,1) if rc != 0 then exitfunc rc endif rc = BtcSetPairable(index[1]) elseif strcmp(tkn$,"bdaddr")==0 then string$[1] = SysInfo$(4) StrShiftLeft(string$[1],1) print "\n";StrHexize$(string$[1]) endif endif //Send a response back to the user UartRsp(rc) endfunc 1 //============================================================================ //============================================================================ // This handler is called when data has arrived at the serial port //============================================================================ function HandlerUartRx() as integer print "HandlerUartRx Begin\n" dim nMatch : nMatch = 0 //Check if CR has been received nMatch=UartReadMatch(stRsp$,13) if nMatch!=0 then //CR exists in the input buffer urtcmd$ = strsplitleft$(stRsp$,nMatch) print "HandlerUartRx End(exitfunc)\n" exitfunc OnUartCmd() endif print "HandlerUartRx End\n" endfunc 1 //============================================================================ //============================================================================ // //============================================================================ function HandlerRemoteName(nStatus) // as integer PRINT "HandlerRemoteName Begin \n" dim RemoteName$ : RemoteName$ = "BT900.BEACON" dim len //--------------------------------------------------------- // BTCGETREMOTEFRIENDLYNAME(address$, name$, nStatus) // byREF address$ AS STRING: // Address of the remote device that the refiendly name relates to. // Returned from the query. // name$ byREF name$ AS STRING // Friendly name of the remote device, returned from the query. // nStatus byREF nStatus AS INTEGER // Status code of the query. // The name string is only valid on a success status of 0. //--------------------------------------------------------- dim raddress$, rname$, stat // is nStatus valid? if (nStatus == 0) then //rc = BtcGetRemoteFriendlyName(address$, name$, nStatus)//pdf //Manual code: rc = BtcGetRemoteFriendlyName(rname$, raddress$, stat) endif PRINT "HandlerRemoteName: ";rname$;" \n" PRINT "HandlerRemoteAddr: ";strhexize$(raddress$);" \n" PRINT "HandlerRemoteAddr: ";StrDehexize$(raddress$);" \n" //--------------------------------------------------------- //--------------------------------------------------------- if strcmp(RemoteName$,rname$)==0 then print "Remote Name is accepted. \n" //------------------------------------------------------------------- //------------------------------------------------------------------- // if rc==0 then // print "Wrote ";len;" bytes\n" // else // print "Error: "; integer.h'rc ; "\n" // endif //------------------------------------------------------------------- //------------------------------------------------------------------- else //------------------------------------------------------------------- print "Remote Name is not recognized. \n" // exitfunc 0 //------------------------------------------------------------------- endif //------------------------------------------------------------------- PRINT "HandlerRemoteName End \n" ENDFUNC 1 //============================================================================ //============================================================================ //#CMD#// ath OR disconnect //============================================================================ function Disconnect() as integer print "Disconnect Begin\n" //------------------------------------------------------------------------ // rc = BtcSppClose(SppConHandler) // (SppPortHandler) (SppConHandler) // AssertStrRc("BtcSppClose in Disconnect()",rc) //------------------------------------------------------------------------ // rc = BtcSppDisconnect(SppConHandler) // AssertStrRc("BtcSppDisconnect in Disconnect()",rc) //------------------------------------------------------------------------ rc=BtcInquiryCancel() AssertStrRc("BtcInquiryCancel in Disconnect()",rc) //------------------------------------------------------------------------ // Possible Error: // 5708: BTC_SPP_INV_HANDLE: The supplied SPP handle is not valid. //------------------------------------------------------------------------ print "Disconnect End\n" endfunc 1 //Don't give response to user //============================================================================ //============================================================================ // rfcomm connect //============================================================================ function Rf_Connect(byval ad$) //l_mac$ -> local mac print "Rf_Connect Begin \n" dim lstr$ // rc = BtcSppConnect(ad$) AssertStrRc("BtcSppConnect in Rf_Connect()",rc) // Possible error's: // 5701: BTC_ALREADY_INQUIRING: // A classic Bluetooth inquiry is already in progress // 5706: BTC_INV_MAC_ADDRESS: // The specified Bluetooth address is not valid. if rc == 0 then print "--- Successfully Connected to Remote device: ";strhexize$(ad$); "\n" else print "Not Connected ... \n" print "Rf_Connect End \n" //exitfunc 0 endif print "Rf_Connect End \n" endfunc 1 //============================================================================ //============================================================================ // This is my attemp to send message //============================================================================ function HandleSendingMessage(byval message$) print "HandleSendingMessage Begin\n" //------------------------------------------------------------------- dim len //------------------------------------------------------------------- len = strlen(message$) rc=BtcSppWrite(SppPortHandler, message$, len) //red 30.12.2018 AssertStrRc("BtcSppWrite in HandleSendingMessage()",rc) if rc==0 then print "Wrote ";len;" bytes: ";message$; "\n" else print "Error: "; integer.h'rc ; "\n" endif //------------------------------------------------------------------- print "HandleSendingMessage End\n" //------------------------------------------------------------------- endfunc 1 //============================================================================ //============================================================================ // This handler is called when there at least one inquiry response waiting to // be read //============================================================================ function HandlerInqResp(respType) as integer print "HandlerInqResp Begin\n" dim ad$,dta$,rsi,nme$,ndx index_array = 0 //rc=BtcInquiryCancel() //--------------------------------------------------------- while BtcInquiryGetReport(ad$,dta$,ndx,rsi)==0 //Get and print name of device //rc = BtcGetEIRByTag(dta$,0x09,nme$) //print "Inquiry result: "; StrHexize$(ad$), rsi, nme$; "\n" print "While Inquiry result: "; StrHexize$(ad$); "\n" mac$ = StrHexize$(ad$) if strcmp(mac$, "0016A40FBEEB") == 0 then mac_array$[index_array] = mac$ adr_array$[index_array] = ad$ mac$ = "0016A40FBEEB" print "Found a match: "; mac$; "\n" index_array = index_array +1 elseif strcmp(mac$, "0016A40FBEEB") == 0 then //your beacon mac_array$[index_array] = mac$ // "0016A40FBEEB" adr_array$[index_array] = ad$ // "/00/16/A4/0F/BE/EB" addr$ = ad$ //mac$ = "0016A40FBEEB" print "Found a match: "; ad$; "\n" index_array = index_array +1 endif BREAK endwhile //--------------------------------------------------------- print "index_array = "; index_array; "\n" //--------------------------------------------------------- if index_array > 0 then rc = Rf_Connect(ad$) endif //--------------------------------------------------------- //wait(500) //--------------------------------------------------------- print "HandlerInqResp End\n" endfunc 1 //============================================================================ //============================================================================ // This handler is called when there is an inquiry timeout //============================================================================ function HandlerBtcInqTimOut() as integer print "-- Handler inquiry timeout() --\n" rc = DeepSleepIn(SIO_20, 2) print "Handler inquiry timeout() \n" endfunc 1 //============================================================================ //============================================================================ // Called on a PIN request from another device //============================================================================ function HandlerPinReq() print "HandlerPinReq Begin\n" rc = BtcSendPINResp("1234") AssertRc(rc) // BtcGetPINRequestBDAddr(strBDAddr$) strBDAddr$ AS STRING // On return, this string contains the Bluetooth address of the device requesting a PIN. // Get the Bluetooth address of the device requesting a pairing using Legacy PIN. rc = BtcGetPinRequestBDAddr(adr_array$[0]) AssertRc(rc) rc = BtcBondingEraseKey(adr_array$[0]) //Erase link key for this bluetooth //device incase already bonded with it AssertRc(rc) print "PIN Req: "; StrHexize$(adr_array$[0]) UartFlush(1) //Flush the uart rx buffer onevent evuartrx call exit//call Handler-Uart-Rx-PIN print "HandlerPinReq End \n" endfunc 1 //============================================================================ //============================================================================ // Called on a Pairing request from another device red 03.01.2019 //============================================================================ function HandlerPairReq() print "HandlerPairReq Begin \n" rc=BtcGetPairRequestBDAddr(mac$) if rc==0 then print "\nPairing requested from device: "; StrHexize$(mac$) print "\nAccepting pair request" rc=BtcSendPairResp(1) else print "\nErr: "; integer.h'rc endif print "HandlerPairReq End \n" endfunc 1 //============================================================================ //============================================================================ // Called after a pairing attempt //============================================================================ function HandlerPairRes(res) dim my_mac$ print "HandlerPairRes Begin \n" if res == 0 then rc = BtcGetPAIRRequestBDAddr(my_mac$) AssertStrRc("BtcGetPAIRRequestBDAddr",rc) print "\n--- Successfully paired with device: "; StrHexize$(my_mac$); " \n" endif print "HandlerPairRes End\n" endfunc 1 //============================================================================ //============================================================================ // Called when the UART transmit bufffer is empty //============================================================================ FUNCTION HandlerUartTxEmpty() //PRINT "\nHandlerUartTxEmpty Begin\n" dim hPort, data$, rLen, UartSent while strlen(RecBuf$) > 0 RecBuf$ = "-"+RecBuf$+"-" UartSent = UARTWRITE(RecBuf$) STRSHIFTLEFT(RecBuf$, UartSent) if (UartSent != strlen(RecBuf$)) then //Can't send any more exitfunc 1 endif endwhile while BtcSppRead(hPort, data$, rLen) == 0 while rLen>0 UartSent = UARTWRITE(data$) STRSHIFTLEFT(data$, UartSent) if UartSent != rLen then //Uart full - add to buffer instead RecBuf$ = data$ exitfunc 1 endif rLen = strlen(data$) endwhile endwhile //PRINT "\nHandlerUartTxEmpty End\n" ENDFUNC 1 //============================================================================ //============================================================================ // Called after an SPP connection attempt //============================================================================ function HandlerSppConn(portHndl, result) as integer print "HandlerSppConn Begin \n" dim conAddr$ SppPortHandler = portHndl print "\n --- Connected : ",SppPortHandler, StrHexize$(mac$) print "\nResult: ",integer.h' result print "\n" //If connected successfully, acquire handle and switch to rfcomm mode if result == 0 then //SppConHandler = SppConHandler //------------------------------------------------------------------- rc = BtcGetBDAddrFromHandle(SppPortHandler, conAddr$) AssertStrRc("Btc_Get-BDAddr-FromHandle in HandlerSppConn()", rc) print "Btc_Get-BDAddr-FromHandle in HandlerSppConn(): \n";strhexize$(conAddr$); "\n" //------------------------------------------------------------------- // BtcQueryRemoteFriendlyName // FUNCTION // Query the friendly name from a remote device specified in the address. // rc = BtcQueryRemoteFriendlyName(conAddr$) AssertStrRc("BtcQueryRemoteFriendlyName in HandlerSppConn()",rc) //------------------------------------------------------------------- //------------------------------------------------------------------- else print "\nConnection failed: ";result; "\n" //Reset(0) endif //------------------------------------------------------------------- message$ = "This is my PIR" rc=HandleSendingMessage(message$) print "HandlerSppConn End\n" endfunc 1 //============================================================================ //============================================================================ // Called when a connection attempt to an SPP device times out //============================================================================ function HandlerSppConnTimOut() as integer print "\nCould not create connection: Connection timed out\n" endfunc 1 //============================================================================ //============================================================================ // Called when data is received via UART when bridged to SPP //============================================================================ // //------------------------------------------------------------------------------ function HandlerUartRxSpp() print "HandlerUartRxSpp Begin \n" dim uLen, wLen if buffer ==0 then //Read data that has arrived through dia the UART uLen = UartRead(u$) if uLen > 0 then if StrCmp(u$,"^")==0 then //Parse the ^ entry if SwitchToCmd() == 1 then print "\n" rc = SendMsgApp(0,CMD_MODE) endif else //Write data to most recent spp connection rc = BtcSppWrite(SppConHandler, u$, wLen) if wLen != uLen then buffer = 1 strshiftleft(u$,wLen) else u$="" endif endif endif endif print "HandlerUartRxSpp End \n" endfunc 1 //============================================================================ //============================================================================ // Called when txbuffer is empty //============================================================================ function HandlerSppTxEmpty(inx) print "HandlerSppTxEmpty Begin \n" dim string_lenght, write_lenght if buffer == 1 then print "buffer == 1\n" string_lenght = strlen(stringbuffer$) rc = BtcSppWrite(SppConHandler, stringbuffer$, write_lenght) AssertRc(rc) if write_lenght != string_lenght then buffer = 1 //Remove the first write_lenght char of string stringbuffer strshiftleft(stringbuffer$,write_lenght) else buffer=0 stringbuffer$ = "" string_lenght = UartRead(stringbuffer$) if string_lenght > 0 then rc = BtcSppWrite(SppConHandler, stringbuffer$, write_lenght) AssertRc(rc) if write_lenght != string_lenght then buffer = 1 strshiftleft(stringbuffer$,write_lenght) else stringbuffer$="" endif endif endif endif print "HandlerSppTxEmpty End \n" endfunc 1 //============================================================================ //============================================================================ // Called when data is received via SPP //============================================================================ function HandlerSppData() print "HandlerSppData Begin \n" //print "Called when data is received via SPP\n" //When receiving SPP data just print it to terminal dim HSppRead, data$, rLen ,exit$, SppData$, UartSent, StrLenght, i data$ = "" SppData$ = "" rlen = 0 RecBuf$ = "" dim pointer : pointer = 0 //---------------------------------------------------------------------------- //print "Clear array" FOR i = 0 TO 127 received$[i] = "" next //---------------------------------------------------------------------------- while BtcSppRead(SppPortHandler, data$, rLen) == 0 //---------------------------------------------------------------------------- if rlen > 0 then RecBuf$ = data$ print "BtcSppRead: ";data$; "\n" else print "No data read!!!\n" endif StrLenght = rLen //---------------------------------------------------------------------------- endwhile //---------------------------------------------------------------------------- dim tkn$ while ExtractStrToken(RecBuf$,tkn$) > 0 received$[pointer] = tkn$ pointer = pointer +1 endwhile //---------------------------------------------------------------------------- if pointer > 0 then tkn$ = received$[pointer-1] // Print "tkn$ = "; tkn$; "\n" if strcmp(tkn$, "exit") == 0 then rc = disconnect() // rc = DeepSleepIn(SIO_20, 2) elseif strcmp(RecBuf$, StrSleep$) == 0 then rc = disconnect() // rc = DeepSleepIn(SIO_20, 2) else rc = disconnect() print "Nothing to compare\n" endif // print "No Data\n" endif //---------------------------------------------------------------------------- RecBuf$ = "" //---------------------------------------------------------------------------- rc = DeepSleepIn(SIO_20, 2) //---------------------------------------------------------------------------- print "HandlerSppData End \n" endfunc 1 //============================================================================ //============================================================================ // Called when an SPP disconnection occurs //============================================================================ function HandlerSppDiscon(porthndl) as integer print "HandlerSppDiscon Begin \n" print "Called when SPP disconnection occurs ",porthndl,"\n" //------------------------------------------------------------------------ rc=BtcInquiryCancel() AssertStrRc("BtcInquiryCancel in HandlerSppDiscon()",rc) //------------------------------------------------------------------------ if rc == 0 then print "\nInquiry Cancelled\n" rc = DeepSleepIn(SIO_20, 2) else print "\nError: ";rc ,"\n" endif print "\nHandlerSppDiscon End\n" endfunc 1 //============================================================================ //============================================================================ // nn //============================================================================ sub nn() print "nn Begin \n" print "nn End\n" endsub //============================================================================ //============================================================================ // Init_Gpio_Dev //============================================================================ // GPIOSETFUNC (SIO_Num, DIO/An, SubFunc) // SubFunc/Transaction(High/Low/Change) // 20 = SIO_20, 1 = DIGITAL_IN, 48 = Wake up when signal changes // rc = gpiosetfunc(20,1,48) // GPIOBINDEVENT (nEventNum, nSigNum, nPolarity) // GPIOASSIGNEVENT (nEventNum, nSigNum, nPolarity) // GPIOBINDEVENT EVGPIOCHAN(0..3) -> Function // GPIOASSIGNEVENT EVDETECTCHAN(0..3) // GPIOASSIGNEVENT (nEventNum, 5, 8, 13, 20 or 22, nPolarity) // SIO_05 PIN 04 DIO Ext Interrupt EVENT // SIO_08 PIN 09 DIO Ext Interrupt EVENT // SIO_13 PIN 15 DIO FREQ or PWM EVENT // SIO_20 PIN 24 DIO AIN (ADC01) or WKUP1 or Ext Interrupt EVENT // SIO_22 PIN 03 nAutoRUN or Ext Interrupt EVENT // Bind event 0 to high or low transition on SIO20 (button 1) // GpioUnbindEvent/GpioUnAssignEvent(EventNumber) 0-3 // ONEVENT symbolic_name DISABLE sub Init_Gpio_Dev() print "Init_GPIO Begin \n" rc = GpioBindEvent(0, SIO_20, D_IN_PU) // Generate a EVGPIOCHAN(0..3) // rc = GpioAssignEvent(1, SIO_22, D_IN_PU) // Generate a EVDETECTCHAN(0..3) // rc = gpiosetfunc(SIO_04, DIGITAL_IN, D_IN_PU)//PIN 2 print "Init_GPIO End \n" endsub //============================================================================ //============================================================================ //============================================================================ // Initialise the Bluetooth device // Set the device to be discoverable, connectible, pairable, // Set the inquiry type to be general, inquiry mode to be extended //============================================================================ //============================================================================ // print "\n" //============================================================================ // This is the first subroutine called when the program starts //============================================================================ sub Initialise() print "Initialise Begin \n" // AssertStrRc("", rc) print "This file is called: BT900.CHILD.sb\n" print "Open UART\n" rc = UartOpen(UART_BAUDRATE, 0, 0, "CN81H") AssertStrRc("UartOpen", rc) //Set friendly name string$[1] = FRIENDLY_NAME print "Set friendly name\n" rc = BtcSetFriendlyName(string$[1]) AssertStrRc("BtcSetFriendlyName", rc) //Configure inquiries from #defined configuration values print "BtcInquiryConfig -0-\n" rc = BtcInquiryConfig(0,INQR_TYPE) AssertStrRc("BtcInquiryConfig 0", rc) print "BtcInquiryConfig -1-\n" rc = BtcInquiryConfig(1,INQR_MODE) AssertStrRc("BtcInquiryConfig 1", rc) print "BtcInquiryConfig -2-\n" rc = BtcInquiryConfig(2,MAX_DEVS_REC_RESP) AssertStrRc("BtcInquiryConfig 2", rc) //Configure discoverability mode print "BtcDiscoveryConfig -General-\n" rc = BtcDiscoveryConfig(0, 0) // DISCOV_MODE 0 = General AssertStrRc("BtcDiscoveryConfig", rc) print "BtcDiscoveryConfig -Inquiry scan interval of 500ms (0x0320)-\n" rc=BtcDiscoveryConfig(1,0x320) //inquiry scan interval of 500ms (0x0320) print "BtcDiscoveryConfig -Inquiry Scan Window of 250ms (0x0190)-\n" rc=BtcDiscoveryConfig(2,0x190) //Inquiry Scan Window of 250ms (0x0190) //Save subsequent pairings -bond // 1 – Pairing information is stored in the bonding manager print "BtcSavePairings(-Yes-)\n" rc = BtcSavePairings(SAVE_PAIRINGS) //SAVE_PAIRINGS = 1 //1 – Pairing information is stored in the bonding manager AssertStrRc("BtcSavePairings", rc) print "BtcSetPairable(-Yes-)\n" rc = BtcSetPairable(1) // PAIRABLE_ON_STARTUP = 1 AssertStrRc("BtcSetPairable", rc) //Set connectable so other devices can connect to this device print "BtcSetConnectable(-Yes-)\n" rc = BtcSetConnectable(1) AssertStrRc("BtcSetConnectable(1)", rc) //This function is used to set the parameters of newly opened SPP connections. //Must be called with no active open connections. //Adjusting these values from the default will affect the maximum number of //SPP connections achievable. print "\n" rc = BtcSPPSetParams(MAX_FRAME_SIZE, RX_CREDITS) AssertStrRc("BtcSPPSetParams", rc) //---------------------------------------------------------------------------- if OPEN_SPP_ON_STARTUP == 1 then //Open SPP port and listen for SPP connections, enable discoverability print "BtcSppOpen\n" rc = BtcSppOpen(SppPortHandler) AssertStrRc("BtcSppOpen", rc) print "BtcSppOpen(SppPortHandler): ";integer.h'SppPortHandler; "\n" endif //---------------------------------------------------------------------------- //Set discoverable if DISCOVERABLE_ON_STARTUP ==1 OR OPEN_SPP_ON_STARTUP==1 print "Set discoverable for 120 seconds\n" rc = BtcSetDiscoverable(0,120) AssertStrRc("BtcSetDiscoverable", rc) //---------------------------------------------------------------------------- if CONNECT_ON_STARTUP == 1 then rc = _connect() if rc == -1 then print "\nConnecting: ";StrHexize$(string$[1]);"\n" endif else //Send user prompt for uart based command interface UartRsp(0) endif //---------------------------------------------------------------------------- //Set the IO capability to Just works print "BtcSecMngrIoCap(Set the IO capability to Just works)\n" rc = BtcSecMngrIoCap(0) AssertStrRc("BtcSecMngrIoCap(0)", rc) // ------------------------------------------------------------------------ //Set the JustWorks configuration to 0 so that pairing works by itself //using BtcSendPAIRResp to respond. print "BtcSecMngrJustWorksConf\n" rc = BtcSecMngrJustWorksConf(0) AssertStrRc("BtcSecMngrJustWorksConf(0)", rc) // ------------------------------------------------------------------------ rc = BtcInquiryStart(INQR_TMT) AssertStrRc("BtcInquiryStart", rc) print "BtcInquiryStart \n" //---------------------------------------------------------------------------- //---------------------------------------------------------------------------- print "Initialise End \n" endsub //============================================================================ //============================================================================ // Enable synchronous event handlers //============================================================================ // EVBTC_REMOTENAME_RECEIVED // This event is thrown when the module receives a response to a // BtcQueryRemoteFriendlyName command, after this event has been received // the values can be read by issuing BtcGetRemoteFriendlyName. // OnEvent EVBTC_REMOTENAME_RECEIVED call HandlerRemoteName //---------------------------------------------------------------------------- // rfcomm related events //---------------------------------------------------------------------------- OnEvent EVSPPDISCON call HandlerSppDiscon //---------------------------------------------------------------------------- // EVBTC_SPP_DATA_RECEIVED: // This event is thrown when data is received via the Serial Port Profile. // OnEvent EVBTC_SPP_DATA_RECEIVED call HandlerSppData //---------------------------------------------------------------------------- // EVSPPCONN: // This event is thrown when a new SPP connection has been established or // an error has occured. // The message is passed to a handler, which should be registered in the // smartBASIC application, and contains nHandle (the handle of the connection) // and result (a result code). // nHandle is only valid on a successful result code (0). // OnEvent EVSPPCONN call HandlerSppConn //---------------------------------------------------------------------------- // EVBTC_SPP_CONN_TIMEOUT: // This event is thrown when a connection attempt to an SPP device times out. // OnEvent EVBTC_SPP_CONN_TIMEOUT call HandlerSppConnTimOut //---------------------------------------------------------------------------- // EVSPPTXEMPTY: // This event is generated when the last byte in the SPP Tx buffer is // transmitted. See example for BtcSppWrite(). // OnEvent EVSPPTXEMPTY call HandlerSppTxEmpty //---------------------------------------------------------------------------- // EVUARTRX: // Data has arrived in UART interface // OnEvent EVUARTRX call HandlerUartRx //---------------------------------------------------------------------------- // EVUARTTXEMPTY: // The UART TX ring buffer is empty // OnEvent EVUARTTXEMPTY call HandlerUartTxEmpty //---------------------------------------------------------------------------- // EVBTC_PIN_REQUEST: // This event is thrown on a PIN request from another device during pairing. // See examples given for EVBTC_PAIR_RESULT and BtcPair. // onevent EVBTC_PIN_REQUEST call HandlerPinReq //---------------------------------------------------------------------------- // EVBTC_PAIR_REQUEST: // This event is thrown on a pairing request from another device. // OnEvent EVBTC_PAIR_REQUEST call HandlerPairReq //---------------------------------------------------------------------------- // EVBTC_PAIR_RESULT: // This message is thrown after a pairing attempt and comes with one parameter // which is the result code. // OnEvent EVBTC_PAIR_RESULT call HandlerPairRes //---------------------------------------------------------------------------- // Events and Messages EVINQRESP // This event is thrown when there is an BTC inquiry report waiting to be read. // The message, which is passed to a handler which should be registered in the // smartBASIC application, contains respType, // the type of inquiry response received. // It is one of the following values: // 0 Standard, 1 With RSSI, 2 Extended (contains EIR data) // OnEvent EVINQRESP call HandlerInqResp //---------------------------------------------------------------------------- // This event is thrown when an inquiry times out. // When an inquiry times out this doesn’t necessarily mean that there are // no more responses waiting, so you can obtain the remaining responses // after a timeout by calling BtcInquiryGetReport(). // OnEvent EVBTC_INQUIRY_TIMEOUT call HandlerBtcInqTimOut //---------------------------------------------------------------------------- // EVGPIOCHANn N is from 0 to 3 where N is platform-dependent and an event // is generated when a preconfigured digital input transition occurs. // The number of digital inputs that can auto-generate is hardware-dependent. // Use GpioBindEvent() to generate these events. // OnEvent EVGPIOCHAN0 call HandlerGpioChan // OnEvent EVGPIOCHAN1 call HandlerGpioChan // OnEvent EVGPIOCHAN2 call HandlerGpioChan // OnEvent EVGPIOCHAN3 call HandlerGpioChan //---------------------------------------------------------------------------- // EVDETECTCHANn N is from 0 to 3 where N is platform dependent and an event // is generated when a preconfigured digital input transition occurs. // The number of digital inputs that can auto-generate is hardware dependent. // Use GpioAssignEvent() to generate these events. //OnEvent EVDETECTCHAN0 call HandlerDetectChan //OnEvent EVDETECTCHAN1 call HandlerDetectChan //OnEvent EVDETECTCHAN2 call HandlerDetectChan //OnEvent EVDETECTCHAN3 call HandlerDetectChan //---------------------------------------------------------------------------- //---------------------------------------------------------------------------- print "\n--Module restarted--\n\n" //---------------------------------------------------------------------------- //---------------------------------------------------------------------------- //Initialise the device Initialise() //---------------------------------------------------------------------------- //print "Friendly name: "; FRIENDLY_NAME$; "\n" //rc = BtcSetFriendlyName(name$) //AssertRc(rc) //---------------------------------------------------------------------------- //---------------------------------------------------------------------------- //---------------------------------------------------------------------------- // Wait for a synchronous event. //---------------------------------------------------------------------------- //---------------------------------------------------------------------------- WaitEvent print "We ended here for some reason.\n" //============================================================================ //============================================================================ // //============================================================================ //FUNCTION NN() //PRINT "\n Begin\n" //PRINT "\n End\n" //ENDFUNC 1 //============================================================================ //============================================================================