Author | Albert Faber, Copyright © 2010, all rights reserved. |
Adapted-by | Joep Suijs |
Compiler | >=2.4j |
An easy to use USB HID keyboard library the USB interface. By default nothing has to be defined by the user, below an example how to use the library include usb_keyboard .. usb_keyboard_init() .. ;-- wait till USB becomes available while ( !usb_is_configured() ) loop end loop .. ;-- main loop var byte ch forever loop ; -- call the flush routine on a regular base in the main loop ; -- in order to keep the USB communicaiton alive usb_keyboard_flush() ..... if ( usb_is_configured() ) if ( pin_x ) ush_keyboard_send_key( 0x00, USB_KEYBOARD_KEY_A ) else ush_keyboard_send_key( 0x00, USB_KEYBOARD_KEY_NONE )
http://www.usb.org for USB specifications
-
const bit USB_EP1 = 1
var volatile bit _usb_keyboard_led_kana at _usb_keyboard_led_status:3
const USB_KEYBOARD_KEY_LEFT_CONTROL = 0xE0
const word USB_EP0_OUT_ADDR = ( USB_BASE_ADDRESS + 0x0010 )
const byte USB_HID_REQ_REPORT = 0x22
var volatile bit _usb_keyboard_led_caps_lock at _usb_keyboard_led_status:1
const byte USB_HID_ENDPOINT = 0x01
const USB_CONFIGURATION_DESCRIPTOR_SIZE = 0x09 + 0x09 + 0x09 + 0x07
const USB_KEYBOARD_KEY_F1 = 0x3A
const USB_KEYBOARD_KEY_U = 0x18
const USB_KEYBOARD_KEY_W = 0x1A
const USB_KEYBOARD_KEY_F7 = 0x40
const USB_KEYBOARD_KEY_5 = 0x22
const byte USB_HID_REQ_PHYSDISCR = 0x23
const USB_KEYBOARD_KEY_3 = 0x20
const byte USB_HID_REQ_HID = 0x21
const USB_KEYBOARD_KEY_RIGHT_SHIFT = 0xE5
const USB_KEYBOARD_KEY_T = 0x17
const USB_KEYBOARD_KEY_NONE = 0x00
const USB_KEYBOARD_KEY_A = 0x04
const USB_KEYBOARD_KEY_9 = 0x26
const USB_KEYBOARD_KEY_0 = 0x27
const USB_KEYBOARD_KEY_E = 0x08
const byte USB_EP2_OUT_SIZE = 8
const USB_KEYBOARD_MKEY_RIGHT_ALT = 0b_01000000
const USB_KEYBOARD_KEY_P = 0x13
var volatile bit _usb_hid_can_tx = true
const USB_KEYBOARD_KEY_COMMA = 0x85 ; JS 2011-12-18 is this correct? not 0x36?
const byte USB_HID_REPORT1[]=
const word USB_EP1_OUT_ADDR = ( USB_EP0_IN_ADDR + USB_EP0_IN_SIZE )
const USB_KEYBOARD_KEY_2 = 0x1F
const byte USB_HCR_SET_REPORT = 0x09
const USB_KEYBOARD_KEY_F2 = 0x3B
const USB_KEYBOARD_KEY_RIGHT_GUI = 0xE7
const USB_KEYBOARD_KEY_S = 0x16
const USB_KEYBOARD_KEY_4 = 0x21
const USB_KEYBOARD_KEY_F5 = 0x3E
const USB_KEYBOARD_KEY_F10 = 0x43
const USB_KEYBOARD_KEY_B = 0x05
const USB_KEYBOARD_KEY_PAGEDOWN = 0x4E
const USB_KEYBOARD_KEY_L = 0x0F
const USB_KEYBOARD_KEY_RIGHT_CONTROL = 0xE4
const byte ASCII_USB_CTRL = 0x40
const USB_KEYBOARD_KEY_I = 0x0C
const byte ASCII_USB_SHIFT = 0x80
const USB_KEYBOARD_KEY_J = 0x0D
const byte USB_EP2_IN_SIZE = 8
const USB_KEYBOARD_KEY_PAGEUP = 0x4B
const USB_KEYBOARD_KEY_F12 = 0x45
const USB_KEYBOARD_KEY_F9 = 0x42
const USB_KEYBOARD_MKEY_RIGHT_SHIFT = 0b_00100000
const USB_KEYBOARD_KEY_F8 = 0x41
var volatile byte usb_ep1in_buf[ 8 ] at USB_EP1_IN_ADDR
const USB_KEYBOARD_KEY_F6 = 0x3F
const byte USB_EP3_OUT_SIZE = 8
const USB_KEYBOARD_MKEY_RIGHT_GUI = 0b_10000000
const USB_KEYBOARD_KEY_RIGHT_ALT = 0xE6
const byte USB_EP0_OUT_SIZE = 8
const USB_KEYBOARD_KEY_7 = 0x24
const bit USB_EP2 = 0
const USB_KEYBOARD_MKEY_LEFT_ALT = 0b_00000100
const USB_KEYBOARD_KEY_Q = 0x14
const USB_KEYBOARD_KEY_O = 0x12
const USB_KEYBOARD_KEY_F4 = 0x3D
const USB_KEYBOARD_KEY_NUM_LOCK = 0x83
const USB_KEYBOARD_KEY_SPACE = 0x2C
const USB_KEYBOARD_KEY_6 = 0x23
const USB_KEYBOARD_KEY_SCROLL_LOCK = 0x84
const USB_KEYBOARD_KEY_X = 0x1B
const word USB_EP3_IN_ADDR = 0x0000
const byte ascii_to_keyscan[] = {
const byte USB_EP0_IN_SIZE = 8
const USB_KEYBOARD_KEY_K = 0x0E
const USB_KEYBOARD_KEY_1 = 0x1E
const byte USB_EP1_IN_SIZE = 8
var byte _usb_keyboard_led_status = 0x00
const byte USB_HCR_SET_PROTOCOL = 0x0B
const USB_KEYBOARD_KEY_8 = 0x25
const USB_KEYBOARD_KEY_H = 0x0B
const USB_KEYBOARD_KEY_EQUAL_SIGN = 0x86 ; JS 2011-12-18 is this correct? not 0x2E?
const word USB_EP2_IN_ADDR = 0x0000
const byte USB_HCR_GET_PROTOCOL = 0x03
const byte USB_EP1_OUT_SIZE = 8
const USB_KEYBOARD_KEY_D = 0x07
const USB_KEYBOARD_KEY_M = 0x10
const word USB_EP0_IN_ADDR = ( USB_EP0_OUT_ADDR + USB_EP0_OUT_SIZE )
var byte usb_kbd_report[8]
const USB_KEYBOARD_KEY_LEFT_ALT = 0xE2
const USB_KEYBOARD_KEY_G = 0x0A
const USB_KEYBOARD_KEY_Y = 0x1C
const byte USB_HCR_GET_REPORT = 0x01
const bit USB_EP3 = 0
const byte USB_CONFIGURATION_DESCRIPTOR[ USB_CONFIGURATION_DESCRIPTOR_SIZE ]=
const USB_KEYBOARD_MKEY_LEFT_SHIFT = 0b_00000010
const byte USB_DEVICE_DESCRIPTOR[USB_DEVICE_DESCRIPTOR_SIZE] = {
const USB_KEYBOARD_KEY_LEFT_SHIFT = 0xE1
const USB_KEYBOARD_KEY_LEFT_GUI = 0xE3
const bit USB_EP0 = 1
const word USB_EP3_OUT_ADDR = 0x0000
const USB_KEYBOARD_KEY_C = 0x06
const byte USB_STRING0[] =
const USB_KEYBOARD_KEY_N = 0x11
const word USB_EP2_OUT_ADDR = 0x0000
const USB_KEYBOARD_KEY_V = 0x19
var volatile bit _usb_keyboard_led_num_lock at _usb_keyboard_led_status:0
const USB_KEYBOARD_KEY_CAPS_LOCK = 0x82
const USB_KEYBOARD_KEY_F11 = 0x44
const byte USB_EP3_IN_SIZE = 8
const byte USB_HCR_SET_IDLE = 0x0A
const USB_KEYBOARD_KEY_F = 0x09
const USB_KEYBOARD_KEY_R = 0x15
const USB_KEYBOARD_KEY_F3 = 0x3C
const USB_KEYBOARD_KEY_Z = 0x1D
var volatile bit _usb_keyboard_led_compose at _usb_keyboard_led_status:2
const USB_KEYBOARD_MKEY_RIGHT_CTRL = 0b_00010000
const USB_KEYBOARD_MKEY_LEFT_GUI = 0b_00001000
const word USB_EP1_IN_ADDR = ( USB_EP1_OUT_ADDR + USB_EP1_OUT_SIZE )
const USB_KEYBOARD_MKEY_LEFT_CTRL = 0b_00000001
const byte USB_HCR_GET_IDLE = 0x02
usb_handle_class_request_callback()
ush_keyboard_send_key( byte in modifier_key, byte in key )
usb_handle_class_ctrl_read_callback()
usb_ep_data_in_callback(byte in end_point, word in buffer_addr, byte in byte_count)
ush_keyboard_send_ascii(byte in ascii)
usb_handle_class_ctrl_write_callback()
usb_keyboard_flush()
usb_keyboard_init()
usb_keyboard_led_num_lock'get() return bit
usb_keyboard_led_compose'get() return bit
usb_keyboard_led_caps_lock'get() return bit
usb_keyboard_led_kana'get() return bit
const bit USB_EP1 = 1
No documentation found
var volatile bit _usb_keyboard_led_kana at _usb_keyboard_led_status:3
No documentation found
const USB_KEYBOARD_KEY_LEFT_CONTROL = 0xE0
No documentation found
const word USB_EP0_OUT_ADDR = ( USB_BASE_ADDRESS + 0x0010 )
No documentation found
const byte USB_HID_REQ_REPORT = 0x22
No documentation found
var volatile bit _usb_keyboard_led_caps_lock at _usb_keyboard_led_status:1
No documentation found
const byte USB_HID_ENDPOINT = 0x01
No documentation found
const USB_CONFIGURATION_DESCRIPTOR_SIZE = 0x09 + 0x09 + 0x09 + 0x07
No documentation found
const USB_KEYBOARD_KEY_F1 = 0x3A
No documentation found
const USB_KEYBOARD_KEY_U = 0x18
No documentation found
const USB_KEYBOARD_KEY_W = 0x1A
No documentation found
const USB_KEYBOARD_KEY_F7 = 0x40
No documentation found
const USB_KEYBOARD_KEY_5 = 0x22
No documentation found
const byte USB_HID_REQ_PHYSDISCR = 0x23
No documentation found
const USB_KEYBOARD_KEY_3 = 0x20
No documentation found
const byte USB_HID_REQ_HID = 0x21
No documentation found
const USB_KEYBOARD_KEY_RIGHT_SHIFT = 0xE5
No documentation found
const USB_KEYBOARD_KEY_T = 0x17
No documentation found
const USB_KEYBOARD_KEY_NONE = 0x00
List with keyboard code definitions, note keycodes are not equivalent with the ASCII codes
const USB_KEYBOARD_KEY_A = 0x04
No documentation found
const USB_KEYBOARD_KEY_9 = 0x26
No documentation found
const USB_KEYBOARD_KEY_0 = 0x27
No documentation found
const USB_KEYBOARD_KEY_E = 0x08
No documentation found
const byte USB_EP2_OUT_SIZE = 8
No documentation found
const USB_KEYBOARD_MKEY_RIGHT_ALT = 0b_01000000
No documentation found
const USB_KEYBOARD_KEY_P = 0x13
No documentation found
var volatile bit _usb_hid_can_tx = true
No documentation found
const USB_KEYBOARD_KEY_COMMA = 0x85 ; JS 2011-12-18 is this correct? not 0x36?
No documentation found
const byte USB_HID_REPORT1[]=
No documentation found
const word USB_EP1_OUT_ADDR = ( USB_EP0_IN_ADDR + USB_EP0_IN_SIZE )
No documentation found
const USB_KEYBOARD_KEY_2 = 0x1F
No documentation found
const byte USB_HCR_SET_REPORT = 0x09
No documentation found
const USB_KEYBOARD_KEY_F2 = 0x3B
No documentation found
const USB_KEYBOARD_KEY_RIGHT_GUI = 0xE7
No documentation found
const USB_KEYBOARD_KEY_S = 0x16
No documentation found
const USB_KEYBOARD_KEY_4 = 0x21
No documentation found
const USB_KEYBOARD_KEY_F5 = 0x3E
No documentation found
const USB_KEYBOARD_KEY_F10 = 0x43
No documentation found
const USB_KEYBOARD_KEY_B = 0x05
No documentation found
const USB_KEYBOARD_KEY_PAGEDOWN = 0x4E
No documentation found
const USB_KEYBOARD_KEY_L = 0x0F
No documentation found
const USB_KEYBOARD_KEY_RIGHT_CONTROL = 0xE4
No documentation found
const byte ASCII_USB_CTRL = 0x40
No documentation found
const USB_KEYBOARD_KEY_I = 0x0C
No documentation found
const byte ASCII_USB_SHIFT = 0x80
No documentation found
const USB_KEYBOARD_KEY_J = 0x0D
No documentation found
const byte USB_EP2_IN_SIZE = 8
No documentation found
const USB_KEYBOARD_KEY_PAGEUP = 0x4B
No documentation found
const USB_KEYBOARD_KEY_F12 = 0x45
No documentation found
const USB_KEYBOARD_KEY_F9 = 0x42
No documentation found
const USB_KEYBOARD_MKEY_RIGHT_SHIFT = 0b_00100000
No documentation found
const USB_KEYBOARD_KEY_F8 = 0x41
No documentation found
var volatile byte usb_ep1in_buf[ 8 ] at USB_EP1_IN_ADDR
No documentation found
const USB_KEYBOARD_KEY_F6 = 0x3F
No documentation found
const byte USB_EP3_OUT_SIZE = 8
No documentation found
const USB_KEYBOARD_MKEY_RIGHT_GUI = 0b_10000000
No documentation found
const USB_KEYBOARD_KEY_RIGHT_ALT = 0xE6
No documentation found
const byte USB_EP0_OUT_SIZE = 8
No documentation found
const USB_KEYBOARD_KEY_7 = 0x24
No documentation found
const bit USB_EP2 = 0
No documentation found
const USB_KEYBOARD_MKEY_LEFT_ALT = 0b_00000100
No documentation found
const USB_KEYBOARD_KEY_Q = 0x14
No documentation found
const USB_KEYBOARD_KEY_O = 0x12
No documentation found
const USB_KEYBOARD_KEY_F4 = 0x3D
No documentation found
const USB_KEYBOARD_KEY_NUM_LOCK = 0x83
No documentation found
const USB_KEYBOARD_KEY_SPACE = 0x2C
No documentation found
const USB_KEYBOARD_KEY_6 = 0x23
No documentation found
const USB_KEYBOARD_KEY_SCROLL_LOCK = 0x84
No documentation found
const USB_KEYBOARD_KEY_X = 0x1B
No documentation found
const word USB_EP3_IN_ADDR = 0x0000
No documentation found
const byte ascii_to_keyscan[] = {
No documentation found
const byte USB_EP0_IN_SIZE = 8
No documentation found
const USB_KEYBOARD_KEY_K = 0x0E
No documentation found
const USB_KEYBOARD_KEY_1 = 0x1E
No documentation found
const byte USB_EP1_IN_SIZE = 8
No documentation found
var byte _usb_keyboard_led_status = 0x00
No documentation found
const byte USB_HCR_SET_PROTOCOL = 0x0B
No documentation found
const USB_KEYBOARD_KEY_8 = 0x25
No documentation found
const USB_KEYBOARD_KEY_H = 0x0B
No documentation found
const USB_KEYBOARD_KEY_EQUAL_SIGN = 0x86 ; JS 2011-12-18 is this correct? not 0x2E?
No documentation found
const word USB_EP2_IN_ADDR = 0x0000
No documentation found
const byte USB_HCR_GET_PROTOCOL = 0x03
No documentation found
const byte USB_EP1_OUT_SIZE = 8
No documentation found
const USB_KEYBOARD_KEY_D = 0x07
No documentation found
const USB_KEYBOARD_KEY_M = 0x10
No documentation found
const word USB_EP0_IN_ADDR = ( USB_EP0_OUT_ADDR + USB_EP0_OUT_SIZE )
No documentation found
var byte usb_kbd_report[8]
No documentation found
const USB_KEYBOARD_KEY_LEFT_ALT = 0xE2
No documentation found
const USB_KEYBOARD_KEY_G = 0x0A
No documentation found
const USB_KEYBOARD_KEY_Y = 0x1C
No documentation found
const byte USB_HCR_GET_REPORT = 0x01
No documentation found
const bit USB_EP3 = 0
No documentation found
const byte USB_CONFIGURATION_DESCRIPTOR[ USB_CONFIGURATION_DESCRIPTOR_SIZE ]=
No documentation found
const USB_KEYBOARD_MKEY_LEFT_SHIFT = 0b_00000010
No documentation found
const byte USB_DEVICE_DESCRIPTOR[USB_DEVICE_DESCRIPTOR_SIZE] = {
No documentation found
const USB_KEYBOARD_KEY_LEFT_SHIFT = 0xE1
No documentation found
const USB_KEYBOARD_KEY_LEFT_GUI = 0xE3
No documentation found
const bit USB_EP0 = 1
No documentation found
const word USB_EP3_OUT_ADDR = 0x0000
No documentation found
const USB_KEYBOARD_KEY_C = 0x06
No documentation found
const byte USB_STRING0[] =
No documentation found
const USB_KEYBOARD_KEY_N = 0x11
No documentation found
const word USB_EP2_OUT_ADDR = 0x0000
No documentation found
const USB_KEYBOARD_KEY_V = 0x19
No documentation found
var volatile bit _usb_keyboard_led_num_lock at _usb_keyboard_led_status:0
No documentation found
const USB_KEYBOARD_KEY_CAPS_LOCK = 0x82
No documentation found
const USB_KEYBOARD_KEY_F11 = 0x44
No documentation found
const byte USB_EP3_IN_SIZE = 8
No documentation found
const byte USB_HCR_SET_IDLE = 0x0A
No documentation found
const USB_KEYBOARD_KEY_F = 0x09
No documentation found
const USB_KEYBOARD_KEY_R = 0x15
No documentation found
const USB_KEYBOARD_KEY_F3 = 0x3C
No documentation found
const USB_KEYBOARD_KEY_Z = 0x1D
No documentation found
var volatile bit _usb_keyboard_led_compose at _usb_keyboard_led_status:2
No documentation found
const USB_KEYBOARD_MKEY_RIGHT_CTRL = 0b_00010000
No documentation found
const USB_KEYBOARD_MKEY_LEFT_GUI = 0b_00001000
No documentation found
const word USB_EP1_IN_ADDR = ( USB_EP1_OUT_ADDR + USB_EP1_OUT_SIZE )
No documentation found
const USB_KEYBOARD_MKEY_LEFT_CTRL = 0b_00000001
List with modifier keyboard code definitions
const byte USB_HCR_GET_IDLE = 0x02
No documentation found
usb_handle_class_request_callback()
No documentation found
ush_keyboard_send_key( byte in modifier_key, byte in key )
ush_keyboard_send_key - send a key code (blocking!) ush_keyboard_send_key() sends a key code toward the USB HID host the call is blocking and assumes that the HID communcation channel has been established. See also definitions for key codes (starting with USB_KEYBOARD_KEY_xxxx) and for the modifier key definitions starting with USB_KEYBOARD_MKEY_. Note that once a key code has been send, the HID host assumes that the key remains pressed, until a USB_KEYBOARD_KEY_NONE (or other key) is send towards the HID host
usb_handle_class_ctrl_read_callback()
No documentation found
usb_ep_data_in_callback(byte in end_point, word in buffer_addr, byte in byte_count)
No documentation found
ush_keyboard_send_ascii(byte in ascii)
ush_keyboard_send_ascii - send a ascii char (blocking!) Translate to keycode & use ush_keyboard_send_key() to send/release. See ush_keyboard_send_key() for details!
usb_handle_class_ctrl_write_callback()
No documentation found
usb_keyboard_flush()
usb_keyboard_flush shall be called on a regular base (millisecond range) in order to keep the USB communcation alive. Therfore it is adviced to call the usb_keyboard _flush procedure in the main loop of your application
usb_keyboard_init()
usb_keyboard_init will setup the HID USB communication with the HOST, however, this can be a time consuming process, therefore this call is non blocking. In order to check if communication with the HOST has been established, one can call the usb_is_configured() function, see usb_is_configured() documentation for additional details.
_usb_hid_tx_report( byte in hid_report_in[], byte in cnt )
_usb_hid_tx_report, internal function to send a HID report
usb_keyboard_led_num_lock'get() return bit
usb_keyboard_led_num_lock - return the keyboard LED status of the num lock LED ( 0 = off, 1 = on )
usb_keyboard_led_compose'get() return bit
usb_keyboard_led_compose - return the keyboard LED status of the compose LED ( 0 = off, 1 = on )
usb_keyboard_led_caps_lock'get() return bit
usb_keyboard_led_caps_lock - return the keyboard LED status of the caps lock LED ( 0 = off, 1 = on )
usb_keyboard_led_kana'get() return bit
usb_keyboard_led_kana - return the keyboard LED status of the kana LED ( 0 = off, 1 = on )
18f14k50 | 18f14k50_usb_hid_keyboard.jal |
18f2450 | 18f2450_usb_hid_keyboard.jal |
18f4550 | 18f4550_usb_hid_keyboard.jal |