View previous topic :: View next topic |
Author |
Message |
iamlost
Joined: 18 Aug 2010 Posts: 3
|
usb keyboard |
Posted: Wed Aug 18, 2010 11:33 am |
|
|
HI,
I am new with USB and I have a problem. I modified a example from ccs
usb_kbmouse2.
The issue is if I have the same code it will send the first one but not the other.
If I have 22344556 I will get only 23456 not 22344556.
When I press the button, send to the USB (keyboard) table numero
replace code ascii with code for USB.
Thank you.
Does anyone know what
Code: | static int8 tx_msg[8]={2,0,0,0,0,0,0,0};
|
means ?
My code is:
Code: |
void usb_keyboard_task(void)
{
static int8 tx_msg[8]={2,0,0,0,0,0,0,0};
if(ascii2!=0)
tx_msg[3]=(ascii2);
else
tx_msg[3]=0;
usb_put_packet(1,tx_msg,sizeof(tx_msg),USB_DTS_TOGGLE);
}
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
while (TRUE) {
if(!input(BUTTON))
{
for (i=0; i<=index; i++)
{
ascii=(numero[i]);
switch (ascii)
{
case 0x30 : ascii2=0x62;
break;
case 0x31 : ascii2=0x59;
break;
case 0x32 : ascii2=0x5a;
break;
case 0x33 : ascii2=0x5b;
break;
case 0x34 : ascii2=0x5c;
break;
case 0x35 : ascii2=0x5d;
break;
case 0x36 : ascii2=0x5e;
break;
case 0x37 : ascii2=0x5f;
break;
case 0x38 : ascii2=0x60;
break;
case 0x39 : ascii2=0x61;
break;
case 0x2e : ascii2=0x63;
break;
case 0x0d : ascii2=0x58;
break;
default : ascii2=0x00;
}
usb_keyboard_task();
usb_rx_task();
delay_ms(10);
}
delay_ms(100);
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Aug 18, 2010 5:56 pm |
|
|
Quote: |
Does anyone know what
static int8 tx_msg[8]={2,0,0,0,0,0,0,0};
means ?
|
Look in this file:
Quote: | c:\program files\picc\examples\ex_usb_kbmouse2.c |
Find the usb_keyboard_task() function declaration. In the comments
just above, it gives the complete explanation of each byte in tx_msg[]. |
|
|
pmuldoon
Joined: 26 Sep 2003 Posts: 218 Location: Northern Indiana
|
|
Posted: Thu Aug 19, 2010 5:48 am |
|
|
is it the STATIC that is confusing you?
That just allows you to create an array as a local, but it reserves the memory space so it can't be reused by other locals from other functions.
Think of it as a global that is only visible within this function.
That way you can preload the default data and just change the byte that needs changing with each call.
Now my question:
Why the ascii2 translation?
Apparently I don't know much about keyboards. At first I thought that sent data would be ASCII, but maybe it's not. So then I vaguely remembered something about scancodes, but when I googled some charts, those numbers didn't seem to work either. I'm sure I'm missing some obvious thing, but how does 4 = 'a' in the example code?
also a suggestion for readability:
is the same as:
and is much easier to follow.
you can also use '.' for the decimal. |
|
|
pmuldoon
Joined: 26 Sep 2003 Posts: 218 Location: Northern Indiana
|
|
Posted: Thu Aug 19, 2010 6:07 am |
|
|
Nevermind. I found my answer in a rather wordy Microsoft document.
There is something called a 'Usage Index' that correlates to the 4='a' thing,
0x62=keypad 0, etc.
Give a man a fish and you feed him for a day, teach a man to fish and you get him out of the house every weekend! |
|
|
iamlost
Joined: 18 Aug 2010 Posts: 3
|
|
Posted: Thu Aug 19, 2010 9:52 am |
|
|
Yes translation is because I get data from rs232 and I send back to USB
and 30(ascii) from rs232 is "0" but for usb Keyboard is something else.
I still have the same issue with 2 or more same number.
Thank you for your help. |
|
|
pmuldoon
Joined: 26 Sep 2003 Posts: 218 Location: Northern Indiana
|
|
Posted: Thu Aug 19, 2010 10:12 am |
|
|
Can you post a complete, simple program to demonstrate the problem?
I don't see anything that would cause repeated values to not be sent.
It could be something at the other end, also.
You might want to play with putting a very long delay in or even sending a 0 between characters just to see what happens.
I don't understand what you're trying to do with this, either:
Code: |
if(ascii2!=0)
tx_msg[3]=(ascii2);
else
tx_msg[3]=0;
|
|
|
|
pmuldoon
Joined: 26 Sep 2003 Posts: 218 Location: Northern Indiana
|
|
Posted: Thu Aug 19, 2010 10:15 am |
|
|
guess i should've previewed:
Can you post a complete, simple program to demonstrate the problem?
I don't see anything that would cause repeated values to not be sent.
It could be something at the other end, also.
You might want to play with putting a very long delay in or even sending a 0 between characters just to see what happens.
I don't understand what you're trying to do with this, either:
Code: |
if(ascii2!=0)
tx_msg[3]=(ascii2);
else
tx_msg[3]=0;
|
|
|
|
iamlost
Joined: 18 Aug 2010 Posts: 3
|
|
Posted: Thu Aug 26, 2010 8:14 am |
|
|
I understand the (static of int8 tx_msg) but what I still don't understand is
the first 2 and the other 0 do. I checked the file:
c:\program files\picc\examples\ex_usb_kbmouse2.c
And again when I have the same number in a row I got only one.
If I have 0.2223455 I get 0.2345 only.
My code is like that:
Code: |
void usb_keyboard_task(void)
{
static int8 tx_msg[8]={2,0,0,0,0,0,0,0};
tx_msg[3]=(ascii2);
usb_put_packet(1,tx_msg,sizeof(tx_msg),USB_DTS_TOGGLE);
}
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
while (TRUE) {
if(!input(BUTTON))
{
for (i=0; i<=index; i++)
{
ascii=(numero[i]);
switch (ascii)
{
case 0x30 : ascii2=0x62;
break;
case 0x31 : ascii2=0x59;
break;
case 0x32 : ascii2=0x5a;
break;
case 0x33 : ascii2=0x5b;
break;
case 0x34 : ascii2=0x5c;
break;
case 0x35 : ascii2=0x5d;
break;
case 0x36 : ascii2=0x5e;
break;
case 0x37 : ascii2=0x5f;
break;
case 0x38 : ascii2=0x60;
break;
case 0x39 : ascii2=0x61;
break;
case 0x2e : ascii2=0x63;
break;
case 0x0d : ascii2=0x58;
break;
default : ascii2=0x00;
}
usb_keyboard_task();
usb_rx_task();
delay_ms(10);
}
delay_ms(100);
} |
|
|
|
pmuldoon
Joined: 26 Sep 2003 Posts: 218 Location: Northern Indiana
|
|
Posted: Thu Aug 26, 2010 8:53 am |
|
|
I don't really know much about usb.
You might take a look at this for a better description of the field values.
http://www.usb.org/developers/devclass_docs/HID1_11.pdf
Maybe the USB disregards duplicate messages? Did you try sending a zero (not the character '0') or null message between characters? |
|
|
cvargcal
Joined: 17 Feb 2015 Posts: 134
|
|
Posted: Thu Oct 04, 2018 6:50 pm |
|
|
Hi, I have the same problem.... someone solution? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Fri Oct 05, 2018 12:25 am |
|
|
The key is that the behaviour is 'scan based'.
The standard PC keyboard sends a code to say 'this key pressed', and another to say 'this key released'. Now if you send 'this key pressed', followed by a second code saying that the same key is pressed, the PC will ignore the second code (after all it is the same key, and it hasn't been released).
So if sending successive keys, if the code you want is the same as the last one sent, you have to send a 'no key' code between (0).
You can either just always send a 0 after each key code, or check if the code is the same as the last one, and just send the 0 when this happens. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Fri Oct 05, 2018 7:10 am |
|
|
Mr T is 100% right, again ! PC KBD code is well documented and you really should read up about 'how it works' especially if you want to control the KBDs LEDs. Yeesh 20 years ago I did a PC KBD to ASCII convertor using the PIC16C84. Still have them running today !
Jay |
|
|
|