#include "pico/stdlib.h"
#include "bsp/board.h"
#include "tusb.h"
#include "usb_descriptors.h"
#include "hardware/gpio.h"
#define BUTTON_PIN 10 // Pin where the button is connected
// Initialize USB and set up other peripherals
void setup() {
board_init(); // Initialize the board
tusb_init(); // Initialize TinyUSB (required for USB HID)
gpio_init(BUTTON_PIN); // Initialize the button pin
gpio_set_dir(BUTTON_PIN, GPIO_IN); // Set as input
gpio_pull_down(BUTTON_PIN); // Pull down to ensure low state when not pressed
}
// Function to handle button presses and send key data to the computer
void loop() {
// Check if the USB stack is ready
tud_task(); // Process USB events
// If the HID is not ready, we can't send data
if (!tud_hid_ready()) {
return;
}
// If the button is pressed
if (gpio_get(BUTTON_PIN)) {
uint8_t keycode = 4; // Keycode for 'a' in HID
tud_hid_keyboard_report(0, 0, &keycode, 1); // Send 'a'
tud_hid_keyboard_report(0, 0, NULL, 0); // Release key
// Debounce: Wait for button release
while (gpio_get(BUTTON_PIN)) {
sleep_ms(10);
}
}
}
int main() {
setup(); // Initialize hardware and USB
while (true) {
loop(); // Continuously check for button presses and process USB events
}
}Loading
pi-pico-w
pi-pico-w