Skip to content

Commit 3edc5d6

Browse files
committed
[sam] added some USB examples
1 parent 7e76ca7 commit 3edc5d6

File tree

7 files changed

+569
-0
lines changed

7 files changed

+569
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
Keyboard logout
3+
4+
This sketch demonstrates the Keyboard library.
5+
6+
When you connect pin 2 to ground, it performs a logout.
7+
It uses keyboard combinations to do this, as follows:
8+
9+
On Windows, CTRL-ALT-DEL followed by ALT-l
10+
On Ubuntu, CTRL-ALT-DEL, and ENTER
11+
On OSX, CMD-SHIFT-q
12+
13+
To wake: Spacebar.
14+
15+
Circuit:
16+
* Arduino Leonardo
17+
* wire to connect D2 to ground.
18+
19+
created 6 Mar 2012
20+
modified 27 Mar 2012
21+
by Tom Igoe
22+
23+
This example is in the public domain
24+
25+
http://www.arduino.cc/en/Tutorial/KeyboardLogout
26+
*/
27+
28+
#define OSX 0
29+
#define WINDOWS 1
30+
#define UBUNTU 2
31+
32+
// change this to match your platform:
33+
int platform = OSX;
34+
35+
void setup() {
36+
// make pin 2 an input and turn on the
37+
// pullup resistor so it goes high unless
38+
// connected to ground:
39+
pinMode(2, INPUT_PULLUP);
40+
Keyboard.begin();
41+
}
42+
43+
void loop() {
44+
while (digitalRead(2) == HIGH) {
45+
// do nothing until pin 2 goes low
46+
delay(500);
47+
}
48+
delay(1000);
49+
50+
switch (platform) {
51+
case OSX:
52+
Keyboard.press(KEY_LEFT_GUI);
53+
// Shift-Q logs out:
54+
Keyboard.press(KEY_LEFT_SHIFT);
55+
Keyboard.press('Q');
56+
delay(100);
57+
Keyboard.releaseAll();
58+
// enter:
59+
Keyboard.write(KEY_RETURN);
60+
break;
61+
case WINDOWS:
62+
// CTRL-ALT-DEL:
63+
Keyboard.press(KEY_LEFT_CTRL);
64+
Keyboard.press(KEY_LEFT_ALT);
65+
Keyboard.press(KEY_DELETE);
66+
delay(100);
67+
Keyboard.releaseAll();
68+
//ALT-s:
69+
delay(2000);
70+
Keyboard.press(KEY_LEFT_ALT);
71+
Keyboard.press('l');
72+
Keyboard.releaseAll();
73+
break;
74+
case UBUNTU:
75+
// CTRL-ALT-DEL:
76+
Keyboard.press(KEY_LEFT_CTRL);
77+
Keyboard.press(KEY_LEFT_ALT);
78+
Keyboard.press(KEY_DELETE);
79+
delay(1000);
80+
Keyboard.releaseAll();
81+
// Enter to confirm logout:
82+
Keyboard.write(KEY_RETURN);
83+
break;
84+
}
85+
// do nothing:
86+
while(true);
87+
}
88+
89+
90+
91+
92+
93+
94+
95+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Keyboard Button test
3+
4+
Sends a text string when a button is pressed.
5+
6+
The circuit:
7+
* pushbutton attached from pin 2 to +5V
8+
* 10-kilohm resistor attached from pin 4 to ground
9+
10+
created 24 Oct 2011
11+
modified 27 Mar 2012
12+
by Tom Igoe
13+
14+
This example code is in the public domain.
15+
16+
http://www.arduino.cc/en/Tutorial/KeyboardButton
17+
*/
18+
19+
const int buttonPin = 2; // input pin for pushbutton
20+
int previousButtonState = HIGH; // for checking the state of a pushButton
21+
int counter = 0; // button push counter
22+
23+
void setup() {
24+
// make the pushButton pin an input:
25+
pinMode(buttonPin, INPUT);
26+
// initialize control over the keyboard:
27+
Keyboard.begin();
28+
}
29+
30+
void loop() {
31+
// read the pushbutton:
32+
int buttonState = digitalRead(buttonPin);
33+
// if the button state has changed,
34+
if ((buttonState != previousButtonState)
35+
// and it's currently pressed:
36+
&& (buttonState == HIGH)) {
37+
// increment the button counter
38+
counter++;
39+
// type out a message
40+
Keyboard.print("You pressed the button ");
41+
Keyboard.print(counter);
42+
Keyboard.println(" times.");
43+
}
44+
// save the current button state for comparison next time:
45+
previousButtonState = buttonState;
46+
}
47+
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
Arduino Programs Blink
3+
4+
This sketch demonstrates the Keyboard library.
5+
6+
When you connect pin 2 to ground, it creates a new
7+
window with a key combination (CTRL-N),
8+
then types in the Blink sketch, then auto-formats the text
9+
using another key combination (CTRL-T), then
10+
uploads the sketch to the currently selected Arduino using
11+
a final key combination (CTRL-U).
12+
13+
Circuit:
14+
* Arduino Leonardo
15+
* wire to connect D2 to ground.
16+
17+
created 5 Mar 2012
18+
modified 29 Mar 2012
19+
by Tom Igoe
20+
21+
This example is in the public domain
22+
23+
http://www.arduino.cc/en/Tutorial/KeyboardReprogram
24+
*/
25+
26+
// use this option for OSX.
27+
// Comment it out if using Windows or Linux:
28+
char ctrlKey = KEY_LEFT_GUI;
29+
// use this option for Windows and Linux.
30+
// leave commented out if using OSX:
31+
// char ctrlKey = KEY_LEFT_CTRL;
32+
33+
34+
void setup() {
35+
// make pin 2 an input and turn on the
36+
// pullup resistor so it goes high unless
37+
// connected to ground:
38+
pinMode(2, INPUT_PULLUP);
39+
// initialize control over the keyboard:
40+
Keyboard.begin();
41+
}
42+
43+
void loop() {
44+
while (digitalRead(2) == HIGH) {
45+
// do nothing until pin 2 goes low
46+
delay(500);
47+
}
48+
delay(1000);
49+
// new document:
50+
Keyboard.press(ctrlKey);
51+
Keyboard.press('n');
52+
delay(100);
53+
Keyboard.releaseAll();
54+
// wait for new window to open:
55+
delay(1000);
56+
57+
// Type out "blink":
58+
Keyboard.println("void setup() {");
59+
Keyboard.println("pinMode(13, OUTPUT);");
60+
Keyboard.println("}");
61+
Keyboard.println();
62+
Keyboard.println("void loop() {");
63+
Keyboard.println("digitalWrite(13, HIGH);");
64+
Keyboard.print("delay(3000);");
65+
// 3000 ms is too long. Delete it:
66+
for (int keystrokes=0; keystrokes < 6; keystrokes++) {
67+
delay(500);
68+
Keyboard.write(KEY_BACKSPACE);
69+
}
70+
// make it 1000 instead:
71+
Keyboard.println("1000);");
72+
Keyboard.println("digitalWrite(13, LOW);");
73+
Keyboard.println("delay(1000);");
74+
Keyboard.println("}");
75+
// tidy up:
76+
Keyboard.press(ctrlKey);
77+
Keyboard.press('t');
78+
delay(100);
79+
Keyboard.releaseAll();
80+
delay(3000);
81+
// upload code:
82+
Keyboard.press(ctrlKey);
83+
Keyboard.press('u');
84+
delay(100);
85+
Keyboard.releaseAll();
86+
87+
// wait for the sweet oblivion of reprogramming:
88+
while(true);
89+
}
90+
91+
92+
93+
94+
95+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Keyboard test
3+
4+
Reads a byte from the serial port, sends a keystroke back.
5+
The sent keystroke is one higher than what's received, e.g.
6+
if you send a, you get b, send A you get B, and so forth.
7+
8+
The circuit:
9+
* none
10+
11+
created 21 Oct 2011
12+
modified 27 Mar 2012
13+
by Tom Igoe
14+
15+
This example code is in the public domain.
16+
17+
http://www.arduino.cc/en/Tutorial/KeyboardSerial
18+
*/
19+
20+
void setup() {
21+
// open the serial port:
22+
Serial.begin(9600);
23+
// initialize control over the keyboard:
24+
Keyboard.begin();
25+
}
26+
27+
void loop() {
28+
// check for incoming serial data:
29+
if (Serial.available() > 0) {
30+
// read incoming serial data:
31+
char inChar = Serial.read();
32+
// Type the next ASCII value from what you received:
33+
Keyboard.write(inChar+1);
34+
}
35+
}
36+
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
2+
/*
3+
KeyboardAndMouseControl
4+
5+
Controls the mouse from five pushbuttons on an Arduino Leonardo.
6+
7+
Hardware:
8+
* 5 pushbuttons attached to D2, D3, D4, D5, D6
9+
10+
11+
The mouse movement is always relative. This sketch reads
12+
four pushbuttons, and uses them to set the movement of the mouse.
13+
14+
WARNING: When you use the Mouse.move() command, the Arduino takes
15+
over your mouse! Make sure you have control before you use the mouse commands.
16+
17+
created 15 Mar 2012
18+
modified 27 Mar 2012
19+
by Tom Igoe
20+
21+
this code is in the public domain
22+
23+
*/
24+
25+
// set pin numbers for the five buttons:
26+
27+
// set pin numbers for the five buttons:
28+
const int upButton = 2;
29+
const int downButton = 3;
30+
const int leftButton = 4;
31+
const int rightButton = 5;
32+
const int mouseButton = 6;
33+
34+
void setup() { // initialize the buttons' inputs:
35+
pinMode(upButton, INPUT);
36+
pinMode(downButton, INPUT);
37+
pinMode(leftButton, INPUT);
38+
pinMode(rightButton, INPUT);
39+
pinMode(mouseButton, INPUT);
40+
41+
Serial.begin(9600);
42+
// initialize mouse control:
43+
Mouse.begin();
44+
Keyboard.begin();
45+
}
46+
47+
void loop() {
48+
// use serial input to control the mouse:
49+
if (Serial.available() > 0) {
50+
char inChar = Serial.read();
51+
52+
switch (inChar) {
53+
case 'u':
54+
// move mouse up
55+
Mouse.move(0, -40);
56+
break;
57+
case 'd':
58+
// move mouse down
59+
Mouse.move(0, 40);
60+
break;
61+
case 'l':
62+
// move mouse left
63+
Mouse.move(-40, 0);
64+
break;
65+
case 'r':
66+
// move mouse right
67+
Mouse.move(40, 0);
68+
break;
69+
case 'm':
70+
// move mouse right
71+
Mouse.click(MOUSE_LEFT);
72+
break;
73+
}
74+
}
75+
76+
// use the pushbuttons to control the keyboard:
77+
if (digitalRead(upButton) == HIGH) {
78+
Keyboard.write('u');
79+
}
80+
if (digitalRead(downButton) == HIGH) {
81+
Keyboard.write('d');
82+
}
83+
if (digitalRead(leftButton) == HIGH) {
84+
Keyboard.write('l');
85+
}
86+
if (digitalRead(rightButton) == HIGH) {
87+
Keyboard.write('r');
88+
}
89+
if (digitalRead(mouseButton) == HIGH) {
90+
Keyboard.write('m');
91+
}
92+
93+
}
94+

0 commit comments

Comments
 (0)