#Region Module Attributes
#FullScreen: False
#IncludeTitle: True
#ApplicationLabel: Megafuture Box test
#VersionCode: 1
#VersionName:
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region
'Activity module
'PROG NAME: B4A_IOIO_UART_Test1.b4a, date: 2011.08.29 by ERIC WANG
'This is a sample program for testing IOIO UART function in B4A,
'Since I only play with B4A for several weeks, errors should be found somewhere.
'I wrote these codeto share my work in reading passive/active RFID, bluetooth, nRF24e1,
'AVR data from a mandown device that my comapny SWEETEK created recently.
'I am trying to read SPI and I2C device in the future. if someone has codes to share that will be very much appreciated.
'NOTE: The default default RXpin=5,pintype=Floating TXpin=4,pintype=Normal, baud=9600 on IOIO Board.
' I tried serial input and AStreams method to read data from PC, and found no luck.
' I don't why, but you need to assign the exact buffer size as found in BytesAvailable or RX won't work.
' please let me know if you tried ASterams and worked, you can email your code to [email protected],
' this sample project is far from finished, so use it with caution.
' if you have any question please contact me via email: [email protected] or [email protected]
' good luck! and have fun.
Sub Process_Globals
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim period As Int
Dim TimerLoop As Timer
Dim TimerRX As Timer
Dim btnSend As Button
Dim btnSetUp As Button
Dim Label1 As Label
Dim Label10 As Label
Dim Label3 As Label
Dim Label4 As Label
Dim Label5 As Label
Dim Label6 As Label
Dim Label7 As Label
Dim Label8 As Label
Dim Label9 As Label
Dim rbtn_loop As RadioButton
Dim rbtn_Single As RadioButton
Dim spn_Baudrate As Spinner
Dim spn_RxPin As Spinner
Dim spn_Rxpintype As Spinner
Dim spn_TxPin As Spinner
Dim spn_Txpintype As Spinner
Dim spn_Period As Spinner
Dim txtRX As EditText
Dim txtSend As EditText
Dim PeriodMap As Map
Dim p_uart As Panel
Dim panel1 As Panel
Dim lbStatus As Label
Dim YOYO As IOIO
Dim led As DigitalOutput
Dim uart1 As Uart
Dim in1 As InputStream
Dim out1 As OutputStream
Dim go As Boolean
Dim RXPIN As Int
Dim RXPINTYPE
Dim TXPIN As Int
Dim TXPINTYPE
Dim BAUDRATE As Int
Dim TPeriod As Int
Dim looped As Boolean
Dim LedFlag As Boolean
End Sub
Sub Activity_Create(FirstTime As Boolean)
Dim panels(5) As Panel
Activity.LoadLayout("1")
Activity.Title="IOIO UART TEST"
If FirstTime Then
p_uart.Initialize("p_Uart")
SetSpinner
'default RXpin=5,pintype=Floating TXpin=4,pintype=Normal, baud=9600)
spn_RxPin.SelectedIndex=1
spn_TxPin.SelectedIndex=0
spn_Baudrate.SelectedIndex=4
spn_Txpintype.SelectedIndex=0
spn_Rxpintype.SelectedIndex=0
spn_Period.SelectedIndex=4
go=False
looped=False
rbtn_Single.Checked=True
YOYO.Initialize
YOYO.WaitForConnect()
TimerLoop.Initialize("TimerLoop",10)
TimerRX.Initialize("TimerRX",100)
led = YOYO.OpenDigitalOutput( 0,led.OP_NORMAL ,True) ' Enable LED_PIN for output
getSetUPValue
uart1= YOYO.OpenUart(RXPIN,RXPINTYPE,TXPIN,TXPINTYPE,BAUDRATE,uart1.PARITY_NONE,uart1.STOPBIT_ONE)
in1=uart1.InputStream
out1=uart1.OutputStream
End If
End Sub
Sub SetSpinner()
spn_Period.TextSize=14
spn_Period.AddAll(Array As String("0.05 sec","0.1 sec","0.2 sec","0.5 sec","1 sec","2 sec","5 sec","10 sec","20 sec"))
PeriodMap.Initialize
PeriodMap.Put("0.05 sec",50)
PeriodMap.Put("0.1 sec",100)
PeriodMap.Put("0.2 sec",200)
PeriodMap.Put("0.5 sec",500)
PeriodMap.Put("1 sec",1000)
PeriodMap.Put("2 sec",2000)
PeriodMap.Put("5 sec",5000)
PeriodMap.Put("10 sec",10000)
PeriodMap.Put("20 sec",20000)
spn_TxPin.TextSize=14
spn_TxPin.AddAll(Array As String("3","4","5","6","7","9","10","11","12","13","14"))
spn_RxPin.TextSize=14
spn_RxPin.AddAll(Array As String("3","4","5","6","7","9","10","11","12","13","14"))
spn_Txpintype.TextSize=14
spn_Txpintype.AddAll(Array As String("OP_NORMAL","OP_OPEN_DRAIN"))
spn_Rxpintype.TextSize=14
spn_Rxpintype.AddAll(Array As String("IP_FLOATING","IP_PULL_DOWN","IP_PULL_UP"))
spn_Baudrate.TextSize=14
spn_Baudrate.AddAll(Array As String("1200","2400","4800","9600","19200","38400","57600","115200"))
End Sub
Sub getSetUPValue
Dim tmp As String
'get RXPIN#
tmp=spn_RxPin.SelectedItem
Select Case tmp
Case "3","4","5","6","7","9","10","11","12","13","14"
RXPIN = tmp
End Select
'get TXPIN#
tmp=spn_TxPin.SelectedItem
Select Case tmp
Case "3","4","5","6","7","9","10","11","12","13","14"
TXPIN = tmp
End Select
'get RXPINTYPE
tmp=spn_Rxpintype.SelectedItem
Select Case tmp
Case "IP_FLOATING"
RXPINTYPE = uart1.IP_FLOATING
Case "IP_PULL_DOWN"
RXPINTYPE = uart1.IP_PULL_DOWN
Case "IP_PULL_UP"
RXPINTYPE = uart1.IP_PULL_UP
End Select
'get TXPINTYPE
tmp=spn_Txpintype.SelectedItem
Select Case tmp
Case "OP_NORMAL"
TXPINTYPE = uart1.OP_NORMAL
Case "OP_OPEN_DRAIN"
TXPINTYPE = uart1.OP_OPEN_DRAIN
End Select
'get baud rate
tmp=spn_Baudrate.SelectedItem
BAUDRATE=tmp
'Get Period
Dim si As Int
si=spn_Period.SelectedIndex
TPeriod=PeriodMap.GetValueAt(si)
End Sub
Sub Activity_Pause (UserClosed As Boolean)
If UserClosed Then
led.Close
YOYO.disconnect()
End If
End Sub
Sub rbtn_Single_CheckedChange(Checked As Boolean)
If Checked Then
looped= False
End If
End Sub
Sub rbtn_loop_CheckedChange(Checked As Boolean)
If Checked Then
looped=True
End If
End Sub
Sub btnSetUp_Click()
If go=True Then
go=False
btnSetUp.Text="SET/GO"
'btnSetUp.Background=black
TimerLoop.Enabled=False
TimerRX.Enabled=False
Else
go=True
btnSetUp.Text="STOP"
getSetUPValue
uart1.Close
in1.Close
out1.Close
uart1= YOYO.OpenUart(RXPIN,RXPINTYPE,TXPIN,TXPINTYPE,BAUDRATE,uart1.PARITY_NONE,uart1.STOPBIT_ONE)
in1=uart1.InputStream
out1=uart1.OutputStream
If rbtn_Single.Checked=False Then
TimerLoop.Initialize("TimerLoop",TPeriod)
TimerLoop.Enabled=True
End If
TimerRX.Enabled=True
Msgbox("READ TO GO","")
End If
End Sub
Sub btnSend_Click
'If rbtn_Single.Checked=True Then
led.Write(Not(LedFlag)) ' Turn on/off stat LED, active LOW
LedFlag = Not ( LedFlag )
If uart1.IsInitialized Then
'getSetUPValue
'uart1.Close
'IN1.Close
'OUT1.Close
'uart1= YOYO.OpenUart(RXPIN,RXPINTYPE,TXPIN,TXPINTYPE,BAUDRATE,uart1.PARITY_NONE,uart1.STOPBIT_ONE)
'in1=uart1.InputStream
'out1=uart1.OutputStream
If txtSend.Text.Length > 0 Then
Dim buffer() As Byte
Dim ST As String
ST=txtSend.Text & Chr(13) & Chr(10) 'CRLF
buffer =ST.GetBytes("UTF8")
out1.WriteBytes(buffer,0,buffer.Length)
out1.Flush
txtSend.SelectAll
End If
End If
End Sub
Sub TimerLoop_Tick
If LedFlag= True Then
LedFlag=False
Else
LedFlag=True
End If
led.Write(LedFlag)
If rbtn_Single.Checked=False Then
If txtSend.Text.Length > 0 Then
Dim buffer() As Byte
Dim ST As String
ST=txtSend.Text & CRLF
buffer =ST.GetBytes("UTF8")
out1.WriteBytes(buffer,0,buffer.Length)
out1.Flush
txtSend.SelectAll
End If
End If
End Sub
Sub TimerRX_Tick()
If in1.IsInitialized Then
'If in1.BytesAvailable>0 Then
TimerRX.Enabled=False
'*** need to assign the exact buffer size as in BytesAvailable or it won't work
Dim buffer(in1.BytesAvailable) As Byte
Dim count As Int
count = in1.ReadBytes(buffer, 0,buffer.Length)
lbStatus.Text="Read " & count & " bytes. @ " & DateTime.Time(DateTime.Now)
If count > 0 Then
txtRX.Text=txtRX.Text & BytesToString(buffer, 0, count, "UTF8")
txtRX.SelectionStart = txtRX.Text.Length
End If
'End If
End If
TimerRX.Enabled=True
End Sub
Sub Label3_Click
txtRX.Text = ""
End Sub