'** Created by Liberty BASIC Workshop - 6/4/2006 11:04:15 PM '** Window Title ' This application was written by Brian Schmalz ' For the UBW hardware interface board. ' E-mail brian_schmalz@yahoo.com for more information, ' and see http://www.greta.dhs.org/UBW for UBW info. ' Started on 6/3/2006. ' ' Note, that with this application it is VERY important ' to use UBW Firmware Version D v1.2 or above to prevent locking up Liberty Basic. ' **** GLOBAL VARIABLES **** global kDisplaySampleMax kDisplaySampleMax = 1000 DIM gDisplay(19,kDisplaySampleMax) global gDisplaySampleStart global gDisplaySampleEnd global gNextPacketNumber gDisplaySampleStart = 0 ' First sample to be displayed on left gDisplaySampleEnd = 0 ' Last sample to be displayed on right gNextPacketNumber = 1 ' Next sample to get updated from UBW ' **** GLOBAL CONSTANTS ***** global gComPort$ gComPort$ = "COM4" global gTimerTick gTimerTick = 100 global gUBWRate$ gUBWRate$ = "20" global kPORTABIT0 global kPORTABIT1 global kPORTABIT2 global kPORTABIT3 global kPORTABIT4 global kPORTABIT5 kPORTABIT0 = 0 kPORTABIT1 = 1 kPORTABIT2 = 2 kPORTABIT3 = 3 kPORTABIT4 = 4 kPORTABIT5 = 5 global kPORTBBIT0 global kPORTBBIT1 global kPORTBBIT2 global kPORTBBIT3 global kPORTBBIT4 global kPORTBBIT5 global kPORTBBIT6 global kPORTBBIT7 kPORTBBIT0 = 6 kPORTBBIT1 = 7 kPORTBBIT2 = 8 kPORTBBIT3 = 9 kPORTBBIT4 = 10 kPORTBBIT5 = 11 kPORTBBIT6 = 12 kPORTBBIT7 = 13 global kPORTCBIT0 global kPORTCBIT1 global kPORTCBIT2 global kPORTCBIT6 global kPORTCBIT7 kPORTCBIT0 = 14 kPORTCBIT1 = 15 kPORTCBIT2 = 16 kPORTCBIT6 = 17 kPORTCBIT7 = 18 global kDISPLAYHEIGHT global kDISPLAYWIDTH kDISPLAYHEIGHT = 290 kDISPLAYWIDTH = 600 [InitColors] 'Please use default colors when possible. 'ForegroundColor$ = "Black" 'BackgroundColor$ = "Buttonface" CurColor$ = "red" True = 1 : False = 0 YPos = 1 [WindowSetup] NOMAINWIN WindowWidth = 800 : WindowHeight = 463 UpperLeftX = INT((DisplayWidth-WindowWidth)/2) UpperLeftY = INT((DisplayHeight-WindowHeight)/2) [ControlSetup] Menu #main, "&File" , "E&xit", [quit] graphicbox #main.display, 90, 5, kDISPLAYWIDTH, kDISPLAYHEIGHT statictext #main.B0, "Port A bit 0", 20, 10 + 0 * int(kDISPLAYHEIGHT/19)-4, 65, 16 statictext #main.B0, "Port A bit 1", 20, 10 + 1 * int(kDISPLAYHEIGHT/19)-4, 65, 16 statictext #main.B0, "Port A bit 2", 20, 10 + 2 * int(kDISPLAYHEIGHT/19)-4, 65, 16 statictext #main.B0, "Port A bit 3", 20, 10 + 3 * int(kDISPLAYHEIGHT/19)-4, 65, 16 statictext #main.B0, "Port A bit 4", 20, 10 + 4 * int(kDISPLAYHEIGHT/19)-4, 65, 16 statictext #main.B0, "Port A bit 5", 20, 10 + 5 * int(kDISPLAYHEIGHT/19)-4, 65, 16 statictext #main.B0, "Port B bit 0", 20, 10 + 6 * int(kDISPLAYHEIGHT/19)-4, 65, 16 statictext #main.B0, "Port B bit 1", 20, 10 + 7 * int(kDISPLAYHEIGHT/19)-4, 65, 16 statictext #main.B0, "Port B bit 2", 20, 10 + 8 * int(kDISPLAYHEIGHT/19)-4, 65, 16 statictext #main.B0, "Port B bit 3", 20, 10 + 9 * int(kDISPLAYHEIGHT/19)-4, 65, 16 statictext #main.B0, "Port B bit 4", 20, 10 + 10 * int(kDISPLAYHEIGHT/19)-4, 65, 16 statictext #main.B0, "Port B bit 5", 20, 10 + 11 * int(kDISPLAYHEIGHT/19)-4, 65, 16 statictext #main.B0, "Port B bit 6", 20, 10 + 12 * int(kDISPLAYHEIGHT/19)-4, 65, 16 statictext #main.B0, "Port C bit 7", 20, 10 + 13 * int(kDISPLAYHEIGHT/19)-4, 65, 16 statictext #main.B0, "Port C bit 0", 20, 10 + 14 * int(kDISPLAYHEIGHT/19)-4, 65, 16 statictext #main.B0, "Port C bit 1", 20, 10 + 15 * int(kDISPLAYHEIGHT/19)-4, 65, 16 statictext #main.B0, "Port C bit 2", 20, 10 + 16 * int(kDISPLAYHEIGHT/19)-4, 65, 16 statictext #main.B0, "Port C bit 6", 20, 10 + 17 * int(kDISPLAYHEIGHT/19)-4, 65, 16 statictext #main.B0, "Port C bit 7", 20, 10 + 18 * int(kDISPLAYHEIGHT/19)-4, 65, 16 stylebits #main.button1, _BS_MULTILINE, 0, 0, 0 button #main.button1, "Quit", [quit], UL, 560, 365, 110, 35 stylebits #main.run, _BS_MULTILINE, 0, 0, 0 button #main.run, "Run", [RunClickWait], UL, 85, 360, 80, 45 stylebits #main.stop, _BS_MULTILINE, 0, 0, 0 button #main.stop, "Stop", [StopClickWait], UL, 175, 360, 75, 45 Open "Window Title" for Window as #main #main "trapclose [quit]" #main.display "down; fill White; flush" #main.display "setfocus" #main "font ms_sans_serif 10" gosub [OpenSerialPort] gosub [SetupUBW] ' Set up the timer to run timer gTimerTick,[TimerTick] [loop] Wait [quit] gosub [CloseSerialPort] close #main : END [TimerTick] timer 0 gosub [CheckForNewPacket] timer gTimerTick,[TimerTick] goto [loop] ' First configure the ports like we want them [SetupUBW] return ' Check to see if we've got any new data from the UBW [CheckForNewPacket] NeedRefresh = False while (lof(#comm) > 14) New$ = trim$(input$(#comm, 15)) if (left$(New$,1) = "I") then call ParseNewPacket, New$ NeedRefresh = True end if wend if NeedRefresh = True then gosub [DrawDisplay] end if return ' We got in a new I packet, so pick it appart sub ParseNewPacket Packet$ PortA = val(mid$(Packet$, 3, 3)) PortB = val(mid$(Packet$, 7, 3)) PortC = val(mid$(Packet$, 11, 3)) if (gNextPacketNumber < kDisplaySampleMax) then call AddPacket PortA, PortB, PortC else print #comm, "T,0,0" end if end sub ' Now add the new data to our little array sub AddPacket PortA, PortB, PortC gDisplay(kPORTABIT0, gNextPacketNumber) = TstBit(PortA,0) gDisplay(kPORTABIT1, gNextPacketNumber) = TstBit(PortA,1) gDisplay(kPORTABIT2, gNextPacketNumber) = TstBit(PortA,2) gDisplay(kPORTABIT3, gNextPacketNumber) = TstBit(PortA,3) gDisplay(kPORTABIT4, gNextPacketNumber) = TstBit(PortA,4) gDisplay(kPORTABIT5, gNextPacketNumber) = TstBit(PortA,5) gDisplay(kPORTBBIT0, gNextPacketNumber) = TstBit(PortB,0) gDisplay(kPORTBBIT1, gNextPacketNumber) = TstBit(PortB,1) gDisplay(kPORTBBIT2, gNextPacketNumber) = TstBit(PortB,2) gDisplay(kPORTBBIT3, gNextPacketNumber) = TstBit(PortB,3) gDisplay(kPORTBBIT4, gNextPacketNumber) = TstBit(PortB,4) gDisplay(kPORTBBIT5, gNextPacketNumber) = TstBit(PortB,5) gDisplay(kPORTBBIT6, gNextPacketNumber) = TstBit(PortB,6) gDisplay(kPORTBBIT7, gNextPacketNumber) = TstBit(PortB,7) gDisplay(kPORTCBIT0, gNextPacketNumber) = TstBit(PortC,0) gDisplay(kPORTCBIT1, gNextPacketNumber) = TstBit(PortC,1) gDisplay(kPORTCBIT2, gNextPacketNumber) = TstBit(PortC,2) gDisplay(kPORTCBIT6, gNextPacketNumber) = TstBit(PortC,6) gDisplay(kPORTCBIT7, gNextPacketNumber) = TstBit(PortC,7) gNextPacketNumber = gNextPacketNumber + 1 end sub ' Draw the current array on the screen [DrawDisplay] XStep = 6 YStep = int(kDISPLAYHEIGHT/19)-4 ' Calculate the start and end if gNextPacketNumber > (gDisplaySampleStart + 1) then LastDisplayedSample = gDisplaySampleStart + 1 gDisplaySampleStart = gNextPacketNumber - 1 TotalSamples = gDisplaySampleStart-LastDisplayedSample + 1 else return end if XPos = kDISPLAYWIDTH-2 - (XStep * TotalSamples) ' Next we grab a bitmap of what's already there, minus the left most sample print #main.display, "getbmp TmpBmp ";(XStep*TotalSamples);" 0 ";kDISPLAYWIDTH-2-(XStep*TotalSamples);" ";kDISPLAYHEIGHT print #main.display, "cls" print #main.display, "down" print #main.display, "drawbmp TmpBmp 0 0" ' We're going to go from right to left (scrolling left) for Sample = LastDisplayedSample to gDisplaySampleStart YPos = 1 for pin = 0 to 18 ' Change the color of each pin's line, so it looks cool print #main.display, "color ";word$("brown red pink blue darkgreen black lightgray", (pin MOD 7) + 1) if gDisplay(pin,Sample) = 1 then ' Print the 1 or 0 horizontal line print #main.display, "line ";XPos;" ";YPos;" ";XPos - XStep - 1;" ";YPos else ' Print the 1 or 0 horizontal line print #main.display, "line ";XPos;" ";YPos+YStep;" ";XPos-XStep-1;" ";YPos+YStep end if if (gDisplay(pin, Sample-1) <> gDisplay(pin,Sample)) then ' draw a horizontal line print #main.display, "line ";XPos - XStep;" ";YPos;" ";XPos - XStep;" ";YPos+YStep end if YPos = YPos + YStep + 4 next pin XPos = XPos + XStep next Sample print #main.display, "flush" unloadbmp "TmpBmp" return ' Open the serial port to the UBW [OpenSerialPort] oncomerror [ErrorSerialPort] open gComPort$;":9600,n,8,1" for random as #comm return ' Close the serial port to the UBW [CloseSerialPort] close #comm return [ErrorSerialPort] oncomerror print "Got an error on ";ComPortNumber;" Error:";ComError$ goto [loop] [RunClickWait] gosub [RunClick] goto [loop] [RunClick] ' Send out a "T" packet with the right information to start I packets print #comm, "T,";gUBWRate$;",0" return [StopClickWait] gosub [StopClick] goto [loop] [StopClick] print #comm, "T,0,0" return ' Flip the bit 'Bit' in byte 'Byte' and return the new byte. function BitFlip(Byte, Bit) if TstBit(Byte, Bit) = 1 then BitFlip = (Byte AND (255 XOR (2^Bit))) else BitFlip = (Byte OR (2^Bit)) end if end function ' Test bit "Bit" in variable "Byte" and return it's value (1 or 0) function TstBit(Byte, Bit) TestBit = 0 if ((Byte AND (2^Bit)) > 0) then TstBit = 1 end if end function