From 64c8b89b5f4cb73f86e11da26840f065633a3445 Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Thu, 15 Dec 2011 15:33:47 -0500 Subject: [PATCH 1/2] Fixing ethernet library on Leonardo (correcting SS pin for 32U4). --- libraries/Ethernet/utility/w5100.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/libraries/Ethernet/utility/w5100.h b/libraries/Ethernet/utility/w5100.h index 153aedbc6..8dccd9f29 100755 --- a/libraries/Ethernet/utility/w5100.h +++ b/libraries/Ethernet/utility/w5100.h @@ -327,7 +327,11 @@ private: inline static void initSS() { DDRB |= _BV(4); }; inline static void setSS() { PORTB &= ~_BV(4); }; inline static void resetSS() { PORTB |= _BV(4); }; -#elif defined(__AVR_ATmega32U4__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB162__) +#elif defined(__AVR_ATmega32U4__) + inline static void initSS() { DDRB |= _BV(6); }; + inline static void setSS() { PORTB &= ~_BV(6); }; + inline static void resetSS() { PORTB |= _BV(6); }; +#elif defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB162__) inline static void initSS() { DDRB |= _BV(0); }; inline static void setSS() { PORTB &= ~_BV(0); }; inline static void resetSS() { PORTB |= _BV(0); }; From f0923daa4fac09e136fedbbe99899ed9a324f5a7 Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Fri, 16 Dec 2011 15:58:42 -0500 Subject: [PATCH 2/2] Revert "Removing Leonardo (and Mouse/Keyboard examples) for Arduino 1.0 release." This reverts commit dca1dc429a740edabcb7a214ddddbab5d1946fd3. --- .../JoystickMouseControl.ino | 115 ++++++++++++++++++ .../KeyboardMessage/KeyboardMessage.ino | 43 +++++++ .../KeyboardSerial/KeyboardSerial.ino | 33 +++++ 3 files changed, 191 insertions(+) create mode 100644 build/shared/examples/10.Mouse/JoystickMouseControl/JoystickMouseControl.ino create mode 100644 build/shared/examples/9.Keyboard/KeyboardMessage/KeyboardMessage.ino create mode 100644 build/shared/examples/9.Keyboard/KeyboardSerial/KeyboardSerial.ino diff --git a/build/shared/examples/10.Mouse/JoystickMouseControl/JoystickMouseControl.ino b/build/shared/examples/10.Mouse/JoystickMouseControl/JoystickMouseControl.ino new file mode 100644 index 000000000..760562928 --- /dev/null +++ b/build/shared/examples/10.Mouse/JoystickMouseControl/JoystickMouseControl.ino @@ -0,0 +1,115 @@ +/* + JoystickMouseControl + + Controls the mouse from a joystick on an Arduino Leonardo. + Uses a pushbutton to turn on and off mouse control. + + The mouse movement is always relative. This sketch reads + two analog inputs that range from 0 to 1023 (or less on either end) + and translates them into ranges of -60 to 60. + The sketch assumes that the joystick resting values are around the + middle of the range, but that they vary within a threshold. + + WARNING: When you use the Mouse.move() command, the Arduino takes + over your mouse! Make sure you have control before you use the command. + This sketch includes a pushbutton to toggle the mouse control state, so + you can turn on and off mouse control. + + created 15 Sept 2011 + by Tom Igoe + + this code is in the public domain + + */ + +// set pin numbers for switch, joystick axes, and LED: +const int switchPin = 6; // switch to turn on and off mouse control +const int xAxis = A1; // joystick X axis +const int yAxis = A2; // joystick Y axis +const int ledPin = 5; // Mouse control LED + +// parameters for reading the joystick: +int range = 12; // output range of X or Y movement +int responseDelay = 2; // response delay of the mouse, in ms +int threshold = range/4; // resting threshold +int center = range/2; // resting position value +int minima[] = { + 1023, 1023}; // actual analogRead minima for {x, y} +int maxima[] = { + 0,0}; // actual analogRead maxima for {x, y} +int axis[] = { + xAxis, yAxis}; // pin numbers for {x, y} +int mouseReading[2]; // final mouse readings for {x, y} + + +boolean mouseIsActive = false; // whether or not to control the mouse +int lastSwitchState = LOW; // previous switch state + +void setup() { + pinMode(switchPin, INPUT); // the switch pin + pinMode(ledPin, OUTPUT); // the LED pin +} + +void loop() { + // read the switch: + int switchState = digitalRead(switchPin); + // if it's changed and it's high, toggle the mouse state: + if (switchState != lastSwitchState) { + if (switchState == HIGH) { + mouseIsActive = !mouseIsActive; + // turn on LED to indicate mouse state: + digitalWrite(ledPin, mouseIsActive); + } + } + // save switch state for next comparison: + lastSwitchState = switchState; + +// read and scale the two axes: + int xReading = readAxis(0); + int yReading = readAxis(1); + +// if the mouse control state is active, move the mouse: + if (mouseIsActive) { + Mouse.move(xReading, yReading, 0); + } + delay(responseDelay); +} + +/* + reads an axis (0 or 1 for x or y) and scales the + analog input range to a range from 0 to +*/ + +int readAxis(int axisNumber) { + int distance = 0; // distance from center of the output range + + // read the analog input: + int reading = analogRead(axis[axisNumber]); + +// of the current reading exceeds the max or min for this axis, +// reset the max or min: + if (reading < minima[axisNumber]) { + minima[axisNumber] = reading; + } + if (reading > maxima[axisNumber]) { + maxima[axisNumber] = reading; + } + + // map the reading from the analog input range to the output range: + reading = map(reading, minima[axisNumber], maxima[axisNumber], 0, range); + + // if the output reading is outside from the + // rest position threshold, use it: + if (abs(reading - center) > threshold) { + distance = (reading - center); + } + + // the Y axis needs to be inverted in order to + // map the movemment correctly: + if (axisNumber == 1) { + distance = -distance; + } + + // return the distance for this axis: + return distance; +} diff --git a/build/shared/examples/9.Keyboard/KeyboardMessage/KeyboardMessage.ino b/build/shared/examples/9.Keyboard/KeyboardMessage/KeyboardMessage.ino new file mode 100644 index 000000000..51b1ce2c6 --- /dev/null +++ b/build/shared/examples/9.Keyboard/KeyboardMessage/KeyboardMessage.ino @@ -0,0 +1,43 @@ +/* + Keyboard Button test + + Sends a text string when a button is pressed. + + The circuit: + * pushbutton attached from pin 4 to +5V + * 10-kilohm resistor attached from pin 4 to ground + + created 24 Oct 2011 + by Tom Igoe + + This example code is in the public domain. + + http://www.arduino.cc/en/Tutorial/KeyboardButton + */ + +const int buttonPin = 4; // input pin for pushbutton +int previousButtonState = HIGH; // for checking the state of a pushButton +int counter = 0; // button push counter + +void setup() { + // make the pushButton pin an input: + pinMode(buttonPin, INPUT); +} + +void loop() { + // read the pushbutton: + int buttonState = digitalRead(buttonPin); + // if the button state has changed, + if ((buttonState != previousButtonState) + // and it's currently pressed: + && (buttonState == HIGH)) { + // increment the button counter + counter++; + // type out a message + Keyboard.print("You pressed the button: "); + Keyboard.print(counter); + Keyboard.println(" times."); + } + // save the current button state for comparison next time: + previousButtonState = buttonState; +} diff --git a/build/shared/examples/9.Keyboard/KeyboardSerial/KeyboardSerial.ino b/build/shared/examples/9.Keyboard/KeyboardSerial/KeyboardSerial.ino new file mode 100644 index 000000000..b64b6272b --- /dev/null +++ b/build/shared/examples/9.Keyboard/KeyboardSerial/KeyboardSerial.ino @@ -0,0 +1,33 @@ +/* + Keyboard test + + Reads a byte from the serial port, sends a keystroke back. + The sent keystroke is one higher than what's received, e.g. + if you send a, you get b, send A you get B, and so forth. + + The circuit: + * none + + created 21 Oct 2011 + by Tom Igoe + +This example code is in the public domain. + + http://www.arduino.cc/en/Tutorial/KeyboardSerial + */ + +void setup() { + // open the serial port: +Serial.begin(9600); +} + +void loop() { + // check for incoming serial data: + if (Serial.available() > 0) { + // read incoming serial data: + char inChar = Serial.read(); + // Type the next ASCII value from what you received: + Keyboard.write(inChar+1); + } +} +