forked from szf2020/RP2040-PIO-USB-Host
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
73 lines (59 loc) · 2.42 KB
/
main.cpp
File metadata and controls
73 lines (59 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "Arduino.h"
#include "RP2040_PIO_USB_Host.h" // depends on https://github.com/sekigon-gonnoc/Pico-PIO-USB.git#0.5.3
// look at https://wiki.osdev.org/USB_Human_Interface_Devices for HID report format for both keyboard and mouse
// does not contain example for setting keyboard leds, look at RP2040_PIO_USB_Host.h. Pretty self explanatory
// also I want to note that I am using the earle-philhower Arduino core: https://arduino-pico.readthedocs.io
// so your implementation for using the second core probably looks different
#define USB_DP_PIN 8 // D- pin must be one more than DP, so must use pin 9 in this case
bool _keyboardUpdated = false;
bool _mouseUpdated = false;
#define MOUSE_DATA_SIZE 5
uint8_t mouseData[MOUSE_DATA_SIZE];
void setup(){
Serial.begin(115200);
}
void printKeyboardHID();
void printMouseHID();
void loop(){
if(_keyboardUpdated){
printKeyboardHID();
// do whatever you want to do with the HID data here
// data accessible via USB_Keyboard.HID_Data[] array, size is 7 bytes
// normally HID report size is 8 bytes, but second byte is reserved and normally 0, so I discarded for my purposes
// look at receiveAndProcessKeyboardHIDReport() function to change this
_keyboardUpdated = false;
}
if(_mouseUpdated){
printMouseHID();
// do whatever you want to do with the mouse data here
// data accessible via USB_Mouse.data[] array, size is 5 bytes (or mouseData, not sure why I set it up this way?)
// more some helper functions are in the PIO_USB_Mouse class
_mouseUpdated = false; // Reset flag
}
}
void setup1(){
USB_Keyboard.begin(USB_DP_PIN); // USB_Keyboard is declared in RP2040_PIO_USB_Host.h
USB_Mouse.begin(USB_DP_PIN, mouseData, MOUSE_DATA_SIZE); // USB_Mouse is declared in RP2040_PIO_USB_Host.h. We pass the array directly
}
void loop1(){ // USB device loop, must be run on dedicated core
if(USB_Keyboard.update()){
_keyboardUpdated = true;
}
if(USB_Mouse.update()){
_mouseUpdated = true;
}
}
void printKeyboardHID(){
Serial.print("Keyboard HID report: ");
for (uint16_t i = 0; i < 7; i++) {
printf("0x%02X ", USB_Keyboard.HID_Data[i]);
}
Serial.println();
}
void printMouseHID(){
Serial.print("Mouse HID report: ");
for (uint16_t i = 0; i < MOUSE_DATA_SIZE; i++) {
printf("0x%02X ", mouseData[i]); // Use mouseData instead of USB_Mouse.HID_Data
}
Serial.println();
}