Skip to content

Commit 368caa8

Browse files
committed
Initial Version
1 parent 77e41fd commit 368caa8

File tree

5 files changed

+571
-0
lines changed

5 files changed

+571
-0
lines changed

src/CMakeLists.txt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Generated Cmake Pico project file
2+
3+
cmake_minimum_required(VERSION 3.13)
4+
5+
set(CMAKE_C_STANDARD 11)
6+
set(CMAKE_CXX_STANDARD 17)
7+
8+
# Initialise pico_sdk from installed location
9+
# (note this can come from environment, CMake cache etc)
10+
set(PICO_SDK_PATH $ENV{PICO_SDK_PATH})
11+
12+
# Pull in Raspberry Pi Pico SDK (must be before project)
13+
include(pico_sdk_import.cmake)
14+
15+
project(mouse-jiggler C CXX ASM)
16+
17+
# Initialise the Raspberry Pi Pico SDK
18+
pico_sdk_init()
19+
20+
# Add executable. Default name is the project name, version 1.0
21+
add_executable(mouse-jiggler main.c usb_descriptors.c)
22+
23+
pico_set_program_name(mouse-jiggler "mouse-jiggler")
24+
pico_set_program_version(mouse-jiggler "1.0")
25+
26+
pico_enable_stdio_uart(mouse-jiggler 1)
27+
pico_enable_stdio_usb(mouse-jiggler 0)
28+
29+
# Add the standard library to the build
30+
target_link_libraries(mouse-jiggler pico_stdlib tinyusb_device tinyusb_board)
31+
32+
pico_add_extra_outputs(mouse-jiggler)
33+
34+
# Make sure TinyUSB can find tusb_config.h
35+
target_include_directories(mouse-jiggler PUBLIC ${CMAKE_CURRENT_LIST_DIR})

src/main.c

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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+
}

src/tusb_config.h

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
#ifndef _TUSB_CONFIG_H_
27+
#define _TUSB_CONFIG_H_
28+
29+
#ifdef __cplusplus
30+
extern "C" {
31+
#endif
32+
33+
//--------------------------------------------------------------------
34+
// COMMON CONFIGURATION
35+
//--------------------------------------------------------------------
36+
37+
// defined by board.mk
38+
#ifndef CFG_TUSB_MCU
39+
#error CFG_TUSB_MCU must be defined
40+
#endif
41+
42+
// RHPort number used for device can be defined by board.mk, default to port 0
43+
#ifndef BOARD_DEVICE_RHPORT_NUM
44+
#define BOARD_DEVICE_RHPORT_NUM 0
45+
#endif
46+
47+
// RHPort max operational speed can defined by board.mk
48+
// Default to Highspeed for MCU with internal HighSpeed PHY (can be port specific), otherwise FullSpeed
49+
#ifndef BOARD_DEVICE_RHPORT_SPEED
50+
#if (CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX || \
51+
CFG_TUSB_MCU == OPT_MCU_NUC505 || CFG_TUSB_MCU == OPT_MCU_CXD56 || CFG_TUSB_MCU == OPT_MCU_SAMX7X)
52+
#define BOARD_DEVICE_RHPORT_SPEED OPT_MODE_HIGH_SPEED
53+
#else
54+
#define BOARD_DEVICE_RHPORT_SPEED OPT_MODE_FULL_SPEED
55+
#endif
56+
#endif
57+
58+
// Device mode with rhport and speed defined by board.mk
59+
#if BOARD_DEVICE_RHPORT_NUM == 0
60+
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | BOARD_DEVICE_RHPORT_SPEED)
61+
#elif BOARD_DEVICE_RHPORT_NUM == 1
62+
#define CFG_TUSB_RHPORT1_MODE (OPT_MODE_DEVICE | BOARD_DEVICE_RHPORT_SPEED)
63+
#else
64+
#error "Incorrect RHPort configuration"
65+
#endif
66+
67+
#ifndef CFG_TUSB_OS
68+
#define CFG_TUSB_OS OPT_OS_NONE
69+
#endif
70+
71+
// CFG_TUSB_DEBUG is defined by compiler in DEBUG build
72+
// #define CFG_TUSB_DEBUG 0
73+
74+
/* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment.
75+
* Tinyusb use follows macros to declare transferring memory so that they can be put
76+
* into those specific section.
77+
* e.g
78+
* - CFG_TUSB_MEM SECTION : __attribute__ (( section(".usb_ram") ))
79+
* - CFG_TUSB_MEM_ALIGN : __attribute__ ((aligned(4)))
80+
*/
81+
#ifndef CFG_TUSB_MEM_SECTION
82+
#define CFG_TUSB_MEM_SECTION
83+
#endif
84+
85+
#ifndef CFG_TUSB_MEM_ALIGN
86+
#define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4)))
87+
#endif
88+
89+
//--------------------------------------------------------------------
90+
// DEVICE CONFIGURATION
91+
//--------------------------------------------------------------------
92+
93+
#ifndef CFG_TUD_ENDPOINT0_SIZE
94+
#define CFG_TUD_ENDPOINT0_SIZE 64
95+
#endif
96+
97+
//------------- CLASS -------------//
98+
#define CFG_TUD_HID 1
99+
#define CFG_TUD_CDC 0
100+
#define CFG_TUD_MSC 0
101+
#define CFG_TUD_MIDI 0
102+
#define CFG_TUD_VENDOR 0
103+
104+
// HID buffer size Should be sufficient to hold ID (if any) + Data
105+
#define CFG_TUD_HID_EP_BUFSIZE 16
106+
107+
#ifdef __cplusplus
108+
}
109+
#endif
110+
111+
#endif /* _TUSB_CONFIG_H_ */

0 commit comments

Comments
 (0)