1+ /*
2+ * The MIT License (MIT)
3+ *
4+ * Copyright (c) 2019 Ha Thach (tinyusb.org)
5+ *
6+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7+ * of this software and associated documentation files (the "Software"), to deal
8+ * in the Software without restriction, including without limitation the rights
9+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+ * copies of the Software, and to permit persons to whom the Software is
11+ * furnished to do so, subject to the following conditions:
12+ *
13+ * The above copyright notice and this permission notice shall be included in
14+ * all copies or substantial portions of the Software.
15+ *
16+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+ * THE SOFTWARE.
23+ *
24+ */
25+
26+ #include <stdlib.h>
27+ #include <stdio.h>
28+ #include <string.h>
29+
30+ #include "bsp/board.h"
31+ #include "tusb.h"
32+
33+ #include "usb_descriptors.h"
34+
35+ //--------------------------------------------------------------------+
36+ // MACRO CONSTANT TYPEDEF PROTYPES
37+ //--------------------------------------------------------------------+
38+
39+ void hid_task (void );
40+
41+ /*------------- MAIN -------------*/
42+ int main (void )
43+ {
44+ board_init ();
45+ tusb_init ();
46+
47+ while (1 )
48+ {
49+ // move the mouse
50+ tud_task ();
51+ hid_task ();
52+ }
53+
54+ return 0 ;
55+ }
56+
57+ //--------------------------------------------------------------------+
58+ // Device callbacks
59+ // Info: Set the board LED to on when working
60+ //--------------------------------------------------------------------+
61+
62+ // Invoked when device is mounted
63+ void tud_mount_cb (void )
64+ {
65+ board_led_write (true);
66+ }
67+
68+ // Invoked when usb bus is resumed
69+ void tud_resume_cb (void )
70+ {
71+ board_led_write (true);
72+ }
73+
74+ // Invoked when device is unmounted
75+ void tud_umount_cb (void )
76+ {
77+ board_led_write (false);
78+ }
79+
80+ // Invoked when usb bus is suspended
81+ void tud_suspend_cb (bool remote_wakeup_en )
82+ {
83+ board_led_write (false);
84+ }
85+
86+ //--------------------------------------------------------------------+
87+ // USB HID
88+ //--------------------------------------------------------------------+
89+
90+ static void send_hid_report (uint8_t report_id )
91+ {
92+ int randomSleep , randX , randY , randDirection ;
93+
94+ // skip if hid is not ready yet
95+ if ( !tud_hid_ready () ) return ;
96+
97+ // get the direction
98+ randX = 10 + rand () % 41 ;
99+ randY = 10 + rand () % 41 ;
100+ randDirection = rand () % 2 ;
101+ if (randDirection )
102+ {
103+ randX *= -1 ;
104+ randY *= -1 ;
105+ }
106+
107+ // move the mouse
108+ tud_hid_mouse_report (REPORT_ID_MOUSE , 0x00 , randX , randY , 0 , 0 );
109+
110+ // sleep for random number of Ms; between 100ms to 1000ms
111+ randomSleep = 100 + rand () % 901 ;
112+ sleep_ms (randomSleep );
113+ }
114+
115+ // Every 10ms, we will sent 1 report for each HID profile (keyboard, mouse etc ..)
116+ // tud_hid_report_complete_cb() is used to send the next report after previous one is complete
117+ void hid_task (void )
118+ {
119+ // Remote wakeup
120+ if ( tud_suspended () )
121+ {
122+ // Wake up host if we are in suspend mode
123+ // and REMOTE_WAKEUP feature is enabled by host
124+ tud_remote_wakeup ();
125+ }else
126+ {
127+ // Send the 1st of report chain, the rest will be sent by tud_hid_report_complete_cb()
128+ send_hid_report (REPORT_ID_KEYBOARD );
129+ }
130+ }
131+
132+ // Invoked when sent REPORT successfully to host
133+ // Application can use this to send the next report
134+ // Note: For composite reports, report[0] is report ID
135+ void tud_hid_report_complete_cb (uint8_t instance , uint8_t const * report , uint8_t len )
136+ {
137+ (void ) instance ;
138+ (void ) len ;
139+
140+ uint8_t next_report_id = report [0 ] + 1 ;
141+
142+ if (next_report_id < REPORT_ID_COUNT )
143+ {
144+ send_hid_report (next_report_id );
145+ }
146+ }
147+
148+ // Invoked when received GET_REPORT control request
149+ // Application must fill buffer report's content and return its length.
150+ // Return zero will cause the stack to STALL request
151+ uint16_t tud_hid_get_report_cb (uint8_t instance , uint8_t report_id , hid_report_type_t report_type , uint8_t * buffer , uint16_t reqlen )
152+ {
153+ return 0 ;
154+ }
155+
156+ // Invoked when received SET_REPORT control request or
157+ // received data on OUT endpoint ( Report ID = 0, Type = 0 )
158+ void tud_hid_set_report_cb (uint8_t instance , uint8_t report_id , hid_report_type_t report_type , uint8_t const * buffer , uint16_t bufsize )
159+ {
160+ return ;
161+ }
0 commit comments