;Bluetooth driver callback routine(s) include "settings.inc" include "ti83plus.inc" include "equates.inc" include "usb8x.inc" include "bluetooth.inc" SEGMENT Main GLOBALS ON EXTERN DispHexHL,DispHexA,USBDriverBuf,IPutC,IPutS11 EXTERN tempWord ;IMPORTANT: THIS RUNNER CANNOT GROW PAST 50 BYTES BluetoothCallbackRunner: push bc in a,(6) push af ld a,(basePage) out (6),a call DoBluetoothCallback pop bc ld a,b out (6),a pop bc ret BluetoothCallbackRunnerEnd: ;TODO: make this capable of receiving other things besides HCI events DoBluetoothCallback: ld a,b cp cbNewData jr z,$F ;ret nz ;NAK or Stall ld a,'S' call IPutC ret $$: ld a,(interruptPipe) cp c jr z,isHCLCommandPacket push ix ;This is an ACL data packet ;Possibilities: ; 1. This is the very first packet. ; 2. This is a continuation of the first packet. ; 3. This is the start of the second (or more) packet. ; 4. This is a continuation of the second (or more) packet. ;If it's 1, set up some pointers. ;If it's 2 (meaning we haven't received the entire header from 1), tack it on. ;If it's 3 (meaning we received a whole packet before and this is a continuation packet), tack it on. ;If it's 4, tack it on. push bc ld ix,responseFlag ;bit ACLPacketStarted,(ix+0) set ACLDataReceived,(ix+0) ld hl,USBDriverBuf+2 ld c,(hl) inc hl ld b,(hl) dec hl dec hl dec hl inc bc inc bc inc bc inc bc ld de,(ptrACLBuffer) ldir xor a ld (de),a pop bc pop ix ret isHCLCommandPacket: push ix push bc ld ix,responseFlag bit cmdStarted,(ix+0) set cmdStarted,(ix+0) ld hl,USBDriverBuf jr nz,alreadyStartedReceiving ;This is the start of an HCI event ld bc,(ptrInputBuffer) ld (ptrInputStart),bc bit deviceConnected,(ix+0) ;*** TESTING jr z,$F ;*** TESTING ld a,(hl) ;*** TESTING ; call DispHexA ;*** TESTING $$: inc hl ld a,(hl) inc a inc a ld (responseDataLeft),a dec hl alreadyStartedReceiving: ;E is the amount of data we have in our buffer ;HL is the data in our buffer to store ;(responseDataLeft) is what we need to store ;(ptrInputBuffer) is where to write it ld b,0 ld c,e ld de,(ptrInputBuffer) push bc ldir ld (ptrInputBuffer),de pop bc ld a,(responseDataLeft) sub c ld (responseDataLeft),a pop bc jr z,receiveComplete ld b,c U_CALL ReqData pop ix ret receiveComplete: set cmdComplete,(ix+0) res cmdStarted,(ix+0) bit inDiscoverableMode,(ix+0) jr nz,receivedDiscoverPacket bit deviceConnected,(ix+0) jr z,$F ld b,c U_CALL ReqData $$: pop ix ret receivedDiscoverPacket: ;See if we need to notify the "user" of this event with flags ld hl,(ptrInputStart) ld a,(hl) cp Event_ConnectionRequest jr nz,$F set receivedConnRequest,(ix+0) res inDiscoverableMode,(ix+0) push bc ld hl,eventDataBuffer+2 ld de,deviceRequestInfo ld bc,10 ldir pop bc $$: ld b,c U_CALL ReqData pop ix ret