1 | /*
|
---|
2 | Esplora.h - Arduino Esplora board library
|
---|
3 | Written by Enrico Gueli
|
---|
4 | Copyright (c) 2012 Arduino(TM) All right reserved.
|
---|
5 |
|
---|
6 | This library is free software; you can redistribute it and/or
|
---|
7 | modify it under the terms of the GNU Lesser General Public
|
---|
8 | License as published by the Free Software Foundation; either
|
---|
9 | version 2.1 of the License, or (at your option) any later version.
|
---|
10 |
|
---|
11 | This library is distributed in the hope that it will be useful,
|
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
14 | Lesser General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU Lesser General Public
|
---|
17 | License along with this library; if not, write to the Free Software
|
---|
18 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
19 | */
|
---|
20 |
|
---|
21 | #ifndef ESPLORA_H_
|
---|
22 | #define ESPLORA_H_
|
---|
23 |
|
---|
24 | #include <Arduino.h>
|
---|
25 |
|
---|
26 | /*
|
---|
27 | * The following constants are used internally by the Esplora
|
---|
28 | * library code.
|
---|
29 | */
|
---|
30 |
|
---|
31 | const byte JOYSTICK_BASE = 16; // it's a "virtual" channel: its ID won't conflict with real ones
|
---|
32 |
|
---|
33 | const byte MAX_CHANNELS = 13;
|
---|
34 |
|
---|
35 | const byte CH_SWITCH_1 = 0;
|
---|
36 | const byte CH_SWITCH_2 = 1;
|
---|
37 | const byte CH_SWITCH_3 = 2;
|
---|
38 | const byte CH_SWITCH_4 = 3;
|
---|
39 | const byte CH_SLIDER = 4;
|
---|
40 | const byte CH_LIGHT = 5;
|
---|
41 | const byte CH_TEMPERATURE = 6;
|
---|
42 | const byte CH_MIC = 7;
|
---|
43 | const byte CH_TINKERKIT_A = 8;
|
---|
44 | const byte CH_TINKERKIT_B = 9;
|
---|
45 | const byte CH_JOYSTICK_SW = 10;
|
---|
46 | const byte CH_JOYSTICK_X = 11;
|
---|
47 | const byte CH_JOYSTICK_Y = 12;
|
---|
48 |
|
---|
49 | /*
|
---|
50 | * The following constants can be used with the readButton()
|
---|
51 | * method.
|
---|
52 | */
|
---|
53 |
|
---|
54 | const byte SWITCH_1 = 1;
|
---|
55 | const byte SWITCH_2 = 2;
|
---|
56 | const byte SWITCH_3 = 3;
|
---|
57 | const byte SWITCH_4 = 4;
|
---|
58 |
|
---|
59 | const byte SWITCH_DOWN = SWITCH_1;
|
---|
60 | const byte SWITCH_LEFT = SWITCH_2;
|
---|
61 | const byte SWITCH_UP = SWITCH_3;
|
---|
62 | const byte SWITCH_RIGHT = SWITCH_4;
|
---|
63 |
|
---|
64 | const byte JOYSTICK_DOWN = JOYSTICK_BASE;
|
---|
65 | const byte JOYSTICK_LEFT = JOYSTICK_BASE+1;
|
---|
66 | const byte JOYSTICK_UP = JOYSTICK_BASE+2;
|
---|
67 | const byte JOYSTICK_RIGHT = JOYSTICK_BASE+3;
|
---|
68 |
|
---|
69 | /*
|
---|
70 | * These constants can be use for comparison with the value returned
|
---|
71 | * by the readButton() method.
|
---|
72 | */
|
---|
73 | const boolean PRESSED = LOW;
|
---|
74 | const boolean RELEASED = HIGH;
|
---|
75 |
|
---|
76 | /*
|
---|
77 | * The following constants can be used with the readTemperature()
|
---|
78 | * method to specify the desired scale.
|
---|
79 | */
|
---|
80 | const byte DEGREES_C = 0;
|
---|
81 | const byte DEGREES_F = 1;
|
---|
82 |
|
---|
83 | /*
|
---|
84 | * The following constants can be used with the readAccelerometer()
|
---|
85 | * method to specify the desired axis to return.
|
---|
86 | */
|
---|
87 | const byte X_AXIS = 0;
|
---|
88 | const byte Y_AXIS = 1;
|
---|
89 | const byte Z_AXIS = 2;
|
---|
90 |
|
---|
91 |
|
---|
92 | class _Esplora {
|
---|
93 | private:
|
---|
94 | byte lastRed;
|
---|
95 | byte lastGreen;
|
---|
96 | byte lastBlue;
|
---|
97 |
|
---|
98 | unsigned int readChannel(byte channel);
|
---|
99 |
|
---|
100 | boolean joyLowHalf(byte joyCh);
|
---|
101 | boolean joyHighHalf(byte joyCh);
|
---|
102 |
|
---|
103 | public:
|
---|
104 | _Esplora();
|
---|
105 |
|
---|
106 | /*
|
---|
107 | * Returns a number corresponding to the position of the
|
---|
108 | * linear potentiometer. 0 means full right, 1023 means
|
---|
109 | * full left.
|
---|
110 | */
|
---|
111 | inline unsigned int readSlider() { return readChannel(CH_SLIDER); }
|
---|
112 |
|
---|
113 | /*
|
---|
114 | * Returns a number corresponding to the amount of ambient
|
---|
115 | * light sensed by the light sensor.
|
---|
116 | */
|
---|
117 | inline unsigned int readLightSensor() { return readChannel(CH_LIGHT); }
|
---|
118 |
|
---|
119 | /*
|
---|
120 | * Returns the current ambient temperature, expressed either in Celsius
|
---|
121 | * or Fahreneit scale.
|
---|
122 | */
|
---|
123 | int readTemperature(const byte scale);
|
---|
124 |
|
---|
125 | /*
|
---|
126 | * Returns a number corresponding to the amount of ambient noise.
|
---|
127 | */
|
---|
128 | inline unsigned int readMicrophone() { return readChannel(CH_MIC); }
|
---|
129 |
|
---|
130 | inline unsigned int readJoystickSwitch() { return readChannel(CH_JOYSTICK_SW); }
|
---|
131 |
|
---|
132 | inline int readJoystickX() {
|
---|
133 | return readChannel(CH_JOYSTICK_X) - 512;
|
---|
134 | }
|
---|
135 | inline int readJoystickY() {
|
---|
136 | return readChannel(CH_JOYSTICK_Y) - 512;
|
---|
137 | }
|
---|
138 |
|
---|
139 | int readAccelerometer(const byte axis);
|
---|
140 |
|
---|
141 | /*
|
---|
142 | * Reads the current state of a button. It will return
|
---|
143 | * LOW if the button is pressed, and HIGH otherwise.
|
---|
144 | */
|
---|
145 | boolean readButton(byte channel);
|
---|
146 |
|
---|
147 | boolean readJoystickButton();
|
---|
148 |
|
---|
149 | void writeRGB(byte red, byte green, byte blue);
|
---|
150 | void writeRed(byte red);
|
---|
151 | void writeGreen(byte green);
|
---|
152 | void writeBlue(byte blue);
|
---|
153 |
|
---|
154 | byte readRed();
|
---|
155 | byte readGreen();
|
---|
156 | byte readBlue();
|
---|
157 |
|
---|
158 | void tone(unsigned int freq);
|
---|
159 | void tone(unsigned int freq, unsigned long duration);
|
---|
160 | void noTone();
|
---|
161 |
|
---|
162 | inline unsigned int readTinkerkitInput(byte whichInput) {
|
---|
163 | return readChannel(whichInput + CH_TINKERKIT_A);
|
---|
164 | }
|
---|
165 | inline unsigned int readTinkerkitInputA() {
|
---|
166 | return readChannel(CH_TINKERKIT_A);
|
---|
167 | }
|
---|
168 | inline unsigned int readTinkerkitInputB() {
|
---|
169 | return readChannel(CH_TINKERKIT_B);
|
---|
170 | }
|
---|
171 | };
|
---|
172 |
|
---|
173 |
|
---|
174 |
|
---|
175 | extern _Esplora Esplora;
|
---|
176 |
|
---|
177 | #endif // ESPLORA_H_
|
---|