#Region Module Attributes
#FullScreen: False
#IncludeTitle: True
#ApplicationLabel: Joystick2ppm
#VersionCode: 1
#VersionName: 0.00
#SupportedOrientations: Landscape
#CanInstallToExternalStorage: False
#End Region
Sub Process_Globals
Dim manager As UsbManager
Dim HIDstreams As UsbDeviceConnection
Dim JPHIDstreams As UsbDeviceConnection
Dim outEndpoint, inEndpoint As UsbEndpoint
Dim JPoutEndpoint, JPinEndpoint As UsbEndpoint
Dim device As UsbDevice
Dim JPdevice As UsbDevice
Dim interface As UsbInterface
Dim JPinterface As UsbInterface
Dim JoystickConnected As Boolean = False
Dim JPConnected As Boolean = False
Dim VID As Int : VID = 0x4D9
Dim PID As Int : PID = 0x3F
Dim JoyVID As Int
Dim JoyPID As Int
Dim RxPlcBuffer(64) As Byte
Dim TxPlcBuffer(64) As Byte
Dim Channel(8) As Int
Dim Reverse(8) As Boolean
Dim Input(20) As Int
Dim Output(8) As Int
Dim Timer1 As Timer
End Sub
Sub Globals
Dim Bt1 As Button
Dim PbX As ProgressBar
Dim PbY As ProgressBar
Dim PbZ As ProgressBar
Dim PbR As ProgressBar
Dim Ck1 As CheckBox
Dim Ck2 As CheckBox
Dim Ck3 As CheckBox
Dim Ck4 As CheckBox
Dim Ck5 As CheckBox
Dim Ck6 As CheckBox
Dim Ck7 As CheckBox
Dim Ck8 As CheckBox
Dim Img1 As ImageView
Dim Ed1 As EditText
End Sub
Sub Activity_Create(FirstTime As Boolean)
Timer1.Initialize("Timer1", 20) ' 1000 = 1 second
Timer1.Enabled = True
If FirstTime Then
manager.Initialize
End If
Activity.LoadLayout("1")
'Load the previous state
If StateManager.RestoreState(Activity, "Main", 60) = False Then
'set the default values
End If
For Each V As View In Activity
If V Is Spinner Then
If V.Tag <>"" Then
Dim Sp As Spinner
Sp = V
Sp.Add("X")
Sp.Add("Y")
Sp.Add("Z")
Sp.Add("Rx")
Sp.Add("Hat")
Sp.Add("B1")
Sp.Add("B2")
Sp.Add("B3")
Sp.Add("B4")
Sp.Add("B5")
Sp.Add("B6")
Sp.Add("B7")
Sp.Add("B8")
End If
End If
Next
End Sub
Sub Activity_Pause(UserClosed As Boolean)
If UserClosed Then
'StateManager.ResetState("Main")
Timer1.Enabled = False
Else
'StateManager.SaveState(Activity, "Main")
End If
StateManager.SaveState(Activity, "Main")
StateManager.SaveSettings
End Sub
Sub Bt1_Click
Dim NbWrite As Int
If JPHIDstreams.IsInitialized Then
TxPlcBuffer(0)= 0x83
NbWrite = JPHIDstreams.BulkTransfer(JPoutEndpoint,TxPlcBuffer,JPoutEndpoint.MaxPacketSize,500)
'If NbWrite = -1 Then JPConnected = False
End If
End Sub
Sub Timer1_Tick
Dim NbRead As Int
Dim i As Int
Dim j As Int
Dim MyStr As String = ""
If JoystickConnected = False Then ConnectJoystick
If JPConnected = False Then ConnectJP
If ( HIDstreams.IsInitialized = True ) Then
NbRead = HIDstreams.BulkTransfer(inEndpoint,RxPlcBuffer,inEndpoint.MaxPacketSize,500)
If NbRead = -1 Then
JoystickConnected = False
JPConnected = False
Bt1.Enabled = False
End If
For i = 0 To NbRead -1
j = ConvertSignedByteToUnsigned(RxPlcBuffer(i))
'j = (RxPlcBuffer(i))
MyStr = MyStr & NumberFormat(j,0, 0) & " "
'Ed1.Visible = True
'Ed1.text = MyStr
Next
Select (JoyVID* 0x10000 + JoyPID) 'use VID and PID as a primary key
Case (0x45E * 0x10000 + 0x001B) 'MicrosoftForceFeedback
DecomMicrosoftForceFeedback
Case (0x46D * 0x10000 + 0xC216) 'LogitecDualAction
DecomLogitecDualAction
Case Else
ToastMessageShow ("unsupported joystick", True)
End Select
PerformMixing 'Apply mixing strategy
OutputPPM 'Send PPM command to JP Board and then to Radio
End If
End Sub
Sub DecomMicrosoftForceFeedback
Dim j As Int
j = ConvertSignedByteToUnsigned(RxPlcBuffer(1)) + Bit.AND(ConvertSignedByteToUnsigned(RxPlcBuffer(2)), 0x3)*256 +512
j= Bit.AND(j , 1023)
PbX.Progress = j*100/1023
Input(0) = j * 64
j = ConvertSignedByteToUnsigned(RxPlcBuffer(3)) + Bit.AND(ConvertSignedByteToUnsigned(RxPlcBuffer(4)), 0x3)*256 +512
j= Bit.AND(j , 1023)
PbY.Progress =j*100/1023
Input(1) = j * 64
j = ConvertSignedByteToUnsigned(RxPlcBuffer(5))+32
j= Bit.AND(j , 63)
PbZ.Progress =j*100/63
Input(2) = j * 1024
j = ConvertSignedByteToUnsigned(RxPlcBuffer(6))
j= Bit.AND(j , 127)
PbR.Progress =j*100/127
Input(3) = j * 512
Select Case Bit.AND(RxPlcBuffer(7), 0x0F)
Case 0
Img1.Bitmap = (LoadBitmap(File.DirAssets, "90.jpg"))
Input(4) = 7280
Case 1
Img1.Bitmap =(LoadBitmap(File.DirAssets, "45.jpg"))
Input(4) = 14560
Case 2
Img1.Bitmap =(LoadBitmap(File.DirAssets, "0.jpg"))
Input(4) = 21840
Case 3
Img1.Bitmap =(LoadBitmap(File.DirAssets, "315.jpg"))
Input(4) = 29120
Case 4
Img1.Bitmap =(LoadBitmap(File.DirAssets, "270.jpg"))
Input(4) = 36400
Case 5
Img1.Bitmap =(LoadBitmap(File.DirAssets, "225.jpg"))
Input(4) = 43680
Case 6
Img1.Bitmap =(LoadBitmap(File.DirAssets, "180.jpg"))
Input(4) = 50960
Case 7
Img1.Bitmap =(LoadBitmap(File.DirAssets, "135.jpg"))
Input(4) = 65535
Case Else
Img1.Bitmap =(LoadBitmap(File.DirAssets, "c.jpg"))
Input(4) = 0
End Select
If Bit.AND(RxPlcBuffer(8), 0x1) > 0 Then
Ck1.checked = True
Input(5) = 0xFFFF
Else
Ck1.Checked = False
Input(5) = 0
End If
If Bit.AND(RxPlcBuffer(8), 0x2) > 0 Then
Ck2.Checked = True
Input(6) = 0xFFFF
Else
Ck2.Checked = False
Input(6) = 0
End If
If Bit.AND(RxPlcBuffer(8), 0x4) > 0 Then
Ck3.Checked = True
Input(7) = 0xFFFF
Else
Ck3.Checked = False
Input(7) = 0
End If
If Bit.AND(RxPlcBuffer(8), 0x8) > 0 Then
Ck4.Checked = True
Input(8) = 0xFFFF
Else
Ck4.Checked = False
Input(8) = 0
End If
If Bit.AND(RxPlcBuffer(8), 0x10) > 0 Then
Ck5.Checked = True
Input(9) = 0xFFFF
Else
Ck5.Checked = False
Input(9) = 0
End If
If Bit.AND(RxPlcBuffer(8), 0x20) > 0 Then
Ck6.Checked = True
Input(10) = 0xFFFF
Else
Ck6.Checked = False
Input(10) = 0
End If
If Bit.AND(RxPlcBuffer(8), 0x40) > 0 Then
Ck7.Checked = True
Input(11) = 0xFFFF
Else
Ck7.Checked = False
Input(11) = 0
End If
If Bit.AND(RxPlcBuffer(8), 0x80) > 0 Then
Ck8.Checked = True
Input(12) = 0xFFFF
Else
Ck8.Checked = False
Input(12) = 0
End If
End Sub
Sub DecomLogitecDualAction
Dim j As Int
j = ConvertSignedByteToUnsigned(RxPlcBuffer(0))
PbX.Progress =j*100/255
j = ConvertSignedByteToUnsigned(RxPlcBuffer(1))
PbY.Progress =j*100/255
j = ConvertSignedByteToUnsigned(RxPlcBuffer(2))
PbZ.Progress =j*100/255
j = ConvertSignedByteToUnsigned(RxPlcBuffer(3))
PbR.Progress =j*100/255
If Bit.AND(RxPlcBuffer(4), 0x10) > 0 Then Ck1.checked = True Else Ck1.Checked = False
If Bit.AND(RxPlcBuffer(4), 0x20) > 0 Then Ck2.Checked = True Else Ck2.Checked = False
If Bit.AND(RxPlcBuffer(4), 0x40) > 0 Then Ck3.Checked = True Else Ck3.Checked = False
If Bit.AND(RxPlcBuffer(4), 0x80) > 0 Then Ck4.Checked = True Else Ck4.Checked = False
If Bit.AND(RxPlcBuffer(5), 0x1) > 0 Then Ck5.Checked = True Else Ck5.Checked = False
If Bit.AND(RxPlcBuffer(5), 0x2) > 0 Then Ck6.Checked = True Else Ck6.Checked = False
If Bit.AND(RxPlcBuffer(5), 0x4) > 0 Then Ck7.Checked = True Else Ck7.Checked = False
If Bit.AND(RxPlcBuffer(5), 0x8) > 0 Then Ck8.Checked = True Else Ck8.Checked = False
Select Case Bit.AND(RxPlcBuffer(4), 0x0F)
Case 0
Img1.Bitmap = (LoadBitmap(File.DirAssets, "90.jpg"))
Case 1
Img1.Bitmap =(LoadBitmap(File.DirAssets, "45.jpg"))
Case 2
Img1.Bitmap =(LoadBitmap(File.DirAssets, "0.jpg"))
Case 3
Img1.Bitmap =(LoadBitmap(File.DirAssets, "315.jpg"))
Case 4
Img1.Bitmap =(LoadBitmap(File.DirAssets, "270.jpg"))
Case 5
Img1.Bitmap =(LoadBitmap(File.DirAssets, "225.jpg"))
Case 6
Img1.Bitmap =(LoadBitmap(File.DirAssets, "180.jpg"))
Case 7
Img1.Bitmap =(LoadBitmap(File.DirAssets, "135.jpg"))
Case Else
Img1.Bitmap =(LoadBitmap(File.DirAssets, "c.jpg"))
End Select
End Sub
Sub PerformMixing
Try 'process mixing & reverse
For Each V As View In Activity
If V Is Spinner Then
Dim Sp As Spinner
Sp = V
If Sp.Tag <> "" Then
Channel(Sp.Tag) = Sp.SelectedIndex 'mixing
End If
End If
If V Is CheckBox Then
Dim Ck As CheckBox
Ck = V
If Ck.Tag <> "" Then
Reverse(Ck.Tag) = Ck.Checked 'reverse mode
End If
End If
Next
Catch
End Try
Try
For Each V As View In Activity
If V Is ProgressBar Then
Dim Pb As ProgressBar
Pb = V
If Pb.Tag <> "" Then
If Channel(Pb.Tag) < 4 Then 'joystick axis
If Reverse(Pb.Tag) Then
Output(Pb.Tag) = (0x10000 - Input(Channel(Pb.Tag)))
Else
Output(Pb.Tag) = Input(Channel(Pb.Tag))
End If
End If
If Channel(Pb.Tag) = 4 Then 'hat
If Reverse(Pb.Tag) Then
Output(Pb.Tag) = (0x10000 - Input(Channel(Pb.Tag)))
Else
Output(Pb.Tag) = Input(Channel(Pb.Tag))
End If
End If
If Channel(Pb.Tag) > 4 AND Channel(Pb.Tag) > 4 Then 'button
If Reverse(Pb.Tag) Then
Output(Pb.Tag) = (0x10000 - Input(Channel(Pb.Tag)))
Else
Output(Pb.Tag) = Input(Channel(Pb.Tag))
End If
End If
Pb.Progress = Output(Pb.Tag) *100/0x10000
End If
End If
Next
Catch
End Try
End Sub
Sub OutputPPM
Dim NbWrite As Int
If JPHIDstreams.IsInitialized Then
For i = 0 To 7
Output(i) = Output(i) * 3000 / 65535 'met à l'échelle pour le PIC 65535 => 1ms = 3000 ticks Timer0
If (Output(i) > 65535) Then Output(i) = 65535
If (Output(i) <0) Then Output(i) = 0
Next
TxPlcBuffer(0)= 0x80 '//0x80 est la commande d'envoi des paramètres au firmware
TxPlcBuffer(1) = Floor(Output(0) / 256)
TxPlcBuffer(2) = Output(0) - 256 * TxPlcBuffer(1)
TxPlcBuffer(3) = Floor(Output(1) / 256)
TxPlcBuffer(4) = Output(1) - 256 * TxPlcBuffer(3)
TxPlcBuffer(5) = Floor(Output(2) / 256)
TxPlcBuffer(6) = Output(2) - 256 * TxPlcBuffer(5)
TxPlcBuffer(7) = Floor(Output(3) / 256)
TxPlcBuffer(8) = Output(3) - 256 * TxPlcBuffer(7)
TxPlcBuffer(9) = Floor(Output(4) / 256)
TxPlcBuffer(10) = Output(4) - 256 * TxPlcBuffer(9)
TxPlcBuffer(11) = Floor(Output(5) / 256)
TxPlcBuffer(12) = Output(5) - 256 * TxPlcBuffer(11)
TxPlcBuffer(13) = Floor(Output(6) / 256)
TxPlcBuffer(14) = Output(6) - 256 * TxPlcBuffer(13)
TxPlcBuffer(15) = Floor(Output(7) / 256)
TxPlcBuffer(16) = Output(7) - 256 * TxPlcBuffer(15)
NbWrite = JPHIDstreams.BulkTransfer(JPoutEndpoint,TxPlcBuffer,JPoutEndpoint.MaxPacketSize,500)
'If NbWrite = -1 Then JPConnected = False
End If
End Sub
Sub ConvertSignedByteToUnsigned(b As Byte) As Int
Return Bit.AND(b, 0xFF)
End Sub
Sub ConnectJP
Dim usbdevices() As UsbDevice
usbdevices = manager.GetDevices
'Iterate over devices and find the correct one
For i = 0 To usbdevices.Length - 1
Dim ud As UsbDevice
ud = usbdevices(i)
Log(ud)
Log(ud.InterfaceCount)
'Iterate over interfaces
For a = 0 To ud.InterfaceCount - 1
Dim inter As UsbInterface
inter = ud.GetInterface(a)
If ud.VendorId = VID AND ud.ProductId = PID Then
'found our device and interface
JPdevice = ud
JPinterface = inter
'Find correct endpoints
For b = 0 To JPinterface.EndpointCount - 1
Dim endpoint As UsbEndpoint
Log (JPinterface.EndpointCount)
endpoint = JPinterface.GetEndpoint(b)
' And claim it
If endpoint.Type = manager.USB_ENDPOINT_XFER_INT Then
If endpoint.Direction = manager.USB_DIR_IN Then
JPinEndpoint = endpoint
Log(JPinEndpoint.MaxPacketSize)
Else If endpoint.Direction = manager.USB_DIR_OUT Then
JPoutEndpoint = endpoint
Log(JPoutEndpoint.MaxPacketSize)
End If
End If
Next
End If
Next
Next
Try
manager.RequestPermission(JPdevice)
If manager.HasPermission(JPdevice) Then
JPHIDstreams = manager.OpenDevice(JPdevice, JPinterface, True)
Log("Connected")
JPConnected = True
Bt1.Enabled = True
End If
Catch
JPConnected = False
Bt1.Enabled = False
End Try
End Sub
Sub ConnectJoystick
Dim usbdevices() As UsbDevice
usbdevices = manager.GetDevices
'Iterate over devices and find the correct one
For i = 0 To usbdevices.Length - 1
Dim ud As UsbDevice
ud = usbdevices(i)
Log(ud)
Log(ud.InterfaceCount)
'Iterate over interfaces
For a = 0 To ud.InterfaceCount - 1
Dim inter As UsbInterface
inter = ud.GetInterface(a)
If inter.InterfaceClass = 3 AND inter.InterfaceSubclass = 0 AND ud.VendorId <> VID AND ud.ProductId <> PID Then 'HID interface
'found our device and interface
device = ud
interface = inter
JoyVID = ud.VendorId
JoyPID = ud.ProductId
'Find correct endpoints
For b = 0 To interface.EndpointCount - 1
Dim endpoint As UsbEndpoint
Log (interface.EndpointCount)
endpoint = interface.GetEndpoint(b)
' And claim it
If endpoint.Type = manager.USB_ENDPOINT_XFER_INT Then
If endpoint.Direction = manager.USB_DIR_IN Then
inEndpoint = endpoint
Log(inEndpoint.MaxPacketSize)
Else If endpoint.Direction = manager.USB_DIR_OUT Then
outEndpoint = endpoint
Log(outEndpoint.MaxPacketSize)
End If
End If
Next
End If
Next
Next
Try
manager.RequestPermission(device)
If manager.HasPermission(device) Then
HIDstreams = manager.OpenDevice(device, interface, True)
JoystickConnected = True
Log("Connected")
End If
Catch
JoystickConnected = False
End Try
End Sub