|
| 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 | +void _Esplora::writeRGB(byte r, byte g, byte b) { |
| 115 | + writeRed(r); |
| 116 | + writeGreen(g); |
| 117 | + writeBlue(b); |
| 118 | +} |
| 119 | + |
| 120 | +#define RGB_FUNC(name, pin, lastVar) \ |
| 121 | +void _Esplora::write##name(byte val) { \ |
| 122 | + if (val == lastVar) \ |
| 123 | + return; \ |
| 124 | + analogWrite(pin, val); \ |
| 125 | + lastVar = val; \ |
| 126 | + delay(5); \ |
| 127 | +} \ |
| 128 | +\ |
| 129 | +byte _Esplora::read##name() { \ |
| 130 | + return lastVar; \ |
| 131 | +} |
| 132 | + |
| 133 | +RGB_FUNC(Red, RED_PIN, lastRed) |
| 134 | +RGB_FUNC(Green, GREEN_PIN, lastGreen) |
| 135 | +RGB_FUNC(Blue, BLUE_PIN, lastBlue) |
| 136 | + |
| 137 | +void _Esplora::tone(unsigned int freq) { |
| 138 | + if (freq > 0) |
| 139 | + ::tone(BUZZER_PIN, freq); |
| 140 | + else |
| 141 | + ::noTone(BUZZER_PIN); |
| 142 | +} |
| 143 | + |
| 144 | +void _Esplora::tone(unsigned int freq, unsigned long duration) { |
| 145 | + if (freq > 0) |
| 146 | + ::tone(BUZZER_PIN, freq, duration); |
| 147 | + else |
| 148 | + ::noTone(BUZZER_PIN); |
| 149 | +} |
| 150 | + |
| 151 | +void _Esplora::noTone() { |
| 152 | + ::noTone(BUZZER_PIN); |
| 153 | +} |
| 154 | + |
| 155 | +int _Esplora::readTemperature(const byte scale) { |
| 156 | + long rawT = readChannel(CH_TEMPERATURE); |
| 157 | + if (scale == DEGREES_C) { |
| 158 | + return (int)((rawT * 500 / 1024) - 50); |
| 159 | + } |
| 160 | + else if (scale == DEGREES_F) { |
| 161 | + return (int)((rawT * 450 / 512 ) - 58); |
| 162 | + } |
| 163 | + else { |
| 164 | + return readTemperature(DEGREES_C); |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +int _Esplora::readAccelerometer(const byte axis) { |
| 169 | + switch (axis) { |
| 170 | + case X_AXIS: return analogRead(ACCEL_X_PIN) - ACCEL_ZERO_X; |
| 171 | + case Y_AXIS: return analogRead(ACCEL_Y_PIN) - ACCEL_ZERO_Y; |
| 172 | + case Z_AXIS: return analogRead(ACCEL_Z_PIN) - ACCEL_ZERO_Z; |
| 173 | + default: return 0; |
| 174 | + } |
| 175 | +} |
0 commit comments