diff options
author | Cristian Maglie <c.maglie@bug.st> | 2013-01-06 18:38:03 +0100 |
---|---|---|
committer | Cristian Maglie <c.maglie@bug.st> | 2013-01-06 18:38:03 +0100 |
commit | c01bc2b62f76a5d417e5ed13dcb0c047a4f67224 (patch) | |
tree | 4703fc452282756f3c40d38f9d41b1a28e0bf0f6 /libraries/Esplora/Experts | |
parent | 5f4e55a3d274838388696c3f8130b0f2dc40daf7 (diff) |
Merged upcoming 1.0.4 and updated revision log
Diffstat (limited to 'libraries/Esplora/Experts')
4 files changed, 498 insertions, 0 deletions
diff --git a/libraries/Esplora/Experts/EsploraKart/EsploraKart.ino b/libraries/Esplora/Experts/EsploraKart/EsploraKart.ino new file mode 100644 index 0000000..4c1621c --- /dev/null +++ b/libraries/Esplora/Experts/EsploraKart/EsploraKart.ino @@ -0,0 +1,125 @@ +/* + Esplora Kart + + This sketch turns the Esplora into a PC game pad. + + It uses the both the analog joystick and the four switches. + By moving the joystick in a direction or by pressing a switch, + the PC will "see" that a key is pressed. If the PC is running + a game that has keyboard input, the Esplora can control it. + + The default configuration is suitable for SuperTuxKart, an + open-source racing game. It can be downloaded from + http://supertuxkart.sourceforge.net/ . + + Created on 22 november 2012 + By Enrico Gueli <enrico.gueli@gmail.com> +*/ + + +#include <Esplora.h> + +/* + You're going to handle eight different buttons. You'll use arrays, + which are ordered lists of variables with a fixed size. Each array + has an index (counting from 0) to keep track of the position + you're reading in the array, and each position can contain a number. + + This code uses three different arrays: one for the buttons you'll read; + a second to hold the current states of those buttons; and a third to hold + the keystrokes associated with each button. + */ + +/* + This array holds the last sensed state of each of the buttons + you're reading. + Later in the code, you'll read the button states, and compare them + to the previous states that are stored in this array. If the two + states are different, it means that the button was either + pressed or released. + */ +boolean buttonStates[8]; + +/* + This array holds the names of the buttons being read. + Later in the sketch, you'll use these names with + the method Esplora.readButton(x), where x + is one of these buttons. + */ +const byte buttons[] = { + JOYSTICK_DOWN, + JOYSTICK_LEFT, + JOYSTICK_UP, + JOYSTICK_RIGHT, + SWITCH_RIGHT, // fire + SWITCH_LEFT, // bend + SWITCH_UP, // nitro + SWITCH_DOWN, // look back +}; + +/* + This array tells what keystroke to send to the PC when a + button is pressed. + If you look at this array and the above one, you can see that + the "cursor down" keystroke is sent when the joystick is moved + down, the "cursor up" keystroke when the joystick is moved up + and so on. +*/ +const char keystrokes[] = { + KEY_DOWN_ARROW, + KEY_LEFT_ARROW, + KEY_UP_ARROW, + KEY_RIGHT_ARROW, + ' ', + 'V', + 'N', + 'B' +}; + +/* + This is code is run only at startup, to initialize the + virtual USB keyboard. +*/ +void setup() { + Keyboard.begin(); +} + +/* + After setup() is finished, this code is run continuously. + Here we continuously check if something happened with the + buttons. +*/ +void loop() { + + // Iterate through all the buttons: + for (byte thisButton=0; thisButton<8; thisButton++) { + boolean lastState = buttonStates[thisButton]; + boolean newState = Esplora.readButton(buttons[thisButton]); + if (lastState != newState) { // Something changed! + /* + The Keyboard library allows you to "press" and "release" the + keys as two distinct actions. These actions can be + linked to the buttons we're handling. + */ + if (newState == PRESSED) { + Keyboard.press(keystrokes[thisButton]); + } + else if (newState == RELEASED) { + Keyboard.release(keystrokes[thisButton]); + } + } + + // Store the new button state, so you can sense a difference later: + buttonStates[thisButton] = newState; + } + + /* + Wait a little bit (50ms) between a check and another. + When a mechanical switch is pressed or released, the + contacts may bounce very rapidly. If the check is done too + fast, these bounces may be confused as multiple presses and + may lead to unexpected behaviour. + */ + delay(50); +} + diff --git a/libraries/Esplora/Experts/EsploraPong/EsploraPong.ino b/libraries/Esplora/Experts/EsploraPong/EsploraPong.ino new file mode 100644 index 0000000..725a109 --- /dev/null +++ b/libraries/Esplora/Experts/EsploraPong/EsploraPong.ino @@ -0,0 +1,44 @@ +/* + Esplora Pong + + This sketch connects serially to a Processing sketch to control a Pong game. + It sends the position of the slider and the states of three pushbuttons to the + Processing sketch serially, separated by commas. The Processing sketch uses that + data to control the graphics in the sketch. + + The slider sets a paddle's height + Switch 1 is resets the game + Switch 2 resets the ball to the center + Switch 3 reverses the players + + You can play this game with one or two Esploras. + + Created on 22 Dec 2012 + by Tom Igoe + + This example is in the public domain. + */ + +#include <Esplora.h> + +void setup() { + Serial.begin(9600); // initialize serial communication +} + +void loop() { + // read the slider and three of the buttons + int slider = Esplora.readSlider(); + int resetButton = Esplora.readButton(SWITCH_1); + int serveButton = Esplora.readButton(SWITCH_3); + int switchPlayerButton = Esplora.readButton(SWITCH_4); + + Serial.print(slider); // print the slider value + Serial.print(","); // add a comma + Serial.print(resetButton); // print the reset button value + Serial.print(","); // add another comma + Serial.print(serveButton); // print the serve button value + Serial.print(","); // add another comma + Serial.println(switchPlayerButton); // print the last button with a newline + delay(10); // delay before sending the next set +} + diff --git a/libraries/Esplora/Experts/EsploraRemote/EsploraRemote.ino b/libraries/Esplora/Experts/EsploraRemote/EsploraRemote.ino new file mode 100644 index 0000000..2701089 --- /dev/null +++ b/libraries/Esplora/Experts/EsploraRemote/EsploraRemote.ino @@ -0,0 +1,116 @@ +/* + Esplora Remote + + This sketch allows to test all the Esplora's peripherals. + It is also used with the ProcessingStart sketch (for Processing). + + When uploaded, you can open the Serial monitor and write one of + the following commands (without quotes) to get an answer: + + "D": prints the current value of all sensors, separated by a comma. + See the dumpInputs() function below to get the meaning of + each value. + + "Rxxx" + "Gxxx" + "Bxxx": set the color of the RGB led. For example, write "R255" + to turn on the red to full brightness, "G128" to turn + the green to half brightness, or "G0" to turn off + the green channel. + + "Txxxx": play a tone with the buzzer. The number is the + frequency, e.g. "T440" plays the central A note. + Write "T0" to turn off the buzzer. + + + Created on 22 november 2012 + By Enrico Gueli <enrico.gueli@gmail.com> + Modified 23 Dec 2012 + by Tom Igoe + */ + +#include <Esplora.h> + +void setup() { + while(!Serial); // needed for Leonardo-based board like Esplora + Serial.begin(9600); +} + +void loop() { + if (Serial.available()) + parseCommand(); +} + +/* + * This function reads a character from the serial line and + * decide what to do next. The "what to do" part is given by + * function it calls (e.g. dumpInputs(), setRed() and so on). + */ +void parseCommand() { + char cmd = Serial.read(); + switch(cmd) { + case 'D': + dumpInputs(); + break; + case 'R': + setRed(); + break; + case 'G': + setGreen(); + break; + case 'B': + setBlue(); + break; + case 'T': + setTone(); + break; + } +} + +void dumpInputs() { + Serial.print(Esplora.readButton(SWITCH_1)); + Serial.print(','); + Serial.print(Esplora.readButton(SWITCH_2)); + Serial.print(','); + Serial.print(Esplora.readButton(SWITCH_3)); + Serial.print(','); + Serial.print(Esplora.readButton(SWITCH_4)); + Serial.print(','); + Serial.print(Esplora.readSlider()); + Serial.print(','); + Serial.print(Esplora.readLightSensor()); + Serial.print(','); + Serial.print(Esplora.readTemperature(DEGREES_C)); + Serial.print(','); + Serial.print(Esplora.readMicrophone()); + Serial.print(','); + Serial.print(Esplora.readJoystickSwitch()); + Serial.print(','); + Serial.print(Esplora.readJoystickX()); + Serial.print(','); + Serial.print(Esplora.readJoystickY()); + Serial.print(','); + Serial.print(Esplora.readAccelerometer(X_AXIS)); + Serial.print(','); + Serial.print(Esplora.readAccelerometer(Y_AXIS)); + Serial.print(','); + Serial.print(Esplora.readAccelerometer(Z_AXIS)); + Serial.println(); +} + +void setRed() { + Esplora.writeRed(Serial.parseInt()); +} + +void setGreen() { + Esplora.writeGreen(Serial.parseInt()); +} + +void setBlue() { + Esplora.writeBlue(Serial.parseInt()); +} + +void setTone() { + Esplora.tone(Serial.parseInt()); +} + diff --git a/libraries/Esplora/Experts/EsploraTable/EsploraTable.ino b/libraries/Esplora/Experts/EsploraTable/EsploraTable.ino new file mode 100644 index 0000000..712dffa --- /dev/null +++ b/libraries/Esplora/Experts/EsploraTable/EsploraTable.ino @@ -0,0 +1,213 @@ +/* + Esplora Table + + Acts like a keyboard that prints sensor + data in a table-like text, row by row. + + At startup, it does nothing. It waits for you to open a + spreadsheet (e.g. Google Drive spreadsheet) so it can write + data. By pressing Switch 1, it starts printing the table + headers and the first row of data. It waits a bit, then it + will print another row, and so on. + + The amount of time between each row is determined by the slider. + If put to full left, the sketch will wait 10 seconds; at + full right position, it will wait 5 minutes. An intermediate + position will make the sketch wait for some time in-between. + + Clicking the Switch 1 at any time will stop the logging. + + The color LED shows what the sketch is doing: + blue = idle, waiting for you to press Switch 1 to start logging + green = active; will print soon + red = printing data to the PC + + Created on 22 november 2012 + By Enrico Gueli <enrico.gueli@gmail.com> + modified 24 Nov 2012 + by Tom Igoe +*/ + +#include <Esplora.h> + +/* + * this variable tells if the data-logging is currently active. + */ +boolean active = false; + +/* + * this variable holds the time in the future when the sketch + * will "sample" the data (sampling is the act of reading some + * input at a known time). This variable is checked continuously + * against millis() to know when it's time to sample. + */ +unsigned long nextSampleAt = 0; + +/* + * This variable just holds the millis() value at the time the + * logging was activated. This is needed to enter the correct + * value in the "Time" column in the printed table. + */ +unsigned long startedAt = 0; + + +/* + * when the "active" variable is set to true, the same is done + * with this variable. This is needed because the code that does + * the "just-after-activation" stuff is run some time later than + * the code that says "be active now". + */ +boolean justActivated = false; + + +/* + * this variable holds the last sensed status of the switch press + * button. If the code sees a difference between the value of + * this variable and the current status of the switch, it means + * that the button was either pressed or released. + */ +boolean lastStartBtn = HIGH; + +/* + * Initialization code. The virtual USB keyboard must be + * initialized; the Serial class is needed just for debugging. + */ +void setup() { + Keyboard.begin(); + Serial.begin(9600); +} + +/* + * This code is run continuously. + */ +void loop() { + /* + * note: we don't use Arduino's delay() here, because we can't + * normally do anything while delaying. Our own version lets us + * check for button presses often enough to not miss any event. + */ + activeDelay(50); + + /* + * the justActivated variable may be set to true in the + * checkSwitchPress() function. Here we check its status to + * print the table headers and configure what's needed to. + */ + if (justActivated == true) { + justActivated = false; // do this just once + printHeaders(); + // do next sampling ASAP + nextSampleAt = startedAt = millis(); + } + + if (active == true) { + if (nextSampleAt < millis()) { + // it's time to sample! + int slider = Esplora.readSlider(); + // the row below maps the slider position to a range between + // 10 and 290 seconds. + int sampleInterval = map(slider, 0, 1023, 10, 290); + nextSampleAt = millis() + sampleInterval * 1000; + + logAndPrint(); + } + + // let the RGB led blink green once per second, for 200ms. + unsigned int ms = millis() % 1000; + if (ms < 200) + Esplora.writeGreen(50); + else + Esplora.writeGreen(0); + + Esplora.writeBlue(0); + } + else + // while not active, keep a reassuring blue color coming + // from the Esplora... + Esplora.writeBlue(20); + +} + +/* + * Print the table headers. + */ +void printHeaders() { + Keyboard.print("Time"); + Keyboard.write(KEY_TAB); + activeDelay(300); // Some spreadsheets are slow, e.g. Google + // Drive that wants to save every edit. + Keyboard.print("Accel X"); + Keyboard.write(KEY_TAB); + activeDelay(300); + Keyboard.print("Accel Y"); + Keyboard.write(KEY_TAB); + activeDelay(300); + Keyboard.print("Accel Z"); + Keyboard.println(); + activeDelay(300); +} + +void logAndPrint() { + // do all the samplings at once, because keystrokes have delays + unsigned long timeSecs = (millis() - startedAt) /1000; + int xAxis = Esplora.readAccelerometer(X_AXIS); + int yAxis = Esplora.readAccelerometer(Y_AXIS); + int zAxis = Esplora.readAccelerometer(Z_AXIS); + + Esplora.writeRed(100); + + Keyboard.print(timeSecs); + Keyboard.write(KEY_TAB); + activeDelay(300); + Keyboard.print(xAxis); + Keyboard.write(KEY_TAB); + activeDelay(300); + Keyboard.print(yAxis); + Keyboard.write(KEY_TAB); + activeDelay(300); + Keyboard.print(zAxis); + Keyboard.println(); + activeDelay(300); + Keyboard.write(KEY_HOME); + + Esplora.writeRed(0); +} + +/** + * Similar to delay(), but allows the program to do something else + * in the meanwhile. In particular, it calls checkSwitchPress(). + * Note 1: it may wait longer than the specified amount, not less; + * Note 2: beware of data synchronization issues, e.g. if the + * activeDelay() function alters some variables used by the + * caller of this function. + */ +void activeDelay(unsigned long amount) { + unsigned long at = millis() + amount; + while (millis() < at) { + checkSwitchPress(); + } +} + +/* + * This function reads the status of the switch; if it sees that + * it was pressed, toggles the status of the "active" variable. + * If it's set to true, also the justActivated variable is set to + * true, so the loop() function above can do the right things. + * This function should be called as often as possible and do as + * little as possible, because it can be called while another + * function is running. + */ +void checkSwitchPress() { + boolean startBtn = Esplora.readButton(SWITCH_DOWN); + + if (startBtn != lastStartBtn) { + if (startBtn == HIGH) { // button released + active = !active; + if (active) + justActivated = true; + } + + lastStartBtn = startBtn; + } +} + |