Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBMouseKeyboard.h Source File

USBMouseKeyboard.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 USBMOUSEKEYBOARD_H
00019 #define USBMOUSEKEYBOARD_H
00020 
00021 #define REPORT_ID_KEYBOARD 1
00022 #define REPORT_ID_MOUSE 2
00023 #define REPORT_ID_VOLUME 3
00024 
00025 #include "USBMouse.h"
00026 #include "USBKeyboard.h"
00027 #include "platform/Stream.h"
00028 #include "USBHID.h"
00029 #include "PlatformMutex.h"
00030 
00031 /**
00032  * \defgroup drivers_USBMouseKeyboard USBMouseKeyboard class
00033  * \ingroup drivers-public-api-usb
00034  * @{
00035  */
00036 
00037 /**
00038  * USBMouseKeyboard example
00039  * @code
00040  *
00041  * #include "mbed.h"
00042  * #include "USBMouseKeyboard.h"
00043  *
00044  * USBMouseKeyboard key_mouse;
00045  *
00046  * int main(void)
00047  * {
00048  *   while(1)
00049  *   {
00050  *       key_mouse.move(20, 0);
00051  *       key_mouse.printf("Hello From MBED\r\n");
00052  *       wait(1);
00053  *   }
00054  * }
00055  * @endcode
00056  *
00057  *
00058  * @code
00059  *
00060  * #include "mbed.h"
00061  * #include "USBMouseKeyboard.h"
00062  *
00063  * USBMouseKeyboard key_mouse(ABS_MOUSE);
00064  *
00065  * int main(void)
00066  * {
00067  *   while(1)
00068  *   {
00069  *       key_mouse.move(X_MAX_ABS/2, Y_MAX_ABS/2);
00070  *       key_mouse.printf("Hello from MBED\r\n");
00071  *       wait(1);
00072  *   }
00073  * }
00074  * @endcode
00075  *
00076  * @note Synchronization level: Thread safe
00077  */
00078 class USBMouseKeyboard: public USBHID, public mbed::Stream {
00079 public:
00080 
00081     /**
00082     * Basic constructor
00083     *
00084     * Construct this object optionally connecting and blocking until it is ready.
00085     *
00086     * @note Do not use this constructor in derived classes.
00087     *
00088     * @param connect_blocking true to perform a blocking connect, false to start in a disconnected state
00089     * @param mouse_type Mouse type: ABS_MOUSE (absolute mouse) or REL_MOUSE (relative mouse) (default: REL_MOUSE)
00090     * @param vendor_id Your vendor_id (default: 0x1234)
00091     * @param product_id Your product_id (default: 0x0001)
00092     * @param product_release Your preoduct_release (default: 0x0001)
00093     *
00094     */
00095     USBMouseKeyboard(bool connect_blocking = true, MOUSE_TYPE mouse_type = REL_MOUSE, uint16_t vendor_id = 0x0021, uint16_t product_id = 0x0011, uint16_t product_release = 0x0001);
00096 
00097     /**
00098     * Fully featured constructor
00099     *
00100     * Construct this object with the supplied USBPhy and parameters. The user
00101     * this object is responsible for calling connect() or init().
00102     *
00103     * @note Derived classes must use this constructor and call init() or
00104     * connect() themselves. Derived classes should also call deinit() in
00105     * their destructor. This ensures that no interrupts can occur when the
00106     * object is partially constructed or destroyed.
00107     *
00108     * @param phy USB phy to use
00109     * @param mouse_type Mouse type: ABS_MOUSE (absolute mouse) or REL_MOUSE (relative mouse) (default: REL_MOUSE)
00110     * @param vendor_id Your vendor_id (default: 0x1234)
00111     * @param product_id Your product_id (default: 0x0001)
00112     * @param product_release Your preoduct_release (default: 0x0001)
00113     *
00114     */
00115     USBMouseKeyboard(USBPhy *phy, MOUSE_TYPE mouse_type = REL_MOUSE, uint16_t vendor_id = 0x0021, uint16_t product_id = 0x0011, uint16_t product_release = 0x0001);
00116 
00117     /**
00118      * Destroy this object
00119      *
00120      * Any classes which inherit from this class must call deinit
00121      * before this destructor runs.
00122      */
00123     virtual ~USBMouseKeyboard();
00124 
00125     /**
00126     * Write a state of the mouse
00127     *
00128     * @param x x-axis position
00129     * @param y y-axis position
00130     * @param buttons buttons state (first bit represents MOUSE_LEFT, second bit MOUSE_RIGHT and third bit MOUSE_MIDDLE)
00131     * @param z wheel state (>0 to scroll down, <0 to scroll up)
00132     * @returns true if there is no error, false otherwise
00133     */
00134     bool update(int16_t x, int16_t y, uint8_t buttons, int8_t z);
00135 
00136 
00137     /**
00138     * Move the cursor to (x, y)
00139     *
00140     * @param x x-axis position
00141     * @param y y-axis position
00142     * @returns true if there is no error, false otherwise
00143     */
00144     bool move(int16_t x, int16_t y);
00145 
00146     /**
00147     * Press one or several buttons
00148     *
00149     * @param button button state (ex: press(MOUSE_LEFT))
00150     * @returns true if there is no error, false otherwise
00151     */
00152     bool press(uint8_t button);
00153 
00154     /**
00155     * Release one or several buttons
00156     *
00157     * @param button button state (ex: release(MOUSE_LEFT))
00158     * @returns true if there is no error, false otherwise
00159     */
00160     bool release(uint8_t button);
00161 
00162     /**
00163     * Double click (MOUSE_LEFT)
00164     *
00165     * @returns true if there is no error, false otherwise
00166     */
00167     bool doubleClick();
00168 
00169     /**
00170     * Click
00171     *
00172     * @param button state of the buttons ( ex: clic(MOUSE_LEFT))
00173     * @returns true if there is no error, false otherwise
00174     */
00175     bool click(uint8_t button);
00176 
00177     /**
00178     * Scrolling
00179     *
00180     * @param z value of the wheel (>0 to go down, <0 to go up)
00181     * @returns true if there is no error, false otherwise
00182     */
00183     bool scroll(int8_t z);
00184 
00185     /**
00186     * To send a character defined by a modifier(CTRL, SHIFT, ALT) and the key
00187     *
00188     * @code
00189     * //To send CTRL + s (save)
00190     *  keyboard.keyCode('s', KEY_CTRL);
00191     * @endcode
00192     *
00193     * @param modifier bit 0: KEY_CTRL, bit 1: KEY_SHIFT, bit 2: KEY_ALT (default: 0)
00194     * @param key character to send
00195     * @returns true if there is no error, false otherwise
00196     */
00197     bool key_code(uint8_t key, uint8_t modifier = 0);
00198 
00199     /**
00200     * Send a character
00201     *
00202     * @param c character to be sent
00203     * @returns true if there is no error, false otherwise
00204     */
00205     virtual int _putc(int c);
00206 
00207     /**
00208     * Control media keys
00209     *
00210     * @param key media key pressed (KEY_NEXT_TRACK, KEY_PREVIOUS_TRACK, KEY_STOP, KEY_PLAY_PAUSE, KEY_MUTE, KEY_VOLUME_UP, KEY_VOLUME_DOWN)
00211     * @returns true if there is no error, false otherwise
00212     */
00213     bool media_control(MEDIA_KEY key);
00214 
00215     /**
00216     * 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:
00217     *   - First bit: NUM_LOCK
00218     *   - Second bit: CAPS_LOCK
00219     *   - Third bit: SCROLL_LOCK
00220     *
00221     * @returns status of lock keys
00222     */
00223     uint8_t lock_status();
00224 
00225     /*
00226     * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
00227     *
00228     * @returns pointer to the report descriptor
00229     */
00230     virtual const uint8_t *report_desc();
00231 
00232     /*
00233     * Called when a data is received on the OUT endpoint. Useful to switch on LED of LOCK keys
00234     *
00235     * @returns if handle by subclass, return true
00236     */
00237     virtual void report_rx();
00238 
00239 
00240 private:
00241     MOUSE_TYPE _mouse_type;
00242     uint8_t _button;
00243     uint8_t _lock_status;
00244     PlatformMutex _mutex;
00245 
00246     bool _mouse_send(int8_t x, int8_t y, uint8_t buttons, int8_t z);
00247 
00248     //dummy otherwise it doesn't compile (we must define all methods of an abstract class)
00249     virtual int _getc();
00250 };
00251 
00252 /** @}*/
00253 
00254 #endif