;User interface API routines for selecting nearby device and waiting for incoming connection include "settings.inc" include "ti83plus.inc" include "equates.inc" include "usb8x.inc" include "bluetooth.inc" SEGMENT Main GLOBALS ON EXTERN IVPutMap,PutSApp,SoftKey,VDispHexA,VPutSApp,IGetKey,DispHexHLB,IPutS,IPutC,DispHexA EXTERN HCI_RequestRemoteName,HCI_DoInquiryScan,RequestHCIData,SendHCICommand,RequestHCIData_NoWait EXTERN HCI_SetScanMode,HCI_GetLocalDeviceName,SetDiscoverableMode,IsConnectionRequestPending EXTERN LeaveDiscoverableMode DoDeviceSelection: ;Presents a dialog to perform an inquiry scan for nearby devices and select one from a list. ;Inputs: ; The usb8x and USB Bluetooth adapter drivers must be initialized to the same memory locations as Blue8x (statVars). ; IX points to 255-byte buffer for temporary storage ; HL points to buffer for inquiry results (98 bytes is probably safe; the bigger the better) ;Outputs: ; Returns carry flag set if any problems ; Returns pointer to inquiry result structure for selected device in IX ; Returns key code in A: ; 0: Device was selected and pointer to inquiry result is in IX. ; kGraph: "Back" was selected and no selection was made. ; kQuit: [2nd]+[QUIT] was pressed and no selection was made. ; kClear: [CLEAR] was pressed and no selection was made. ;Destroys: ; saveSScreen ld (ptrTempStorage),ix ld (ptrInquiryResults),hl RestartInquiryScan: B_CALL ClrLCDFull ld hl,5*256+5 ld (curRow),hl ld hl,sInquiryKeys call SoftKey ld hl,0 ld (penCol),hl ld hl,sScanning call VPutSApp ld hl,0 ld de,6*256+95 B_CALL InvertRect ld ix,(ptrTempStorage) xor a ld (ix+offNumDevicesFound),a ld hl,(ptrInquiryResults) ld b,8 ;about 10 seconds for an inquiry scan call HCI_DoInquiryScan jr c,ErrInquiryScan ld ix,(ptrTempStorage) ld a,b ld (ix+offNumDevicesFound),a xor a ld (ix+offSelectedItem),a DisplayInquiryResults: B_CALL ClrLCDFull ld hl,sInquiryKeys call SoftKey ld hl,0 ld (penCol),hl ld hl,sDevicesFound call VPutSApp ld ix,(ptrTempStorage) ld a,(ix+offNumDevicesFound) push af call VDispHexA ld hl,0 ld de,6*256+95 set plotLoc,(iy+plotFlags) B_CALL InvertRect pop bc ld hl,7*256+5 ld (penCol),hl ld a,b or a jr z,DisplayInquiryResultsDone ld ix,(ptrInquiryResults) displayNamesLoop: push bc ld hl,(ptrTempStorage) ld de,offNameBuffer add hl,de push ix call HCI_RequestRemoteName jr nc,displayName ;We had a problem getting the name, so just display the BD_ADDR instead ld b,6 $$: ld a,(ix+0) call VDispHexA inc ix dec b jr z,displayName ld a,':' call IVPutMap jr $B displayName: ld hl,(ptrTempStorage) ld de,offNameBuffer add hl,de call VPutSApp ld a,(penRow) add a,7 ld (penRow),a ld a,5 ld (penCol),a pop ix ld bc,14 add ix,bc pop bc djnz displayNamesLoop DisplayInquiryResultsDone: ld ix,(ptrTempStorage) ld a,(ix+offNumDevicesFound) or a jr z,inquiryResultsKeyLoop call SetSelectedCoordinates ld a,Lconvert call IVPutMap inquiryResultsKeyLoop: call IGetKey ld ix,(ptrTempStorage) cp kTrace jr z,SelectDevice cp kGraph ret z cp kQuit ret z cp kClear ret z cp kYequ jr z,RestartInquiryScan cp kUp jr z,DoUp cp kDown jr z,DoDown jr inquiryResultsKeyLoop DoUp: ld a,(ix+offSelectedItem) or a jr z,inquiryResultsKeyLoop call SetSelectedCoordinates ld a,6 call IVPutMap dec (ix+offSelectedItem) call SetSelectedCoordinates ld a,Lconvert call IVPutMap jr inquiryResultsKeyLoop DoDown: ld a,(ix+offNumDevicesFound) dec a ld b,a ld a,(ix+offSelectedItem) cp b jr z,inquiryResultsKeyLoop call SetSelectedCoordinates ld a,6 call IVPutMap inc (ix+offSelectedItem) call SetSelectedCoordinates ld a,Lconvert call IVPutMap jr inquiryResultsKeyLoop SelectDevice: B_CALL ClrLCDFull B_CALL HomeUp ld bc,14 ld ix,(ptrTempStorage) ld a,(ix+offSelectedItem) ld ix,(ptrInquiryResults) or a jr z,$F getSelectedResultLoop: add ix,bc dec a jr nz,getSelectedResultLoop $$: xor a ret SetSelectedCoordinates: ld hl,7*256+0 ld a,(ix+offSelectedItem) ld b,a or a jr z,$F ld a,h showSelectedLoop: add a,7 djnz showSelectedLoop ld h,a $$: ld (penCol),hl ret ErrInquiryScan: B_CALL ClrLCDFull B_CALL HomeUp ld hl,sInquiryError call PutSApp B_CALL GetKey scf ret sInquiryKeys: DB "Scan",0 DB 0 ;"PgUp",0 DB 0 ;"PgDn",0 DB " Sel",0 DB "Back",0 sInquiryError: DB "ERROR! " DB "An error " DB "occurred while " DB "scanning for " DB "nearby devices. " DB " " DB "Press any key",0CEh,0 sScanning: DB "Scanning for devices...",0 sDevicesFound: DB "Devices found: ",0 AwaitIncomingConnection: ;Presents a waiting screen to the user with the current device name and waits for an incoming connection from a remote device. ;Inputs: ; HL points to 255-byte buffer for temporary storage ;Outputs: ; Returns carry flag set if problems or user canceled ; HL points to data block for device that requested connection: ; ;6; BD_ADDR ; ;3; Class_of_Device ; ;1; Link_Type push hl call HCI_GetLocalDeviceName ;doesn't matter if this fails set textInverse,(iy+textFlags) pop hl ld b,16 ld a,(hl) DisplayNameLoop: or a jr z,$F call IPutC inc hl ld a,(hl) $$: djnz DisplayNameLoop res textInverse,(iy+textFlags) ld hl,sMakeVisible call IPutS ;Set discoverable mode call SetDiscoverableMode jr c,ErrMakeVisible visibleKeyLoop: ei halt in a,(4) bit 3,a scf ret z ;Check to see if we're received the request call IsConnectionRequestPending jr z,visibleKeyLoop ;Get out of discoverable mode and return remote device information ExitMakeVisible: push hl call LeaveDiscoverableMode pop hl ret c xor a ret ErrMakeVisible: in a,(4) bit 3,a jr z,ErrMakeVisible ;debounce ON key xor a ld (kbdKey),a ld (kbdScanCode),a B_CALL ClrLCDFull B_CALL HomeUp ld hl,sVisibleError call IPutS B_CALL GetKey scf ret sVisibleError: DB "ERROR! " DB "An error " DB "occurred with " DB "the device. " DB "Press any key",0CEh,0 sMakeVisible: DB "This device is " DB "in discoverable " DB "mode and waiting" DB "for an incoming " DB "connection. " DB "Press ",LlBrack,"ON] to " DB "go back.",0