Page 1 of 1

HID Device -> Rotary Encoder Problem

Posted: Mon Jul 26, 2010 12:29 pm
by ElGreco
Hi,
i want to use several rotary encoders as an HID-device (joystick). they work quite fine if i use them as an analog axis which sends an 8bit value. but now i want them to act like a joystick-button, lets say button 1 for left- and button 2 for right turn. the problem is, that the "button is pressed"-conditions last only very shortly, so that no pc-program take notice of it. does anyone have an idea what i could do to make it work? my software is based on the redkontrol-project.
thanks in advice, best regards

Re: HID Device -> Rotary Encoder Problem

Posted: Mon Jul 26, 2010 4:33 pm
by frank26080115
two solutions:

make your host application event driven (I can't help much with this)

or

elongate the period which your button appears pressed, using a hardware timer or loop counter

Code: Select all

int pressedFor; // counter
while (1)
{
   usbPoll();
   
   if (buttonIsPressed())
   {
      pressedFor = 0; // reset counter
   }
   
   if (pressedFor < 10) // hold down button for 10 loops
   {
      report.button = 1;
      pressedFor++; // delay button release
   }
   else
   {
      report.button = 0; // 10 loops passed, release button
   }
   
   if (usbInterruptIsReady())
   {
      usbSetInterrupt(report, sizeof(report));
   }
}

Re: HID Device -> Rotary Encoder Problem

Posted: Fri Jul 30, 2010 12:49 pm
by ElGreco
Thank you very much for your answer, i'll try it using a timer.

I also tried another approach, but i had a problem there too. i used the rotary encoder as an analog axis with 8 bit for values from 0-255. works fine. but now i want to have an range from 0-359 (9 bit). i tried many varants for the device descriptor, but none of them worked properly. i set REPORT_SIZE to 9, LOGICAL_MINIMUM to 0 and LOGICAL_MAXIMUM to 359, but it won't work, the windows-joystick calibration reads only values until 255. is there any other part in the descriptor i have to change?
thanks in advice!

Re: HID Device -> Rotary Encoder Problem

Posted: Fri Jul 30, 2010 6:09 pm
by frank26080115
359? uh, i'd suggest you avoid that, stick to 0-255 and use software to multiply the number by 1.40784314

Re: HID Device -> Rotary Encoder Problem

Posted: Fri Jul 30, 2010 9:18 pm
by ElGreco
i need exactly 360 possible values, its gonna be a flight simulation device, and a circle has 360 degrees ;)

Re: HID Device -> Rotary Encoder Problem

Posted: Sat Jul 31, 2010 1:33 am
by frank26080115
ElGreco wrote:i need exactly 360 possible values, its gonna be a flight simulation device, and a circle has 360 degrees ;)

does the encoder have exactly 360 discrete values? if not, then i suggest you either use the number of discrete values per full rotation of the encoder, or stick with 256, or maybe 65536, and then process the number using host-side software

what about declaring a report main item size as 16 bit but still reporting 0-359?