REM Use the iPad screen to enter values into the Learn Site REM Examples for Activity Bot REM Send the value of the PING sensor back to be displayed REM Send the value of the wheel ticks as a result of REM movement. REM Use a toggle switch to connect to WiFi REM Have a text box for ip address REM Initial ip addr is DIM ipaddr$ AS STRING !!! DEFAULT IP ADDRESS ipaddr$ = "192.168.1.3" DIM ipport$ AS STRING !!! DEFAULT PORT ipport$ = "9750" REM Only one port being used so hard code it ipport% = val(ipport$) !! Seperate message buffers for Rx and Tx messages DIM RxMessage~(16) DIM TxMessage~(16) !Only allow landscape home on right System.setAllowedOrientations(4) Graphics.setFont("Monospaced",18,1) xcenter = Graphics.Width/2 !! Graphics.Width is not the right edge?? so tweeked it xright = Graphics.Width + 150 DIM quit AS Button quit = Graphics.newButton(xright,250) quit.setTitle("Quit") !! Define switches for connecting and AB led's DIM swip as Switch DIM swP26 as Switch DIM swP27 as Switch DIM lblip as Label DIM lblP26 as Label DIM lblP27 as Label REM Connection variables DIM found AS INTEGER DIM connected AS INTEGER swip = Graphics.newSwitch(xright,25) !Default to off !Color RED until connected swip.setColor(1,0,0) swip.setOn(0) swP26 = Graphics.newSwitch(xright,65) !Default to off swP26.setOn(0) swP26.setEnabled(0) swP27 = Graphics.newSwitch(xright,105) !Default to off swP27.setOn(0) swP27.setEnabled(0) lblip = Graphics.newLabel(xright-100,25,100) lblP26 = Graphics.newLabel(xright-100,65,100) lblP27 = Graphics.newLabel(xright-100,105,100) lblP26.setText("P26LED") lblP27.setText("P27LED") lblip.setText("CONNECT") REM Define a text field to enter a different ip address DIM txip AS TEXTFIELD txip = Graphics.newTextField(xright-300,25,200) txip.setFont("Monospaced",24,1) txip.setBackgroundColor(1,1,0) txip.setColor(1,0,0) !! Only numbers and puncuation txip.setKeyboardType(3) txip.setText(ipaddr$) REM Define the labels and text fields for abdrive code DIM lblrt AS LABEL DIM lbllf AS LABEL DIM lbltp AS LABEL DIM txrt AS TEXTFIELD DIM txlf AS TEXTFIELD DIM txtp AS TEXTFIELD rt$ = "0" lf$ = "0" tp$ = "1000" REM Variables to hold user entered screen values rt% = 0 lf% = 0 tp% = 0 mycmd~ = 0 REM Create the labels and fields lblrt = Graphics.newLabel(20,200,100) lblrt.setAlignment(3) lblrt.setFont("Monospaced",24,1) lblrt.setText("Right") txrt = Graphics.newTextField(125,200,100) txrt.setFont("Monospaced",24,1) txrt.setBackgroundColor(1,1,0) txrt.setColor(0,0,1) !! Only numbers and puncuation txrt.setKeyboardType(3) txrt.setText(rt$) lbllf = Graphics.newLabel(20,250,100) lbllf.setAlignment(3) lbllf.setFont("Monospaced",24,1) lbllf.setText("Left") txlf = Graphics.newTextField(125,250,100) txlf.setFont("Monospaced",24,1) txlf.setBackgroundColor(1,1,0) txlf.setColor(0,0,1) !! Only numbers and puncuation txlf.setKeyboardType(3) txlf.setText(lf$) lbltp = Graphics.newLabel(20,300,100) lbltp.setAlignment(3) lbltp.setFont("Monospaced",24,1) lbltp.setText("Tpause") txtp = Graphics.newTextField(125,300,100) txtp.setFont("Monospaced",24,1) txtp.setBackgroundColor(1,1,0) txtp.setColor(0,0,1) !! Only numbers and puncuation txtp.setKeyboardType(3) txtp.setText(tp$) REM Define TextViews to show example code DIM tvspeed AS TEXTVIEW DIM tvramp AS TEXTVIEW DIM tvgoto AS TEXTVIEW tvspeed = Graphics.newTextView(50,450,220,70) tvspeed.setEditable(0) tvspeed.setAlignment(1) tvspeed.setFont("MonoSpaced",14,1) tvspeed.setBackgroundColor(0,1,0) tvspeed.setText("drive_speed(Left,Right)" & Chr(10) _ & "pause(Tpause)" & Chr(10) _ & "drive_speed(0,0)" & Chr(10)) DIM exspd AS BUTTON exspd = Graphics.newButton(80,410,150) exspd.setTitle("Execute Speed") tvramp = Graphics.newTextView(350,450,220,70) tvramp.setEditable(0) tvramp.setAlignment(1) tvramp.setFont("MonoSpaced",14,1) tvramp.setBackgroundColor(0,1,0) tvramp.setText("drive_ramp(Left,Right)" & Chr(10) _ & "pause(Tpause)" & Chr(10) _ & "drive_ramp(0,0)" & Chr(10)) DIM exrmp AS BUTTON exrmp = Graphics.newButton(380,410,150) exrmp.setTitle("Execute Ramp") tvgoto = Graphics.newTextView(650,450,220,70) tvgoto.setEditable(0) tvgoto.setAlignment(1) tvgoto.setFont("MonoSpaced",14,1) tvgoto.setBackgroundColor(0,1,0) tvgoto.setText("drive_goto(Left,Right)" & Chr(10) _ & "pause(Tpause)" & Chr(10) ) DIM exgo2 AS BUTTON exgo2 = Graphics.newButton(680,410,150) exgo2.setTitle("Execute GoTo") REM Create Labels and TextFields to show incoming values REM The Ping sensor data and Wheel ticks pi$ = "0" ticks$ = "10000" DIM lblping AS LABEL DIM txping AS TEXTFIELD lblping = Graphics.newLabel(325,200,100) lblping.setAlignment(3) lblping.setFont("Monospaced",24,1) lblping.setText("Ping:") txping = Graphics.newTextField(425,200,100) txping.setFont("Monospaced",24,1) txping.setBackgroundColor(1,1,0) txping.setColor(0,0,1) txping.setText(pi$) DIM lblin AS LABEL lblin = Graphics.newLabel(525,215,80) lblin.setFont("Monospaced",20,1) lblin.setText("inches") DIM lblticks AS LABEL DIM txticks AS TEXTFIELD lblticks = Graphics.newLabel(325,300,100) lblticks.setAlignment(3) lblticks.setFont("Monospaced",24,1) lblticks.setText("Ticks:") txticks = Graphics.newTextField(425,300,100) txticks.setFont("Monospaced",24,1) txticks.setBackgroundColor(1,1,0) txticks.setColor(0,0,1) txticks.setText(ticks$) t= system.ticks REM Start out with controls that cause events disabled REm until we connect to Wifi EnableEvents(0) REM USE FULL SCREEN !Pass 1 to go full screen and in this !mode setAllowedOrientations works System.showGraphics(1) REM SHOW GRAPHICS WITH DEBUGGER TOOLBAR !System.showGraphics !!! Sub's !!! REM ENABLE/DISABLE EVENT CONTROLS BASED ON CONNECTED SUB EnableEvents(OnOff AS INTEGER) swP26.setEnabled(OnOff) swP27.setEnabled(OnOff) exspd.setEnabled(OnOff) exrmp.setEnabled(OnOff) exgo2.setEnabled(OnOff) END SUB SUB MessageTx(numbytes AS BYTE) FOR i% = 1 TO numbytes PUT #1,, Txmessage~(i%) NEXT END SUB SUB SendMsgA1(db AS Byte) Txmessage~(1) = $A1 Txmessage~(2) = 2 Txmessage~(3) = db ! Compute the checksum checksum~ = BITNOT(2 + db) + 1 Txmessage~(4) = checksum~ MessageTx(4) END SUB SUB SendMsgA7(CMD AS BYTE,R AS INTEGER,L AS INTEGER,T AS INTEGER) ! Send TextField values and CMD based on which button clicked TxMessage~(1) = $A7 TxMessage~(2) = 8 TxMessage~(3) = CMD TxMessage~(4) = (R BITAND $00FF) TxMessage~(5) = (R BITAND $FF00) >> 8 TxMessage~(6) = (L BITAND $00FF) TxMessage~(7) = (L BITAND $FF00) >> 8 TxMessage~(8) = (T BITAND $00FF) TxMessage~(9) = (T BITAND $FF00) >> 8 checksum~ = 0 FOR ix% = 2 TO 9 checksum~ = checksum~ + TxMessage~(ix%) NEXT checksum~ = BITNOT(checksum~) + 1 TxMessage~(10) = checksum~ MessageTx(10) END SUB REM Modified version of Jeff T Connect SUB connect ON ERROR GOTO 100 ipaddr$ = txip.getText Comm.openTCPIP(1,ipaddr$,ipport%) t=system.ticks WHILE system.ticks - t < 1 IF comm.isreadyforoutput(1) THEN found=1 connected=1 EnableEvents(1) !! Green switch if made connection swip.setColor(0,1,0) t=t-1 END IF WEND IF not found THEN ERROR 53 ELSE ERROR 0 END IF 100 IF err.number=53 THEN close #1 connected=0 swip.setOn(0) swip.setColor(1,0,0) EnableEvents(0) ON ERROR GOTO 0 END IF END SUB SUB valueChanged(ctrl AS Control, time AS DOUBLE) IF ctrl = swP26 THEN IF swP26.isON THEN SendMsgA1(1) ELSE SendMsgA1(2) END IF END IF IF ctrl = swP27 THEN IF swP27.isON THEN SendMsgA1(4) ELSE SendMsgA1(8) END IF END IF IF ctrl = swip THEN IF swip.isON THEN connect ELSE IF connected THEN close #1 connected = 0 found = 0 EnableEvents(0) END IF swip.setColor(1,0,0) END IF END IF END SUB SUB touchUpInside(ctrl AS Button, when AS Double) if ctrl = quit then STOP ELSE IF ctrl = exspd THEN mycmd~ = 1 rt% = VAL(txrt.getText) lf% = VAL(txlf.getText) tp% = VAL(txtp.getText) SendMsgA7(mycmd~,rt%,lf%,tp%) ELSE IF ctrl = exrmp THEN mycmd~ = 3 rt% = VAL(txrt.getText) lf% = VAL(txlf.getText) tp% = VAL(txtp.getText) SendMsgA7(mycmd~,rt%,lf%,tp%) ELSE IF ctrl = exgo2 THEN mycmd~ = 2 rt% = VAL(txrt.getText) lf% = VAL(txlf.getText) tp% = VAL(txtp.getText) SendMsgA7(mycmd~,rt%,lf%,tp%) END IF END SUB