1 | /*
|
---|
2 | Esplora.cpp - 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 |
|
---|
22 | #include "Esplora.h"
|
---|
23 |
|
---|
24 | _Esplora Esplora;
|
---|
25 |
|
---|
26 | /*
|
---|
27 | * The following constants tell, for each accelerometer
|
---|
28 | * axis, which values are returned when the axis measures
|
---|
29 | * zero acceleration.
|
---|
30 | */
|
---|
31 | const int ACCEL_ZERO_X = 320;
|
---|
32 | const int ACCEL_ZERO_Y = 330;
|
---|
33 | const int ACCEL_ZERO_Z = 310;
|
---|
34 |
|
---|
35 | const byte MUX_ADDR_PINS[] = { A0, A1, A2, A3 };
|
---|
36 | const byte MUX_COM_PIN = A4;
|
---|
37 |
|
---|
38 | const int JOYSTICK_DEAD_ZONE = 100;
|
---|
39 |
|
---|
40 | const byte RED_PIN = 5;
|
---|
41 | const byte BLUE_PIN = 9;
|
---|
42 | const byte GREEN_PIN = 10;
|
---|
43 |
|
---|
44 | const byte BUZZER_PIN = 6;
|
---|
45 |
|
---|
46 | // non-multiplexer Esplora pins:
|
---|
47 | // Accelerometer: x-A5, y-A7, z-A6
|
---|
48 | // External outputs: D3, D11
|
---|
49 | // Buzzer: A8
|
---|
50 | // RGB Led: red-D5, green-D10/A11, blue-D9/A10
|
---|
51 | // Led 13: D13
|
---|
52 |
|
---|
53 | const byte ACCEL_X_PIN = A5;
|
---|
54 | const byte ACCEL_Y_PIN = A11;
|
---|
55 | const byte ACCEL_Z_PIN = A6;
|
---|
56 |
|
---|
57 | const byte LED_PIN = 13;
|
---|
58 |
|
---|
59 | _Esplora::_Esplora() {
|
---|
60 | for (byte p=0; p<4; p++) {
|
---|
61 | pinMode(MUX_ADDR_PINS[p], OUTPUT);
|
---|
62 | }
|
---|
63 | pinMode(RED_PIN, OUTPUT);
|
---|
64 | pinMode(GREEN_PIN, OUTPUT);
|
---|
65 | pinMode(BLUE_PIN, OUTPUT);
|
---|
66 | }
|
---|
67 |
|
---|
68 | unsigned int _Esplora::readChannel(byte channel) {
|
---|
69 | digitalWrite(MUX_ADDR_PINS[0], (channel & 1) ? HIGH : LOW);
|
---|
70 | digitalWrite(MUX_ADDR_PINS[1], (channel & 2) ? HIGH : LOW);
|
---|
71 | digitalWrite(MUX_ADDR_PINS[2], (channel & 4) ? HIGH : LOW);
|
---|
72 | digitalWrite(MUX_ADDR_PINS[3], (channel & 8) ? HIGH : LOW);
|
---|
73 | // workaround to cope with lack of pullup resistor on joystick switch
|
---|
74 | if (channel == CH_JOYSTICK_SW) {
|
---|
75 | pinMode(MUX_COM_PIN, INPUT_PULLUP);
|
---|
76 | unsigned int joystickSwitchState = (digitalRead(MUX_COM_PIN) == HIGH) ? 1023 : 0;
|
---|
77 | digitalWrite(MUX_COM_PIN, LOW);
|
---|
78 | return joystickSwitchState;
|
---|
79 | }
|
---|
80 | else
|
---|
81 | return analogRead(MUX_COM_PIN);
|
---|
82 | }
|
---|
83 |
|
---|
84 | boolean _Esplora::joyLowHalf(byte joyCh) {
|
---|
85 | return (readChannel(joyCh) < 512 - JOYSTICK_DEAD_ZONE)
|
---|
86 | ? LOW : HIGH;
|
---|
87 | }
|
---|
88 |
|
---|
89 | boolean _Esplora::joyHighHalf(byte joyCh) {
|
---|
90 | return (readChannel(joyCh) > 512 + JOYSTICK_DEAD_ZONE)
|
---|
91 | ? LOW : HIGH;
|
---|
92 | }
|
---|
93 |
|
---|
94 | boolean _Esplora::readButton(byte ch) {
|
---|
95 | if (ch >= SWITCH_1 && ch <= SWITCH_4) {
|
---|
96 | ch--;
|
---|
97 | }
|
---|
98 |
|
---|
99 | switch(ch) {
|
---|
100 | case JOYSTICK_RIGHT:
|
---|
101 | return joyLowHalf(CH_JOYSTICK_X);
|
---|
102 | case JOYSTICK_LEFT:
|
---|
103 | return joyHighHalf(CH_JOYSTICK_X);
|
---|
104 | case JOYSTICK_UP:
|
---|
105 | return joyLowHalf(CH_JOYSTICK_Y);
|
---|
106 | case JOYSTICK_DOWN:
|
---|
107 | return joyHighHalf(CH_JOYSTICK_Y);
|
---|
108 | }
|
---|
109 |
|
---|
110 | unsigned int val = readChannel(ch);
|
---|
111 | return (val > 512) ? HIGH : LOW;
|
---|
112 | }
|
---|
113 |
|
---|
114 | boolean _Esplora::readJoystickButton() {
|
---|
115 | if (readChannel(CH_JOYSTICK_SW) == 1023) {
|
---|
116 | return HIGH;
|
---|
117 | } else if (readChannel(CH_JOYSTICK_SW) == 0) {
|
---|
118 | return LOW;
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 |
|
---|
123 | void _Esplora::writeRGB(byte r, byte g, byte b) {
|
---|
124 | writeRed(r);
|
---|
125 | writeGreen(g);
|
---|
126 | writeBlue(b);
|
---|
127 | }
|
---|
128 |
|
---|
129 | #define RGB_FUNC(name, pin, lastVar) \
|
---|
130 | void _Esplora::write##name(byte val) { \
|
---|
131 | if (val == lastVar) \
|
---|
132 | return; \
|
---|
133 | analogWrite(pin, val); \
|
---|
134 | lastVar = val; \
|
---|
135 | delay(5); \
|
---|
136 | } \
|
---|
137 | \
|
---|
138 | byte _Esplora::read##name() { \
|
---|
139 | return lastVar; \
|
---|
140 | }
|
---|
141 |
|
---|
142 | RGB_FUNC(Red, RED_PIN, lastRed)
|
---|
143 | RGB_FUNC(Green, GREEN_PIN, lastGreen)
|
---|
144 | RGB_FUNC(Blue, BLUE_PIN, lastBlue)
|
---|
145 |
|
---|
146 | void _Esplora::tone(unsigned int freq) {
|
---|
147 | if (freq > 0)
|
---|
148 | ::tone(BUZZER_PIN, freq);
|
---|
149 | else
|
---|
150 | ::noTone(BUZZER_PIN);
|
---|
151 | }
|
---|
152 |
|
---|
153 | void _Esplora::tone(unsigned int freq, unsigned long duration) {
|
---|
154 | if (freq > 0)
|
---|
155 | ::tone(BUZZER_PIN, freq, duration);
|
---|
156 | else
|
---|
157 | ::noTone(BUZZER_PIN);
|
---|
158 | }
|
---|
159 |
|
---|
160 | void _Esplora::noTone() {
|
---|
161 | ::noTone(BUZZER_PIN);
|
---|
162 | }
|
---|
163 |
|
---|
164 | int _Esplora::readTemperature(const byte scale) {
|
---|
165 | long rawT = readChannel(CH_TEMPERATURE);
|
---|
166 | if (scale == DEGREES_C) {
|
---|
167 | return (int)((rawT * 500 / 1024) - 50);
|
---|
168 | }
|
---|
169 | else if (scale == DEGREES_F) {
|
---|
170 | return (int)((rawT * 450 / 512 ) - 58);
|
---|
171 | }
|
---|
172 | else {
|
---|
173 | return readTemperature(DEGREES_C);
|
---|
174 | }
|
---|
175 | }
|
---|
176 |
|
---|
177 | int _Esplora::readAccelerometer(const byte axis) {
|
---|
178 | switch (axis) {
|
---|
179 | case X_AXIS: return analogRead(ACCEL_X_PIN) - ACCEL_ZERO_X;
|
---|
180 | case Y_AXIS: return analogRead(ACCEL_Y_PIN) - ACCEL_ZERO_Y;
|
---|
181 | case Z_AXIS: return analogRead(ACCEL_Z_PIN) - ACCEL_ZERO_Z;
|
---|
182 | default: return 0;
|
---|
183 | }
|
---|
184 | }
|
---|