That is normal. You will get an event only once if the joystick is not moving but then if you move it then you will have more events.
I am not sure I understand the problem
This does not work, because the loop eats up processor capacity.
I thought about executing the code while a button was pressed, but till now no succes.
Here is the code:
public static void Main()
{
USBHostController.DeviceConnectedEvent += DeviceConnectedEvent;
Thread.Sleep(Timeout.Infinite);
}
static void DeviceConnectedEvent(USBH_Device device)
{
Debug.Print("Device connected");
switch (device.TYPE)
{
case USBH_DeviceType.Serial_FTDI: // FTDI connected
Debug.Print("FTDI");
serialUSB = new USBH_SerialUSB(device, 115200, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One);
serialUSB.Open();
serialUSBThread = new Thread(SerialUSBThread);
serialUSBThread.Start();
break;
case USBH_DeviceType.Joystick:
Debug.Print("Joystick connected");
joyStick = new USBH_Joystick(device);
joyStick.JoystickXYMove += JoystickXYMove;
joyStick.JoystickButtonDown += JoystickButtonPressed;
break;
}
}
static void SerialUSBThread()
{
while (true)
{
for (int i = 0; i < 18; i++)
{
byte[] data = Encoding.UTF8.GetBytes("#" + Servo[i].ToString() + "P" + Position[i].ToString() + "T150\r");
serialUSB.Write(data, 0, data.Length);
Thread.Sleep(10);
}
}
}
static void JoystickXYMove(USBH_Joystick sender, USBH_JoystickEventArgs args)
{
if (args != null)
{
if (args.ChangedButton == 1)
{
Position[1] = Position[1] + 10;
Thread.Sleep(1000);
}
else { };
}
}
static void JoystickButtonPressed(USBH_Joystick sender, USBH_JoystickEventArgs args)
{
switch (args.ChangedButton)
{
case 0:
for (int i = 0; i < Position.Length; i++)
{
Position[i] = 1500;
}
break;
}
}
You start the “Increment” thread during the first XY event (if appropriate, of course) and when an event appears some time after saying that XY is now 0, then you stop the “Increment” thread.
static void IncrementThreadImplementation()
{
while (true)
{
Position += 10;
Thread.Sleep(10);
}
}
In your DeviceConnected event, add the following :
IncrementThread = new Thread(IncrementThreadImplementation);
and declare the thread variable globally :
static Thread IncrementThread;
Of course, all this is untested because I don’t have a joystick and my FEZ is at home (I’m at work, right now ), but I hope you will see the idea behind this.
Also, the tests about “button.pressed” or “button.released” may not suit your needs, but that’s all I’ve seen at the moment to show the method.
This does work partially. Still, it stays an event. It’s making me nuts. When I press (and keep) a button, both pressed and released get executed. Why? No idea. It does not recognize the button being pressed, it’s still a single event…
Here is the debug code which shows that it gets executed, and then gets stopped.
[quote]Device connected
Device connected
FTDI
Device connected
Joystick connected
pressed
The thread ‘’ (0x6) has exited with code 0 (0x0).[/quote]
Maybe your joystick is not sending what GHI code is expecting ?
If you can, you could try to use the joystick as a raw USB device and see what you get in the receive buffer of the device (method xxxx.TransferData() )
Here, an USB sniffer might help.
Maybe you could also check the idea of a separate thread with an USB keyboard ? Keypressed events should be similar to those of the joystick. Then, when it’s working as you want, you may try to debug the joystick code separately, if needed.
No, I don’t have one But I will try to get one from someone and make some coding this week-end.
The keyboard was here only to validate that the idea of a separate thread is ok for what you want to do, nothing else. If it works, then you can take time to see what’s happening with the joystick.
Also, if there’s a bug or an error in your code, GHI’s assembly or joystick’s data, then nothing will work as expected, no matter what you do. That’s why I suggested to check with the keyboard…
Btw, if I can get it to work with your code and my joystick, then you’re stuck Or maybe you could do the same : borrow another joystick from a friend ? Unless you don’t have friends
This thread got very confusing you you will probably not find the answer you are looking for!
Try this, on a new thread, instead of posting code, explain what you are trying to do and then someone will probably give you an outline of the right way to code it.
But no one knows it, that’s the problem… There are only single events. I press a button, button pressed event gets fired. Done. There is no event that checks if the button is still pressed. That’s all I want to know. How to “ping” the position of the XY and the button. That’s all…
Some tests with other joysticks seems mandatory, now… Investigations should be done to determine where the problem is : joystick or GHI’s code (or both ? )
Because the ButtonState property of the event args does indeed expose USBH_JoystickButtonState.Released, which would suits your needs.