Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBKeyboard.h Source File

USBKeyboard.h

00001 /*
00002  * Copyright (c) 2018-2019, Arm Limited and affiliates.
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #ifndef USBKEYBOARD_H
00019 #define USBKEYBOARD_H
00020 
00021 #include "USBHID.h"
00022 #include "platform/Stream.h"
00023 #include "PlatformMutex.h"
00024 
00025 /* Modifiers, left keys then right keys. */
00026 enum MODIFIER_KEY {
00027     KEY_CTRL = 0x01,
00028     KEY_SHIFT = 0x02,
00029     KEY_ALT = 0x04,
00030     KEY_LOGO = 0x08,
00031     KEY_RCTRL = 0x10,
00032     KEY_RSHIFT = 0x20,
00033     KEY_RALT = 0x40,
00034     KEY_RLOGO = 0x80,
00035 };
00036 
00037 
00038 enum MEDIA_KEY {
00039     KEY_NEXT_TRACK,     /*!< next Track Button */
00040     KEY_PREVIOUS_TRACK, /*!< Previous track Button */
00041     KEY_STOP,           /*!< Stop Button */
00042     KEY_PLAY_PAUSE,     /*!< Play/Pause Button */
00043     KEY_MUTE,           /*!< Mute Button */
00044     KEY_VOLUME_UP,      /*!< Volume Up Button */
00045     KEY_VOLUME_DOWN,    /*!< Volume Down Button */
00046 };
00047 
00048 enum FUNCTION_KEY {
00049     KEY_F1 = 128,   /* F1 key */
00050     KEY_F2,         /* F2 key */
00051     KEY_F3,         /* F3 key */
00052     KEY_F4,         /* F4 key */
00053     KEY_F5,         /* F5 key */
00054     KEY_F6,         /* F6 key */
00055     KEY_F7,         /* F7 key */
00056     KEY_F8,         /* F8 key */
00057     KEY_F9,         /* F9 key */
00058     KEY_F10,        /* F10 key */
00059     KEY_F11,        /* F11 key */
00060     KEY_F12,        /* F12 key */
00061 
00062     KEY_PRINT_SCREEN,   /* Print Screen key */
00063     KEY_SCROLL_LOCK,    /* Scroll lock */
00064     KEY_CAPS_LOCK,      /* caps lock */
00065     KEY_NUM_LOCK,       /* num lock */
00066     KEY_INSERT,         /* Insert key */
00067     KEY_HOME,           /* Home key */
00068     KEY_PAGE_UP,        /* Page Up key */
00069     KEY_PAGE_DOWN,      /* Page Down key */
00070 
00071     RIGHT_ARROW,        /* Right arrow */
00072     LEFT_ARROW,         /* Left arrow */
00073     DOWN_ARROW,         /* Down arrow */
00074     UP_ARROW,           /* Up arrow */
00075 };
00076 
00077 /**
00078  * \defgroup drivers_USBKeyboard USBKeyboard class
00079  * \ingroup drivers-public-api-usb
00080  * @{
00081  */
00082 
00083 /**
00084  * USBKeyboard example
00085  * @code
00086  *
00087  * #include "mbed.h"
00088  * #include "USBKeyboard.h"
00089  *
00090  * USBKeyboard key;
00091  *
00092  * int main(void)
00093  * {
00094  *   while (1) {
00095  *       key.printf("Hello World\r\n");
00096  *       wait(1);
00097  *   }
00098  * }
00099  *
00100  * @endcode
00101  *
00102  * @note Synchronization level: Thread safe
00103  */
00104 class USBKeyboard: public USBHID, public mbed::Stream {
00105 public:
00106 
00107     /**
00108     * Basic constructor
00109     *
00110     * Construct this object optionally connecting and blocking until it is ready.
00111     *
00112     * @note Do not use this constructor in derived classes.
00113     *
00114     * @param connect_blocking true to perform a blocking connect, false to start in a disconnected state
00115     * @param vendor_id Your vendor_id
00116     * @param product_id Your product_id
00117     * @param product_release Your product_release
00118     */
00119     USBKeyboard(bool connect_blocking = true, uint16_t vendor_id = 0x1235, uint16_t product_id = 0x0050, uint16_t product_release = 0x0001);
00120 
00121     /**
00122     * Fully featured constructor
00123     *
00124     * Construct this object with the supplied USBPhy and parameters. The user
00125     * this object is responsible for calling connect() or init().
00126     *
00127     * @note Derived classes must use this constructor and call init() or
00128     * connect() themselves. Derived classes should also call deinit() in
00129     * their destructor. This ensures that no interrupts can occur when the
00130     * object is partially constructed or destroyed.
00131     *
00132     * @param phy USB phy to use
00133     * @param vendor_id Your vendor_id
00134     * @param product_id Your product_id
00135     * @param product_release Your product_release
00136     */
00137     USBKeyboard(USBPhy *phy, uint16_t vendor_id = 0x1235, uint16_t product_id = 0x0050, uint16_t product_release = 0x0001);
00138 
00139     /**
00140      * Destroy this object
00141      *
00142      * Any classes which inherit from this class must call deinit
00143      * before this destructor runs.
00144      */
00145     virtual ~USBKeyboard();
00146 
00147     /**
00148     * To send a character defined by a modifier(CTRL, SHIFT, ALT) and the key
00149     *
00150     * @code
00151     * //To send CTRL + s (save)
00152     *  keyboard.key_code('s', KEY_CTRL);
00153     * @endcode
00154     *
00155     * @param modifier bit 0: KEY_CTRL, bit 1: KEY_SHIFT, bit 2: KEY_ALT (default: 0)
00156     * @param key character to send
00157     * @returns true if there is no error, false otherwise
00158     */
00159     bool key_code(uint8_t key, uint8_t modifier = 0);
00160 
00161     /**
00162     * Send a character
00163     *
00164     * @param c character to be sent
00165     * @returns true if there is no error, false otherwise
00166     */
00167     virtual int _putc(int c);
00168 
00169     /**
00170     * Control media keys
00171     *
00172     * @param key media key pressed (KEY_NEXT_TRACK, KEY_PREVIOUS_TRACK, KEY_STOP, KEY_PLAY_PAUSE, KEY_MUTE, KEY_VOLUME_UP, KEY_VOLUME_DOWN)
00173     * @returns true if there is no error, false otherwise
00174     */
00175     bool media_control(MEDIA_KEY key);
00176 
00177     /*
00178     * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
00179     *
00180     * @returns pointer to the report descriptor
00181     */
00182     virtual const uint8_t *report_desc();
00183 
00184     /*
00185     * Called when a data is received on the OUT endpoint. Useful to switch on LED of LOCK keys
00186     */
00187     virtual void report_rx();
00188 
00189     /**
00190     * Read status of lock keys. Useful to switch-on/off LEDs according to key pressed. Only the first three bits of the result is important:
00191     *   - First bit: NUM_LOCK
00192     *   - Second bit: CAPS_LOCK
00193     *   - Third bit: SCROLL_LOCK
00194     *
00195     * @returns status of lock keys
00196     */
00197     uint8_t lock_status();
00198 
00199 protected:
00200     /*
00201     * Get configuration descriptor
00202     *
00203     * @returns pointer to the configuration descriptor
00204     */
00205     virtual const uint8_t *configuration_desc(uint8_t index);
00206 
00207 private:
00208 
00209     //dummy otherwise it doesn't compile (we must define all methods of an abstract class)
00210     virtual int _getc();
00211 
00212     uint8_t _configuration_descriptor[41];
00213     uint8_t _lock_status;
00214     PlatformMutex _mutex;
00215 
00216 };
00217 
00218 /** @}*/
00219 
00220 #endif