HID/examples/Mouse/Mouse.ino

64 lines
1.7 KiB
Arduino
Raw Normal View History

2014-06-03 15:10:40 +00:00
/*
Copyright (c) 2014 NicoHood
See the readme for credit to other people.
Mouse example
2014-06-21 22:32:31 +00:00
Press a button to click with mouse. See official documentation for more infos
2014-06-03 15:10:40 +00:00
*/
2014-06-05 20:54:29 +00:00
// not needed for Leonardo/Micro
2014-06-03 15:10:40 +00:00
#include <HID.h>
2014-06-05 20:54:29 +00:00
// for Leonardo/Micro: make sure to activate desired USB functions in HID_Reports.h
2014-06-03 15:10:40 +00:00
const int pinLed = 13;
const int pinButton = 8;
void setup() {
pinMode(pinLed, OUTPUT);
pinMode(pinButton, INPUT_PULLUP);
// Starts Serial at baud 115200. end just ends the Serial
// Make sure to end your special HIDs before, this does not clear them!
// You need this baud for the HID library but still can use other bauds
// without HID functions.
2014-06-05 20:54:29 +00:00
// not needed for Leonardo/Micro, Serial will not be set
2014-06-03 15:10:40 +00:00
HID.begin();
// Sends a clean report to the host. This is important because
// the 16u2 of the Uno/Mega is not turned off while programming
// so you want to start with a clear report to avoid strange bugs.
// its exactly the same like the end() function.
// You can also unplug the device if anything goes wrong.
// To prevent the 16u2 to send more reports just pull the Serial TX (pin1) low
// or see readme for turning off HID functions.
// If you did anything wrong (keyboard is doing weird stuff)
// just logout (no shutdown needed).
Mouse.begin();
}
void loop() {
if (!digitalRead(pinButton)) {
digitalWrite(pinLed, HIGH);
// Same use as the official library, pretty much self explaining
Mouse.click();
Serial.println("Serial port is still working and not glitching out");
// simple debounce
delay(300);
digitalWrite(pinLed, LOW);
}
}
/*
Definitions:
MOUSE_LEFT
MOUSE_RIGHT
MOUSE_MIDDLE
MOUSE_PREV
MOUSE_NEXT
*/