No0ne / hid2cdc

USB keyboard to USB CDC converter using a Raspberry Pi Pico
MIT License
6 stars 2 forks source link

could CDC be added? #1

Closed robert-rozee closed 8 months ago

robert-rozee commented 8 months ago

hi, how practical would it be to add CDC device support as a third device option, with keyboard and mouse input (instead of sent out 2x PS/2 ports) being translated to VT escape sequences and piped back out to the USB CDC serial port? my usage case is with a VGA picomite (https://geoffg.net/picomitevga.html) which can NOT talk to USB devices - it can only talk to a serial terminal emulator running on an attached host. i'm envisioning that the picomite would be attached to a USB hub (as a device) along with the keyboard and mouse, and the "ps2x2pico+CDC" would then sit there as a host translating between keyboard+mouse and the picomite.

see the following discussion thread: https://www.thebackshed.com/forum/ViewTopic.php?FID=16&TID=16529

cheers, rob :-)

No0ne commented 8 months ago

Hi, yes I think this should be possible. But since this doesn't have anything to do with PS/2 I'll start another project for this. As freetime is rare right now I don't know when I can get working on this.

robert-rozee commented 8 months ago

that would be brilliant! if there is anything i can do to help, let me know. a few folks on the backshed forum (https://www.thebackshed.com/forum/ViewTopic.php?FID=16) have already started playing around with the ps2x2pico just to do USB->PS/2 keyboard translation - finding a PS/2 keyboard these days is starting to get a tad hit-and-miss.

cheers, rob :-)

No0ne commented 8 months ago

Here is a really quick&dirty demo I cobbled together to confirm if this is what you suggested. Please try this: https://github.com/No0ne/hid2cdc/raw/main/hid2cdc.uf2

robert-rozee commented 8 months ago

i've started collecting together the necessary bits - it may take me a day or two to get it all together! in the meantime, i've started a new thread on the backshed forums here: https://www.thebackshed.com/forum/ViewTopic.php?FID=16&TID=16545 so far one other member has indicated he will also give the usb2cdc.uf2 firmware a try - hopefully a few others will also step forward.

cheers, rob :-)

robert-rozee commented 8 months ago

bigmik on TBS forums beat me to testing, here is his response on the forum...

Posted: 01:37pm 30 Dec 2023 Hi Rob, All, Well I have built it up and it is 75% working.

