-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
475674b
commit 5d06a61
Showing
33 changed files
with
2,438 additions
and
276 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
/* | ||
Esplora.cpp - Arduino Esplora board library | ||
Written by Enrico Gueli | ||
Copyright (c) 2012 Arduino LLC. All right reserved. | ||
This library is free software; you can redistribute it and/or | ||
modify it under the terms of the GNU Lesser General Public | ||
License as published by the Free Software Foundation; either | ||
version 2.1 of the License, or (at your option) any later version. | ||
This library is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public | ||
License along with this library; if not, write to the Free Software | ||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
*/ | ||
|
||
|
||
#include "Esplora.h" | ||
|
||
_Esplora Esplora; | ||
|
||
/* | ||
* The following constants tell, for each accelerometer | ||
* axis, which values are returned when the axis measures | ||
* zero acceleration. | ||
*/ | ||
const int ACCEL_ZERO_X = 320; | ||
const int ACCEL_ZERO_Y = 330; | ||
const int ACCEL_ZERO_Z = 310; | ||
|
||
const byte MUX_ADDR_PINS[] = { A0, A1, A2, A3 }; | ||
const byte MUX_COM_PIN = A4; | ||
|
||
const int JOYSTICK_DEAD_ZONE = 100; | ||
|
||
const byte RED_PIN = 5; | ||
const byte BLUE_PIN = 9; | ||
const byte GREEN_PIN = 10; | ||
|
||
const byte BUZZER_PIN = 6; | ||
|
||
// non-multiplexer Esplora pins: | ||
// Accelerometer: x-A5, y-A11, z-A6 | ||
// External outputs: D3, D11 | ||
// Buzzer: D6 | ||
// RGB Led: red-D5, green-D10, blue-D9 | ||
// Led 13: D13 | ||
|
||
const byte ACCEL_X_PIN = A5; | ||
const byte ACCEL_Y_PIN = A11; | ||
const byte ACCEL_Z_PIN = A6; | ||
|
||
const byte LED_PIN = 13; | ||
|
||
_Esplora::_Esplora() { | ||
for (byte p=0; p<4; p++) { | ||
pinMode(MUX_ADDR_PINS[p], OUTPUT); | ||
} | ||
pinMode(RED_PIN, OUTPUT); | ||
pinMode(GREEN_PIN, OUTPUT); | ||
pinMode(BLUE_PIN, OUTPUT); | ||
} | ||
|
||
unsigned int _Esplora::readChannel(byte channel) { | ||
digitalWrite(MUX_ADDR_PINS[0], (channel & 1) ? HIGH : LOW); | ||
digitalWrite(MUX_ADDR_PINS[1], (channel & 2) ? HIGH : LOW); | ||
digitalWrite(MUX_ADDR_PINS[2], (channel & 4) ? HIGH : LOW); | ||
digitalWrite(MUX_ADDR_PINS[3], (channel & 8) ? HIGH : LOW); | ||
// workaround to cope with lack of pullup resistor on joystick switch | ||
if (channel == CH_JOYSTICK_SW) { | ||
pinMode(MUX_COM_PIN, INPUT_PULLUP); | ||
unsigned int joystickSwitchState = (digitalRead(MUX_COM_PIN) == HIGH) ? 1023 : 0; | ||
digitalWrite(MUX_COM_PIN, LOW); | ||
return joystickSwitchState; | ||
} | ||
else | ||
return analogRead(MUX_COM_PIN); | ||
} | ||
|
||
boolean _Esplora::joyLowHalf(byte joyCh) { | ||
return (readChannel(joyCh) < 512 - JOYSTICK_DEAD_ZONE) | ||
? LOW : HIGH; | ||
} | ||
|
||
boolean _Esplora::joyHighHalf(byte joyCh) { | ||
return (readChannel(joyCh) > 512 + JOYSTICK_DEAD_ZONE) | ||
? LOW : HIGH; | ||
} | ||
|
||
boolean _Esplora::readButton(byte ch) { | ||
if (ch >= SWITCH_1 && ch <= SWITCH_4) { | ||
ch--; | ||
} | ||
|
||
switch(ch) { | ||
case JOYSTICK_RIGHT: | ||
return joyLowHalf(CH_JOYSTICK_X); | ||
case JOYSTICK_LEFT: | ||
return joyHighHalf(CH_JOYSTICK_X); | ||
case JOYSTICK_UP: | ||
return joyLowHalf(CH_JOYSTICK_Y); | ||
case JOYSTICK_DOWN: | ||
return joyHighHalf(CH_JOYSTICK_Y); | ||
} | ||
|
||
unsigned int val = readChannel(ch); | ||
return (val > 512) ? HIGH : LOW; | ||
} | ||
|
||
boolean _Esplora::readJoystickButton() { | ||
if (readChannel(CH_JOYSTICK_SW) == 1023) { | ||
return HIGH; | ||
} else if (readChannel(CH_JOYSTICK_SW) == 0) { | ||
return LOW; | ||
} | ||
} | ||
|
||
|
||
void _Esplora::writeRGB(byte r, byte g, byte b) { | ||
writeRed(r); | ||
writeGreen(g); | ||
writeBlue(b); | ||
} | ||
|
||
#define RGB_FUNC(name, pin, lastVar) \ | ||
void _Esplora::write##name(byte val) { \ | ||
if (val == lastVar) \ | ||
return; \ | ||
analogWrite(pin, val); \ | ||
lastVar = val; \ | ||
delay(5); \ | ||
} \ | ||
\ | ||
byte _Esplora::read##name() { \ | ||
return lastVar; \ | ||
} | ||
|
||
RGB_FUNC(Red, RED_PIN, lastRed) | ||
RGB_FUNC(Green, GREEN_PIN, lastGreen) | ||
RGB_FUNC(Blue, BLUE_PIN, lastBlue) | ||
|
||
void _Esplora::tone(unsigned int freq) { | ||
if (freq > 0) | ||
::tone(BUZZER_PIN, freq); | ||
else | ||
::noTone(BUZZER_PIN); | ||
} | ||
|
||
void _Esplora::tone(unsigned int freq, unsigned long duration) { | ||
if (freq > 0) | ||
::tone(BUZZER_PIN, freq, duration); | ||
else | ||
::noTone(BUZZER_PIN); | ||
} | ||
|
||
void _Esplora::noTone() { | ||
::noTone(BUZZER_PIN); | ||
} | ||
|
||
int _Esplora::readTemperature(const byte scale) { | ||
long rawT = readChannel(CH_TEMPERATURE); | ||
if (scale == DEGREES_C) { | ||
return (int)((rawT * 500 / 1024) - 50); | ||
} | ||
else if (scale == DEGREES_F) { | ||
return (int)((rawT * 450 / 512 ) - 58); | ||
} | ||
else { | ||
return readTemperature(DEGREES_C); | ||
} | ||
} | ||
|
||
int _Esplora::readAccelerometer(const byte axis) { | ||
switch (axis) { | ||
case X_AXIS: return analogRead(ACCEL_X_PIN) - ACCEL_ZERO_X; | ||
case Y_AXIS: return analogRead(ACCEL_Y_PIN) - ACCEL_ZERO_Y; | ||
case Z_AXIS: return analogRead(ACCEL_Z_PIN) - ACCEL_ZERO_Z; | ||
default: return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
/* | ||
Esplora.h - Arduino Esplora board library | ||
Written by Enrico Gueli | ||
Copyright (c) 2012 Arduino LLC. All right reserved. | ||
This library is free software; you can redistribute it and/or | ||
modify it under the terms of the GNU Lesser General Public | ||
License as published by the Free Software Foundation; either | ||
version 2.1 of the License, or (at your option) any later version. | ||
This library is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public | ||
License along with this library; if not, write to the Free Software | ||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
*/ | ||
|
||
#ifndef ESPLORA_H_ | ||
#define ESPLORA_H_ | ||
|
||
#include <Arduino.h> | ||
|
||
/* | ||
* The following constants are used internally by the Esplora | ||
* library code. | ||
*/ | ||
|
||
const byte JOYSTICK_BASE = 16; // it's a "virtual" channel: its ID won't conflict with real ones | ||
|
||
const byte MAX_CHANNELS = 13; | ||
|
||
const byte CH_SWITCH_1 = 0; | ||
const byte CH_SWITCH_2 = 1; | ||
const byte CH_SWITCH_3 = 2; | ||
const byte CH_SWITCH_4 = 3; | ||
const byte CH_SLIDER = 4; | ||
const byte CH_LIGHT = 5; | ||
const byte CH_TEMPERATURE = 6; | ||
const byte CH_MIC = 7; | ||
const byte CH_TINKERKIT_A = 8; | ||
const byte CH_TINKERKIT_B = 9; | ||
const byte CH_JOYSTICK_SW = 10; | ||
const byte CH_JOYSTICK_X = 11; | ||
const byte CH_JOYSTICK_Y = 12; | ||
|
||
/* | ||
* The following constants can be used with the readButton() | ||
* method. | ||
*/ | ||
|
||
const byte SWITCH_1 = 1; | ||
const byte SWITCH_2 = 2; | ||
const byte SWITCH_3 = 3; | ||
const byte SWITCH_4 = 4; | ||
|
||
const byte SWITCH_DOWN = SWITCH_1; | ||
const byte SWITCH_LEFT = SWITCH_2; | ||
const byte SWITCH_UP = SWITCH_3; | ||
const byte SWITCH_RIGHT = SWITCH_4; | ||
|
||
const byte JOYSTICK_DOWN = JOYSTICK_BASE; | ||
const byte JOYSTICK_LEFT = JOYSTICK_BASE+1; | ||
const byte JOYSTICK_UP = JOYSTICK_BASE+2; | ||
const byte JOYSTICK_RIGHT = JOYSTICK_BASE+3; | ||
|
||
/* | ||
* These constants can be use for comparison with the value returned | ||
* by the readButton() method. | ||
*/ | ||
const boolean PRESSED = LOW; | ||
const boolean RELEASED = HIGH; | ||
|
||
/* | ||
* The following constants can be used with the readTemperature() | ||
* method to specify the desired scale. | ||
*/ | ||
const byte DEGREES_C = 0; | ||
const byte DEGREES_F = 1; | ||
|
||
/* | ||
* The following constants can be used with the readAccelerometer() | ||
* method to specify the desired axis to return. | ||
*/ | ||
const byte X_AXIS = 0; | ||
const byte Y_AXIS = 1; | ||
const byte Z_AXIS = 2; | ||
|
||
|
||
class _Esplora { | ||
private: | ||
byte lastRed; | ||
byte lastGreen; | ||
byte lastBlue; | ||
|
||
unsigned int readChannel(byte channel); | ||
|
||
boolean joyLowHalf(byte joyCh); | ||
boolean joyHighHalf(byte joyCh); | ||
|
||
public: | ||
_Esplora(); | ||
|
||
/* | ||
* Returns a number corresponding to the position of the | ||
* linear potentiometer. 0 means full right, 1023 means | ||
* full left. | ||
*/ | ||
inline unsigned int readSlider() { return readChannel(CH_SLIDER); } | ||
|
||
/* | ||
* Returns a number corresponding to the amount of ambient | ||
* light sensed by the light sensor. | ||
*/ | ||
inline unsigned int readLightSensor() { return readChannel(CH_LIGHT); } | ||
|
||
/* | ||
* Returns the current ambient temperature, expressed either in Celsius | ||
* or Fahreneit scale. | ||
*/ | ||
int readTemperature(const byte scale); | ||
|
||
/* | ||
* Returns a number corresponding to the amount of ambient noise. | ||
*/ | ||
inline unsigned int readMicrophone() { return readChannel(CH_MIC); } | ||
|
||
inline unsigned int readJoystickSwitch() { return readChannel(CH_JOYSTICK_SW); } | ||
|
||
inline int readJoystickX() { | ||
return readChannel(CH_JOYSTICK_X) - 512; | ||
} | ||
inline int readJoystickY() { | ||
return readChannel(CH_JOYSTICK_Y) - 512; | ||
} | ||
|
||
int readAccelerometer(const byte axis); | ||
|
||
/* | ||
* Reads the current state of a button. It will return | ||
* LOW if the button is pressed, and HIGH otherwise. | ||
*/ | ||
boolean readButton(byte channel); | ||
|
||
boolean readJoystickButton(); | ||
|
||
void writeRGB(byte red, byte green, byte blue); | ||
void writeRed(byte red); | ||
void writeGreen(byte green); | ||
void writeBlue(byte blue); | ||
|
||
byte readRed(); | ||
byte readGreen(); | ||
byte readBlue(); | ||
|
||
void tone(unsigned int freq); | ||
void tone(unsigned int freq, unsigned long duration); | ||
void noTone(); | ||
|
||
inline unsigned int readTinkerkitInput(byte whichInput) { | ||
return readChannel(whichInput + CH_TINKERKIT_A); | ||
} | ||
inline unsigned int readTinkerkitInputA() { | ||
return readChannel(CH_TINKERKIT_A); | ||
} | ||
inline unsigned int readTinkerkitInputB() { | ||
return readChannel(CH_TINKERKIT_B); | ||
} | ||
}; | ||
|
||
|
||
|
||
extern _Esplora Esplora; | ||
|
||
#endif // ESPLORA_H_ |
Oops, something went wrong.