A Composite device with USBSerial and USBKeyboard
Dependents: USBSerialKeyboard_Example
USBSerialKeyboard.cpp@1:9367d07d0707, 2013-03-30 (annotated)
- Committer:
- p3p
- Date:
- Sat Mar 30 12:27:31 2013 +0000
- Revision:
- 1:9367d07d0707
- Parent:
- 0:93ff83b03fd8
Initial Release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
p3p | 0:93ff83b03fd8 | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
p3p | 0:93ff83b03fd8 | 2 | * |
p3p | 0:93ff83b03fd8 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
p3p | 0:93ff83b03fd8 | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
p3p | 0:93ff83b03fd8 | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
p3p | 0:93ff83b03fd8 | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
p3p | 0:93ff83b03fd8 | 7 | * Software is furnished to do so, subject to the following conditions: |
p3p | 0:93ff83b03fd8 | 8 | * |
p3p | 0:93ff83b03fd8 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
p3p | 0:93ff83b03fd8 | 10 | * substantial portions of the Software. |
p3p | 0:93ff83b03fd8 | 11 | * |
p3p | 0:93ff83b03fd8 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
p3p | 0:93ff83b03fd8 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
p3p | 0:93ff83b03fd8 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
p3p | 0:93ff83b03fd8 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
p3p | 0:93ff83b03fd8 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
p3p | 0:93ff83b03fd8 | 17 | */ |
p3p | 0:93ff83b03fd8 | 18 | |
p3p | 0:93ff83b03fd8 | 19 | #include "stdint.h" |
p3p | 0:93ff83b03fd8 | 20 | #include "USBSerialKeyboard.h" |
p3p | 0:93ff83b03fd8 | 21 | |
p3p | 0:93ff83b03fd8 | 22 | int USBSerialKeyboard::_putc(int c) { |
p3p | 0:93ff83b03fd8 | 23 | if (!terminal_connected) |
p3p | 0:93ff83b03fd8 | 24 | return 0; |
p3p | 0:93ff83b03fd8 | 25 | send((uint8_t *)&c, 1); |
p3p | 0:93ff83b03fd8 | 26 | return 1; |
p3p | 0:93ff83b03fd8 | 27 | } |
p3p | 0:93ff83b03fd8 | 28 | |
p3p | 0:93ff83b03fd8 | 29 | int USBSerialKeyboard::_getc() { |
p3p | 0:93ff83b03fd8 | 30 | uint8_t c; |
p3p | 0:93ff83b03fd8 | 31 | while (buf.isEmpty()); |
p3p | 0:93ff83b03fd8 | 32 | buf.dequeue(&c); |
p3p | 0:93ff83b03fd8 | 33 | return c; |
p3p | 0:93ff83b03fd8 | 34 | } |
p3p | 0:93ff83b03fd8 | 35 | |
p3p | 0:93ff83b03fd8 | 36 | bool USBSerialKeyboard::EP1_OUT_callback() { |
p3p | 0:93ff83b03fd8 | 37 | uint32_t bytesRead = 0; |
p3p | 0:93ff83b03fd8 | 38 | uint8_t led[65]; |
p3p | 0:93ff83b03fd8 | 39 | USBDevice::readEP(EPINT_OUT, led, &bytesRead, MAX_HID_REPORT_SIZE); |
p3p | 0:93ff83b03fd8 | 40 | |
p3p | 0:93ff83b03fd8 | 41 | // we take led[1] because led[0] is the report ID |
p3p | 0:93ff83b03fd8 | 42 | //lock_status = led[1] & 0x07; |
p3p | 0:93ff83b03fd8 | 43 | |
p3p | 0:93ff83b03fd8 | 44 | // We activate the endpoint to be able to recceive data |
p3p | 0:93ff83b03fd8 | 45 | if (!readStart(EPINT_OUT, MAX_HID_REPORT_SIZE)) |
p3p | 0:93ff83b03fd8 | 46 | return false; |
p3p | 0:93ff83b03fd8 | 47 | return true; |
p3p | 0:93ff83b03fd8 | 48 | } |
p3p | 0:93ff83b03fd8 | 49 | |
p3p | 0:93ff83b03fd8 | 50 | bool USBSerialKeyboard::writeBlock(uint8_t * buf, uint16_t size) { |
p3p | 0:93ff83b03fd8 | 51 | if(size > MAX_PACKET_SIZE_EPBULK) { |
p3p | 0:93ff83b03fd8 | 52 | return false; |
p3p | 0:93ff83b03fd8 | 53 | } |
p3p | 0:93ff83b03fd8 | 54 | if(!send(buf, size)) { |
p3p | 0:93ff83b03fd8 | 55 | return false; |
p3p | 0:93ff83b03fd8 | 56 | } |
p3p | 0:93ff83b03fd8 | 57 | return true; |
p3p | 0:93ff83b03fd8 | 58 | } |
p3p | 0:93ff83b03fd8 | 59 | |
p3p | 0:93ff83b03fd8 | 60 | |
p3p | 0:93ff83b03fd8 | 61 | |
p3p | 0:93ff83b03fd8 | 62 | bool USBSerialKeyboard::EP2_OUT_callback() { |
p3p | 0:93ff83b03fd8 | 63 | uint8_t c[65]; |
p3p | 0:93ff83b03fd8 | 64 | uint32_t size = 0; |
p3p | 0:93ff83b03fd8 | 65 | |
p3p | 0:93ff83b03fd8 | 66 | //we read the packet received and put it on the circular buffer |
p3p | 0:93ff83b03fd8 | 67 | readEP(c, &size); |
p3p | 0:93ff83b03fd8 | 68 | for (int i = 0; i < size; i++) { |
p3p | 0:93ff83b03fd8 | 69 | buf.queue(c[i]); |
p3p | 0:93ff83b03fd8 | 70 | } |
p3p | 0:93ff83b03fd8 | 71 | |
p3p | 0:93ff83b03fd8 | 72 | //call a potential handler |
p3p | 0:93ff83b03fd8 | 73 | rx.call(); |
p3p | 0:93ff83b03fd8 | 74 | |
p3p | 0:93ff83b03fd8 | 75 | // We reactivate the endpoint to receive next characters |
p3p | 0:93ff83b03fd8 | 76 | readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK); |
p3p | 0:93ff83b03fd8 | 77 | return true; |
p3p | 0:93ff83b03fd8 | 78 | } |
p3p | 0:93ff83b03fd8 | 79 | |
p3p | 0:93ff83b03fd8 | 80 | uint8_t USBSerialKeyboard::available() { |
p3p | 0:93ff83b03fd8 | 81 | return buf.available(); |
p3p | 0:93ff83b03fd8 | 82 | } |
p3p | 0:93ff83b03fd8 | 83 | |
p3p | 0:93ff83b03fd8 | 84 | bool USBSerialKeyboard::keypress(uint8_t key, uint8_t modifier){ |
p3p | 0:93ff83b03fd8 | 85 | HID_REPORT report; |
p3p | 0:93ff83b03fd8 | 86 | |
p3p | 0:93ff83b03fd8 | 87 | report.data[0] = REPORT_ID_KEYBOARD; |
p3p | 0:93ff83b03fd8 | 88 | report.data[1] = modifier; |
p3p | 0:93ff83b03fd8 | 89 | report.data[2] = 0; |
p3p | 0:93ff83b03fd8 | 90 | report.data[3] = keymap[key].usage; |
p3p | 0:93ff83b03fd8 | 91 | report.data[4] = 0; |
p3p | 0:93ff83b03fd8 | 92 | report.data[5] = 0; |
p3p | 0:93ff83b03fd8 | 93 | report.data[6] = 0; |
p3p | 0:93ff83b03fd8 | 94 | report.data[7] = 0; |
p3p | 0:93ff83b03fd8 | 95 | report.data[8] = 0; |
p3p | 0:93ff83b03fd8 | 96 | |
p3p | 0:93ff83b03fd8 | 97 | report.length = 9; |
p3p | 0:93ff83b03fd8 | 98 | |
p3p | 0:93ff83b03fd8 | 99 | if (!USBDevice::write(EPINT_IN, report.data, report.length, MAX_HID_REPORT_SIZE)) { |
p3p | 0:93ff83b03fd8 | 100 | return false; |
p3p | 0:93ff83b03fd8 | 101 | } |
p3p | 0:93ff83b03fd8 | 102 | return true; |
p3p | 0:93ff83b03fd8 | 103 | } |
p3p | 0:93ff83b03fd8 | 104 | |
p3p | 0:93ff83b03fd8 | 105 | bool USBSerialKeyboard::keyrelease(){ |
p3p | 0:93ff83b03fd8 | 106 | |
p3p | 0:93ff83b03fd8 | 107 | HID_REPORT report; |
p3p | 0:93ff83b03fd8 | 108 | report.data[0] = REPORT_ID_KEYBOARD; |
p3p | 0:93ff83b03fd8 | 109 | report.data[1] = 0; |
p3p | 0:93ff83b03fd8 | 110 | report.data[2] = 0; |
p3p | 0:93ff83b03fd8 | 111 | report.data[3] = 0; |
p3p | 0:93ff83b03fd8 | 112 | report.data[4] = 0; |
p3p | 0:93ff83b03fd8 | 113 | report.data[5] = 0; |
p3p | 0:93ff83b03fd8 | 114 | report.data[6] = 0; |
p3p | 0:93ff83b03fd8 | 115 | report.data[7] = 0; |
p3p | 0:93ff83b03fd8 | 116 | report.data[8] = 0; |
p3p | 0:93ff83b03fd8 | 117 | report.length = 9; |
p3p | 0:93ff83b03fd8 | 118 | |
p3p | 0:93ff83b03fd8 | 119 | |
p3p | 0:93ff83b03fd8 | 120 | if (!USBDevice::write(EPINT_IN, report.data, report.length, MAX_HID_REPORT_SIZE)) { |
p3p | 0:93ff83b03fd8 | 121 | return false; |
p3p | 0:93ff83b03fd8 | 122 | } |
p3p | 0:93ff83b03fd8 | 123 | |
p3p | 0:93ff83b03fd8 | 124 | return true; |
p3p | 0:93ff83b03fd8 | 125 | } |