Harvie / ps2dev

Arduino library to emulate PS2 keyboard/mouse
MIT License
116 stars 27 forks source link

Handle communication in interrupts #16

Open Harvie opened 3 years ago

Harvie commented 3 years ago

Maxim Devaev ( https://github.com/pikvm ) wrote this:

Hi! I am very impressed with your work on the ps2dev library. I'm the founder of the pikvm project (github.com/pikvm/pikvm), and for one of the ways to emulate HID, I use your library. My users often ask me to implement not only the PS/2 keyboard, but also the mouse, but I don't understand the PS/2 features too well.

I want to offer you a small contract. I need you to improve your library so that it uses hardware interrupts and does not use delay, so that I can use it in the main loop of my Arduino firmware. If you are interested, I will explain everything to you in detail.

Of course, I'm not asking you to do this just for me, it will be Open Source. As a payment, I can offer you money or one of my devices https://www.youtube.com/watch?v=LwsznhIBPMc (I will send this to you by mail).

What do you say?

Harvie commented 3 years ago

Hello, nice to hear that you've found useful application for ps2dev library. i really like DIY IPKVM projects, working on somewhat similar stuff myself. So you are already using ps2dev for keyboard and want to use it for mouse as well? There are several things to discuss:

1.) The mouse CAN be done, but is far from polished up. ps2dev was originaly RAW PS2 library (just send and receive bytes over ps2). i've added the keyboard_handle() infrastructure to handle the keyboard protocol to spare user from details and just press the keys. But such thing was not yet done for the mouse protocol. It can be done, but will take few hours to go through the code. Also you would probably need to run two instances of ps2dev, i've never tried it.

2.) How much time do you really need in your main loop? It seems rather weird that you don't have time to call keyboard_handle() once every 10ms... It is probably possible to setup timer interrupt to call it for you every 10ms, but that way it might affect the timing of other jobs, it is probably better to call it yourself from the loop. Also in the README i've mentioned it might be possible to attach interrupt to CLK pin, that might be even more effective. I don't really remember how PS2 works, but i beleive it has very loose requirements on the timing, so this might be easy way as well. it might even be as easy as calling attachinterrupt() to setup keyboard_handle() as ISR for CLK pin. But i don't remember the details, i need to refresh how this all even works :-)

Or do you mean to use interrupts even for bit-banging (bit timing)? That would require lots of changes and probably might be smart to fork the library for such major change. But i think it would be wise to polish the mouse API first, so after fork both of the libraries have same nice API.

I am rather busy, so i would consider the rewrite of bit timing as last resort, but yes, it is probably interresting topic and i don't say it should not be done. Perhaps you can tell me more about your overall design and what is being run in your main loop, so i can think more about it. What do you run besides of ps2dev? What kind of microcontroller do you use?

mdevaev commented 3 years ago

Sup! Thank you for answering me. Let me clarify the details. Here the firmware of the HID controller. It's based on arduino micro pro (atmega32u4). Here the main(). I use HID Project and ps2dev to provide users with different HID options (USB and PS/2).

As for periodic calls - as you can see, I already have something similar in main, but I need to know that these calls do not contain a delay and that they do not have any recommendations regarding the frequency of the call. That is, main should spin without any delays. That's why I said that it would be great to have a bitbang without delay or with the help of a periodic call that will decide internally when it's time to send the next bit, and will not call delay.

Harvie commented 3 years ago

@mdevaev BTW do you know you can use raspberry pi zero (running Linux) to act as USB mouse/keyboard? https://randomnerdtutorials.com/raspberry-pi-zero-usb-keyboard-hid/ and use GPIO on raspberry to handle the PS2 communication? But that would perhaps require ps2dev to be ported to raspberry pi (which should be relatively easy). That would remove need for separate microcontroller.

mdevaev commented 3 years ago

Yes, I've been doing this for a long time. Arduino is another way, which, however, is still needed since the core modules are imperfect and do not work well with ancient motherboards.

The GPIO has a voltage of 3.3 v while the PS/2 uses 5v. In order not to damange the GPIO, you need a level shifter. In addition, it complicates the design. The Arduino does a great job and it's a dedicated fast controller for any HID purpose. In general, I don't need a port on Raspberry, only Arduino.

mdevaev commented 3 years ago

Please let me know when you are going to do this :) Plans, etc

sgwong commented 3 years ago

@Harvie , I am really interested on this feature. I am requesting this feature on PI-KVM but @mdevaev told me that you are main developer for the ps/2 stuff. I can offer reward in cash if this request able to done quickly. Hopefully can hear your feedback on this.

Harvie commented 3 years ago

Have you seen this? https://github.com/ndusart/ps2-keyboard It seems to use interrupts rather than delays... i've mentioned that in README some time ago

mdevaev commented 3 years ago

@Harvie This library is tied to using TIMER1 without the ability to configure it, and there is no mouse support. If you don't have time, I could contact the author of that library.