All letters and Numbers work Esc works Shift works Enter (both) work numpad works inc *-+/ but numlock does nothing All shift Special chrs work (ie #$%^& etc)

The following keys do not seem to work:

I am not sure of Delete insert Home end etc

Excellent so far I have option US keyboard and key arrangement looks good. Regards, Mick (the Big one)

Posted: 02:27pm 30 Dec 2023
Hi Rob, All, Probably as you expected, I tried Option keyboard UK instead of US and the key mapping remained the same (fixed at US layout).

A suggestion, possibly, for `No-one' is maybe use some link options on the HID2CDC GPIO pins to dictate regional mapping of the keyboard.

My brain is already kicking over with this new feature. (ie hiding a wireless dongle inside a piece of equipment).

Is it possible that this HID2CDC might ultimately support USB sticks (Hard drives even), although I suspect not as there surely is a lot of overhead for formats etc.

I will test a wireless USB keyboard and see what happens.

If you want to knock a piece of code to test what is transmitted from various keys and/or a mouse please send it to me and I will 'put it to the sword'.

Regards, Mick

... so it sounds like a success! probably the next priority is to get the CONTROL key working, as this will allow the picomite's onboard editor (which can be operated with just control keys) to be used to write basic programs to do further testing of function key mappings.

cheers, rob :-)

No0ne commented 8 months ago

Cool! Yeah the non-functioning keys where to be expected as I just used the HID to ASCII mapping array built into the tinyusb library which has no idea about VT100 codes. Now I've to implement my own mapping with all special keys and everything should work.

To switch different countries we could use two or three GPIO jumpers.

Also mass storage devices with xmodem transfer should be possible.

The only thing I'm not shure about is mouse support. Can you display an on-screen mouse cursor on the VGA output? When using a VT100 emulator you can always see your hosts cursor, but thats not possible here.

robert-rozee commented 8 months ago

currently there is no on-screen mouse cursor on the VGA output, but i am hopeful that Peter Mather (who ported mmbasic to the pico) can be persuaded to add it. there are defined VT escape sequences to enable mouse tracking, see: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-Mouse-Tracking the one looking most appropriate is SET_ANY_EVENT_MOUSE, that seems to enable reporting whenever the mouse pointer leaves a character cell. i'll need to investigate this further (compare how TeraTerm reacts when it is enabled), but for now it can be pushed to the bottom of the todo list.

xmodem: excellent idea, i'd not thought of that!

VT function key definitions are:

VK_UP: escape+"[A" VK_DOWN: escape+"[B" VK_RIGHT: escape+"[C" VK_LEFT: escape+"[D"

VK_HOME: escape+"[1~" VK_INSERT: escape+"[2~" VK_END: escape+"[4~" VK_PRIOR: escape+"[5~" VK_NEXT: escape+"[6~" VK_DELETE: 0x7F

VK_F1: escape+"[11~" // unshifted function keys VK_F2: escape+"[12~" VK_F3: escape+"[13~" VK_F4: escape+"[14~" VK_F5: escape+"[15~" VK_F6: escape+"[17~" VK_F7: escape+"[18~" VK_F8: escape+"[19~" VK_F9: escape+"[20~" VK_F10: escape+"[21~" VK_F11: escape+"[23~" VK_F12: escape+"[24~"

VK_F3: escape+"[25~" // shifted function keys (maps to F13 onwards) VK_F4: escape+"[26~" VK_F5: escape+"[28~" VK_F6: escape+"[29~" VK_F7: escape+"[31~" VK_F8: escape+"[32~" VK_F9: escape+"[33~" VK_F10: escape+"[34~"

cheers, rob :-)

No0ne commented 8 months ago

I uploaded another test with working arrow keys, but the F-keys are still not working 🤷🏻 (https://aperiodic.net/phil/archives/Geekery/term-function-keys/)

robert-rozee commented 8 months ago

yes, i remember a few years back looking at the function keys and finding them a bit of a dog's breakfast! the 'accepted standard' (TeraTerm) seems to be for f1 to f5 to use the X11/R6 bindings, and F6 to F12 to use VT220. then F13 to F20 is mapped onto shift-F3 to shift-f12 (still with VT220 bindings). the list i posted earlier was taken from my GFXterm emulator, which was written to work with mmbasic running on a pic32mx170.

did you get the control key working?

i'll post on the backshed forum thread that a new version of HID2CDC is up.

cheers, rob :-)

No0ne commented 8 months ago

CTRL A-Z should work now, but F-keys still no luck, I'm using your code table now.

No0ne commented 8 months ago

Ah! F-keys are working, got the wrong ASCII value there. This is the right one: https://geoffg.net/Downloads/Terminal/TerminalEscapeCodes.pdf

I think now only key repeat aka. typematic is missing, right?

robert-rozee commented 8 months ago

it is looking good!

apparently we still seem to have the DEL key mapping to nothing - it should produce 0x7F. also caps lock doesn't function, the same for num lock (although num-lock is probably far less important).

it would also be good if ctrl-[, ctrl-], ctrl-\, ctrl-/, ctrl-~ produce 0x1B, 0x1D, 0x1C, 0x1F and 0x 1E respectively, allowing all control codes to be generated from the keyboard (not sure if i have ctrl-/ and ctrl-~ correct).

for diagnostics purposes, it would also be rather handy if output was also echoed out a serial port pin (GP0).

cheers, rob :-)

No0ne commented 8 months ago

Typematic implemented, DEL is fixed, capslock works (currently only as shiftlock), numlock does nothing atm. I've mapped ctrl-space and [ and ] and \ according to: https://vt100.net/docs/vt100-ug/chapter3.html#T3-5 But I don't know which keys should produce the last two ctrl-codes RS and US. Serial debug also implemented, sends 115200n8 at GPIO0.

robert-rozee commented 8 months ago

from: https://vt100.net/docs/vt102-ug/chapter4.html

Table 4-3 VT102 Key Changes Control Code . . . . . . VT102 . . . . . . . . . . . . . Previous Terminals NUL (octal 000) . . . . CTRL Space Bar .. . . . CTRL @ RS (octal 036) . . . . . CTRL ~ . . . . . . . . . . . . CTRL ^ US (octal 037) . . . . . CTRL ? . . . . . . . . . . . . CTRL _

i'd suggest implementing both mappings, although it is more a matter of completeness - just so that every possible ascii code between 0x00 and 0x7F can be typed in.

would it be easy to also have the three key combination ctrl-alt-delete pulse a output pin low?

have posted an update to the backshed forum thread.

cheers, rob :-)

No0ne commented 8 months ago

RS/US is fixed Keycombo ctrl-alt-del would be easy, yes!

robert-rozee commented 8 months ago

bigmik has requested num-lock! and i'm keen on having ctrl-alt-delete control a reset line (GPIO3 perhaps?).

you've done an amazing programming job, and it seems to be generating quite a bit of interest amongst picomite VGA users. the HID2CDC also has applicability to the CMM2 (see https://geoffg.net/maximite.html ) that has less-than-perfect onboard USB keyboard support, but still requires a PS/2 mouse (or USB mouse via a rather expensive proprietary chip from HobbyTronics).

cheers, rob :-)

robert-rozee commented 8 months ago

scrub any urgency for num-lock, bigmik wrote: "Re. numlock, when I requested numlock it was really only for completeness, in fact the way it is at the moment the numbers are defaulted on the keypad, which is the way I would always want it to be. Can you let ‘no0ne’ know that if it is too difficult to implement numlock leave it the way it is.

Regards, Mick"

cheers, rob :-)

robert-rozee commented 8 months ago

looks like you've already implemented num-lock. am impressed!

the cleaned up code looks really good, and highly readable. one small suggestion for line 125 in hid2cdc.c, where control key codes are handled: seq[0] = key - 3;

you might like to change it to: seq[0] = 1 + key - HID_KEY_A;

cheers, rob :-)

No0ne commented 8 months ago

Ctrl-alt-del reset now works on GPIO3. Connect it straight to RUN on the picomite.

Numlock currently only toggles the led, will fix that when I implement multi-country support.

After that I think we can call it a 1.0 :D

robert-rozee commented 8 months ago

found a few small bugs:

  1. the four function key combinations shift-f1, shift-f2, shift-f11, shift-f12 all generate codes, which they should not do. ideally, they should generate nothing (not even 0x00. incidentally, other keys that have no ascii or VT code should also generate nothing, instead of 0x00.

  2. i believe the function keys f1 to f20 should not auto-repeat, although opinion on this may vary.

  3. (this next one is a bit more obscure!) if you press and hold, for instance, shift-A then the letter 'A' will auto-repeat. if you then release the shift key, then (in this example) the letter 'a' will start auto-repeating. doing the reverse behaves differently - press and holding 'a' produces a repeating letter 'a', but if you then press shift everything stops until you release the keys (no 'A's appear). the same happens for numeric keys and function keys (eg, shift-f5). on the PC i am typing this on, both cases auto-repeat, ie by pressing and releasing shift repeatedly you get 'aaaaaaaaAAAAAAAAAAAaaaaaaaaaAAAAAAAAaaaaaaAAAAAAA'. i am not sure which behavior is preferable, but whatever is chosen, it should probably be consistent.

cheers, rob :-)

robert-rozee commented 8 months ago

looks like i was mistaken over what to do with shift-f1, -f2, -f11, -f12. in fact they should behave as below:

shift-f1 -> generate same escape sequence as f11 shift-f2 -> generate same escape sequence as f12 shift-f11 -> generate same escape sequence as unshifted f11 (ie, ignore shift status) shift-f12 -> generate same escape sequence as unshifted f12 (ie, ignore shift status)

above is based upon here: https://aperiodic.net/phil/archives/Geekery/term-function-keys/ (search for text "rxvt" on page)

cheers, rob :-)

No0ne commented 8 months ago

1.: should be fixed now

  1. and 3.: will look at this how it works under DOS after fixing numlock and adding qwertz support I will make a 1.0 release

Please open a new issue for each new feature/bug you want/found, thanks!

robert-rozee commented 8 months ago

many thanks. will dig around a bit here and see how wide the use of different keyboard layouts is in the mmbasic community - will start a new issue when i have more details.

cheers, rob :-)

No0ne commented 8 months ago

2.+3. I checked under DOS with a real PS/2 keyboard: It works exactly the same, F-keys repeat and shift-A repeats 'A', releasing shift repeats 'a' and pressing in reverse 'a' stops repeating. So I don't know if I should change that?

No0ne commented 8 months ago

Numlock and QWERTZ is fixed now, last testing round before 1.0 🤞🏻

robert-rozee commented 8 months ago

just did some testing of the new version, and everything seems to work pretty well :-) i did manage to get the numlock LED stuck in the off state at one point, but that could well be a shortcoming in the keyboard i am using. at the time i was pressing random keys in quick succession to check for any glitching, and could not subsequently repeat this behaviour of the LED.

a few minor suggestions:

  1. it would be good if ctrl-alt-delete using the delete key on the numeric keypad worked even if numlock was ON. currently it only works if numlock is OFF. note - this does not affect pressing ctrl-alt-delete using the dedicated delete key - that always works.

  2. a jumper setting to control the initial state of numlock at startup would be really handy.

  3. ditto for a jumper to select keyboard language at startup. quite a few picomite VGA users are in europe and this would help with bringing a few more of them onboard for testing.

  4. would it be possible to have a longer initial delay before autorepeat kicks in? currently it is 250ms, which seems a bit quick. the vt220 keyboard has it set at 500ms. 400ms may be a good compromise, although this is largely a personal preference that is different for everyone! the current repeat rate (50ms) is good.

cheers, rob :-)

No0ne commented 8 months ago

Released 1.0! UF2 file is now only available via the Release page: https://github.com/No0ne/hid2cdc/releases (uf2 is deleted from repository!)