[136] | 1 | /*
|
---|
| 2 | Copyright (c) 2012 Arduino. All right reserved.
|
---|
| 3 |
|
---|
| 4 | This library is free software; you can redistribute it and/or
|
---|
| 5 | modify it under the terms of the GNU Lesser General Public
|
---|
| 6 | License as published by the Free Software Foundation; either
|
---|
| 7 | version 2.1 of the License, or (at your option) any later version.
|
---|
| 8 |
|
---|
| 9 | This library is distributed in the hope that it will be useful,
|
---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
---|
| 12 | See the GNU Lesser General Public License for more details.
|
---|
| 13 |
|
---|
| 14 | You should have received a copy of the GNU Lesser General Public
|
---|
| 15 | License along with this library; if not, write to the Free Software
|
---|
| 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
| 17 | */
|
---|
| 18 |
|
---|
| 19 | #define ARDUINO_MAIN
|
---|
| 20 | #include "Arduino.h"
|
---|
| 21 |
|
---|
| 22 | #ifdef HID_ENABLED
|
---|
| 23 | const int buttonPin = 4; // input pin for pushbutton
|
---|
| 24 | int previousButtonState = HIGH; // for checking the state of a pushButton
|
---|
| 25 | int counter = 0; // button push counter
|
---|
| 26 | #endif
|
---|
| 27 |
|
---|
| 28 | void setup(void)
|
---|
| 29 | {
|
---|
| 30 | #ifdef HID_ENABLED
|
---|
| 31 | Mouse.begin();
|
---|
| 32 |
|
---|
| 33 | // make the pushButton pin an input:
|
---|
| 34 | pinMode(buttonPin, INPUT);
|
---|
| 35 | // initialize control over the keyboard:
|
---|
| 36 | Keyboard.begin();
|
---|
| 37 | #endif
|
---|
| 38 |
|
---|
| 39 | #ifdef CDC_ENABLED
|
---|
| 40 | SerialUSB.begin(115200);
|
---|
| 41 | #endif
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 |
|
---|
| 45 | void loop(void)
|
---|
| 46 | {
|
---|
| 47 | #ifdef HID_ENABLED
|
---|
| 48 | Mouse.move(1, 0, 0);
|
---|
| 49 |
|
---|
| 50 | // read the pushbutton:
|
---|
| 51 | int buttonState = digitalRead(buttonPin);
|
---|
| 52 | // if the button state has changed, and it's currently pressed:
|
---|
| 53 | if ((buttonState != previousButtonState) && (buttonState == HIGH))
|
---|
| 54 | {
|
---|
| 55 | // increment the button counter
|
---|
| 56 | counter++;
|
---|
| 57 | // type out a message
|
---|
| 58 | Keyboard.print("You pressed the button ");
|
---|
| 59 | Keyboard.print(counter);
|
---|
| 60 | Keyboard.println(" times.");
|
---|
| 61 | }
|
---|
| 62 | // save the current button state for comparison next time:
|
---|
| 63 | previousButtonState = buttonState;
|
---|
| 64 | #endif
|
---|
| 65 |
|
---|
| 66 | #ifdef CDC_ENABLED
|
---|
| 67 | if (SerialUSB.available() > 0)
|
---|
| 68 | {
|
---|
| 69 | char inChar;
|
---|
| 70 | while( -1 == (inChar = SerialUSB.read()));
|
---|
| 71 | SerialUSB.print(inChar);
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | delay(10);
|
---|
| 75 | #endif
|
---|
| 76 | }
|
---|
| 77 |
|
---|