OVERVIEW
This ADB to USB adapter project is an example of the program using Arduino Keyboard function. The programs can be downloaded in the installation page of this blog topic. The download kit includes a SimpleAdb library for Arduino IDE and an example program using the library.
Within the example program m0116.ino, functions of the SimpleAdb library and Arduino Keyboard functions are called to implement the ADB to USB adapter. The program is consisted of a main loop and a key input handler.
The key input handler is implemented in the SimpleAdb library. In the key input handler, key codes are read from ADB keyboard by polling every 11 ms. The polling sequence is triggered by a timer interrupt. The key input handler is the interrupt service routine of the timer interrupt, and the key codes read from a keyboard are stored in a type ahead buffer. The type ahead buffer is an interface between the key input handler and the main loop of the example program. The type ahead buffer is controlled by 2 pointers, a write pointer and a read pointer. The write pointer is incremented by the key input handler when it reads the key code from the keyboard and write it to the buffer. The read pointer is incremented by the main loop when it read the key code from the buffer.
In the main loop, it checks the pointers of the type ahead buffer. If the both pointer have the same count, the main loop does nothing and wait for a while until a wait timer expired. If the write pointer is bigger, it read the key code from the type ahead buffer and increments the read pointer. After reading the key code from the type ahead buffer, it converts the code to a character by using a mapping table. The converted character is sent to the host PC by using the Keyboard function of Arduino.
The timer to trigger the polling sequence of the key input handler is set in the setup function of the example program.
FUNCTIONS IN THE SimpleAdb LIBRARY
Function name | Description |
---|---|
begin | initialize the type ahead buffer and the pointers reset ADB start the polling timer enable interrupt |
available | check type ahead buffer pointers and return pending bytes count |
getKeyCode | read a word (2 bytes) from the type ahead buffer and increment the read pointer of the type ahead buffer |
FUNCTIONS IN THE EXAMPLE PROGRAM
Function name | Description |
---|---|
mapKey | implement the conversion table to map the key code to the character |
setup | initialize Arduino pin mode initialize Keyboard function initialize SimpleAdb library |
loop | wait for a while check type ahead buffer if there are no pending keys, goto start of the loop if there are the key codes in the buffer, get it convert the key code to the character by calling mapKey send the character to the PC by calling Keyboard function, press or release goto start of the loop |
KEY MAPPING OF SPECIAL KEYS
Key mappings below are defined in the function mapKey and easily modified by user. The other keys not shown below will send the character printed on the key top.
ADB key | converted key |
---|---|
option | left alt |
apple command | left gui |
caps lock | used as function key modifier |
powerkey | right alt |
delete | backspace |
keypad clear | delete |
keypad enter | return |
keypad * | main key "*" Keyboard.press will send shift + "8" |
keypad + | main key "+" Keyboard.press will send shift + "=" |
other keypad keys | same as main key |
caps lock + numeric | F1 to F10 |
caps lock + "-" | F11 |
caps lock + "=" | F12 |
caps lock + "," | End |
caps lock + "k" | Home |
caps lock + "l" | PageUp |
caps lock + "." | PageDown |