DEFINE LOADER_USED 1 ' for bootloader
DEFINE RESET_ORG 800h ' For Microchip USB Bootloader
DEFINE INTERRUPT_ORG 808h ' For Microchip USB Bootloader
DEFINE OSC 48
buffer VAR BYTE[16]
bufferIn VAR BYTE[16]
temp VAR BYTE[5]
cnt VAR BYTE
transmitID VAR BYTE
transmitID = 101
n_2400 CON 16780 ' baud mode for serin2 (2400-8-N-1 inverted)
rx VAR portc.7
PAUSE 1000
USBINIT
init:
' wait for input before starting the loop
USBSERVICE ' Must service USB regularly
cnt = 16 ' Specify input buffer size
USBIN 3, buffer, cnt, init
' Wait for USB input
waitForInput:
USBSERVICE ' Must service USB regularly
cnt = 16 ' Specify input buffer size
USBIN 3, bufferIn, cnt, receiveloop
GOTO outloop
receiveloop:
' wait for the "B" character.
' If not received within 1ms: return to main code (not shown here)
' If received: place 3 remaining data bytes in 'temp' string.
SERIN2 rx, n_2400, 1, waitForInput, [WAIT("B"),STR temp\3]
' error-check if both data bytes are similar
IF temp[1] = temp[2] THEN
' check in new data is part of a previous 'blast' or a new transmission
' (the tranmiter sends 20 transmissions for each new value).
IF transmitID <> temp[0] THEN
' accumilate the tilt data into a buffer by incrementing its value.
' this buffer is sent to Processing upon request.
' Processing will receive the number of tilts since its last request.
buffer[0] = buffer[0] + temp[1]
transmitID = temp[0] ' save this transmission's ID to check agains new transmissions.
GOTO waitForInput ' goto main routine (not shown here).
ENDIF
ENDIF
outloop:
USBSERVICE ' Must service USB regularly
USBOUT 3, buffer, 1, outloop
buffer[0] = 0
GOTO waitForInput ' Wait for next buffer