source: rtos_arduino/trunk/arduino_lib/libraries/Esplora/src/Esplora.cpp@ 136

Last change on this file since 136 was 136, checked in by ertl-honda, 8 years ago

ライブラリとOS及びベーシックなサンプルの追加.

File size: 4.6 KB
Line 
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 */
31const int ACCEL_ZERO_X = 320;
32const int ACCEL_ZERO_Y = 330;
33const int ACCEL_ZERO_Z = 310;
34
35const byte MUX_ADDR_PINS[] = { A0, A1, A2, A3 };
36const byte MUX_COM_PIN = A4;
37
38const int JOYSTICK_DEAD_ZONE = 100;
39
40const byte RED_PIN = 5;
41const byte BLUE_PIN = 9;
42const byte GREEN_PIN = 10;
43
44const 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
53const byte ACCEL_X_PIN = A5;
54const byte ACCEL_Y_PIN = A11;
55const byte ACCEL_Z_PIN = A6;
56
57const 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
68unsigned 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
84boolean _Esplora::joyLowHalf(byte joyCh) {
85 return (readChannel(joyCh) < 512 - JOYSTICK_DEAD_ZONE)
86 ? LOW : HIGH;
87}
88
89boolean _Esplora::joyHighHalf(byte joyCh) {
90 return (readChannel(joyCh) > 512 + JOYSTICK_DEAD_ZONE)
91 ? LOW : HIGH;
92}
93
94boolean _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
114boolean _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
123void _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) \
130void _Esplora::write##name(byte val) { \
131 if (val == lastVar) \
132 return; \
133 analogWrite(pin, val); \
134 lastVar = val; \
135 delay(5); \
136} \
137\
138byte _Esplora::read##name() { \
139 return lastVar; \
140}
141
142RGB_FUNC(Red, RED_PIN, lastRed)
143RGB_FUNC(Green, GREEN_PIN, lastGreen)
144RGB_FUNC(Blue, BLUE_PIN, lastBlue)
145
146void _Esplora::tone(unsigned int freq) {
147 if (freq > 0)
148 ::tone(BUZZER_PIN, freq);
149 else
150 ::noTone(BUZZER_PIN);
151}
152
153void _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
160void _Esplora::noTone() {
161 ::noTone(BUZZER_PIN);
162}
163
164int _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
177int _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}
Note: See TracBrowser for help on using the repository browser.